<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="type">
							<el-select size="default" v-model="inputForm.type" placeholder="请选择合同类型"
								style="width: 100%;">
								<el-option v-for="item in $dictUtils.getDictList('labor_contract_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="code">
							<el-input v-model="inputForm.code" placeholder="请输入合同编号"></el-input>
						</el-form-item>
					</el-col> -->


					<el-col :span="12">

						<el-form-item label="合同期限" prop="term">
							<el-select size="default" v-model="inputForm.term" placeholder="请选择合同期限"
								style="width: 100%;">
								<el-option v-for="item in $dictUtils.getDictList('labor_contract_term')"
									: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="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="trialEndDate">
							<el-date-picker v-model="inputForm.trialEndDate" type="date" placeholder="请选择试用期结束日期"
								style="width: 100%;"></el-date-picker>
						</el-form-item>
					</el-col>

					<!-- <el-col :span="12">
						<el-form-item label="办理日期" prop="processeDate">
							<el-date-picker v-model="inputForm.processeDate" type="date" placeholder="请选择办理日期"
								style="width: 100%;"></el-date-picker>
						</el-form-item>
					</el-col> -->

					<el-col :span="12">
						<el-form-item label="附件" prop="degreeFile">
							<UpLoadComponent ref="uploadComponent"></UpLoadComponent>
						</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'
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: {
				enrollmentRegistrationId: '',
				type: "",
				code: "",
				term: "",
				startDate: "",
				endDate: "",
				trialEndDate: "",
				processeDate: "",
				files: []
			},
			keyWatch: '',
			rules: {
				type: [
					{ required: true, message: '请选择合同类型', trigger: 'blur' },
				],
				term: [
					{ required: true, message: '请选择合同期限', trigger: 'blur' },
				],

			}
		}
	},
	created() {
	},
	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: {
		getKeyWatch(keyWatch) {
			this.keyWatch = keyWatch
		},
		init(method, id, reId) {
			this.method = method
			this.inputForm = {
				id: "",
				enrollmentRegistrationId: '',
				type: "",
				code: "",
				term: "",
				startDate: "",
				endDate: "",
				trialEndDate: "",
				processeDate: "",
				files: []
			}
			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.findLaborContractInfoById({
						id: this.inputForm.id
					}).then(res => {
						this.inputForm = this.recover(this.inputForm, res)
						this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
						this.$refs.uploadComponent.newUpload(method, this.inputForm.files, 'laborContract', 50)
						this.loading = false
					})
				} else {
					this.inputForm.id = id
					this.$refs.uploadComponent.newUpload(method, this.inputForm.files, 'laborContract', 50)
					this.loading = false
				}
			})
		},
		// 表单提交
		doSubmit() {
			if (this.$refs.uploadComponent.checkProgress()) {
				return
			}
			this.$refs['inputForm'].validate((valid) => {
				if (valid) {
					if (this.inputForm.startDate) {
						this.inputForm.startDate = new Date(this.inputForm.startDate).getTime();
					}
					if (this.inputForm.endDate) {
						this.inputForm.endDate = new Date(this.inputForm.endDate).getTime();
					}
					if (this.inputForm.trialEndDate) {
						this.inputForm.trialEndDate = new Date(this.inputForm.trialEndDate).getTime();
					}
					if (this.inputForm.processeDate) {
						this.inputForm.processeDate = new Date(this.inputForm.processeDate).getTime();
					}
					this.inputForm.files = this.$refs.uploadComponent.getDataList()
					this.inputForm.status = "5"
					enrollmentRegistrationService.saveLaborContractInfo(this.inputForm).then(res => {
						this.close()
						this.$message.success(res)
						this.$emit("addLaborContractInfo", this.inputForm.enrollmentRegistrationId)
						this.loading = false
					})
				}
			})
		},
		close() {
			this.$refs.inputForm.resetFields()
			this.$refs.uploadComponent.clearUpload()
			this.visible = false
		},
	}
}
</script>


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