<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
			@close="handleClose">
			<el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
				<el-form-item label="类型" prop="type">
					<el-select v-model="formData.type" placeholder="请选择类型">
						<el-option label="全部" value=""></el-option>
						<el-option v-for="(item, index) in dictData.activity_zone" :key="index" :label="item.remark"
							:value="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="表单标题" prop="title">
					<el-input v-model="formData.title" clearable placeholder="请输入表单标题" />
				</el-form-item>
				<el-form-item label="商品分类" prop="cate_ids">
					<el-cascader class="w-[500px]" v-model="formData.cate_ids" filterable clearable
						:before-filter="beforeFilter" :props="props" placeholder="请选择商品分类" />
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input :rows="3" type="textarea" v-model="formData.remark" clearable placeholder="请输入备注" />
				</el-form-item>
			</el-form>
		</popup>
	</div>
</template>

<script lang="ts" setup name="categoryEdit">
	import type { FormInstance } from 'element-plus'
	import { apiStoreCategoryLists } from '@/api/store_category'
	import { apiActivityZoneFormAdd, apiActivityZoneFormEdit } from '@/api/activity_zone_form'
	import type { PropType } from 'vue'
	defineProps({
		dictData: {
			type: Object as PropType<Record<string, any[]>>,
			default: () => ({})
		}
	})
	const emit = defineEmits(['success', 'close'])
	const formRef = shallowRef<FormInstance>()
	const popupRef = shallowRef<InstanceType<typeof Popup>>()
	const mode = ref('add')

	// 弹窗标题
	const popupTitle = computed(() => {
		return mode.value == 'edit' ? '编辑表单' : '新增表单'
	})

	// 表单数据
	const formData = reactive({
		type: '',
		title: '',
		cate_ids: [],
		remark: '',
	})

	let resolves
	let nodes
	const flag = ref(false)
	let name
	const beforeFilter = (value) => {
		name = value
		flag.value = true
		props.lazyLoad(nodes, resolves)
		return false
	}
	const props = {
		value: 'id',
		label: 'name',
		lazy: true,
		multiple: true,
		lazyLoad(node, resolve) {
			if (!resolves) resolves = resolve
			if (!nodes) nodes = node
			const pid = node.value || 0
			let data = {}
			if (flag.value) {
				data = { name: name }
				if (!name) {
					data = {
						pid: pid,
						page_size: 10000
					}
				}
			} else {
				data = {
					pid: pid,
					page_size: 10000
				}
			}
			apiStoreCategoryLists({
				...data
			}).then((res) => {
				resolve(
					res.lists.map((item : any) => {
						item.leaf = item.is_children == 0
						flag.value = false
						return item
					})
				)
			})
		}
	}

	// 表单验证
	const formRules = reactive<any>({
		type: [
			{
				required: true,
				message: '请输入表单类型',
				trigger: ['blur']
			}
		],
		title: [
			{
				required: true,
				message: '请输入表单名称',
				trigger: ['blur']
			}
		],
		cate_ids: [
			{
				required: true,
				message: '请输入商品分类',
				trigger: ['blur']
			}
		]
	})

	// 获取详情
	const setFormData = async (data : Record<any, any>) => {
		for (const key in formData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				formData[key] = data[key]
			}
		}
	}

	// 提交按钮
	const handleSubmit = async () => {
		await formRef.value?.validate()
		const data = { ...formData }
		mode.value == 'edit' ? await apiActivityZoneFormEdit(data) : await apiActivityZoneFormAdd(data)
		popupRef.value?.close()
		emit('success')
	}

	//打开弹窗
	const open = (type = 'add') => {
		mode.value = type
		popupRef.value?.open()
	}

	// 关闭回调
	const handleClose = () => {
		emit('close')
	}

	defineExpose({
		open,
		setFormData
	})
</script>