<template>
	<view class="article-list-page">
		<view class="banner">
			<view class="img">
				<image :src="topImg" mode="aspectFill" style="width: 100%;height: 100%;"></image>
				<view class="topText tn-flex tn-ml-xl">
					<view class="icon tn-mt-sm">
						<image :src="config.logo" mode="aspectFill" style="width: 100%;height: 100%;"></image>
					</view>
					<view class="title tn-ml tn-text-3xl tn-text-bold tn-mt">
						{{config.siteName}}
					</view>
				</view>
			</view>
			<view class="search tn-pl-sm tn-pr-sm tn-pt-xs tn-pb-xs" @click="showSearch">
				<TnIcon name="search"></TnIcon>
				<span class="tn-pl-xs">搜 索</span>
			</view>
		</view>
		<div class="container tn-shadow tn-m-sm tn-p-sm tn-radius">
			<view class="topic tn-text-xl tn-text-bold tn-pt-sm tn-pb tn-border-bottom tn-grey-light_border" style="letter-spacing: 4rpx;">
				#{{headerTitle}}
			</view>
			<view class="item tn-flex tn-flex-center-between tn-p-sm tn-border-bottom tn-grey-light_border" 
				v-for="(item,index) in articleList" 
				:key="index" 
				@click="toDetail(item)">
				<view class="left tn-flex tn-flex-column tn-pr-sm">
					<view class="title tn-text-lg tn-text-ellipsis-2">
						{{item.title}}
					</view>
					<view class="info tn-flex tn-flex-row tn-flex-center-between tn-text-sm">
						<view class="author">
							<TnIcon name="pushpin"/>{{item.user}}
						</view>
						<view class="time">
							<TnIcon name="time"/>{{dayjs(item.createTime).format('YYYY-MM-DD')}}
						</view>
					</view>
				</view>
				<view class="right tn-radius tn-shadow tn-shadow-blur">
					<image :src="item.image?item.image:defaultImg" mode="aspectFill" style="width: 150rpx;height: 150rpx;display: block;"></image>
				</view>
			</view>
			<view class="loadmore tn-mt tn-mb">
				<TnLoadmore :status="loadmoreStatus" color="#666" loading-icon-mode="flower" />
			</view>
			
		</div>
		<TnPopup v-model="showPopup" open-direction="top" :radius="0">
		    <view class="tn-pt-sm tn-pb-sm tn-pl-lg tn-pr-lg">
				<TnSearchBox
				    v-model="searchValue"
					shape="round"
				    @search="searchBtnClickEvent"
					@clear="clearEvent"
				  />
			</view>
		</TnPopup>
	</view>
</template>

<script lang="ts" setup>
	import { onMounted, reactive, ref } from 'vue';
	import request from '@/utils/request';
	import { onLoad,onReachBottom } from "@dcloudio/uni-app";
	import dayjs from "dayjs";
	import type { LoadmoreStatus } from '@tuniao/tnui-vue3-uniapp'
	import TnLoadmore from '@/uni_modules/tuniaoui-vue3/components/loadmore/src/loadmore.vue'
	import TnPopup from '@/uni_modules/tuniaoui-vue3/components/popup/src/popup.vue'
	import TnSearchBox from '@/uni_modules/tuniaoui-vue3/components/search-box/src/search-box.vue'
	import config from '@/config/config'
	
	const topImg = ref('/static/index_top.jpg')
	
	const headerTitle = ref('');
	
	const defaultImg = ref('http://static.cqtlcm.com/jujiao.jpg');
	
	const cateId = ref()
	
	const articleList = ref([])
	
	const params = reactive<{
		page?:number,
		limit?:number,
		keyword?:string,
		cateId?:number
	}>({
		page:1,
		limit:10,
	})

	
	const hasMore = ref(true);
	
	const loadmoreStatus = ref<LoadmoreStatus>('loadmore')
	
	const showPopup = ref(false)
	
	const searchValue = ref('')
	
	const showSearch = () => {
		showPopup.value = true
	}
	
	const clearEvent = () => {
		params.page = 1
		articleList.value = []
		getList(params)
	}
	
	const searchBtnClickEvent = (value: string) => {
		params.page = 1;
		articleList.value = [];
		params.keyword = value;
		showPopup.value = false;
		getList(params)
	}
	
	const getMenuData = () => {
		request({
			url:'/front/content/category/info',
			method:'GET',
			data:{
				'cateId':cateId.value
			}
		}).then((res)=>{
			uni.setNavigationBarTitle({
				title:res.data.data.cateName
			})
			headerTitle.value = res.data.data.cateName
			// if(res.data.data.image){
			// 	topImg.value = res.data.data.image
			// }
		})
	}	
		
	const getList = (params:any) => {
		loadmoreStatus.value = 'loading'
		request({
			url:'/front/content/list',
			data:params,
			method:'GET'
		}).then((res)=>{
			if(res.data.data.list && res.data.data.list.length > 0){
				res.data.data.list.forEach((item:any)=>{
					if(item.image){
						item.image = JSON.parse(item.image)[0]
					}
				})
			}
			if(res.data.data.list.length < params.limit){
				loadmoreStatus.value = 'nomore'
				hasMore.value = false
			}
			
			articleList.value = articleList.value.concat(res.data.data.list)
		})
	}	
					
	const toDetail = (item:any) =>{
		if(item.type === 0){
			let routeUrl = '/pages/detail/index'
			if(item.template){
				routeUrl = item.template //定义模板后
			}
			uni.navigateTo({
				url:routeUrl + '?cateId=' + item.cateId + '&id=' + item.id
			})
		}else if(item.type === 1){
			//外链
			if(item.link && item.link.slice(0,25) !== 'https://mp.weixin.qq.com/'){
				uni.navigateTo({
					url:'/pages/webview/index?url=' + item.link
				})
			}else{
				window.location.href = item.link
			}
		}else{
			uni.navigateTo({
				url:'/pages/detail/album?id=' + item.id
			})
		}
	}
	
	onReachBottom(() => {
		if(hasMore.value){
			params.page += 1
			getList(params)
		}else{
			loadmoreStatus.value = 'nomore'
		}
	})
	
	onLoad((option) => {
		cateId.value = option.cateId
		params.cateId = option.cateId
	})
	
	onMounted(()=>{
		getMenuData()
		getList(params)
	})
</script>

<style lang="scss" scoped>
	.article-list-page{
		min-height: 100vh;
		.banner{
			height: 450rpx;
			position: relative;
			.img{
				width: 100%;
				height: 100%;
				.topText{
					color: #fff;
					position: absolute;
					top: 100rpx;
					left: 0;
					.icon{
						width: 100rpx;
						height: 100rpx;
						border-radius: 50%;
					}
					.title{
						letter-spacing: 4rpx;
						text-shadow: 6rpx 6rpx 8rpx #5c5c5c;
					}
				}
			}
			::after{
				content: "";
				position: absolute;
				width: 100%;
				height: 120rpx;
				top: 330rpx;
				left: 0;
				background:linear-gradient(180deg,rgba(255, 255, 255, 0.01),rgba(255, 255, 255, 0.9))
			}
			.search{
				color: #fff;
				position: absolute;
				top: 20rpx;
				right: 20rpx;
				border-radius: 30rpx;
				background-color: rgba(255, 255, 255, 0.5);
			}
		}
		
		.container{
			position: relative;
			top: -130rpx;
			background-color: #FFF;
			.topic{
				border-radius: 0;
			}
			.item{
				background-color: #FFF;
				border-radius: 0;
				.left{
					height: 170rpx;
					justify-content: space-between;
					.title{
						line-height: 1.5em;
						color: #444;
					}
					.info{
						color: #666;
					}
				}
			}
		}
	}
</style>