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

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

	import {
		getYearlyList,
	} from "@/api/work/weeklyAndMonthly.js"

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

	// 获取列表
	let searhInfo = {};
	const getList = () => {
		let searchParams = Object.assign({}, {
			orderType: orderByStatus.value
		}, searhInfo)
		// loading.value = true
		getYearlyList(searchParams).then(res => {
			loading.value = false
			projectsList.value = res.data.list
			listTotal.value = res.data.list.length
		}).catch(() => {
			loading.value = false
		})
	}

	// 主要列表
	let projectsList = ref([])
	let listTotal = ref(0)

	// 按金额排序
	let orderByStatus = ref("")
	const changeOrderByStatus = () => {
		if (orderByStatus.value === "desc") {
			orderByStatus.value = "asc"
		} else if (orderByStatus.value === "asc") {
			orderByStatus.value = ""
		} else {
			orderByStatus.value = "desc"
		}
		getList();
	}

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

	// 去录入
	function gotoInput(title, is_lock, subId, year) {
		if (is_lock === '1') {
			uni.showToast({
				title: '该项目的年度计划被锁定，不能录入了！',
				icon: 'none',
				mask: false,
				duration: 2000,
				style: {
					fontSize: "23px"
				}
			})
		} else {
			uni.navigateTo({
				url: `/pages/yearly/input/index?title=${title}&subId=${subId}&year=${year}`
			})
		}
	}

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

	onLoad((options) => {
		searhInfo = options;
		getList()
	})

	onPageScroll((e) => {
		scrollTop.value = e.scrollTop
	})
</script>

<template>
	<view class="container">
		<page-title>项目信息</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 projectsList" :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.title ?? "--"}}</view>
					</view>

					<view class="card-item">
						<view class="card-item-name">总投资</view>
						<view class="card-item-content">{{item.amt ?? "--"}}</view>
					</view>

					<view class="card-item">
						<view class="card-item-name">年度计划金额</view>
						<view class="card-item-content">{{item.amt_jh || "--"}}</view>
					</view>

					<view class="card-item">
						<view class="card-item-name">申报单位</view>
						<view class="card-item-content">{{item.unit_name || "--"}}</view>
					</view>

					<view class="card-item">
						<view class="card-item-name">锁定</view>
						<view class="card-item-content">{{item.isLockName || "--"}}</view>
					</view>

					<view class="card-item bottom-item">
						<view class="card-btn text-btn" @click="gotoInput(item.title,item.is_lock,item.sub_id,item.year)">
							<image src="@/static/icon-input.svg" mode=""></image>
							录入
						</view>

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

					<!-- 编号 -->
					<view class="card-fold-option">
						<view class="card-fold-count">{{index + 1}} / {{listTotal}}</view>

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

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

					<view class="card-icon" :class="item.status === 1 ? 'no-input' : ''"></view>
				</view>

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

					<view class="card-fold-option">
						<view class="card-fold-count">{{index + 1}} / {{listTotal}}</view>

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

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

			<empty-show v-if="projectsList.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" scoped>
	.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;
		}
	}

	.bottom-item {
		margin-bottom: 20rpx;
	}
</style>