<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.fdyName}}
						<uni-icons type="right" size="18" color="#202020"></uni-icons>
					</view>
					<view class="content">
						<view>自评分数：{{handleSelfScoring(item)}}</view>
						<view>填报时间：{{item.fillingYear}}年{{item.fillingMonth}}月</view>
						<view>填报进度：<text class="progress">{{item.completionStatus}}%</text></view>
						<view>提交状态：
							<text v-if="item.auditStatus==2&&item.commitStatus==0">待修改</text>
							<text v-else>{{item.commitStatus|formateCommitStatus}}</text>
						</view>
						<!-- auditStatus  0 待审核  2 不通过   4 1 通过 -->
						<view class="status refuse" @tap.stop="showRefuseModal(item)"
							v-if="item.auditStatus==2&&item.commitStatus==0">
							<text class="status-text">不通过</text>
							<view class="triangle-right"></view>
						</view>
						<!-- <view class="status submit" v-else-if="item.auditStatus==2&&item.commitStatus==1">
							<text class="status-text">待审核</text>
							<view class="triangle-right"></view>
						</view> -->
						<view class="status agree" v-else-if="item.roleAudit == 4 && item.auditStatus == 1">
							<text class="status-text">通过</text>
							<view class="triangle-right"></view>
						</view>
						<view class="status submit" v-else-if="item.commitStatus==0">
							<text class="status-text">填报中</text>
							<view class="triangle-right"></view>
						</view>
						<!-- substatus -->
						<view class="status submit" v-else @tap.stop="showAuditModal(item)">
							<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="form-item">
						<label>选择月份</label>
						<picker v-if="months.length>0" name="month" range-key="dictValue" @change="monthChange"
							:value="monthIndex" :range="months">
							<view class="uni-input">
								<text class="val">{{months[monthIndex].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>
		<uni-popup ref="popupAuditStatus" class="popup-audit " background-color="#ffffff">
			<view class="popup-content">
				<view class="title">
					审批进度
				</view>
				<view class="steps" v-for="(item,index) in auditSteps" :key="index">
					<view class="step">
						<uni-icons v-if="roleAudit>index&&item.auditStatus!=2" type="checkbox-filled" size="26"
							color="green"></uni-icons>
						<uni-icons v-else-if="roleAudit>index&&item.auditStatus==2" type="close" size="26"
							color="red"></uni-icons>
						<view class="step-num" v-else>
							{{index+1}}
						</view>
						<image src="../../../../static/line.png" mode="widthFix"></image>
						<view class="desc" :class="className('类名',item.auditStatus,item.roleAudit,index)">
							<text>{{item.title}}</text>
							<text>审核意见:{{item.roleAudit<=roleAudit?item.desc||'暂无填写':'等待审核'}}</text>
						</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getDicts
	} from "@/api/system/dict/data.js"
	import {
		kpiFillingAdd,
		kpiFillingList
	} from '@/api/instructor/superintendent.js';
	export default {
		data() {
			return {
				topLoading: true,
				loading: false,
				type: 'center',
				years: [],
				months: [],
				yearIndex: 0,
				monthIndex: 0,
				statusText: "审核状态",
				statusList: ["全部", "待审核", "通过", "不通过"],
				timeText: "全部",
				timeList: ["2024年", "2025年"],
				FillingList: [], //填报列表
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					fillingYear: "",
					auditStatus: "",
					fdyName: uni.getStorageSync("stuName"),
				},
				//审批进度
				auditSteps: [{
					title: '学务办初审并赋分',
					desc: '暂无填写',
					roleAudit: 1
				}, {
					title: '党总支书记审核',
					desc: '暂无填写',
					roleAudit: 2
				}, {
					title: '科室复核',
					desc: '暂无填写',
					roleAudit: 3
				}, {
					title: '学工处长审核',
					desc: '暂无填写',
					roleAudit: 4
				}],
				roleAudit: 0,
				auditStatus: 0
			}
		},
		filters: {
			formateCommitStatus(val) {
				if (val == 0) {
					return "填报中";
				} else {
					return "已提交"
				}
			}
		},
		onShow() {
			this.topLoading = true;
			this.queryParams.pageNum = 1;
			this.FillingList = [];
			this.getKpiFillingList();
		},
		onLoad() {
			this.getYears();
			this.getMonths();
		},
		methods: {
			className(text, auditStatus, roleAudit, index) {
				if (text == "类名") {
					if (auditStatus == 2) {
						return 'error'
					} else if (this.roleAudit >= roleAudit) {
						return 'success'
					} else {
						return 'await'
					}
				}
			},
			//审批进度弹窗
			showAuditModal(item) {
				this.roleAudit = item.roleAudit;
				this.auditStatus = item.auditStatus;
				this.$refs.popupAuditStatus.open(this.type);
				let tip = "";
				this.auditSteps = this.auditSteps.map((data, index) => {
					if (index == 0) {
						data.desc = item.xwOpinion;
					} else if (index == 1) {
						data.desc = item.dzzsjOpinion;
					} else if (index == 2) {
						data.desc = item.ksOpinion;
					} else if (index == 3) {
						data.desc = item.xgczOpinion;
					}
					if (item.roleAudit == index + 1 && item.auditStatus == 2) {
						data.auditStatus = item.auditStatus;
					} else {
						data.auditStatus = "";
					}
					return data;
				})
			},
			showRefuseModal(item) {
				const searchParams = new URLSearchParams({
					year: item.fillingYear,
					month: item.fillingMonth,
					month: item.fillingMonth,
					reportingId: item.id,
					commitStatus: item.commitStatus,
					roleAudit: item.roleAudit
				});
				if (item.auditStatus == 2) {
					let refuseTxt = "";
					let title = "";
					if (item.roleAudit == 1) {
						refuseTxt = item.xwOpinion;
						title = "学务";
					} else if (item.roleAudit == 2) {
						refuseTxt = item.dzzsjOpinion;
						title = "书记";
					} else if (item.roleAudit == 3) {
						title = "科室";
						refuseTxt = item.ksOpinion;
					} else {
						title = "学工处长";
						refuseTxt = item.xgczOpinion;
					}
					uni.showModal({
						title: title + "驳回意见",
						content: refuseTxt,
						confirmText: "去修改",
						success(bool) {
							if (bool.confirm) {
								uni.navigateTo({
									url: `/pages/instructor/performance-appraisal/performance-evaluation/personal-reporting?${searchParams.toString()}`
								})
							}
						}
					})
				} else {
					uni.navigateTo({
						url: `/pages/instructor/performance-appraisal/performance-evaluation/personal-reporting?${searchParams.toString()}`
					})
				}
			},
			// 计算自评分数
			handleSelfScoring(row) {
				let scoring = 0
				// 学生管理分数
				row.kpiFillingStuMgtList.forEach(element => {
					if (element.hasOwnProperty('id')) {
						scoring += element.cadreScoring
						scoring += element.classScoring
						scoring += element.stuActivityScoring
						scoring += element.stuTalkScoring
						scoring += element.visitDormitoryScoring
					}
				});
				// 业务工作分数
				row.kpiFillingBusinessWorksList.forEach(element => {
					if (element.hasOwnProperty('id')) {
						scoring += element.stuLeaveMaterialsScoring
						scoring += element.stuFillingMaterialsScoring
						scoring += element.stuBasicDataScoring
						scoring += element.stuDisciplinaryViolationScoring
						scoring += element.handleEventsScoring
					}
				});
				// 考勤管理分数
				row.kpiFillingAMgtList.forEach(element => {
					if (element.hasOwnProperty('id')) {
						scoring += element.dutyWorkScoring
						scoring += element.conferenceScoring
					}
				});
				return scoring
			},
			scrolltolower() {
				if (this.queryParams.pageNum < this.totalPages) {
					this.queryParams.pageNum++;
					this.loading = true;
					setTimeout(() => {
						this.getKpiFillingList()
					}, 1000)
				} else {

				}
			},
			async getKpiFillingList() {
				let res = await kpiFillingList(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;
				}
			},
			popupChange(e) {
				if (e.show) {
					this.years.shift();
				} else {
					this.years.unshift({
						dictValue: "全部"
					})
				}
			},
			yearChange(e) {
				this.yearIndex = e.detail.value;
			},
			monthChange(e) {
				this.monthIndex = e.detail.value;
			},
			onChangeTime(e) {
				this.topLoading = true;
				this.FillingList = [];
				this.timeText = this.years[e.detail.value].dictValue;
				if (this.timeText == "全部") {
					this.queryParams.fillingYear = "";
				} else {
					this.queryParams.fillingYear = this.timeText;
				}
				this.queryParams.pageNum = 1;
				setTimeout(() => {
					this.getKpiFillingList();
				}, 500)
			},
			onChangeStatus(e) {
				this.topLoading = true;
				this.FillingList = [];
				let index = e.detail.value;
				let auditStatus;
				if (index === 0) { // "全部"状态  
					auditStatus = ''; // 或者根据你的需求设置为null或undefined  
				} else {
					auditStatus = index - 1; // 因为数组索引从0开始，而你可能希望值从1开始  
				}
				this.statusText = this.statusList[index];
				this.queryParams.auditStatus = auditStatus;
				this.queryParams.pageNum = 1;
				setTimeout(() => {
					this.getKpiFillingList();
				}, 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 month = this.months[e.detail.value.month].dictValue;

				let formData = {
					fillingYear: year,
					fillingMonth: month,
				}
				let res = await kpiFillingAdd(formData);
				if (res.code == 200) {
					const searchParams = new URLSearchParams({
						year,
						month,
						commitStatus: 0,
						reportingId: res.data.id,
						completionStatus: 0
					});
					uni.navigateTo({
						url: `/pages/instructor/performance-appraisal/performance-evaluation/personal-reporting?${searchParams.toString()}`
					})
				}

			},
			toPersonalReportingPage(params) {
				const searchParams = new URLSearchParams({
					year: params.fillingYear,
					month: params.fillingMonth,
					reportingId: params.id,
					commitStatus: params.commitStatus,
					completionStatus: params.completionStatus
				});

				uni.navigateTo({
					url: `/pages/instructor/performance-appraisal/performance-evaluation/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: "全部"
				})
			},
			async getMonths() {
				let months = await getDicts("sys_teacher_kpi_filling_month");
				this.months = months.data;
			},

		}
	}
</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;
		}
	}

	.popup-audit {
		.popup-content {
			height: 750rpx !important;
		}

		.title {
			margin-bottom: 20rpx;
		}

		.steps {
			.step {
				margin-bottom: 90rpx;
				display: flex;
				margin-left: 40rpx;
				align-items: center;

				.step-num {
					border: 2px solid;
					width: 40rpx;
					height: 40rpx;
					flex-shrink: 0;
					line-height: 40rpx;
					border-radius: 50%;
					display: flex;
					justify-content: center;
					align-items: center;
					color: #9E9E9E;

					&.default {
						color: #303133;
					}
				}



				.uni-icons {
					color: #C0C4CC;
				}

				image {
					flex: 0.7;
					margin: 0 15rpx;
				}

				.desc {
					flex: 1.5;
					display: flex;
					flex-direction: column;

					text:first-child {
						color: #303133;
						font-weight: bold;
					}

					text:last-child {
						font-size: 24rpx;
						margin-top: 10rpx;
					}

					&.default {
						color: #303133 !important;

						text:first-child {
							color: #303133;
							font-weight: bold;
						}
					}

					&.success {
						color: #13ce66 !important;

						text:first-child {
							color: #13ce66;
							font-weight: bold;
						}
					}

					&.error {
						color: #ff4949 !important;

						text:first-child {
							color: #ff4949;
							font-weight: bold;
						}
					}

					&.await {
						color: #C0C4CC !important;

						text:first-child {
							color: #C0C4CC;
							font-weight: bold;
						}
					}
				}
			}
		}
	}

	.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 #FF4183;
							}
						}
					}
				}
			}
		}

		.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,
		.popup-audit {
			width: 80%;

			.popup-content {
				width: 600rpx;
				height: 600rpx;
				padding: 40rpx 50rpx 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: 40rpx;
					padding: 0 50rpx;

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

					picker {
						margin-top: 20rpx;
						border: 1px solid darkgray;
						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>