<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.jg }}</text>
				</view>
				<view class="form-item">
					<text class="label">辅导员：</text>
					<text class="uni-input">{{formData.applicantName}}</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="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&&role!=='学生'">回退</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">
					<button class="download" @tap="onLook">下载退学下文</button>
				</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.reentryNumber" />
					</view>
					<view class="form-item">
						<label class="label"><text class="required">*</text>注销类型</label>
						<picker v-if="rt_logout_types.length>0" range-key="dictLabel" :value="logout_type_index"
							:range="rt_logout_types" @change="logoutTypeChange">
							<view class="uni-input">
								<text
									class="val">{{logout_type_index==-1?'请选择注销类型':rt_logout_types[logout_type_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="dropOutYears.length>0" @change="onYearsChangeTime" range-key="dictLabel"
							:range="dropOutYears" :value="dropOutIndex">
							<view class="uni-input">
								<text
									class="val">{{dropOutIndex==-1?'请选择退学年份':dropOutYears[dropOutIndex].dictLabel}}</text>
								<uni-icons type="calendar" size="25" color="#202020"></uni-icons>
							</view>
						</picker>
					</view>
					<view class="form-item">
						<label class="label"><text class="required">*</text>退学下文</label>
						<view class="upload-file" v-if="tempfiles.length==0" @tap="onUploadFile">
							<view class="add">
								+ 添加文件
							</view>
							<view class="tip">
								<view>文件大小:不超过 5MB</view>
								<view>文件格式:doc/xls/ppt/txt/pdf/xlsx </view>
							</view>
							<view class="file-list">
								<view class="item" v-for="(file,index) in tempfiles" :key="index">
									<image src="../../../../static/file.png" mode="widthFix"></image>
									<view class="right">
										<text>{{file.name}}</text>
										<text>{{file.name}}</text>
									</view>
								</view>
							</view>
						</view>
						<view class="file-list" v-else>
							<view class="files">
								<view class="item" v-for="(file,index) in tempfiles" :key="index">
									<image src="../../../../static/file.png" mode="widthFix"></image>
									<view class="right">
										<text>{{file.name}}</text>
										<text>{{file.size}}k</text>
									</view>
									<view class="del" @tap="onDel(index)">
										删除
									</view>
								</view>
							</view>
							<view v-if="tempfiles.length<3" class="btn" @tap="onUploadFile">
								+ 添加文件
							</view>
						</view>
					</view>
					<view class="form-item">
						<label class="label">备注</label>
						<textarea placeholder-class="placeholderColor" placeholder="请输入备注" v-model="taskForm.remark"
							rows="3"></textarea>
					</view>
				</view>
				<view class="btns">
					<button type="default" @tap="onCancel">取消</button>
					<button type="primary" @tap="approveDialogConfirm">确定</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 style="color: red;">*</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 {
		getRtStuDisqualificationByProcInsId,
		updateDisqualification
	} from '@/api/disqualifiCationIndex/disqualifiCationIndex'
	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 {
				navs: [{
						text: "表单信息",
						val: 0
					},
					{
						text: "流程跟踪",
						val: 1
					}
				],
				navIndex: 0,
				taskDotIndex: 0, //回退列表索引
				returnTaskList: [], // 回退列表数据
				formData: null,
				rt_quit_type: "",
				rt_quit_types: [],
				rt_logout_types: [], //注销类型列表
				logout_type_index: -1, //注销索引
				dropOutYears: [], //退学年份列表
				dropOutIndex: -1, //退学年份索引
				role: uni.getStorageSync("roles"),
				tempfiles: [], //临时文件
				inputReject: "",
				taskName: "", //节点名
				category: "",
				taskForm: {
					// multiple: false,
					comment: '', // 意见内容
					procInsId: '', // 流程实例编号
					instanceId: '', // 流程实例编号
					deployId: '', // 流程定义编号
					taskId: '', // 流程任务编号
					procDefId: '', // 流程编号
					vars: '',
					targetKey: '',
					// quitYear: '',
					ideologicalEducation: "",
					instructionSchoolHours: "",
					//
					reentryNumber: "", //退学文号
					disqualificatioType: "", //注销类型
					reentryYear: '', //退学年份
					ihandlingSuggestion: '', //选择的文件
				},
			}
		},
		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.getRtStuDisqualificationByProcInsId(this.taskForm.procInsId)
		},
		methods: {
			navChange(index) {
				this.navIndex = index;
			},
			taskDotChange(index, item) {
				this.taskDotIndex = index;
				this.taskForm.targetKey = item.id;
			},
			getRtStuDisqualificationByProcInsId(procInsId) {
				getRtStuDisqualificationByProcInsId(procInsId).then((res) => {
					this.formData = res.data;
					this.get_penalty_type(this.formData.dropOutType);
					this.get_logout_type();
					console.log(this.tag);
					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.length > 0) {
								const prevPage = pages[pages.length - 2]
								if (prevPage && typeof prevPage.getList === 'function') {
									prevPage.handleChange(2);
								}
							}

						}
					})
				})
				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',
				// 		'quitYear'
				// 	];
				// }
				if (this.taskName == '学生教育管理科审批归档') {
					requiredFields = [
						'reentryNumber',
						'disqualificatioType',
						'reentryYear',
						'ihandlingSuggestion'
					];
				}
				const emptyField = requiredFields.find(field => this.taskForm[field] === "");
				if (emptyField) {
					uni.showToast({
						title: `请填写完整内容`,
						icon: "none"
					})
					return;
				}
				console.log(this.taskForm);
				uni.showLoading({
					title: "正在审批"
				});
				this.taskForm.comment = '同意';
				complete(this.taskForm).then(res => {
					if (res.code == 200) {
						if (this.taskName == '学生教育管理科审批归档' && this.category == 'disqualification') {
							//待修改
							this.formData.reentryNumber = this.taskForm.reentryNumber;
							this.formData.disqualificatioType = this.taskForm.disqualificatioType;
							this.formData.reentryYear = this.taskForm.reentryYear;
							this.formData.ihandlingSuggestion = this.taskForm.ihandlingSuggestion;
							this.formData.remark = this.taskForm.remark;
							this.updateDisqualification()
						}
					} else {
						uni.showToast({
							title: "审批成功"
						});
					}
					uni.hideLoading();
					uni.navigateBack({
						success: () => {
							const pages = getCurrentPages();
							if (pages.length > 0) {
								const prevPage = pages[pages.length - 2]
								if (prevPage && typeof prevPage.getList === 'function') {
									prevPage.handleChange(2);
								}
							}
						}
					})
				})
			},
			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.length > 0) {
									const prevPage = pages[pages.length - 2]
									if (prevPage && typeof prevPage.getList === 'function') {
										prevPage.handleChange(2);
									}
								}
							}
						})
					}
				})
			},
			updateDisqualification() {
				updateDisqualification(this.formData).then((response) => {
					uni.showToast({
						title: response.msg
					})
				})
			},
			get_penalty_type(ty) {
				getDicts('rt_quit_type').then(res => {
					this.rt_quit_types = res.data;
					this.rt_quit_type = getDiseaseTypeByDictValue("rt_quit_type", ty, res.data);
				});
			},
			//获取退学年份
			getYear() {
				getDicts('sys_teacher_kpi_filling_year').then(res => {
					this.dropOutYears = res.data;
				});
			},
			//切换退学年份
			onYearsChangeTime(e) {
				this.dropOutIndex = e.detail.value;
				this.taskForm.reentryYear = this.dropOutYears[e.detail.value].dictValue;
			},
			//获取注销类型
			get_logout_type(ty) {
				getDicts('rt_logout_type').then(res => {
					this.rt_logout_types = res.data;
				});
			},
			// 切换类型选择
			logoutTypeChange(e) {
				this.logout_type_index = e.detail.value;
				this.taskForm.disqualificatioType = this.rt_logout_types[e.detail.value].dictValue;
			},
			//上传文件
			onUploadFile() {
				uni.chooseFile({
					count: 3,
					extension: ['.doc', '.xls', '.ppt', '.docx'],
					success: (file) => {
						if (file.tempFiles.length > 0) {
							const fileExtension = file.tempFiles[0].name.split('.').pop(); // 提取后缀名 
							const fileSizeKB = (file.tempFiles[0].size / 1024).toFixed(2);
							this.tempfiles.push({
								name: file.tempFiles[0].name,
								extension: fileExtension,
								size: fileSizeKB
							})
						}

						uploadFile("/common/upload", file.tempFilePaths[0]).then((res) => {
							let result = JSON.parse(res);
							if (result.code == 200) {
								uni.showToast({
									title: "文件上传成功"
								})
								if (this.taskForm.ihandlingSuggestion !== "") {
									this.taskForm.ihandlingSuggestion = this.taskForm
										.ihandlingSuggestion +
										"," + result.fileName;
								} else {
									this.taskForm.ihandlingSuggestion = result.fileName;
								}
							}
						})
					}
				})
			},
			onDel(index) {
				uni.showModal({
					title: "确定删除文件吗?",
					success: (res) => {
						if (res.confirm) {
							this.tempfiles.splice(index, 1);
							let newFiles = this.taskForm.ihandlingSuggestion.split(",");
							newFiles.splice(index, 1)
							this.taskForm.ihandlingSuggestion = newFiles.join(",")
						}
					}
				})
			},
			onLook(){
				
			}

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

			}

			.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;
				// padding: 0 40rpx;

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

				.content {
					padding: 0 40rpx;
					.download{
						background-color: #FF4949;
						color: white;
						margin-top:60px;
					}
					.show-letter {
						.title {
							font-size: 30rpx;
						}

						.stu-name {
							margin-bottom: 8rpx;
						}

						.desc {
							color: #797979;
						}

						.bottom {
							button {
								margin: 20rpx 0;
							}

							.gaizhang {
								text-align: right;
								position: relative;

								image {
									width: 200rpx;
								}

								.time {
									position: absolute;
									right: 30rpx;
									bottom: 55rpx;
									display: flex;
									flex-direction: column;
								}


							}
						}
					}

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

						&.download {
							margin: 50rpx 0 30rpx;
							display: flex;
							align-items: center;
							color: #4097FE;
							justify-content: center;

							.file-icon {
								width: 50rpx;
								margin-right: 20rpx;
							}

							text {
								border-bottom: 1px solid #4097FE;
								padding-bottom: 2px;
							}
						}

						.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: 100rpx;
							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;
								}
							}
						}

						.upload-file {
							border: 1px solid #E1E1E1;
							padding: 20rpx;
							display: flex;
							flex-direction: column;
							align-items: center;
							text-align: center;

							.tip {
								margin-top: 15rpx;
								// color: #202020;
								color: #F56C6C;
								opacity: 0.8;
								font-size: 24rpx;

								view {
									margin-bottom: 8rpx;
								}
							}
						}

						.file-list {
							border: 1px solid #D8D8D8;
							border-radius: 20rpx;

							.files {
								.item {
									display: flex;
									padding: 40rpx 20rpx;
									align-items: center;

									&:not(:last-child) {
										border-bottom: 1px solid #E1E1E1;
									}

									image {
										width: 60rpx;
										margin-right: 20rpx;
									}

									.right {
										flex: 1;
										display: flex;
										flex-direction: column;

										text:last-child {
											color: #969696;
											margin-top: 10rpx;
										}
									}

									.del {
										color: red;
									}
								}

								.img {
									position: relative;
									width: 160rpx;
									height: 170rpx;
									margin-bottom: 15rpx;
									margin-right: 15rpx;

									image {
										width: 100%;
										height: 100%;
									}

									.remove {
										position: absolute;
										right: 0;
										top: 0;
										color: white;
										background-color: rgba(0, 0, 0, 0.4);
										width: 52rpx;
										height: 52rpx;
										display: flex;
										justify-content: center;
										align-items: center;
										border-radius: 50%;

									}
								}

							}

							.btn {
								border-top: 1px solid #D8D8D8;
								height: 100rpx;
								display: flex;
								justify-content: center;
								align-items: center;
								font-size: 32rpx;
							}
						}

						.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>