<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
	<div>
		<el-dialog :title="title" :close-on-click-modal="false" draggable width="1200px" height="500px" @close="close"
			append-to-body v-model="visible">
			<el-form :model="inputForm" ref="inputForm" v-loading="loading" :rules="rules"
				:class="method === 'view' ? 'readonly' : ''"
				:disabled="status === 'audit' || status === 'taskFormDetail' || method === 'view'" label-width="100px"
				@submit.native.prevent>
				<el-row :gutter="26">
					<el-col :span="12">
						<el-form-item label="开始时间" prop="startDate">

							<el-date-picker v-model="inputForm.startDate" type="date" placeholder="选择开始时间"
								style="width: 100%;"></el-date-picker>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="结束时间" prop="endDate">
							<el-date-picker v-model="inputForm.endDate" type="date" placeholder="选择结束时间"
								style="width: 100%;"></el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="毕业院校" prop="graduatedFrom">
							<el-input v-model="inputForm.graduatedFrom" placeholder="请输入毕业院校"></el-input>
						</el-form-item>
					</el-col>
					<!-- 
					<el-col :span="12">
						<el-form-item label="其他毕业院校" prop="otherGraduatedFrom">
							<el-input v-model="inputForm.otherGraduatedFrom" placeholder="如无可不填"></el-input>
						</el-form-item>
					</el-col> -->

					<el-col :span="12">
						<el-form-item label="专业" prop="speciality">
							<el-input v-model="inputForm.speciality" placeholder="请输入专业名称"></el-input>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="学历" prop="education">
							<el-select size="default" v-model="inputForm.education" placeholder="请选择学历"
								style="width: 100%;">
								<el-option v-for="item in $dictUtils.getDictList('education')" :key="item.value"
									:label="item.label" :value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="学位" prop="degree">
							<el-select size="default" @change="handleSelectDegree" v-model="inputForm.degree"
								placeholder="请选择学位" style="width: 100%;">
								<el-option v-for="item in $dictUtils.getDictList('degree')" :key="item.value"
									:label="item.label" :value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="是否最高学历" prop="isHighestEducation">

							<el-radio-group v-model="inputForm.isHighestEducation">
								<el-radio label="1">是</el-radio>
								<el-radio label="0">否</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="是否最高学位" prop="isHighestDegree">

							<el-radio-group v-model="inputForm.isHighestDegree">
								<el-radio label="1">是</el-radio>
								<el-radio label="0">否</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="学习形式" prop="learnForms">
							<el-select size="default" v-model="inputForm.learnForms" placeholder="请选择学习形式"
								style="width: 100%;">
								<el-option v-for="item in $dictUtils.getDictList('learn_form')" :key="item.value"
									:label="item.label" :value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="毕业类型" prop="graduationType">
							<el-select size="default" v-model="inputForm.graduationType" placeholder="请选择毕业类型"
								style="width: 100%;">
								<el-option v-for="item in $dictUtils.getDictList('graduation_type')" :key="item.value"
									:label="item.label" :value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>

					<!-- <el-col :span="12"> -->
					<!-- <el-form-item label="培养方式" prop="cultureMethod">
							<el-select v-model="inputForm.cultureMethod" placeholder="请选择培养方式">
								<el-option label="统招" value="统招"></el-option>
								<el-option label="委培" value="委培"></el-option>
								<el-option label="自费" value="自费"></el-option>
								<el-option label="统招全日制" value="1"></el-option>
								<el-option label="国家开放大学" value="2"></el-option>
								<el-option label="成人高考" value="3"></el-option>
								<el-option label="网络远程教育" value="4"></el-option>
								<el-option label="自考" value="5"></el-option>
							</el-select>
						</el-form-item> -->
					<!-- </el-col> -->

					<el-col :span="12">
						<el-form-item class="star-require fileUpload" label="学历附件" prop="educationFile">
							<UpLoadComponent ref="educationUploadComponent"></UpLoadComponent>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item :class="{
							fileUpload: true,
							'star-require': degreeFileFlag ? true : false
						}" label="学位附件" prop="degreeFile">
							<UpLoadComponent ref="degreeUploadComponent"></UpLoadComponent>
						</el-form-item>
					</el-col>

					<!-- <el-col :span="24">
						<el-form-item label="专业描述" prop="specialityDesc">
							<el-input type="textarea" v-model="inputForm.specialityDesc" placeholder="请输入专业描述"
								:rows="4"></el-input>
						</el-form-item>
					</el-col> -->
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="close()" icon="el-icon-circle-close">关闭</el-button>
					<el-button v-if="method === 'edit' || method == 'add'" type="primary" icon="el-icon-circle-check"
						@click="doSubmit()">确定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script>
import UpLoadComponent from '@/views/common/UpLoadComponentCardStyle'
import enrollmentRegistrationService from '@/api/human/enrollment/EnrollmentRegistrationService'
import OSSSerivce, {
	httpRequest,
	fileName,
	toHref,
} from '@/api/sys/OSSService'
export default {
	props: {
		businessId: {
			type: String,
			default: ''
		},
		formReadOnly: {
			type: Boolean,
			default: false
		},
		status: {
			type: String,
			default: ''
		}
	},
	data() {
		return {
			degreeFileFlag: false,
			disabled: false,
			title: '',
			method: '',
			visible: false,
			loading: false,
			inputForm: {
				graduatedFrom: "",
				otherGraduatedFrom: "",
				startDate: "",
				endDate: "",
				speciality: "",//专业
				education: "",//学历
				degree: "",//学位
				isHighestEducation: "1",
				isHighestDegree: "1",
				learnForms: "",//学习形式
				graduationType: "",//毕业类型
				cultureMethod: "",//培养方式
				educationFile: [],
				degreeFile: [],
				specialityDesc: "",
				enrollmentRegistrationId: ""
			},
			keyWatch: '',
			rules: {
				graduatedFrom: [
					{ required: true, message: '毕业院校不能为空', trigger: 'blur' }
				],
				startDate: [
					{ required: true, message: '开始时间不能为空', trigger: 'change' }
				],
				endDate: [
					{ required: true, message: '结束时间不能为空', trigger: 'change' },
					{
						validator: (rule, value, callback) => {
							const start = this.inputForm.startDate;
							if (!start || !value) return callback();
							if (new Date(start).getTime() > new Date(value).getTime()) {
								callback(new Error('结束时间必须晚于开始时间'));
							} else {
								callback();
							}
						},
						trigger: 'change'
					}
				],
				speciality: [
					{ required: true, message: '专业不能为空', trigger: 'blur' }
				],
				education: [
					{ required: true, message: '请选择学历', trigger: 'change' }
				],
				degree: [
					{ required: true, message: '请选择学位', trigger: 'change' }
				],
				learnForms: [
					{ required: true, message: '请选择学习形式', trigger: 'change' }
				],
				graduationType: [
					{ required: true, message: '请选择毕业类型', trigger: 'change' }
				],
				cultureMethod: [
					{ required: true, message: '请选择培养方式', trigger: 'change' }
				],
				// educationFile: [
				// 	{ required: true, message: '请上传学历附件', trigger: 'change' }
				// ],
				// degreeFile: [
				// 	{ required: true, message: '请上传学位附件', trigger: 'change' }
				// ]
			},
			fileType: ['jpg', 'jpeg', 'png', 'webp'],

		}
	},
	created() {
		this.ossService = new OSSSerivce()
	},
	components: {
		UpLoadComponent
	},
	computed: {
		bus: {
			get() {
				return this.businessId
			},
			set(val) {
				this.businessId = val
			}
		},
		name() {
			return this.$store.state.user.name
		},
		userId() {
			return this.$store.state.user.id
		}
	},
	watch: {
		'keyWatch': {
			handler(newVal) {
				if (this.commonJS.isNotEmpty(this.bus)) {
					this.init('', this.bus)
				} else {
					this.$nextTick(() => {
						this.$refs.inputForm.resetFields()
					})
				}
			}
		},
		'loading': {
			handler(newVal) {
				this.$emit('changeLoading', newVal)
			}
		}
	},
	methods: {
		validateEndDate(rule, value, callback) {
			const start = this.inputForm.startDate;
			const end = value;

			if (!start || !end) {
				return callback(); // 其中一个为空，先跳过
			}

			const startTime = new Date(start).getTime();
			const endTime = new Date(end).getTime();

			if (startTime > endTime) {
				callback(new Error('结束时间必须晚于开始时间'));
			} else {
				callback();
			}
		},
		getKeyWatch(keyWatch) {
			this.keyWatch = keyWatch
		},
		init(method, id, reId) {
			this.method = method
			this.inputForm = {
				graduatedFrom: "",
				otherGraduatedFrom: "",
				startDate: "",
				endDate: "",
				speciality: "",//专业
				education: "",//学历
				degree: "",//学位
				isHighestEducation: "1",
				isHighestDegree: "1",
				learnForms: "",//学习形式
				graduationType: "",//毕业类型
				cultureMethod: "",//培养方式
				educationFile: [],
				degreeFile: [],
				specialityDesc: "",
				enrollmentRegistrationId: ""
			}
			this.currentUserId = ""
			if (method === 'add') {
				this.title = `新增教育经历`
			} else if (method === 'edit') {
				this.title = '修改教育经历'
			} else if (method === 'view') {
				this.title = '查看教育经历'
			}
			this.inputForm.id = id
			this.inputForm.userId = reId
			this.visible = true
			this.loading = true
			this.$nextTick(() => {
				if (id) {
					enrollmentRegistrationService.findEduInfoById({
						id: this.inputForm.id
					}).then(res => {
						this.inputForm = this.recover(this.inputForm, res)
						this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
						if (!this.inputForm.educationFile) {
							this.inputForm.educationFile = []
						}
						if (!this.inputForm.degreeFile) {
							this.inputForm.degreeFile = []
						}
						this.$refs.educationUploadComponent.newUpload(method, this.inputForm.educationFile, 'education', 50, null, null, null, null, [...this.fileType])
						this.$refs.degreeUploadComponent.newUpload(method, this.inputForm.degreeFile, 'degree', 50, null, null, null, null, [...this.fileType])
						this.loading = false
					})
				} else {
					this.inputForm.id = id
					this.$refs.educationUploadComponent.newUpload(method, this.inputForm.educationFile, 'education', 50, null, null, null, null, [...this.fileType])
					this.$refs.degreeUploadComponent.newUpload(method, this.inputForm.degreeFile, 'degree', 50, null, null, null, null, [...this.fileType])
					this.loading = false
				}
			})
		},
		// 表单提交
		doSubmit() {
			if (this.$refs.educationUploadComponent.checkProgress() || this.$refs.degreeUploadComponent.checkProgress()) {
				return
			}

			this.$refs['inputForm'].validate((valid) => {
				if (valid) {
					let eduFiles = this.$refs.educationUploadComponent.getDataList()
					if (!eduFiles || eduFiles.length === 0) {
						this.$message.error('请上传学历附件')
						return
					}
					if (this.inputForm.degree && this.inputForm.degree != '1') {
						let degreeFiles = this.$refs.degreeUploadComponent.getDataList()
						if (!degreeFiles || degreeFiles.length === 0) {
							this.$message.error('请上传学位附件')
							return
						}
					}


					this.inputForm.startDate = new Date(this.inputForm.startDate).getTime(); // 转为时间戳
					this.inputForm.endDate = new Date(this.inputForm.endDate).getTime(); // 转为时间戳
					this.inputForm.educationFile = this.$refs.educationUploadComponent.getDataList()
					this.inputForm.degreeFile = this.$refs.degreeUploadComponent.getDataList()
					this.inputForm.status = "5"
					enrollmentRegistrationService.saveEduInfo(this.inputForm).then(res => {
						this.close()
						this.$message.success(res)
						this.$emit("addEduInfo", this.inputForm.enrollmentRegistrationId)
						this.loading = false
					})
				}
			})
		},
		close() {
			this.$refs.educationUploadComponent.clearUpload()
			this.$refs.degreeUploadComponent.clearUpload()
			this.$refs.inputForm.resetFields()
			this.visible = false
		},
		handleSelectDegree(value) {
			if (value === '1') {
				this.degreeFileFlag = false
			} else {
				this.degreeFileFlag = true
			}
		},
	}
}
</script>


<style lang="less">
.fileUpload {
	.el-form-item__content {
		width: 100%;
	}
}

.star-require .el-form-item__label::before {
	content: "*";
	color: red;
}
</style>
