<template>
	<view class="detail">
		<Nav :navs="navs" @change="navChange" />
		<view class="form" v-if="navIndex==0">
			<view class="container" style="padding-bottom: 60px;" v-if="formData!=null">
				<!--学号 -->
				<view class="form-item">
					<text class="label">学号：</text>
					<text class="uni-input">{{ formData.stuNo }}</text>
				</view>

				<!--姓名 -->
				<view class="form-item">
					<text class="label">姓名：</text>
					<text class="uni-input">{{ formData.stuName }}</text>
				</view>

				<!-- 性别 -->
				<view class="form-item">
					<text class="label">性别：</text>
					<text class="uni-input">{{ formData.gender }}</text>
				</view>

				<!--院部 -->
				<view class="form-item">
					<text class="label">院部：</text>
					<text class="uni-input">{{ formData.departmentName }}</text>
				</view>

				<!--班级 -->
				<view class="form-item">
					<text class="label">年级：</text>
					<text class="uni-input">{{ formData.gradeName }}</text>
				</view>
				<view class="form-item">
					<text class="label">班级：</text>
					<text class="uni-input">{{ formData.className }}</text>
				</view>

				<!--联系电话 -->
				<view class="form-item">
					<text class="label">出生日期：</text>
					<text class="uni-input">{{ formData.birthday }}</text>
				</view>

				<!--家长姓名 -->
				<view class="form-item">
					<text class="label">家长姓名：</text>
					<text class="uni-input">{{ formData.parentName }}</text>
				</view>

				<!--家长联系电话 -->
				<view class="form-item">
					<text class="label">家长电话：</text>
					<text class="uni-input">{{ formData.parentPhone }}</text>
				</view>
				<view class="form-item">
					<text class="label">民族：</text>
					<text class="uni-input">{{ formData.mz }}</text>
				</view>
				<view class="form-item">
					<text class="label">籍贯：</text>
					<text class="uni-input">{{ formData.jg }}</text>
				</view>
				<view class="form-item">
					<text class="label">退学类型：</text>
					<text class="uni-input">{{rt_quit_type}}</text>
				</view>
				<view class="form-item">
					<text class="label">申请原因：</text>
					<text class="uni-input">{{ formData.reasonApplying }}</text>
				</view>

				<view class="form-item">
					<text class="label">辅导员联系情况：</text>
					<text class="uni-input">{{formData.ideologicalEducation}}</text>
				</view>
				<view class="form-item">
					<text class="label">在校时间说明：</text>
					<text class="uni-input">{{formData.instructionSchoolHours}}</text>
				</view>
				<view class="form-item">
					<text class="label">退学文号：</text>
					<text class="uni-input">{{formData.quitNumber}}</text>
				</view>
				<view class="form-item">
					<text class="label">退学类别：</text>
					<text class="uni-input">{{formData.rt_logouttype}}</text>
				</view>
				<view class="form-item">
					<text class="label">退学年份：</text>
					<text class="uni-input">{{formData.quitYear}}</text>
				</view>
				<view class="form-item">
					<text class="label">备注：</text>
					<text class="uni-input">{{formData.remark}}</text>
				</view>
				<view class="form-item" style="height: auto;">
					<text class="label">签名：</text>
					<view class="uni-input">
						<image class="form-img" :src="ImageUrl+formData.applySignature" mode=""></image>
					</view>
				</view>
				<view class="btns" v-if="tag==1">
					<button class="turn-down" @tap="onReject" v-if="role!=='学生'">驳回</button>
					<button class="return" @tap="onReturn" v-if="returnTaskList.length>0&&taskName!=='学生接收'">回退</button>
					<button class="submit" @tap="onComplete">审批</button>
				</view>
			</view>
		</view>
		<view class="progress" v-if="navIndex==1">
			<FlowStep :procInsId="taskForm.procInsId" :deployId="taskForm.deployId" />
		</view>
		<uni-popup class="popup" ref="approveDialog" type="dialog" background-color="#fff">
			<view class="popup-content">
				<view class="title">
					流程审批
				</view>
				<view class="content" v-if="taskName=='辅导员联系情况'">
					<view class="form-item">
						<label class="label"><text class="required">*</text>辅导员联系情况</label>
						<textarea type="text" placeholder-class="placeholderColor" placeholder="请输入辅导员联系情况"
							v-model="taskForm.ideologicalEducation" />
					</view>
					<view class="form-item">
						<label class="label"><text class="required">*</text>在校时间说明</label>
						<input type="text" placeholder-class="placeholderColor" placeholder="请输入在校时间说明"
							v-model="taskForm.instructionSchoolHours" />
					</view>
				</view>
				<view class="content" v-if="taskName=='学生教育管理科审核'">
					<view class="form-item">
						<label class="label"><text class="required">*</text>退学文号</label>
						<input type="text" placeholder-class="placeholderColor" placeholder="请输入退学文号"
							v-model="taskForm.quitNumber" />
					</view>
					<view class="form-item">
						<label class="label"><text class="required">*</text>退学类别</label>
						<picker v-if="dropOut_categorys.length>0" range-key="dictLabel" :value="dropOut_category_index"
							:range="dropOut_categorys" @change="dropOutTypeChange">
							<view class="uni-input">
								<text
									class="val">{{dropOut_category_index==-1?'请选择退学类别':dropOut_categorys[dropOut_category_index].dictLabel}}</text>
								<uni-icons type="down" size="16" color="#202020"></uni-icons>
							</view>
						</picker>
					</view>
					<view class="form-item">
						<label class="label"><text class="required">*</text>退学年份</label>
						<picker v-if="quitSchoolYears.length>0" @change="onQuitYearChangeTime" :range="quitSchoolYears"
							:value="quitSchoolIndex" range-key="dictLabel">
							<view class="uni-input">
								<text
									class="val">{{quitSchoolIndex==-1?'请选择退学年份':quitSchoolYears[quitSchoolIndex].dictLabel}}</text>
								<uni-icons type="calendar" size="25" color="#202020"></uni-icons>
							</view>
						</picker>
					</view>
					<view class="form-item">
						<label class="label">备注</label>
						<textarea v-model="taskForm.remark" type="text" placeholder-class="placeholderColor"
							placeholder="请输入备注"></textarea>
					</view>
				</view>
				<view class="btns">
					<button type="default" @tap="onCancel">取消</button>
					<button type="primary" @tap="approveDialogConfirm" :disabled="isSubmitting">确定</button>
				</view>
			</view>
		</uni-popup>
		<!-- 回退弹窗 -->
		<uni-popup class="popup return-dialog" ref="returnDialog" type="dialog" background-color="#fff">
			<view class="popup-content">
				<view class="title">
					退回流程
				</view>
				<view class="content">
					<view class="form-item">
						<label>退回节点</label>
						<scroll-view scroll-y="true">
							<view class="list">
								<view class="item" @tap="taskDotChange(index,item)"
									:class="index==taskDotIndex?'active':''" v-for="(item,index) in returnTaskList"
									:key="index">
									{{item.name}}
								</view>
							</view>
						</scroll-view>
					</view>
					<view class="form-item">
						<label><text class="required">*</text>退回意见</label>
						<textarea v-model="taskForm.comment"></textarea>
					</view>
				</view>
				<view class="btns">
					<button type="default" @tap="onCancel">取消</button>
					<button type="primary" @tap="returnDialogConfirm">确定</button>
				</view>
			</view>
		</uni-popup>
		<!-- 驳回弹窗 -->
		<uni-popup ref="rejectDialog" type="dialog">
			<uni-popup-dialog mode="input" title="驳回" v-model="inputReject" @confirm="rejectDialogConfirm"
				placeholder="请输入驳回理由"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import Nav from "@/components/navs/navs.vue"
	import FlowStep from "@/components/flow-step/flow-step.vue"
	import {
		baseUrl
	} from "@/config.js";
	import {
		getDiseaseTypeByDictValue
	} from "@/utils/dict.js";
	import uploadFile from "@/plugins/upload.js";
	import {
		getRtStuDropOutSchoolByprocInsId,
		updateRtStuDropOutSchool
	} from '@/api/rtStuDropOutSchool/rtStuDropOutSchool'
	import {
		getLeaveApplicationByProcInsId,
		complete,
		reject,
		returnList,
		returnTask,
		getDisciplinaryApplicationByProcInsId,
		getStuDisciplinaryRelieveByProcInsId,
		updateDisciplinaryApplication
	} from "@/api/task.js";
	import {
		getDicts
	} from "@/api/system/dict/data";
	export default {
		components: {
			Nav,
			FlowStep
		},
		data() {
			return {
				isSubmitting: false, //表单提交标志位
				navs: [{
						text: "表单信息",
						val: 0
					},
					{
						text: "流程跟踪",
						val: 1
					}
				],
				navIndex: 0,
				taskDotIndex: 0, //回退列表索引
				returnTaskList: [], // 回退列表数据
				formData: null,
				rt_quit_type: "", //退学类型
				rt_quit_types: [], //退学类型列表
				quit_type_index: 0, //退学类型索引

				dropOut_categorys: [], //退学类别列表
				dropOut_category_index: -1, //退学类别索引

				quitSchoolYears: [], //退学年份列表
				quitSchoolIndex: -1, //退学年份索引
				ImageUrl: baseUrl,
				role: uni.getStorageSync("roles"),
				inputReject: "",
				taskName: "", //节点名
				category: "",
				taskForm: {
					multiple: false,
					comment: '', // 意见内容
					procInsId: '', // 流程实例编号
					instanceId: '', // 流程实例编号
					deployId: '', // 流程定义编号
					taskId: '', // 流程任务编号
					procDefId: '', // 流程编号
					vars: '',
					targetKey: '',
					quitNumber: "", //退学文号
					quitYear: '',
					ideologicalEducation: "",
					instructionSchoolHours: "",
					//退学类别
					dropOutCategory: ''
				},
			}
		},
		onLoad(option) {
			let query = JSON.parse(option.query);
			this.taskId = query.taskId;
			this.category = query.category;
			this.tag = query.tag;
			this.taskForm.deployId = query.deployId;
			this.taskForm.instanceId = query.procInsId
			this.taskForm.procInsId = query.procInsId
			this.taskForm.executionId = query.executionId
			this.taskForm.taskId = query.taskId
			this.taskName = query.taskName
			this.getRtStuDropOutSchoolByprocInsId(this.taskForm.procInsId)
		},
		methods: {
			navChange(index) {
				this.navIndex = index;
			},
			taskDotChange(index, item) {
				this.taskDotIndex = index;
				this.taskForm.targetKey = item.id;
			},
			getRtStuDropOutSchoolByprocInsId(procInsId) {
				getRtStuDropOutSchoolByprocInsId(procInsId).then((res) => {
					this.formData = res.data;
					this.get_penalty_type(this.formData.dropOutType);
					this.get_penalty_category(this.formData.dropOutCategory);
					if (this.tag == 1) {
						this.getReturnList();
					}
					this.getYear();
				})
			},
			onComplete() {
				this.$refs.approveDialog.open();
			},
			onReturn() {
				this.$refs.returnDialog.open();
			},
			getReturnList() {
				returnList(this.taskForm).then((res) => {
					this.returnTaskList = res.data
				})
			},
			onReject() {
				this.$refs.rejectDialog.open();
			},
			onCancel() {
				this.$refs.approveDialog.close();
				this.$refs.returnDialog.close();
			},
			//回退确认
			returnDialogConfirm() {
				if (this.taskForm.targetKey == "") {
					this.taskForm.targetKey = this.returnTaskList[0].id;
				}
				if (this.taskForm.comment == "") {
					uni.showToast({
						title: `请填写回退意见`,
						icon: "none"
					});
					return;
				}
				uni.showLoading({
					title: "正在回退"
				});
				returnTask(this.taskForm).then((res) => {
					uni.hideLoading();
					uni.showToast({
						title: res.msg
					})
						uni.navigateBack({
							success: () => {
								const pages = getCurrentPages();
								if (pages && pages.length > 0) {
									const prevPage = pages[pages.length - 2] || pages[pages.length - 1];
                                    if (prevPage && typeof prevPage.handleChange === 'function') {
                                        prevPage.handleChange(this.tag || 1);
                                    }
								}

							}
						})
				})
				this.$refs.returnDialog.close();
			},
			approveDialogConfirm() {
				let requiredFields = [];
				if (this.taskName == '辅导员联系情况' && this.category == 'dropOut') {
					requiredFields = [
						'ideologicalEducation',
						'instructionSchoolHours'
					];
				} else if (this.taskName == '学生教育管理科审核' && this.category == 'dropOut') {
					requiredFields = [
						'quitNumber',
						'dropOutCategory',
						'quitYear'
					];
				}
				const emptyField = requiredFields.find(field => this.taskForm[field] === "");
				if (emptyField) {
					uni.showToast({
						title: `请填写完整内容`,
						icon: "none"
					})
					return;
				}
				uni.showLoading({
					title: "正在审批"
				});
				this.isSubmitting = true; // 设置为正在提交
				this.taskForm.comment = '同意';
				complete(this.taskForm).then(res => {
					if (res.code == 200) {
						if (this.taskName == '辅导员联系情况' && this.category == 'dropOut') {
							this.formData.ideologicalEducation = this.taskForm.ideologicalEducation
							this.formData.instructionSchoolHours = this.taskForm.instructionSchoolHours
							this.updateRtStuDropOutSchool()
						} else if (this.taskName == '学生教育管理科审核' && this.category == 'dropOut') {
							// 学生教育管理科审核，赋值
							this.formData.dropOutCategory = this.taskForm.dropOutCategory
							this.formData.quitNumber = this.taskForm.quitNumber
							this.formData.quitYear = this.taskForm.quitYear
							this.formData.remark = this.taskForm.remark
							this.updateRtStuDropOutSchool()
						}
					} else {
						uni.showToast({
							title: "审批成功"
						});
					}
					uni.hideLoading();
						uni.navigateBack({
							success: () => {
								const pages = getCurrentPages();
								if (pages && pages.length > 0) {
									const prevPage = pages[pages.length - 2] || pages[pages.length - 1];
                                    if (prevPage && typeof prevPage.handleChange === 'function') {
                                        prevPage.handleChange(this.tag || 1);
                                    }
								}

							}
						})
				})
			},
			rejectDialogConfirm() {
				if (this.inputReject == "") {
					uni.showToast({
						title: `请填写驳回理由`,
						icon: "none"
					})
					return;
				}
				uni.showLoading({
					title: "正在驳回"
				});
				let sData = {
					taskId: this.taskForm.taskId,
					comment: this.inputReject
				}

				reject(sData).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: "已驳回"
						})
						uni.hideLoading();
							uni.navigateBack({
								success: () => {
									const pages = getCurrentPages();
									if (pages && pages.length > 0) {
										const prevPage = pages[pages.length - 2] || pages[pages.length - 1];
                                        if (prevPage && typeof prevPage.handleChange === 'function') {
                                            prevPage.handleChange(this.tag || 1);
                                            console.log('刷新任务数据');
                                        }
									}
								}
							})
					}
				})
			},
			updateRtStuDropOutSchool() {
				updateRtStuDropOutSchool(this.formData).then((response) => {
					uni.showToast({
						title: response.msg
					})
				})
			},
			//获取退学类型
			get_penalty_type(ty) {
				getDicts('rt_reentry_type').then(res => {
					this.rt_quit_types = res.data;
					console.log("rt_quit_types");
					console.log(this.rt_quit_types);
					this.rt_quit_type = getDiseaseTypeByDictValue("rt_reentry_type", ty, res.data);
				});
			},
			//获取退学类别
			get_penalty_category(ty) {
				getDicts('rt_logout_type').then(res => {
					this.dropOut_categorys = res.data;
					this.formData.rt_logouttype = getDiseaseTypeByDictValue("rt_logout_type", ty, res.data);
				});
			},
			//切换退学类型
			dropOutTypeChange(e) {
				this.dropOut_category_index = e.detail.value;
				this.taskForm.dropOutCategory = this.dropOut_categorys[e.detail.value].dictValue;
			},
			//获取退学年份
			getYear() {
				getDicts('sys_teacher_kpi_filling_year').then(res => {
					this.quitSchoolYears = res.data;
				});
			},
			//切换退学年份
			onQuitYearChangeTime(e) {
				this.quitSchoolIndex = e.detail.value;
				this.taskForm.quitYear = this.quitSchoolYears[e.detail.value].dictValue;
			},
		}
	}
</script>

<style scoped lang="scss">
	.detail {
		padding-top: 90rpx;

		.container {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 10rpx;

			.form-item {
				width: 95%;
				height: 80rpx;
				display: flex;
				align-items: center;
				margin-bottom: 20rpx;
				justify-content: space-between;
				border-bottom: 1px solid #ededee;

			}

			.form-img {
				width: 100px;
				height: 100px;
				margin-bottom: 20rpx;
				border-bottom: 1px solid #ededee;
			}

			.label {
				text-align: right;
				margin-right: 20rpx;
				text-wrap: nowrap;
			}

			.uni-input {
				color: #888889;
				text-align: right;
			}

			.btn {
				width: 100%;
				height: 90rpx;
				display: flex;
				justify-content: space-between;

				position: fixed;
				bottom: 20px;
				left: 0;
				margin: 0 auto;
			}

			button {
				width: 30%;
				background-color: #87CEFA;
				color: white;
			}

			.btns {
				display: flex;
				background: white;
				position: fixed;
				bottom: 0;
				left: 0;
				right: 0;
				z-index: 999;

				button {
					height: 50px;
					line-height: 50px;
					flex: 1;
					border-radius: 0;

					&.turn-down {
						background-color: #FF3334;
					}

					&.return {
						background-color: #FFBA00;
					}

					&.submit {
						background-color: #0092FF;
					}
				}
			}
		}

		.progress {
			padding: 0 30rpx 30rpx;

			.title {
				font-weight: bold;
				color: #202020;
				display: inline-block;
				font-size: 30rpx;
				margin-bottom: 30rpx;
			}
		}

		.popup {
			.popup-content {
				width: 90vw;
				max-height: 86vh;
				overflow: scroll;

				.title {
					font-size: 40rpx;
					font-weight: bold;
					text-align: center;
					margin: 20rpx 0;
				}

				.content {
					padding: 0 40rpx;

					.form-item {
						.required {
							color: red;
							margin-right: 8rpx;
						}

						.label {
							display: inline-block;
							margin: 30rpx 0 20rpx;
							color: #202020;
							font-weight: 600;
						}


						input {
							display: inline-block;
							padding: 0 20rpx;
							height: 70rpx;
							// color: #D8D8D8;
						}

						textarea {
							height: 200rpx;
							padding: 10rpx;
						}

						.placeholderColor {
							color: #D8D8D8;
						}

						input,
						textarea {
							width: 100%;
							border: 1px solid #E1E1E1;
							border-radius: 16rpx;

						}

						picker {
							border: 1px solid #E1E1E1;
							height: 70rpx;
							line-height: 70rpx;
							padding: 0 30rpx;

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

								.val {
									flex: 1;
								}
							}
						}

						.uni-file-picker {
							/deep/ .uni-progress-bar {
								background-color: transparent !important;
							}
						}

					}
				}

				.btns {
					display: flex;
					margin-top: 80rpx;

					button {
						flex: 1;
						border-radius: 0;
						border-bottom: 0;

						&:first-child {
							background-color: transparent;
							border: 1px solid #4097FE;
							border-bottom: 0;
							color: #4097FE;
						}
					}
				}
			}
		}

		.return-dialog {
			.form-item {
				label {
					font-weight: bold;
					display: inline-block;
					margin-bottom: 10px;
				}

				&:last-child {
					margin-top: 15px;
				}

				.list {
					height: auto;
					max-height: 200px;
					display: flex;
					flex-wrap: wrap;
					// height: 200px;
					// display: flex;
					// flex-wrap: wrap;

					.item {
						width: 100%;
						height: 40px;
						line-height: 40px;
						text-align: center;

						&.active {
							background-color: #0092FF;
							color: white;
						}
					}
				}
			}
		}
	}
</style>