<template>
	<view class="container">
		<!-- 分段器区域 -->
		<common-controlTag :tagList="tagList" rangekey="name" type="between" @change="handleChange"></common-controlTag>

		<!-- 列表区域 -->
		<view class="list">
			<!-- 每一条新闻区域 -->
			<view class="list_box" v-for="item in dataList" :key="item.id" @click="clickItem(item)">
				<image class="img" src="@/static/images/10.png" mode="aspectFill"></image>
				<view class="box_info">
					<view class="info_title">{{ item.theme }}</view>
					<view class="">{{ dayjs(item.createTime).format('YYYY-MM-DD HH:mm:ss') }}</view>
				</view>
			</view>
		</view>

		<!-- 没有数据时展示的页面 -->
		<noData v-if="!dataList.length"></noData>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import { getCategoryNewss, getNewsByCategoryId } from '@/api/index.js'
import dayjs from 'dayjs'

// 分段器数组
const tagList = ref([])
// 分段器当前索引
const currentIndex = ref(0)
// 当前页
const currentPage = ref(1)
// 每页多少条
const pageCount = ref(6)
// 总条数
const total = ref(0)
// 列表数据
const dataList = ref([])

onLoad(() => {
	// 获取新闻分类列表数据
	getCategory()
})

// 获取新闻分类列表数据
const getCategory = async () => {
	const res = await getCategoryNewss()
	// console.log(res)
	tagList.value = res.data

	// 获取新闻分页数据
	getData()
}

// 获取新闻分页数据
const getData = async () => {
	let data = {
		currentPage: currentPage.value,
		pageCount: pageCount.value,
		categoryId: tagList.value[currentIndex.value].id
	}

	const res = await getNewsByCategoryId(data)
	// console.log(res)
	dataList.value = [...dataList.value, ...res.data.list]
	total.value = res.data.totalCount
}

// 分段器切换时的回调
const handleChange = (e) => {
	if (currentIndex.value != e) {
		currentIndex.value = e
		currentPage.value = 1
		dataList.value = []
		getData()
	}
}

// 点击每一条新闻的回调
const clickItem = (item) => {
	// console.log(item)
	let info = encodeURIComponent(JSON.stringify(item))
	uni.navigateTo({
		url: `/pages/news_detail/news_detail?info=${info}`
	})
}
</script>

<style lang="scss" scoped>
.container {
	padding: 20rpx 18rpx;
	min-height: 100vh;

	.list {
		margin-top: 20rpx;

		.list_box {
			display: flex;
			padding: 30rpx;
			margin-bottom: 20rpx;
			width: 714rpx;
			height: 234rpx;
			border-radius: 28rpx;
			box-shadow: 0 4rpx 34rpx rgba(211, 211, 211, 0.32);

			.img {
				margin-right: 24rpx;
				width: 249rpx;
				height: 168rpx;
			}

			.box_info {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				flex: 1;
				font-size: 24rpx;
				color: #808080;

				.info_title {
					color: #000;
					font-size: 28rpx;
					font-weight: bold;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
				}
			}
		}
	}
}
</style>
