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

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

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

	function backToBefore() {
		uni.reLaunch({
			url: "/pages/index"
		})
	};

	function searchClick() {
		goToPage('/pages/cbProject/search/index')
	}

	let scrollTop = ref(0)
	let loading = ref(true)
	// 参数
	let searchInfo = ref({
		pageNo: 1,
		pageSize: 10,
		queryType: 3,
		orderBy: "amtTotal",
	})

	// 获取列表
	function getList() {
		if (searchInfo.value.pageNo == 1) {
			loading.value = true
		}

		let params = Object.assign({
			orderType: orderByStatus.value
		}, searchInfo.value)

		getProjectInfoList(params).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
		})
	}

	// 按金额排序
	let orderByStatus = ref(null)
	const changeOrderByStatus = () => {
		if (orderByStatus.value === "desc") {
			orderByStatus.value = "asc"
		} else if (orderByStatus.value === "asc") {
			orderByStatus.value = null
		} else {
			orderByStatus.value = "desc"
		}
		searchInfo.value.pageNo = 1;
		projectList.value = [];
		moreListFlag = true;
		getList();
	}

	// 触底加载flag
	let moreListFlag = true

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

	function goToDetail(id, subName) {
		uni.navigateTo({
			url: `/pages/projectInfo/detail/index?id=${id}&subName=${subName}`
		})
	}

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

	function goToReport(type, subId, subName) {
		uni.navigateTo({
			url: `/pages/projectInfo/report/index?type=${type}&subId=${subId}&subName=${subName}`
		})
	}

	// 折叠/展开
	const changeFoldItem = (status, id) => {
		let item = projectList.value.find(item => item.id === id);
		item.unfold = status;
	}

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

			getList();
		})
		getList();
	})

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

	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='true'>储备项目</page-title>

		<view class="cards-list">
			<view class="order-by">
				<view class=" order-by-item">
					金额
					<view class="order-by-icon" :class="orderByStatus" @click="changeOrderByStatus()"></view>
				</view>
			</view>

			<view v-for="(item,index) in projectList" :key="index">
				<!-- 未折叠卡片 -->
				<view class="card" v-if="item.unfold">
					<!-- 项目名称 -->
					<view class="card-item">
						<view class="card-item-name">项目名称</view>
						<view class="card-item-content">{{item.subName ?? "--"}}</view>
					</view>

					<!-- 总投资（万元） -->
					<view class="card-item">
						<view class="card-item-name">总投资（万元）</view>
						<view class="card-item-content">{{item.amtTotal ?? "--"}}</view>
					</view>

					<!-- 年度计划投资-申报单位（万元） -->
					<view class="card-item">
						<view class="card-item-name">年度计划投资-申报单位（万元）</view>
						<view class="card-item-content">{{item.yearAmt ?? "--"}}</view>
					</view>

					<!-- 已完成投资（万元）-->
					<view class="card-item">
						<view class="card-item-name">已完成投资（万元）</view>
						<view class="card-item-content">{{item.yearAmtSj ?? "--"}}</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-btn fat-btn" @click="goToDetail(item.id,item.subName)">项目查看</view>
					</view>

					<!-- 周月年报按钮 -->
					<view class="card-item bottom-item">
						<view class="card-btn thin-btn report-btn" @click="goToReport('weekly',item.id,item.subName)">周报</view>

						<view class="card-btn thin-btn report-btn" @click="goToReport('monthly',item.id,item.subName)">月报</view>

						<view class="card-btn thin-btn report-btn" @click="goToReport('yearly',item.id,item.subName)">年度计划</view>
					</view>

					<!-- 编号 -->
					<view class="card-fold-option"
						:class="item.status_fgw === '2' ? 'card-fold-red' :item.status_fgw === '1' ?'card-fold-yellow':''">
						<view class="card-fold-count">{{index + 1}} / {{listTotal}}</view>

						<view class="card-fold-center" @click.stop="changeFoldItem(false,item.id)">
							<view class="card-fold-btn card-unfold-btn"></view>
						</view>

						<view class="card-fold-chaos"></view>
					</view>
				</view>

				<!-- 折叠卡片 -->
				<view class="card-fold" v-else>
					{{item.subName ?? "--"}}

					<view class="card-fold-option"
						:class="item.status_fgw === '2' ? 'card-fold-red' :item.status_fgw === '1' ?'card-fold-yellow':''">
						<view class="card-fold-count">{{index + 1}} / {{listTotal}}</view>

						<view class="card-fold-center" @click.stop="changeFoldItem(true, item.id)">
							<view class="card-fold-btn"></view>
						</view>

						<view class="card-fold-chaos"></view>
					</view>
				</view>
			</view>
		</view>

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

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

<style lang="scss" scoped>
	.card-light {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.card-light-bottom {
		justify-content: center;
		width: 282rpx;
		height: 76rpx;
		background-size: 100% 100%;
	}

	.light-red {
		background-image: url('@/static/icon-light-red.png');
	}

	.light-yellow {
		background-image: url('@/static/icon-light-yellow.png');
	}

	.light-green {
		background-image: url('@/static/icon-light-green.png');
	}

	.card-fold {
		position: relative;
		width: 100%;
		min-height: 152rpx;
		margin-bottom: 20rpx;
		padding: 24rpx 30rpx 52rpx;
		box-sizing: border-box;
		background: #FFFFFF;
		border-radius: 40rpx;
		overflow: hidden;
	}

	.card-fold-option {
		position: absolute;
		display: flex;
		justify-content: space-between;
		align-items: center;
		left: 0;
		bottom: 0;
		width: 100%;
		height: 38rpx;
		padding: 0 40rpx;
		box-sizing: border-box;
		background: linear-gradient(270deg, #CADDFF 4%, rgba(219, 232, 255, 0) 100%);
		z-index: 999;

		.card-fold-count {
			flex: 1;
			font-size: 28rpx;
			color: #1869F6;
		}

		.card-fold-center {
			flex: 1;

			.card-fold-btn {
				width: 32rpx;
				height: 20rpx;
				margin: 0 auto;
				background-image: url("@/static/icon-fold.png");
				background-size: 100% 100%;
			}

			.card-unfold-btn {
				transform: rotate(180deg);
			}
		}

		.card-fold-chaos {
			flex: 1;
		}
	}

	.card-fold-red {
		background: linear-gradient(270deg, #FF8080 0%, rgba(219, 232, 255, 0) 100%);

		.card-fold-count {
			color: #FF0000;
		}

		.card-fold-center {
			.card-fold-btn {
				background-image: url("@/static/icon-fold-red.png");
			}
		}
	}

	.card-fold-yellow {
		background: linear-gradient(270deg, #FFAA00 4%, rgba(219, 232, 255, 0) 100%);

		.card-fold-count {
			color: #E19703;
		}

		.card-fold-center {
			.card-fold-btn {
				background-image: url("@/static/icon-fold-yellow.png");
			}
		}
	}

	.card {
		padding: 16rpx 40rpx 60rpx;
		overflow: hidden;
	}

	.order-by {
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		height: 106rpx;
		margin-bottom: 34rpx;
		padding: 0 40rpx;
		border-radius: 40rpx;
		background: #FFF;

		.order-by-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 32rpx;
			color: #343437;

			.order-by-icon {
				width: 22rpx;
				height: 46rpx;
				margin-left: 24rpx;
				background-image: url("@/static/orderBy-none.png");
				background-size: 100% 100%;
			}

			.desc {
				background-image: url("@/static/orderBy-desc.png");
			}

			.asc {
				background-image: url("@/static/orderBy-asc.png");
			}
		}
	}
</style>