<template>
	<view class="personal-reporting">
		<scroll-view class="nav-scroll" scroll-x="true">
			<view class="navs">
				<text @tap="tabChange(index)" :class="[tabIndex==index?'active':'']" v-for="(item,index) in tabs"
					:key="index">{{item}}</text>
			</view>
		</scroll-view>
		<view class="form">
			<StudentManagement :commitStatus="commitStatus" :queryDetailParams="queryDetailParams" ref="studentManagement" v-if="tabIndex==0" />
			<ProfessionalWork :commitStatus="commitStatus" :queryDetailParams="queryDetailParams" ref="ProfessionalWork" v-if="tabIndex==1" />
			<AttendanceManagement :commitStatus="commitStatus" :queryDetailParams="queryDetailParams" ref="AttendanceManagement"
				v-if="tabIndex==2" />
			<NegativeList :commitStatus="commitStatus" :queryDetailParams="queryDetailParams" ref="NegativeList" v-if="tabIndex==3" />
			<OverWork :commitStatus="commitStatus" :queryDetailParams="queryDetailParams" ref="OverWork" v-if="tabIndex==4" />
			<StudentEmergencies :commitStatus="commitStatus" :queryDetailParams="queryDetailParams" ref="StudentEmergencies" v-if="tabIndex==5" />
			<view class="btns" v-if="commitStatus==0">
				<button type="primary" class="prev-page" v-if="tabIndex!=0" @click="onPrevPage">上一页</button>
				<button type="primary" class="next-page" @click="onNextPage" v-if="tabIndex!=5">保存</button>
				<button type="primary" class="next-page" @click="onSubmit" v-if="tabIndex==5">提交</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		kpiFillingUpdate,
		teacherKpiFillingMgtAdd,
		teacherKpiFillingMgtUpdate,
		teacherKpiFillingMgtDetail,
		kpiFillingBusinessWorkAdd,
		kpiFillingBusinessWorkUpdate,
		kpiFillingAMgtAdd,
		kpiFillingAMgtUpdate,
		kpiFillingNegativeListAdd,
		kpiFillingNegativeListUpdate
	} from "@/api/instructor/superintendent.js"
	import StudentManagement from "../components/student-management.vue";
	import ProfessionalWork from "../components/professional-work.vue";
	import AttendanceManagement from "../components/attendance-management.vue";
	import NegativeList from "../components/negative-list.vue";
	import OverWork from "../components/reward-performance-overwork.vue";
	import StudentEmergencies from "../components/student-emergencies.vue";
	export default {
		components: {
			StudentManagement,
			ProfessionalWork,
			AttendanceManagement,
			NegativeList,
			OverWork,
			StudentEmergencies,
			reportingId: ""
		},
		data() {
			return {
				tabs: ["学生管理", "业务管理", "考勤管理", "负面清单", "超工作量奖励绩效", "处理学生突发事件"],
				tabIndex: 0,
				fillingYear: "",
				fillingMonth: "",
				queryDetailParams: {
					fdyName: uni.getStorageSync("stuName"),
					fillingMonth: "",
					fillingYear: ""
				},
				//当前填报信息id
				reportingId: "",
				//提交状态
				commitStatus: "",
				completionStatus: "", //完成状态
				roleAudit:"",//角色
				tabConfigs: {
					0: {
						ref: 'studentManagement',
						addFunc: teacherKpiFillingMgtAdd,
						updateFunc: teacherKpiFillingMgtUpdate,
					},
					1: {
						ref: 'ProfessionalWork',
						addFunc: kpiFillingBusinessWorkAdd,
						updateFunc: kpiFillingBusinessWorkUpdate,
					},
					2: {
						ref: 'AttendanceManagement',
						addFunc: kpiFillingAMgtAdd,
						updateFunc: kpiFillingAMgtUpdate,
					},
					3: {
						ref: 'NegativeList',
						addFunc: kpiFillingNegativeListAdd,
						updateFunc: kpiFillingNegativeListUpdate,
					},
					4: {
						ref: 'OverWork',
					},
					5: {
						ref: 'StudentEmergencies',
					}
				}
			}
		},
		onLoad(option) {
			this.fillingYear = option.year;
			this.fillingMonth = option.month;
			this.reportingId = option.reportingId;
			this.queryDetailParams.fillingMonth = option.month;
			this.queryDetailParams.fillingYear = option.year;
			this.commitStatus = option.commitStatus;
			this.completionStatus = option.completionStatus;
			this.roleAudit = option.roleAudit;
		},
		methods: {
			tabChange(index) {
				this.tabIndex = index;
			},
			async onPrevPage() {
				this.tabIndex--
			},
			// 辅助方法，用于检查formData中是否有空值  
			hasEmptyValues(formData, excludedKeys = []) {
				for (const key in formData) {
					if (formData.hasOwnProperty(key) && !excludedKeys.includes(key)) { // 排除指定的字段  
						const value = formData[key];
						if (value === null || value === undefined || value === "" ||
							(typeof value === 'object' && Object.keys(value).length === 0) // 检查嵌套对象是否为空对象  
						) {
							return true; // 发现空值，返回true  
						}
					}
				}
				return false; // 没有发现空值，返回false  
			},
			async onNextPage() {
				if (this.tabIndex in this.tabConfigs) {
					const {
						ref,
						addFunc,
						updateFunc
					} = this.tabConfigs[this.tabIndex];
					// 获取表单数据  
					let formData = this.$refs[ref].getFormData();
					formData = {
						...formData,
						departmentName: uni.getStorageSync("deptName"),
						fdyName: uni.getStorageSync("stuName"),
						fillingYear: this.fillingYear,
						fillingMonth: this.fillingMonth,
					};
					// 检查formData中是否有空值  
					if (this.hasEmptyValues(formData, ['id'])) {
						uni.showToast({
							title: "请填写完整内容！",
							icon: 'none' // 根据需要设置图标  
						});
						return; // 停止执行后续操作  
					}
					if (this.tabIndex !== 4 && this.tabIndex !== 5) {
						if (formData.id || formData.id === 0) {
							let res = await updateFunc(formData);
							if (res.code == 200) {
								uni.showToast({
									title: "修改成功",
								});
							}
						} else {
							let res = await addFunc(formData);
							if (res.code == 200) {
								uni.showToast({
									title: "保存成功",
								});
								this.completionStatus = this.completionStatus != null ? parseInt(this
									.completionStatus) + 20 : 20;
								await kpiFillingUpdate({
									id: this.reportingId,
									completionStatus: this.completionStatus
								});
							}
						}
					}
					setTimeout(() => {
						if (this.tabIndex < Object.keys(this.tabConfigs).length - 1) { // 确保不会超出最大索引  
							this.tabIndex++;
						}
					}, 500);
				}
			},
			onSubmit() {
				if (this.completionStatus < 80) {
					uni.showModal({
						title: "当前内容没有填写完毕，是否提交?",
						success:(res) => {
							if (res.confirm) {
								this.updatekpiFilling()
							}
						}
					})
				} else {
					uni.showModal({
						title: "确定提交填报数据吗?",
						success:(bool) => {
							if (bool.confirm) {
								this.updatekpiFilling()
							}
						}
					})
				}
			},
			async updatekpiFilling() {
				let submitFormData = {
					completionStatus: 100,
					commitStatus: 1,
					id: this.reportingId
				}
				if(this.roleAudit==1){
					submitFormData.auditStatus=0;
				}
				console.log(submitFormData);
				// return;
				let res = await kpiFillingUpdate(submitFormData);
				if (res.code == 200) {
					uni.showToast({
						title: "提交成功",
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.personal-reporting {
		background-color: #F5F5F7;
		min-height: 95vh;

		.nav-scroll {
			position: fixed;
			white-space: nowrap;
			width: 100%;
			background-color: white;
			z-index: 2;

			.navs {
				padding: 26rpx 40rpx;

				text {
					margin-right: 30rpx;
					color: #202020;
					opacity: 0.5;

					&.active {
						opacity: 1;
						font-weight: bold;
						border-bottom: 3px solid #1890FF;
						padding-bottom: 10px;
					}
				}
			}
		}

		.form {
			padding: 120rpx 40rpx 100rpx;
			margin-bottom: 100px;

			.btns {
				position: fixed;
				bottom: 0;
				left: 0;
				right: 0;
				display: flex;
				margin-top: 100rpx;
				padding: 20rpx 20rpx;
				background-color: white;

				button {
					flex: 1;
					height: 44px;
				}

				.prev-page {
					background-color: transparent;
					color: #1890FF;
					border: 1px solid #1890FF;
					margin-right: 40rpx;
				}
			}
		}

	}
</style>