<template>
    <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="60%">
        <BasicForm @register="registerForm">
        <!-- 行业分类 -->
      <template #suspect="{ model, field }">
        <a-select
          v-model:value="model[field]"
          placeholder="请选择行业分类"
          :fieldNames="{ label: 'text', value: 'value' }"
          :options="isSuspectOptions"
          @change="changeSuspect"
        ></a-select>
      </template>
      <template #category="{ model, field }">
        <a-tree-select
          v-model:value="model[field]"
          show-search
          placeholder="请选择二级"
          allow-clear
          tree-default-expand-all
          :field-names="{ label: 'name', value: 'id', children: 'children' }"
          :tree-data="treeData"
          tree-node-filter-prop="label"
        >
          <template #suffixIcon>
            <SmileOutlined />
          </template>
        </a-tree-select>
      </template>
        </BasicForm>
    </BasicModal>
</template>

<script lang="ts" setup>
import { ref, computed, unref } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { FormSchema } from '/@/components/Table';
// import { formSchema } from '../GWrdkjbxxb.data';
import { saveOrUpdate } from '../api';
import { getTown, cityTree, categoryList, dict } from '/@/api/common/api'
import { useSuspectsData,useCategoryData } from '../data';
// Emits声明
const emit = defineEmits(['register', 'success']);

const isSuspectOptions = useSuspectsData().data; // 行业分类
const useCategory = useCategoryData();
const treeData = useCategory.data; // 行业类别-数据
const changeSuspect = useCategory.changeIsSuspect; // 行业类别-方法

const isUpdate = ref(true);
const formSchema: FormSchema[] = [
  // TODO 主键隐藏字段，目前写死为ID
  { label: '', field: 'id', component: 'Input', show: false },
  {
    label: '地块名称',
    field: 'provinceCode',
    component: 'Input',
    required: true,
  },
  {
    label: '行业分类',
    field: 'isSuspect',
    component: 'ApiSelect',
    slot: 'suspect',
  },
  {
    label: '行业类别',
    field: 'hylb',
    component: 'TreeSelect',
    slot: 'category',
  },
  {
    label: '地块类型',
    field: 'dklx',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: dict,
      params: { key: 'dk_dklx' },
      labelField: 'text',
      valueField: 'value'
    },
  },
  {
    label: '涉及的专项工作',
    field: 'gzlx',
    component: 'Select',
    required: true,
    componentProps: {
      options: [
        {
          label: '危险化学品搬迁',
          value: '1'
        },
        {
          label: '长江经济带整治',
          value: '2'
        },
        {
          label: '黄河流域整治',
          value: '3'
        },
        {
          label: '其他',
          value: '4'
        },
      ]
    },
  },
  {
    label: '政策依据',
    field: 'faliYiju',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: dict,
      params: { key: 'dk_zcyj' },
      labelField: 'text',
      valueField: 'value',
    },
  },
  {
    label: '涉及的重点区域',
    field: 'gzfw',
    component: 'Select',
    required: true,
    componentProps: {
      options: [
        {
          label: '长江经济带',
          value: '1'
        },
        {
          label: '黄河流域',
          value: '2'
        },
        {
          label: '粤港澳大湾区',
          value: '3'
        },
        {
          label: '其他',
          value: '4'
        },
      ]
    },
  },
  {
    label: '备注',
    field: 'bz',
    component: 'InputTextArea',
  },
];
//表单配置
const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 110,
    schemas: formSchema,
    autoAdvancedCol: 2,
    baseColProps: { span: 12 },
    showActionButtonGroup: false,
});
//表单赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
    isUpdate.value = !!data?.isUpdate;
    if (unref(isUpdate)) {
        //表单赋值
        await setFieldsValue({
            ...data.record,
        });
    }
});
//设置标题
const title = computed(() => (!unref(isUpdate) ? '添加' : '编辑'));
//表单提交事件
async function handleSubmit(v) {
    try {
        let values = await validate();
        setModalProps({ confirmLoading: true });
        //提交表单
        await saveOrUpdate(values, isUpdate.value);
        //关闭弹窗
        closeModal();
        //刷新列表
        emit('success', { isUpdate: isUpdate.value, values });
    } finally {
        setModalProps({ confirmLoading: false });
    }
}
</script>

<style lang="less" scoped></style>