<script setup>
	import {
		ref
	} from 'vue'

	import {
		onLoad,
		onUnload,
		onPullDownRefresh,
		onReachBottom,
		onPageScroll
	} from "@dcloudio/uni-app"

	import {
		goToPage
	} from "@/utils/common.js"

	import {
		getProjectInfoList,
	} from "@/api/work/projectInfo.js"

	let scrollTop = ref(0);
	let loading = ref(true);

	// 参数
	let searchInfo = ref({
		pageNo: 1,
		pageSize: 10,
	})

	// 触底加载flag
	let moreListFlag = true

	let projectList = ref([])

	let listTotal = ref(0)

	// 获取列表
	function getList() {
		if (searchInfo.value.pageNo == 1) {
			loading.value = true
		}
		getProjectInfoList(searchInfo.value).then(res => {
			loading.value = false
			projectList.value = projectList.value.concat(res.data.list)
			listTotal.value = res.data.total
			if (res.data.total == searchInfo.value.pageNo * searchInfo.value.pageSize - (10 - res.data.list.length))
				moreListFlag = false;
		}).catch(() => {
			loading.value = false
		})
	}

	// 去详情页
	function goToDetail(id, subName) {
		goToPage(`/pages/projectInfo/detail/index?id=${id}&subName=${subName}`)
	}

	// 去录入页
	function goToInput(subId) {
		goToPage(`/pages/questionInput/input/index?subId=${subId}`)
	}

	// 去搜索页
	function searchClick() {
		goToPage('/pages/questionInput/search/index')
	}

	onLoad(() => {
		uni.$on('questionInputSearch', resolve => {
			searchInfo.value = Object.assign(searchInfo.value, resolve);
			searchInfo.value.pageNo = 1;
			projectList.value = [];
			listTotal.value = 0;
			moreListFlag = true;
			getList();
		})
		getList();
	})

	onUnload(() => {
		uni.$off('questionInputSearch');
	});

	onPageScroll((e) => {
		scrollTop.value = e.scrollTop
	})

	onPullDownRefresh(() => {
		searchInfo.value.pageNo = 1;
		projectList.value = [];
		moreListFlag = true;
		try {
			getList();
		} finally {
			uni.stopPullDownRefresh()
		}
	})

	onReachBottom(() => {
		if (!moreListFlag) {
			return uni.showToast({
				title: "已经到底了。",
				icon: "none",
				duration: 2000
			})
		}
		searchInfo.value.pageNo++;
		getList();
	})
</script>

<template>
	<view class="container">
		<page-title @searchClick='searchClick' showSearch>问题录入</page-title>

		<view class="cards-list">
			<view class="card" v-for="(item,index) in projectList" :key="index">
				<view v-if="item.usersub == 1">
					<view style="height:20rpx"></view>

					<view class="special-item">
						<card-title :numerator="index+1" :denominator="listTotal" isSpecial></card-title>

						<view class="card-name">
							<view class="card-name-title">
								<text class="card-name-description">项目名称</text>
							</view>

							<text class="card-name-text">{{item.subName || "--"}}</text>
						</view>
					</view>
				</view>

				<view v-else>
					<card-title :numerator="index+1" :denominator="listTotal"></card-title>

					<!-- 项目名称 -->
					<view class="card-name">
						<view class="card-name-title">
							<text class="card-name-description">项目名称</text>
						</view>

						<text class="card-name-text">{{item.subName || "--"}}</text>
					</view>
				</view>

				<view class="card-item">
					<view class="card-item-name">监管单位</view>
					<view class="card-item-content">{{item.manageName || "--"}}</view>
				</view>

				<view class="card-item">
					<view class="card-btn" @click="goToDetail(item.id,item.subName)">项目查看</view>
					<view class="card-btn empty-btn" @click="goToInput(item.id)">问题录入</view>
				</view>
			</view>

			<empty-show v-if="projectList.length===0"></empty-show>
		</view>

		<u-back-top :scroll-top="scrollTop"></u-back-top>

		<u-loading-page :loading="loading"></u-loading-page>
	</view>
</template>

<style lang="scss">
</style>