<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="60%">
    <div class="qdml-add p-5">
      <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 { formSchema } from './GWrdkjbxxb.data';
  import { useUserStore } from '/@/store/modules/user';
  import { saveOrUpdate, edit } from './GWrdkjbxxb.api';
  // import { getTown, categoryList, dict } from '/@/api/common/api';

  const userStore: any = useUserStore();

  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  //表单配置
  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;

    if (unref(isUpdate)) {
      //表单赋值
      const res = await edit(data.record.wrdkid);
      res.faliErji1 = res.faliErji;

      if (!res.isnyd2yzlg) res.isnyd2yzlg = '0';
      if (!res.iswlyd2yzlg) res.iswlyd2yzlg = '0';
      await setFieldsValue({
        ...res,
      });
    }
  });
  //设置标题
  const title = computed(() => (!unref(isUpdate) ? '添加' : '编辑'));
  //表单提交事件
  async function handleSubmit() {
    try {
      let values = await validate();
      setModalProps({ confirmLoading: true });

      if (userStore?.userInfo?.userIdentity == 3 || userStore?.userInfo?.userIdentity == 4) {
        values.cityCode = userStore?.userInfo?.codeRegion.substring(0, userStore?.userInfo?.codeRegion.length - 2) + '00';
      }
      //提交表单
      if (userStore?.userInfo?.userIdentity == '3' && !values.wrdkid) {
        if (!values.countryCode) values.countryCode = userStore?.userInfo?.codeRegion;
      }
      // values.hyml=z 代表选的是其他

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

<style lang="less">
  .qdml-add {
    .ant-row {
      display: flex;
      flex-wrap: wrap;

      .ant-form-item-label {
        background-color: #fbfbfb;
        text-align: center;
        border: 1px solid #e6e6e6;
        margin-right: 3px;
      }
    }
  }
</style>
