<template>
	<xn-form-container
		:title="'位置信息管理新增'"
		:width="800"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form
			:model="formState"
			ref="formStateRef"
			:rules="formStateRules"
			name="basic"
			autocomplete="off"
			@finish="onFinish"
			@finishFailed="onFinishFailed"
		>
			<div class="form">
				<a-row>
					<a-col :span="12">
						<a-form-item class="form-item" label="制度名称" name="systemName" :label-col="{ span: 10 }">
							<a-input
								placeholder="请输入制度名称"
								style="width: 250px"
								:disabled="disabled"
								v-model:value="formState.systemName"
							/>
						</a-form-item>
					</a-col>
					<a-col :span="12">
						<a-form-item class="form-item" label="发布人" name="createUser" :label-col="{ span: 10 }">
							<a-input style="width: 250px" :disabled="true" v-model:value="formState.createUser" />
						</a-form-item>
					</a-col>
				</a-row>
				<a-row>
					<a-col :span="12">
						<a-form-item class="form-item" label="发布时间" name="publishTime" :label-col="{ span: 10 }">
							<a-date-picker
								:disabled="disabled"
								v-model:value="formState.publishTime"
								valueFormat="YYYY-MM-DD HH:mm:ss"
								style="width: 250px"
							/>
						</a-form-item>
					</a-col>
				</a-row>

				<!--  -->
			</div>

			<div class="attachment">
				<a-form-item ref="uploadDataList" class="form-item" label="附件" name="attachment" :label-col="{ span: 2 }">
					<DIYUPLoad
						:beforeUpload="beforeUpload"
						multiple
						:disabled="disabled"
						:change="changeUpload"
						listType="picture-card"
						v-model:urls="formState.attachment"
					></DIYUPLoad>
				</a-form-item>
			</div>
			<!-- 发布内容 -->
			<div>发布内容</div>
			<div class="input">
				<a-textarea
					placeholder="请输入发布内容"
					v-model:value="formState.publishContent"
					:disabled="disabled"
					allow-clear
				/>
			</div>

			<a-form-item>
				<div class="item-submit">
					<a-button class="btn1" :disabled="disabled" @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)
				}
			}
			const detail = async (v) => {
				const res = await business.enterpriseManagementdetail({ id: v })
				formState.value = res
				formState.value.attachment = res.attachment.split(',')
				formState.value.createUser = res.createUserName
				console.log(formState.value)
			}
			const onClose = () => {
				visible.value = false
			}
			watch(
				() => visible.value,
				(newVal) => {
					if (!newVal) {
						setTimeout(() => {
							formState.value = {
								systemName: '',
								createUser: JSON.parse(localStorage.getItem('USER_INFO')).name,
								publishTime: '',
								publishContent: '',
								attachment: '',
								id: ''
							}
						}, 0)
					}
				},
				{ immediate: true, deep: true }
			)

			const formState = ref({
				systemName: '',
				createUser: JSON.parse(localStorage.getItem('USER_INFO')).name,
				publishTime: '',
				publishContent: '',
				attachment: '',
				id: ''
			})
			const formStateRules = ref({
				systemName: [{ required: true, message: '请填写!', trigger: 'blur' }],
				publishTime: [{ required: true, message: '请填写!', trigger: 'change' }],
				attachment: [{ required: true, message: '请填写!', trigger: 'change' }]
			})
			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.enterpriseManagementedit(val)
				} else {
					const res = await business.enterpriseManagementAdd(val)
					console.log('add')
				}
			}

			const delADD = () => {
				formStateRef.value.validate().then(() => {})
			}

			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,
				delADD,
				formStateRef,
				formStateRules,
				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: 250px;
	}
	.ant-form-item-label {
		width: 150px;
	}
	.attachment {
		margin-top: 30px;
		margin-left: 15px;
	}
</style>
