<template>
	<xn-form-container :title="'人才培训管理'" :width="800" :visible="visible" :destroy-on-close="true" @close="onClose">
		<a-form :model="formState" name="basic" autocomplete="off" @finish="onFinish" @finishFailed="onFinishFailed">
			<a-form-item
				class="form-item"
				label=" 培训名称"
				name="trainingName"
				:label-col="{ span: 4 }"
				:rules="[{ required: true, message: '请填写!', trigger: 'change' }]"
			>
				<a-input placeholder="请输入培训名称" :disabled="disabled" v-model:value="formState.trainingName" />
			</a-form-item>

			<a-form-item
				class="form-item"
				label="培训地点"
				:rules="[{ required: true, message: '请填写!', trigger: 'change' }]"
				name="trainingLocation"
				:label-col="{ span: 4 }"
			>
				<a-input placeholder="请输入培训地点" :disabled="disabled" v-model:value="formState.trainingLocation" />
			</a-form-item>
			<a-form-item
				class="form-item"
				label="培训日期"
				name="trainingDate"
				:label-col="{ span: 4 }"
				:rules="[{ required: true, message: '请填写!', trigger: 'change' }]"
			>
				<a-date-picker
					style="width: 100%"
					:disabled="disabled"
					v-model:value="formState.trainingDate"
					valueFormat="YYYY-MM-DD HH:mm:ss"
				/>
			</a-form-item>

			<!--  -->
			<a-form-item
				class="peix-run"
				label="培训人员"
				name="trainingPersonnel"
				:label-col="{ span: 4 }"
				:rules="[{ required: true, message: '请填写!', trigger: 'change' }]"
			>
				<a-input
					placeholder="请输入培训人员"
					:disabled="disabled"
					v-model:value="formState.trainingPersonnel"
					allow-clear
				/>
			</a-form-item>
			<a-form-item
				class="peix-run shuo-ming"
				label="说明"
				name="description"
				:label-col="{ span: 4 }"
				:rules="[{ required: true, message: '请填写!', trigger: 'change' }]"
			>
				<a-textarea placeholder="请输入说明" :disabled="disabled" v-model:value="formState.description" allow-clear />
			</a-form-item>
			<a-form-item>
				<div class="item-submit">
					<a-button class="btn1" @click="onClose">取消</a-button>
					<a-button class="btn1" :disabled="disabled" type="primary" html-type="submit">保存</a-button>
				</div>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
		</template>
	</xn-form-container>
</template>
<script lang="js">
	import business from '@/api/business/personnelApi'
	import { ref } from 'vue'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import global from '@/api/basic.info/global'
	export default {
		name: 'FromName',
		components: {},
		emits: ['change'],

		setup(props, { emit }) {
			const disabled = ref(false)
			const visible = ref(false)
			const records = ref('')
			const onOpen = (record, flag, view, row) => {
				records.value = record
				visible.value = flag
				disabled.value = view
				if (row) {
					detail(row.id)
					formState.value.id = row.id
				}
			}
			const detail = async (v) => {
				const res = await business.RCPXGLdetail({ id: v })
				formState.value = res
				formState.value.trainingDate = res.createTime
				console.log(formState.value)
			}
			const onClose = () => {
				visible.value = false
			}
			watch(
				() => visible.value,
				(newVal) => {
					if (!newVal) {
						setTimeout(() => {
							formState.value = {
								id: '',
								trainingName: '',
								trainingLocation: '',
								trainingDate: '',
								trainingPersonnel: '',
								description: ''
							}
						}, 0)
					}
				},
				{ immediate: true, deep: true }
			)

			const formState = ref({
				id: '',
				trainingName: '',
				trainingLocation: '',
				trainingDate: '',
				trainingPersonnel: '',
				description: ''
			})

			const formStateRef = ref(null)

			const onFinish = async (values) => {
				// formState.value.attachment = values.attachment.join(',')
				await handleAdd(formState.value)
				emit('change')
				formState.value = {
					systemName: '',
					createUser: '',
					publishTime: '',
					publishContent: '',
					attachment: '',
					id: ''
				}
				onClose()
			}

			const handleAdd = async (val) => {
				if (records.value == 'edit') {
					console.log(records.value)
					const res = await business.RCPXGLedit(val)
				} else {
					const res = await business.RCPXGLadd(val)
					console.log('add')
				}
			}

			const onFinishFailed = (errorInfo) => {
				console.log('Failed:', errorInfo)
			}

			function changeUpload(fd) {
				return global.uploadFile(fd).then((reg) => {
					uploadDataList.value.clearValidate()
					return reg
				})
			}
			const uploadDataList = ref(null)
			function beforeUpload() {
				uploadDataList.value.clearValidate()
			}

			return {
				uploadDataList,
				beforeUpload,
				formStateRef,
				changeUpload,
				formState,
				onFinish,
				onFinishFailed,
				disabled,
				onOpen,
				visible,
				onClose
			}
		}
	}
</script>
<style lang="less" scoped>
	.box1 {
		display: flex;
		justify-content: space-between;
		.box-left {
			width: 350px;
			align-items: center;
		}
		.box-right {
			width: 350px;
			margin-right: 10px;
			align-items: center;
		}
	}
	.common {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-bottom: 30px;
		.title {
			width: 130px;
			text-align: right;
			margin-right: 5px;
		}
	}
	.input {
		margin-right: 10px;
		margin-left: 40px;
		margin-top: 20px;
		.ant-input-affix-wrapper {
			height: 100px;
		}
	}
	.btnBox {
		text-align: center;
		margin-top: 100px;
		.snowy-buttom-left {
			margin-left: 100px;
		}
		.acc {
			width: 80px;
			height: 40px;
		}
	}
	.form {
		display: flex;
		flex-wrap: wrap;
		// justify-content: center;
		.form-item {
			width: 250px;
			margin-right: 120px;
		}
	}
	.item-submit {
		display: flex;
		justify-content: center;
		margin-top: 100px;
		.btn1 {
			width: 100px;
			margin-right: 20px;
		}
	}
	.ant-select {
		width: 250px;
	}
	.ant-input {
		// width: 350px;
	}
	.ant-form-item-label {
		width: 100px;
	}
	.attachment {
		display: flex;
		margin-top: 30px;
		div:first-child {
			width: 110px;
			text-align: right;
			padding-right: 45px;
		}
		span {
			color: red;
			margin-right: 3px;
		}
		p {
			font-size: 12px;
			color: #eb4897;
		}
	}
	// .peix-run {
	// 	::v-deep(.ant-form-item-required) {
	// 		margin-left: 22px;
	// 	}
	// }
	.shuo-ming {
		::v-deep(.ant-input) {
			height: 100px;
		}
	}
</style>
