<template>
	<view class="container">
		<page-title>问题清单</page-title>

		<view class="cards-list">
			<view class="card" v-for="(item,index) in itemList" :key="index">
				<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.unitTitle || "--"}}</text>
				</view>

				<view class="card-item">
					<view class="card-item-name">项目名称</view>

					<view class="card-item-content">{{item.sub_name || "--"}}</view>
				</view>

				<view class="card-item">
					<view class="card-item-name">当前状态</view>

					<view class="card-item-content">{{item.status || "--"}}</view>
				</view>

				<view class="card-item">
					<view class="card-item-name">问题描述</view>

					<view class="card-item-content">{{item.title || "--"}}</view>
				</view>

				<view class="card-item">
					<view class="card-item-name">要求解决日期</view>

					<view class="card-item-content">{{item.date_conf || "--"}}</view>
				</view>

				<view class="card-item">
					<view class="card-btn" @click="gotoDetail('project',item.sub_id,item.subName)">项目查看</view>

					<view class="card-btn empty-btn" @click="gotoDetail('problem',item.id)">问题查看</view>
				</view>
			</view>

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

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

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

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

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

	import {
		getOverseeListBySubId,
		getOverseeListBySubIdNo
	} from "@/api/work/oversee.js"

	function backToBefore() {
		uni.navigateBack({})
	};

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

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

	let pageType = null

	function getList() {
		if(searchInfo.value.pageNo == 1){
			loading.value = true
		};

		if (pageType === 'notDeal') {
			getOverseeListBySubIdNo(searchInfo.value).then(res => {
				loading.value = false;
				itemList.value = itemList.value.concat(res.data.list);
				listTotal = res.data.total;
				if (res.data.total == searchInfo.value.pageNo*searchInfo.value.pageSize-(10 - res.data.list.length)) moreListFlag = false;
			}).catch(() => {
				loading.value = false
			})
		} else {
			getOverseeListBySubId(searchInfo.value).then(res => {
				loading.value = false;
				itemList.value = itemList.value.concat(res.data.list);
				listTotal = res.data.total;
				if (res.data.total == searchInfo.value.pageNo*searchInfo.value.pageSize-(10 - res.data.list.length)) moreListFlag = false;
			}).catch(() => {
				loading.value = false
			})
		}
	}

	let itemList = ref([])
	let listTotal = ref(0)

	// 触底加载flag
	let moreListFlag = true

	function gotoDetail(type, id, subName) {
		if (type === "project") {
			uni.navigateTo({
				url: `/pages/projectInfo/detail/index?id=${id}&subName=${subName}`
			})
		} else {
			uni.navigateTo({
				url: `/pages/oversee/detail/index?id=${id}`
			})
		}
	}

	function goToPage(url) {
		uni.navigateTo({
			url
		})
	}

	onLoad((options) => {
		pageType = options.type
		searchInfo.value = Object.assign(searchInfo.value, options);
		let filterArr = ["null", "undefined", ""]
		for (let i in searchInfo.value) {
			if (filterArr.includes(searchInfo.value[i])) {
				searchInfo.value[i] = null
			}
		}
		getList()
	})

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


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

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

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