<template>
	<view class="filling-record">
		<view class="tabs">
			<view class="left">
				<picker range-key="dictValue" :value="yearIndex" :range="years" @change="onChangeTime" name="year">
					<view class="uni-input">
						<text class="val">{{timeText}}</text>
						<uni-icons type="down" size="16" color="#1890FF"></uni-icons>
					</view>
				</picker>
			</view>
			<view class="right">
				<picker :range="statusList" @change="onChangeStatus">
					<view class="uni-input">
						<text class="val">{{statusText}}</text>
						<uni-icons type="down" size="16" color="#1890FF"></uni-icons>
					</view>
				</picker>
			</view>
		</view>
		<scroll-view scroll-y="true" @scrolltolower="scrolltolower">
			<view class="list">
				<view class="item" @tap="toPersonalReportingPage(item)" v-for="item in FillingList" :key="item.id">
					<view class="top">
						填报人:{{item.fdName}}
						<uni-icons type="right" size="18" color="#202020"></uni-icons>
					</view>
					<view class="content">
						<view>填报年份：{{item.years}}年</view>
						<view>填报时间：{{item.fdTime}}</view>
						<view>提交状态：{{item.substatus|formateCommitStatus}}</view>
						<view class="status submit" v-if="(item.substatus==0||item.substatus==null)&&item.shstatus!=2">
							<text class="status-text">填报中</text>
							<view class="triangle-right"></view>
						</view>
						<view class="status agree" v-else-if="item.xwstatus == 1 && item.yjstatus == 1
						          && item.ksstatus == 1 && item.xgstatus == 1 ">
							<text class="status-text">通过</text>
							<view class="triangle-right"></view>
						</view>
						<view @tap.stop="showRefuseModal(item)" class="status refuse" v-else-if="item.shstatus==2&&item.substatus==0">
							<text class="status-text">不通过</text>
							<view class="triangle-right"></view>
						</view>
						<view class="status submit" v-else>
							<text class="status-text">待审核</text>
							<view class="triangle-right"></view>
						</view>
						<!-- <view class="status submit"
							v-else-if="item.xwstatus==0||item.xwstatus==null||item.xwstatus == 1 && (item.yjstatus == 0 ||  item.yjstatus == null)||item.yjstatus == 1 && (item.ksstatus == 0 ||  item.ksstatus == null)||item.ksstatus == 1 && (item.xgstatus == 0 ||  item.xgstatus == null)">
							<text class="status-text">待审核</text>
							<view class="triangle-right"></view>
						</view> -->


					</view>
				</view>
			</view>
			<view class="empty" v-if="FillingList.length==0&&topLoading==false">
				<image src="@/static/empty.png" mode="widthFix"></image>
				暂时没有数据
			</view>
			<view class="loading-more-top" v-if="topLoading">
				<uni-load-more style="padding-top: 90px;" status="loading" />
			</view>
			<view class="loading-more" v-if="loading">
				<uni-load-more status="loading" />
			</view>
			<view class="no-more" v-if="!loading&&FillingList.length!=0">
				到底啦~~
			</view>
		</scroll-view>
		<view class="add" @click="addRecordsPopup">
			+
		</view>
		<uni-popup @change="popupChange" ref="popupTime" class="popup-time" background-color="#ffffff">
			<view class="popup-content">
				<view class="title">
					填报时间
				</view>
				<form @submit="topersonalReporting">
					<view class="form-item">
						<label>填报年份</label>
						<picker v-if="years.length>0" range-key="dictValue" :value="yearIndex" :range="years"
							@change="yearChange" name="year">
							<view class="uni-input">
								<text class="val">{{years[yearIndex].dictValue}}</text>
								<uni-icons type="calendar" size="25" color="#202020"></uni-icons>
							</view>
						</picker>
					</view>
					<view class="btns">
						<button type="default" @tap="onCancel">取消</button>
						<button form-type="submit" type="primary">确定</button>
					</view>
				</form>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getDicts
	} from "@/api/system/dict/data.js"
	import {
		getCurrentDateTime
	} from "@/utils/getCurrentDateTime.js"
	import {
		performanceList,
		performanceAdd
	} from '@/api/compositive-performance/index.js';
	export default {
		data() {
			return {
				topLoading: true,
				loading: false,
				type: 'center',
				years: [],
				yearIndex: 0,
				monthIndex: 0,
				statusText: "审核状态",
				statusList: ["全部", "待审核", "审核通过", "不通过"],
				timeText: "全部",
				timeList: ["2024年", "2025年"],
				FillingList: [], //填报列表
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					years: "",
					fdName: uni.getStorageSync("stuName")
				},
			}
		},
		filters: {
			formateCommitStatus(val) {
				if (val == 0 || val == null) {
					return "填报中";
				} else if (val == 1) {
					return "已提交";
				}
			}
		},
		onShow() {
			this.getPerformanceList();
		},
		onLoad() {
			this.getYears();
		},
		methods: {
			popupChange(e) {
				if (e.show) {
					this.years.shift();
				} else {
					this.years.unshift({
						dictValue: "全部"
					})
				}
			},
			scrolltolower() {
				if (this.queryParams.pageNum < this.totalPages) {
					this.queryParams.pageNum++;
					this.loading = true;
					setTimeout(() => {
						this.getPerformanceList()
					}, 1000)
				} else {

				}
			},
			async getPerformanceList() {
				let res = await performanceList(this.queryParams);
				if (res.code == 200) {
					this.loading = false;
					if (this.queryParams.pageNum == 1) {
						this.FillingList = res.rows; // 如果是第一页，直接显示新数据
					} else {
						this.FillingList = this.FillingList.concat(res.rows); // 否则追加新数据到列表中
					}
					this.totalPages = Math.ceil(res.total / this.queryParams.pageSize);
					this.topLoading = false;

					// //计算填报进度值
					// this.FillingList.forEach((progress, index) => {
					// 	this.FillingList[index].fillProgress = 0;
					// 	if (progress.activeList.length > 0) {
					// 		this.FillingList[index].fillProgress = Number(this.FillingList[index]
					// 			.fillProgress) + 20;
					// 	}
					// 	if (progress.passList.length > 0) {
					// 		this.FillingList[index].fillProgress = Number(this.FillingList[index]
					// 			.fillProgress) + 20;
					// 	}
					// 	if (progress.rwList.length > 0) {
					// 		this.FillingList[index].fillProgress = Number(this.FillingList[index]
					// 			.fillProgress) + 20;
					// 	}
					// 	if (progress.servingList.length > 0) {
					// 		this.FillingList[index].fillProgress = Number(this.FillingList[index]
					// 			.fillProgress) + 20;
					// 	}
					// 	if (progress.taskList.length > 0) {
					// 		this.FillingList[index].fillProgress = Number(this.FillingList[index]
					// 			.fillProgress) + 20;
					// 	}
					// })
				}
			},
			yearChange(e) {
				this.yearIndex = e.detail.value;
			},
			onChangeTime(e) {
				this.topLoading = true;
				this.FillingList = [];
				this.timeText = this.years[e.detail.value].dictValue;
				if (this.timeText == "全部") {
					this.queryParams.years = "";
				} else {
					this.queryParams.years = this.timeText;
				}
				this.queryParams.pageNum = 1;
				setTimeout(() => {
					this.getPerformanceList();
				}, 500)
			},
			onChangeStatus(e) {
				this.topLoading = true;
				this.FillingList = [];
				let index = e.detail.value;
				let auditStatus;
				if (index == 0) {
					this.queryParams.xwstatus = "";
					this.queryParams.yjstatus = "";
					this.queryParams.ksstatus = "";
					this.queryParams.xgstatus = "";
					this.queryParams.shstatus = "";
				} else if (index == 1) {
					this.queryParams.shstatus = 0;
				} else if (index == 2) {
					this.queryParams.shstatus = 1;
				} else {
					// 获取审核不通过的数据
					this.queryParams.shstatus = 2;

				}
				this.statusText = this.statusList[index];
				this.queryParams.pageNum = 1;
				setTimeout(() => {
					this.getPerformanceList();
				}, 500)
			},
			addRecordsPopup() {
				this.$refs.popupTime.open(this.type);
			},
			async topersonalReporting(e) {
				this.$refs.popupTime.close();
				let year = this.years[(e.detail.value.year) + 1].dictValue;
				let formData = {
					fdTime: getCurrentDateTime(),
					shstatus: 0,
					years: year,
				}
				let res = await performanceAdd(formData);
				if (res.code == 200) {
					const searchParams = new URLSearchParams({
						year: year,
						id: res.data.id
					});
					uni.navigateTo({
						url: `./personal-reporting?${searchParams.toString()}`
					})
				}

			},
			toPersonalReportingPage(params) {
				const searchParams = new URLSearchParams({
					id: params.id,
					year: params.years,
					substatus: params.substatus
				});
				uni.navigateTo({
					url: `./personal-reporting?${searchParams.toString()}`
				})
			},
			onCancel() {
				this.$refs.popupTime.close();
			},
			async getYears() {
				let years = await getDicts("sys_teacher_kpi_filling_year");
				this.years = years.data;
				this.years.unshift({
					dictValue: "全部"
				})
			},
			//驳回弹窗
			showRefuseModal(item) {
				let refuseTxt = "";
				let title = "";
				if (item.xwstatus == 2) {
					title = "学务";
					refuseTxt = item.xwidea
				}
				if (item.yjstatus == 2) {
					title = "书记";
					refuseTxt = item.yjidea
				}
				if (item.ksstatus == 2) {
					title = "科室";
					refuseTxt = item.ksidea
				}
				if (item.xgstatus == 2) {
					title = "学工处长";
					refuseTxt = item.xgidea
				}
				uni.showModal({
					title: title + "驳回意见",
					content: refuseTxt,
					confirmText: "去修改",
					success: (bool) => {
						if (bool.confirm) {
							const searchParams = new URLSearchParams({
								id: item.id,
								year: item.years,
								substatus: item.substatus
							});
							uni.navigateTo({
								url: `./personal-reporting?${searchParams.toString()}`
							})
						}
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.empty {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 200rpx;
		color: #9E9E9E;
		font-size: 36rpx;

		image {
			width: 250rpx;
			margin-bottom: 50rpx;
		}
	}

	.filling-record {
		background-color: #F5F5F7;
		min-height: 100vh;

		.tabs {
			position: fixed;
			left: 0;
			right: 0;
			display: flex;
			background-color: white;
			z-index: 2;
			color: #1890FF;
			box-shadow: 1px 1px 2px #999;
			height: 90rpx;
			line-height: 90rpx;
			padding: 20rpx 0;

			&>view {
				flex: 1;
				display: flex;
				align-items: center;
				justify-content: center;

				&:first-child {
					border-right: 1.4px solid #1890FF;
				}
			}
		}

		scroll-view {
			height: calc(100vh - 10px);

			.no-more {
				text-align: center;
				color: gray;
				padding-bottom: 10px;
			}
		}

		.list {
			padding: 120rpx 40rpx 15rpx 40rpx;

			.item {
				background-color: white;
				margin-bottom: 20rpx;
				padding: 40rpx;
				border-radius: 20rpx;
				border-bottom-right-radius: 0;
				position: relative;

				.top {
					display: flex;
					justify-content: space-between;
					border-bottom: 1px solid #F5F5F7;
					padding-bottom: 20rpx;

					.uni-icons {
						opacity: 0.5;
					}
				}

				.content {
					padding-top: 20rpx;

					&>view:not(:last-child) {
						margin-bottom: 15rpx;

						.progress {
							color: #1890FF;
						}
					}

					.status {
						position: absolute;
						bottom: 0;
						right: 0;
						color: white;

						.status-text {
							position: absolute;
							bottom: 15px;
							right: 8%;
							font-size: 35rpx;
						}

						.triangle-right {
							width: 0;
							height: 0;
							/* 上边框设置为透明 */
							border-left: 120px solid transparent;
						}

						&.submit {
							color: #202020;

							.triangle-right {
								/* 左边框设置为与文本相同的颜色 */
								border-bottom: 100px solid #D7D7D7;
							}
						}

						&.agree {
							.triangle-right {
								/* 左边框设置为与文本相同的颜色 */
								border-bottom: 100px solid #2FB15B;
							}
						}

						&.refuse {
							.triangle-right {
								/* 左边框设置为与文本相同的颜色 */
								border-bottom: 100px solid #FF759F;
							}
						}
					}
				}
			}
		}

		.add {
			background-color: #1890FF;
			width: 90rpx;
			height: 90rpx;
			border-radius: 50%;
			color: white;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 60rpx;
			position: fixed;
			bottom: 50px;
			right: 25px;
		}

		.popup-time {
			width: 80%;

			.popup-content {
				width: 450rpx;
				height: 400rpx;
				padding: 40rpx 0rpx 40rpx;

				.title {
					text-align: center;
					padding-bottom: 20rpx;
					font-weight: bold;
					font-size: 32rpx;
					color: #3D3D3D;
					border-bottom: 1px solid #F5F5F7;
				}

				.form-item {
					width: 100%;
					margin-top: 50rpx;
					padding: 0 40rpx;

					label {
						font-weight: bold;
						color: #202020;
					}

					picker {
						margin-top: 20rpx;
						border: 1px solid #E1E1E1;
						height: 72rpx;
						line-height: 72rpx;
						padding-left: 8px;
						border-radius: 10rpx;

						.uni-input {
							display: flex;
							color: #202020;

							.val {
								flex: 1;
							}

							.uni-icons {
								margin: 0 20rpx;
								opacity: 0.5;
							}
						}
					}
				}

				.btns {
					display: flex;
					margin-top: 50rpx;
					position: absolute;
					bottom: 0;
					left: 0;
					right: 0;

					button {
						flex: 1;
						border-radius: 0;
						height: 80rpx;
						line-height: 80rpx;

						&:first-child {
							background-color: #ffffff;
							border-top: 1px solid #F5F5F7;
						}

						&:last-child {
							background-color: #258FE4;
							border-top: 1px solid #258FE4;
						}
					}
				}

			}
		}
	}
</style>