<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="getTitle"
    @ok="handleSubmit"
    wrapClassName="custom-modal-box"
  >
    <BasicForm @register="registerForm" />
  </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 { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { getUnitTypeList } from '/@/api/erp/unit/type';
  import { addUnitList, updateUnitList, getUnitListInfo } from '/@/api/erp/unit/list';
  const { t } = useI18n();
  const FormSchema: FormSchema[] = [
    {
      field: 'unitTypeId',
      label: '单位类型',
      component: 'ApiSelect',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请选择单位类型',
        api: getUnitTypeList,
        labelField: 'name',
        valueField: 'id',
        getPopupContainer: () => document.body,
        onChange: (_, options) => {
          const isStandard = options?.isHaveStandard ? 0 : 1;
          setFieldsValue({ isStandard });
        },
      },
    },
    {
      field: 'name',
      label: '单位名称',
      component: 'Input',
      required: true,
      colProps: { span: 24 },
      dynamicDisabled: () => {
        return isUpdate.value;
      },
      componentProps: {
        placeholder: '请输入单位名称',
      },
    },
    {
      field: 'symbol',
      label: '单位符号',
      component: 'Input',
      required: true,
      colProps: { span: 24 },
      dynamicDisabled: () => {
        return isUpdate.value;
      },
      componentProps: {
        placeholder: '请输入单位符号',
      },
    },
    {
      field: 'isStandard',
      label: '是否基准单位',
      component: 'Select',
      colProps: { span: 24 },
      required: true,
      componentProps: {
        placeholder: '请选择是否基准单位',
        options: [
          { label: '是', value: 1 },
          { label: '否', value: 0 },
        ],
      },
    },
    {
      field: 'state',
      label: '状态',
      component: 'Switch',
      colProps: { span: 24 },
    },
  ];

  const { notification } = useMessage();
  const isUpdate = ref(true);
  const rowId = ref('');

  const emit = defineEmits(['success']);

  const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
    labelWidth: 100,
    schemas: FormSchema,
    showActionButtonGroup: false,
    actionColOptions: {
      span: 23,
    },
  });

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ confirmLoading: false, destroyOnClose: true });

    isUpdate.value = !!data?.isUpdate;

    if (unref(isUpdate)) {
      rowId.value = data.id;
      const record = await getUnitListInfo(data.id);

      setFieldsValue({
        ...record,
      });
    } else {
      setFieldsValue({
        state: 1,
      });
    }
  });

  const getTitle = computed(() => (!unref(isUpdate) ? '新增单位列表' : '编辑单位列表'));

  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });

      if (!unref(isUpdate)) {
        await addUnitList(values);
        notification.success({
          message: '新增单位列表',
          description: t('成功'),
        });
      } else {
        values.id = rowId.value;
        await updateUnitList(values);
        notification.success({
          message: '编辑单位列表',
          description: t('成功'),
        });
      }

      closeModal();
      emit('success');
    } catch (error) {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
