<template>
	<view class="container">
		<view v-show="menuCurrent === 0">
			<view class="menus-box">
				<view class="menus-item-box" @click="toReportList(key, data.value)" v-for="(data,key) in categoryList"
					:key="key">
					<view class="icon-box">
						<view class="iconfont" :class="iconList[key]? iconList[key] : 'icon-hetong'"></view>
					</view>
					<view class="text-box">
						{{ key }}
					</view>
				</view>
				<!-- <view class="menus-item-box" @click="toReportList('二手住宅市场')">
					<view class="icon-box">
						<view class="iconfont icon-hetong"></view>
					</view>
					<view class="text-box">
						二手住宅市场
					</view>
				</view>
				<view class="menus-item-box" @click="toReportList('住宅租赁市场')">
					<view class="icon-box">
						<view class="iconfont icon-chengshi"></view>
					</view>
					<view class="text-box">
						住宅租赁市场
					</view>
				</view>
				<view class="menus-item-box" @click="toReportList('房地产中介行业')">
					<view class="icon-box">
						<view class="iconfont icon-zhongjie"></view>
					</view>
					<view class="text-box">
						房地产中介行业
					</view>
				</view> -->
			</view>
		</view>
		<view v-show="menuCurrent === 1">
			<view class="header-box">
				<view class="search-box">
					<u-search v-model="searchForm.keyword" :clearabled="true" bg-color="#E5E5E5"
						:input-style="searchInputStyle" placeholder="搜索您想要的内容"
						@search="searchHistory(searchForm.keyword, 1)"
						@custom="searchHistory(searchForm.keyword, 1)"></u-search>
				</view>
			</view>
			<u-empty margin-top="50" mode="data" v-if="list.length === 0" iconSize="120" textSize="58" text="暂无数据">
			</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="time">
							<span class="iconfont icon-time"></span>
							<span>{{item.viewDate}}</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 v-if="item.free > 0" class="button free">免费</view>
							<view v-else-if="item.memberFree > 0" class="button member-free">会员免费</view>
							<view v-else :class="['button', item.status > 0 ? 'free' : 'buy']">
								{{item.status > 0 ? '已购买' : '立即购买'}}
							</view> -->
						</view>
					</view>
				</view>
			</view>
			<!-- <u-loadmore :status="status" margin-top="20" margin-bottom="20"/> -->
		</view>
		<view v-show="menuCurrent === 2">
			<view class="header-box">
				<view class="search-box">
					<u-search v-model="searchForm.keyword" :clearabled="true" bg-color="#E5E5E5"
						:input-style="searchInputStyle" placeholder="搜索您想要的内容" @search="searchMy(searchForm.keyword, 1)"
						@custom="searchMy(searchForm.keyword, 1)"></u-search>
				</view>
			</view>
			<u-empty 
			margin-top="50"
			mode="data" v-if="list2.length === 0" iconSize="120" textSize="58" text="暂无数据">
			</u-empty>
			<view class="list-box">
				<view class="list-item-box" v-for="item in list2" :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="time">
							<span>购买时间:&nbsp;{{item.buyDate}}</span>
						</view>
						<view class="func">
							<view class="price">实付款：{{item.buyPrice}}元</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-loadmore v-if="menuCurrent !== 0 && status !== 'nomore'" :status="status" margin-top="20" margin-bottom="20"
			@loadmore="loadMore" />
		<view class="bottom-block"></view>
		<view class="bottom-box">
			<view class="menu-box">
				<view class="menu-item-box" :class="{'is-active': menuCurrent === 0}" @click="onMenuClick(0)">
					<view class="iconfont icon-home"></view>
					<view class="text">首页</view>
				</view>
				<view class="menu-item-box" :class="{'is-active': menuCurrent === 1}" @click="onMenuClick(1)">
					<view class="iconfont icon-time"></view>
					<view class="text">我的浏览</view>
				</view>
				<view class="menu-item-box" :class="{'is-active': menuCurrent === 2}" @click="onMenuClick(2)">
					<view class="iconfont icon-baogao"></view>
					<view class="text">我的报告</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		computed
	} from 'vue'
	import {
		onLoad,
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		getReportCate,
		loadReportHistory,
		loadMyReport
	} from '@/api/report.js'

	import {
		useReportStore
	} from '@/store/reportStore.js'

	const reportStore = useReportStore();

	const categoryList = ref({});
	const pageNum = ref(1);
	const status = ref('nomore');
	const list = ref([]);
	const list2 = ref([]);
	const listCount = ref(0);
	const listCount2 = ref(0);
	const iconList = {
		"二手住宅市场": "icon-hetong",
		"住宅租赁市场": "icon-chengshi",
		"房地产中介行业": "icon-zhongjie"
	}
	const customButtonStyle = {
		height: '40rpx',
		lineHeight: '40rpx',
		padding: '0 30rpx'
	}
	const searchInputStyle = {
		backgroundColor: '#E5E5E5'
	}
	const searchForm = ref({
		keyword: '',
		type: 0
	})

	const menuCurrent = ref(0)

	function onMenuClick(index) {
		menuCurrent.value = index;
		searchForm.value.keyword = ""
		status.value = 'loadmore'
		if (index === 1) {
			// 浏览记录
			searchHistory("", 1)
		} else if (index === 2) {
			// 我的报告
			searchMy("", 1)
		}
	}

	function searchHistory(keyword, pageNumber, pageSize) {
		status.value = 'loading'
		loadReportHistory({
			keyword,
			pageNumber: pageNumber,
			pageSize: pageSize ? pageSize : 10
		}).then(res => {
			if (res.code === 0) {
				if (pageNumber === 1) {
					list.value = res.data
				} else {
					list.value = [...list.value, ...res.data]
				}
				listCount.value = res.count
				if (list.value.length === res.count) {
					status.value = 'nomore'
				} else {
					status.value = 'loadmore'
				}
				pageNum.value = pageNumber + 1;
			}
		})
	}

	function searchMy(keyword, pageNumber, pageSize) {
		status.value = 'loading'
		loadMyReport({
			keyword,
			pageNumber: pageNumber,
			pageSize: pageSize ? pageSize : 10
		}).then(res => {
			if (res.code === 0) {
				if (pageNumber === 1) {
					list2.value = res.data
				} else {
					list2.value = [...list2.value, ...res.data]
				}
				listCount2.value = res.count
				if (list2.value.length === res.count) {
					status.value = 'nomore'
				} else {
					status.value = 'loadmore'
				}
				pageNum.value = pageNumber + 1;
			}
		})
	}

	function toReportList(model, val) {
		uni.setStorageSync("reportCate", categoryList.value[model].child);
		uni.navigateTo({
			url: `/pages/researchReport/reportList/reportList?model=${model}&value=${val}`
		})
	}

	async function init() {
		const cateRes = await getReportCate();
		if (cateRes.code === 0) {
			const result = cateRes.data.reduce((acc, curr) => {
				const modifiedChild = [{
						value: " ",
						label: "全部"
					}, // 新增的默认项（value 为空格）
					...curr.child.map(child => ({
						value: child.code, // 将子对象的 code 改为 value
						label: child.name
					}))
				];
				acc[curr.name] = {
					value: curr.code,
					child: modifiedChild
				};
				return acc;
			}, {});
			categoryList.value = result;
			reportStore.setReportCate(result);
		}
	}




	function onClickReport(report) {
		let id;
		if(menuCurrent.value===1){
			id = report.reportId
		}else{
			id = report.id
		}
		uni.navigateTo({
			url: `/pages/reportDetail/reportDetail?id=${id}&title=${report.title}`
		})
	}

	function loadMore() {
		if (menuCurrent.value === 1) {
			if (list.value.length === listCount) {
				return
			}
			searchHistory(searchForm.value.keyword, pageNum.value)
		} else {
			if (list2.value.length === listCount2) {
				return
			}
			searchMy(searchForm.value.keyword, pageNum.value)
		}
	}
	onReachBottom(() => {
		loadMore()
	})
	onLoad((load) => {
		// console.log(load,"reporthome")
		if (load.menuCurrent) {
			menuCurrent.value = Number(load.menuCurrent)
		}
		init()
	})
</script>

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

	.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);
					}

					.time {
						font-size: $uni-font-size-2;
						color: $uni-text-color-grey;

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

					.func {
						display: flex;
						justify-content: flex-end;
						font-size: $uni-font-size-2;
						font-weight: bold;

						.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-2;
						}

						.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('https://www.gzrea.cn/wxmp/static/icon/bg-label.png');
							color: $uni-text-color;
						}
					}
				}
			}
		}

		.menus-box {
			// padding: 0 20rpx;
			width: 85%;
			margin: 0 auto;
			.menus-item-box {
				width: 100%;
				// padding: 35rpx 50rpx;
				padding: 40rpx 30rpx;
				margin: 35rpx 0;
				display: flex;
				gap: 30rpx;
				align-items: center;
				background-color: #006AF4;
				border-radius: $uni-card-border-radius;

				&:active {
					background-color: #005AF4;
				}

				.icon-box {
					width: 40%;
					text-align: center;
					width: 100rpx;
					height: 100rpx;
					line-height: 100rpx;
					text-align: center;
					border-radius: 50%;
					border: 3rpx solid $uni-text-color-inverse;
					background-color: #004DC9;

					.iconfont {
						font-size: 60rpx;
						color: $uni-text-color-inverse;
					}
				}

				.text-box {
					font-size: 50rpx;
					font-weight: bold;
					color: $uni-text-color-inverse;
				}
			}
		}

		.bottom-block {
			// height: 112rpx;
			height: calc(112rpx + env(safe-area-inset-bottom, 0));
		}

		.bottom-box {
			padding: 5rpx 20rpx;
			background-color: $uni-bg-color-grey;
			border: 1rpx solid #E9E9E9;
			padding-bottom: calc(5rpx + env(safe-area-inset-bottom, 0));
			@include bottomMagnet();

			.menu-box {
				height: 100rpx;
				display: flex;
				align-items: center;

				.menu-item-box {
					width: calc(100% / 3);
					text-align: center;

					.iconfont {
						font-size: 55rpx;
					}

					.text {
						font-size: $uni-font-size-2;
					}
				}

				.is-active {
					color: $uni-color-primary;
				}
			}
		}
	}
</style>