<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">
			<!-- <div class="form"> -->
			<a-row>
				<a-col :span="24">
					<a-form-item
						class="form-item"
						label="模块名称"
						name="moduleName"
						:label-col="{ span: 3 }"
						:rules="[{ required: true, message: '请输入模块名称!', trigger: 'change' }]"
					>
						<a-input
							placeholder="请输入模块名称"
							style="width: 500px"
							:disabled="disabled"
							v-model:value="formState.moduleName"
						/>
					</a-form-item>
				</a-col>
			</a-row>
			<br />

			<a-row>
				<a-col :span="24">
					<a-form-item class="form-item" label="链接地址" name="linkAddress" :label-col="{ span: 3 }">
						<a-textarea
							placeholder="请输入链接地址"
							style="width: 500px"
							:disabled="disabled"
							v-model:value="formState.linkAddress"
						/>
					</a-form-item>
				</a-col>
			</a-row>
			<!-- </div> -->

			<a-form-item>
				<div class="item-submit">
					<a-button class="btn1" :disabled="disabled" @click="onFinish(2)">提交</a-button>
					<a-button class="btn1" :disabled="disabled" type="primary" @click="onFinish(1)">保存</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/home/home.js'
	import { ref } from 'vue'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import global from '@/api/home/global.js'
	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 rest = () => {
				for (let key in formState.value) {
					formState.value[key] = ''
				}
			}
			const detail = async (v) => {
				const res = await business.XIANGGUANdetail({ id: v })
				formState.value = res
			}
			const onClose = () => {
				visible.value = false
				rest()
			}
			watch(
				() => visible.value,
				(newVal) => {
					if (!newVal) {
						setTimeout(() => {
							rest()
						}, 0)
					}
				},
				{ immediate: true, deep: true }
			)

			const formState = ref({
				moduleName: '',
				linkAddress: '',
				status: 0
			})
			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 (status) => {
				formStateRef.value.validate().then(async () => {
					try {
						formState.value.status = status
						await handleAdd(formState.value)
						emit('change')
						onClose()
					} catch (err) {
						console.log(err)
					}
				})
			}

			const handleAdd = async (val) => {
				if (records.value == 'edit') {
					const res = await business.XIANGGUANedit(val)
				} else {
					const res = await business.XIANGGUANadd(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 {
				formStateRef,
				formStateRules,
				formState,
				onFinish,
				onFinishFailed,
				disabled,
				onOpen,
				visible,
				onClose
			}
		}
	}
</script>
<style lang="less" scoped>
	.btnBox {
		text-align: center;
		margin-top: 100px;
		.snowy-buttom-left {
			margin-left: 100px;
		}
		.acc {
			width: 80px;
			height: 40px;
		}
	}

	.item-submit {
		display: flex;
		justify-content: center;
		margin-top: 100px;
		.btn1 {
			width: 100px;
			margin-right: 20px;
		}
	}
</style>
