<template>
	<xn-form-container
		:title="formData.id ? '编辑扩展字段配置' : '增加扩展字段配置'"
		width="50%"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="名称：" name="name">
						<a-input v-model:value="formData.name" placeholder="请输入名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="所属模块：" name="model">
						<a-select
							v-model:value="formData.model"
							placeholder="请选择所属模块"
							:options="tool.dictList('EXTEND_FIELD_MODEL')"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="字段名：" name="fieldName">
						<a-input v-model:value="formData.fieldName" placeholder="请输入字段名" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="字段类型：" name="fieldType">
						<a-input v-model:value="formData.fieldType" placeholder="请输入字段名" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="展示形式：" name="showType">
						<a-select
							v-model:value="formData.showType"
							placeholder="请选择展示形式"
							:options="tool.dictList('EXTEND_FIELD_SHOW_TYPE')"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="启用状态：" name="enabledState">
						<a-select
							v-model:value="formData.enabledState"
							placeholder="请选择启用状态"
							:options="enabledStateOptions"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="备注：" name="remarks">
						<a-textarea v-model:value="formData.remarks" placeholder="请输入备注" allow-clear />
					</a-form-item>
				</a-col>

				<a-col
					:span="24"
					v-if="formData.showType && formData.showType !== 'input' && formData.showType !== 'inputNumber'"
				>
					<h3>扩展数据：</h3>
					<div v-for="(item, index) in selectableData" :key="index" class="mb-2">
						<a-space>
							<a-button type="primary" @click="addSelectableData" v-if="index === 0">
								<PlusOutlined />
							</a-button>
							<a-button type="primary" danger @click="delSelectableData(index)" v-if="index !== 0">
								<MinusOutlined />
							</a-button>
							<a-input style="width: 100%" v-model:value="item.name" placeholder="请输入内容"></a-input>
						</a-space>
					</div>
				</a-col>
			</a-row>
		</a-form>

		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="extendFieldForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import extendFieldApi from '@/api/base/extendfield/extendFieldApi'
	import { message } from 'ant-design-vue'

	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const enabledStateOptions = ref([])

	// 增加数据
	let selectableData = ref([
		{
			name: '',
		}
	])

	const addSelectableData = () => {
		selectableData.value.push({
			name: '',
		})
	}

	const delSelectableData = (index) => {
		selectableData.value.splice(index, 1)
	}

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
			if (recordData.showValues) {
				selectableData.value = JSON.parse(recordData.showValues)
			} else {
				selectableData.value = [
					{
						filedName: ''
					}
				]
			}
		}
		enabledStateOptions.value = tool.dictList('COMMON_STATUS')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {}
	// 验证并提交数据
	const onSubmit = () => {
		if (
			formData.value.showType === 'select' &&
			formData.value.showType === 'radio' &&
			formData.value.showType === 'checkbox' &&
			selectableData.value.length === 0
		) {
			return message.error('请添加选项要展示的数据')
		}

		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			if (formDataParam.showType === 'input' || formDataParam.showType === 'inputNumber') {
				formDataParam.showValues = ''
			} else {
				formDataParam.showValues = JSON.stringify(selectableData.value)
			}

			extendFieldApi
				.extendFieldSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
