<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="companyName">
							<el-input v-model="inputForm.companyName" placeholder="请输入单位名称"></el-input>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="职务" prop="position">
							<el-input v-model="inputForm.position" placeholder="请输入职务"></el-input>
						</el-form-item>
					</el-col>

					<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="certifier">
							<el-input v-model="inputForm.certifier" placeholder=""></el-input>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="是否本单位工作经历" prop="isCurrentCompanyExperience">
							<el-radio-group v-model="inputForm.isCurrentCompanyExperience"
								@change="handleChangeIsCurrentCompanyExperience">
								<el-radio label="1">是</el-radio>
								<el-radio label="0">否</el-radio>
							</el-radio-group>
						</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 enrollmentRegistrationService from '@/api/human/enrollment/EnrollmentRegistrationService'
export default {
	props: {
		businessId: {
			type: String,
			default: ''
		},
		formReadOnly: {
			type: Boolean,
			default: false
		},
		status: {
			type: String,
			default: ''
		}
	},
	data() {
		return {
			disabled: false,
			title: '',
			method: '',
			visible: false,
			loading: false,
			inputForm: {
				id: "",
				enrollmentRegistrationId: '',
				companyName: '',
				position: '',
				startDate: '',
				endDate: '',
				certifier: '',
				isCurrentCompanyExperience: "1",
				status: ""
			},
			keyWatch: '',
			rules: {
				companyName: [
					{ required: true, message: '请输入单位名称', trigger: 'blur' }
				],
				position: [
					{ required: true, message: '请输入职务', trigger: 'blur' }
				],
				startDate: [
					{ required: true, message: '请选择开始时间', trigger: 'change' },
				],
				endDate: [],
				isCurrentCompanyExperience: [
					{ required: true, message: '请选择是否本单位经历', trigger: 'change' },
				]
			},
		}
	},
	created() {
	},
	components: {
	},
	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: {
		getKeyWatch(keyWatch) {
			this.keyWatch = keyWatch
		},
		init(method, id, reId) {
			this.method = method
			this.inputForm = {
				id: "",
				enrollmentRegistrationId: '',
				companyName: '',
				position: '',
				startDate: '',
				endDate: '',
				certifier: '',
				isCurrentCompanyExperience: "1",
				status: ""
			}
			if (method === 'add') {
				this.title = `新增工作经历`
			} else if (method === 'edit') {
				this.title = '修改工作经历'
			} else if (method === 'view') {
				this.title = '查看工作经历'
			}
			this.inputForm.userId = reId
			this.visible = true
			this.loading = true
			this.$nextTick(() => {
				if (id) {
					enrollmentRegistrationService.findWorkInfoById({
						id: id
					}).then(res => {
						this.inputForm = this.recover(this.inputForm, res)
						this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
						if (this.inputForm.id && this.inputForm.status != "1") {
							this.inputForm.id == this.generateFallbackId()
						}
						this.loading = false
					})
				} else {
					this.loading = false
				}
			})
		},
		generateFallbackId() {
			const timestamp = Date.now(); // 13位
			const randomPart = Math.floor(Math.random() * 1e6); // 6位随机数
			return String(timestamp) + String(randomPart);  // 返回 19 位字符串
		},
		// 表单提交
		doSubmit() {
			this.$refs['inputForm'].validate((valid) => {
				if (valid) {
					this.inputForm.startDate = new Date(this.inputForm.startDate).getTime(); // 转为时间戳
					this.inputForm.endDate = new Date(this.inputForm.endDate).getTime(); // 转为时间戳
					// this.$emit("addWorkInfo", JSON.parse(JSON.stringify(this.inputForm)))
					// this.close()
					if (this.inputForm.id && this.inputForm.status != "1" && this.inputForm.status != "4") {
						enrollmentRegistrationService.updateInfoStatus({
							id: this.inputForm.id,
							status: "6",
							type: "2"
						}).then(result => {
							// 暂存状态
							this.inputForm.id = ""
							this.inputForm.status = 1
							enrollmentRegistrationService.saveWorkInfo(this.inputForm).then(res => {
								this.$message.success(res)
								this.$emit("addWorkInfo", this.inputForm.enrollmentRegistrationId)
								this.loading = false
								this.close()
							})
						})
					} else {
						// 暂存状态
						this.inputForm.status = 1
						enrollmentRegistrationService.saveWorkInfo(this.inputForm).then(res => {
							this.$message.success(res)
							this.$emit("addWorkInfo", this.inputForm.enrollmentRegistrationId)
							this.loading = false
							this.close()
						})
					}

				}
			})
		},
		close() {
			this.$refs.inputForm.resetFields()
			this.visible = false
		},
		handleChangeIsCurrentCompanyExperience(value) {
			console.log(value);
			if (value == 1) {
				this.rules.endDate = []
			} else {
				this.rules.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'
				}
			}
		}
	}
}
</script>


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