<template>
	<view class="container">
		<view class="header-box">
			<view class="search-box">
				<u-search v-model="searchForm.keyword" :clearabled="true" bg-color="#E5E5E5"
					:input-style="searchInputStyle" placeholder="搜索您想要的内容" @search="toSearch" @custom="toSearch"></u-search>
			</view>
			<view class="tab-box">
				<u-tabs name="label" :list="searchType" :is-scroll="true" v-model="searchForm.type"
					@change="onSearchTypeChange" font-size="24" :bold="false" inactive-color="#000000"
					active-color="#000000" :bar-style="{'background-color': '#2979ff'}" :gutter="25"
					height="45"></u-tabs>
			</view>
		</view>
		<u-empty
			mode="data"
			v-if="list.length === 0"
			iconSize="120"
			textSize="58"
			text="暂无数据"
			margin-top="50"
		>
		</u-empty>
		<view class="list-box">
			<view class="list-item-box" v-for="item in list" :key="item.id" @click="onClickReport(item)">
				<view class="image-box">
					<image :src="item.cover" mode="aspectFill"></image>
				</view>
				<view class="info-box">
					<view class="title">
						{{item.title}}
					</view>
					<view class="type">
						<span>{{item.type}}</span>
					</view>
					<view class="func">
						<!-- 如果是免费,直接显示免费 -->
						<view v-if="item.viewMode==='1'" class="func-box" style="justify-content: flex-end;">
							<view class="button free" >免费</view>
						</view>
						<view v-else-if="item.viewMode==='2' && isMember" class="func-box">
							<view class="price">¥{{ item.price }}元</view>
							<view class="button member-free">会员免费</view>
						</view>
						<view v-else-if="item.viewMode==='2' && !isMember" class="func-box">
							<view style="flex: 0 0 auto;display: flex;align-items: center;">
								<view class="member-free">
									会员免费
								</view>
								<view class="not-member-price">
									非会员:¥{{item.price}}元
								</view>
							</view>
							<view :class="['button', item.hasBuy ? 'free' : 'buy']">
								{{item.hasBuy ? '已购买' : '立即购买'}}
							</view>
						</view>
						<view v-else-if="item.viewMode==='3' && item.price===item.priceMember" class="func-box">
							<view class="price">¥{{ item.price }}元</view>
							<view :class="['button', item.hasBuy ? 'free' : 'buy']">
								{{item.hasBuy ? '已购买' : '立即购买'}}
							</view>
						</view>
						<view v-else-if="item.viewMode==='3' && item.price!==item.priceMember" class="func-box">
							<view style="flex: 0 0 auto;display: flex;align-items: center;">
								<view class="member-free">
									{{`会员:${item.priceMember}元`}}
								</view>
								<view class="not-member-price">
									非会员:¥{{item.price}}元
								</view>
							</view>
							<view :class="['button', item.hasBuy ? 'free' : 'buy']">
								{{item.hasBuy ? '已购买' : '立即购买'}}
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-loadmore v-if="list.length !==0 && status !== 'nomore'" 
		:status="status" margin-top="20" margin-bottom="20" 
		@loadmore="loadmore" />
	</view>
</template>

<script setup>
	import {
		ref,
		computed
	} from 'vue'
	import {
		onLoad, onReachBottom
	} from '@dcloudio/uni-app'
	import {
		loadReportList
	} from '@/api/report.js'
	import {
		useReportStore
	} from '@/store/reportStore.js'
	import {
		useAuthStore
	} from '@/store/authStore.js'
	const reportStore = useReportStore();
	const authStore = useAuthStore();

	const customButtonStyle = {
		height: '40rpx',
		lineHeight: '40rpx',
		padding: '0 30rpx'
	}

	const searchInputStyle = {
		backgroundColor: '#E5E5E5'
	}
	const isMember = ref(false);
	const categoryList = ref({});
	const pageNum = ref(1);
	const pageSize = ref(10);
	const count = ref(0);
	const model = ref(null);
	const status = ref('loadmore');

	const searchType = ref([{
		label: '全部',
		value: ' '
	}])
	// 对应tab的code
	const currentType = ref(null);

	function onSearchTypeChange(val) {
		currentType.value = searchType.value[val].value;
		pageNum.value = 1;
		search(searchForm.value.keyword, 1, model.value, currentType.value);
	}
	const searchForm = ref({
		keyword: '',
		type: 0
	})

	const modelName = ref()

	const list = ref([])
	
	const listFilter = ref([])

	function onClickReport(report) {
		uni.navigateTo({
			url: `/pages/reportDetail/reportDetail?id=${report.id}&title=${report.title}`
		})
	}

	function init() {
		searchForm.value.keyword = '';
		pageNum.value = 1
		search('', 1, model.value, currentType.value)
	}

	function search(keyword, pageNumber, model, type, pageSize) {
		status.value = 'loading'
		const form = {
			keyword,
			model,
			type,
			pageNumber,
			pageSize: pageSize ? pageSize : 10
		}

		loadReportList(form).then(res => {
			// console.log(res)
			if (res.code === 0) {
				if (pageNumber === 1) {
					list.value = res.data
				} else {
					list.value = [...list.value, ...res.data];
				}
				pageNum.value = pageNumber + 1;
				count.value = res.count;
				if(list.value.length===count.value){
					status.value = 'nomore' 
				}else{
					status.value ='loadmore'
				}
			}
		}).catch(() => {
			status.value = 'loadmore'
		})
	}

	function loadmore() {
		if(list.value.length === count.value){
			return
		}
		search(searchForm.value.keyword, pageNum.value, model.value, currentType.value);
	}

	function toSearch() {
		search(searchForm.value.keyword, 1, model.value, currentType.value);
	}

	onReachBottom(()=>{
		loadmore()
	})

	onLoad((load) => {
		isMember.value = authStore.userInfo.isMember === '0' ? false : true;
		if (load.model) {
			// console.log(load, reportStore.reportCate , "传过来的值")
			searchType.value = reportStore.reportCate[load.model].child;

			modelName.value = load.model
			model.value = load.value

			currentType.value = searchType.value[0].value;

			uni.setNavigationBarTitle({
				title: modelName.value
			})
			init()
		}
	})
</script>

<style lang="scss">
	$image-width: 230rpx;

	.container {
		// height: 100vh;
		width: 100vw;
		background-color: $uni-text-color-inverse;

		.header-box {
			padding: 0 20rpx;
			background-color: $uni-text-color-inverse;
			@include topMagnet();
		}

		.search-box {
			margin-bottom: 20rpx;

			::v-deep(.u-search) {
				background-color: #e5e5e5;
				border-radius: 50rpx;

				.u-action {
					width: 18%;
					background-color: $uni-color-primary;
					border-radius: 50rpx;
					color: $uni-text-color-inverse;
					margin-right: 8rpx;
					font-size: 28rpx;
					line-height: 50rpx;
					letter-spacing: 3rpx;
					text-align: center;
				}
			}
		}

		.list-box {
			padding: 0 20rpx;

			.list-item-box {
				padding: 30rpx 20rpx;
				display: flex;
				gap: 20rpx;
				// height: 210rpx;
				border-bottom: 5rpx solid #E6E6E6;

				&:active {
					background-color: $uni-bg-color-hover;
				}

				.image-box {
					width: $image-width;

					image {
						width: $image-width;
						flex: 0 0 $image-width;
						height: 100%;
						border-radius: $uni-card-border-radius;
					}
				}

				.info-box {
					.title {
						font-size: $uni-title-font-size-2;
						font-weight: bold;
						line-height: 40rpx;
						margin-bottom: 15rpx;
						@include text-line-overflow(2);
					}

					.type {
						font-size: $uni-font-size-2;

						.iconfont {
							font-size: $uni-font-size-2;
							padding-right: 10rpx;
						}
					}

					.func {
						display: flex;
						justify-content: space-between;
						align-items: flex-end;
						font-size: $uni-font-size-2;
						font-weight: bold;
						margin-top: 10rpx;

						.func-box {
							display: flex;
							justify-content: space-between;
							width: 100%;
							align-items: center;
						}

						.button {
							text-align: center;
							width: 130rpx;
						}

						.price {
							color: $uni-color-error;
							font-size: $uni-title-font-size-3;
						}

						.not-member-price {
							font-size: $uni-font-size-4;
							color: $uni-text-color-grey;
							text-align: end;
						}

						.buy {
							padding: 6rpx 25rpx;
							background-color: $uni-color-error;
							border-radius: $uni-card-border-radius;
							color: $uni-text-color-inverse;
						}

						.free {
							padding: 6rpx 25rpx;
							background-color: $uni-color-primary;
							border-radius: $uni-card-border-radius;
							color: $uni-text-color-inverse;
						}

						.member-free {
							padding: 10rpx 20rpx;
							@include backgroundImg('http://www.gzrea.org.cn:8543/icon/wxmp/bg-label.png');
							color: $uni-text-color;
						}
					}
				}
			}
		}
	}
</style>