<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" :showFooter="showFooter" @ok="handleSubmit" width="70%">
    <a-collapse v-model:activeKey="activeKey" :bordered="false">
      <a-collapse-panel key="1" header="开发利用信息（自然资源部门提供" :forceRender="true">
        <KfForm :data="datas" ref="kfForms" :showFooter="showFooter" />
      </a-collapse-panel>
      <a-collapse-panel key="2" header="基础信息（生态环境部门提供）" :forceRender="true">
        <jcForm ref="jcForms" :showFooter="showFooter" :data="datas" />
      </a-collapse-panel>
      <a-collapse-panel key="3" header="管控修复信息（生态环境部门提供）" :forceRender="true"
        ><gkxfForm ref="gkxfForms" :showFooter="showFooter" :data="datas" />
      </a-collapse-panel>
      <a-collapse-panel key="4" header="处罚和整改信息" :forceRender="true"
        ><cfzgForm :showFooter="showFooter" ref="cfzgForms" :data="datas" />
      </a-collapse-panel>
    </a-collapse>
    <div class="mt-4"><BasicForm @register="registerForm"> </BasicForm></div>
  </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 KfForm from '../components/kfForm.vue';
  import jcForm from '../components/jcform.vue';
  import gkxfForm from '../components/gkxfFrom.vue';
  import cfzgForm from '../components/cfzgForm.vue';
  import { formSchema } from '../data';
  import { saveAdd, saveEdit } from '../api';
  import { useUserStore } from '/@/store/modules/user';
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const activeKey = ref(['1', '2', '3', '4']);
  const isUpdate = ref(true);
  const datas = ref<any>({ id: '' });
  const kfForms = ref<any>(null);
  const jcForms = ref<any>(null);
  const gkxfForms = ref<any>(null);
  const cfzgForms = ref<any>(null);
  const showFooter = ref(true);
  const userStore: any = useUserStore();
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 220,
    schemas: formSchema,
    showActionButtonGroup: false,
    //默认row行配置,当 layout 为 horizontal 生效
    rowProps: { gutter: 24, justify: 'start', align: 'middle' },
    //全局col列占比(每列显示多少位)，和schemas中的colProps属性一致
    baseColProps: { span: 12 },
    //使用labelCol的样式参数来控制标题宽度
    labelCol: { style: { width: '150px' } },
    //row行的样式
    baseRowStyle: { width: '100%' },
    //表单布局属性，支持（vertical,horizontal），默认为horizontal
    layout: 'horizontal',
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
    isUpdate.value = !!data?.isUpdate;
    showFooter.value = data.showFooter;
    if (unref(isUpdate)) {
      datas.value = data.record;
      datas.value.showFooter = data.showFooter;
      //表单赋值
      await setFieldsValue({
        ...data.record,
      });
    } else {
      datas.value = {};
    }
  });
  //设置标题
  const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
  //表单提交事件
  async function handleSubmit() {
    try {
      const kfForm = await kfForms?.value.getFormValue();
      const jcForm = await jcForms?.value.getFormValue();
      const gkxfForm = await gkxfForms?.value.getFormValue();
      const cfzgForm = await cfzgForms?.value.getFormValue();
      let values = await validate();
      delete values.showFooter;
      const params = { ...kfForm, ...jcForm, ...gkxfForm, ...cfzgForm, ...values };

      if (userStore?.userInfo?.userIdentity != 1) {
        // 获取市的值
        params.cityCode = userStore?.userInfo?.codeRegion.substring(0, userStore?.userInfo?.codeRegion.length - 2) + '00';
      }
      // 土地使用权人4 如果有权限 再加个4
      if (userStore?.userInfo?.userIdentity == '3' && !params.id) {
        if (!params.countryCode) params.countryCode = userStore?.userInfo?.codeRegion;
      }
      setModalProps({ confirmLoading: true });
      console.log(params, 'params');

      //提交表单
      if (isUpdate.value) {
        await saveEdit(params);
      } else await saveAdd(params);

      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success', { isUpdate: isUpdate.value, values });
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style lang="less" scoped>
  :deep(.ant-input-number) {
    width: 100%;
  }

  :deep(.ant-picker) {
    width: 100%;
  }
</style>
