<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="getTitle"
    @ok="handleSubmit"
    wrapClassName="custom-modal-box"
  >
    <BasicForm @register="registerForm">
      <template #user="{ model }">
        <SelectUser
          v-model:value="model.principalIds"
          suffix="ant-design:setting-outlined"
          placeholder="请选择负责人"
        />
      </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 { addDeviceInfo, updateDeviceInfo, getDeviceInfo } from '/@/api/erp/device/info';
  import { getSupplierList } from '/@/api/erp/supplier/list';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import SelectUser from '/@/components/Form/src/components/SelectUser.vue';

  const { t } = useI18n();
  const FormSchema: FormSchema[] = [
    {
      field: 'typeId',
      label: '设备类型',
      component: 'DicSelect',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请选择设备类型',
        itemId: '1671412696707850241',
        isShowAdd: false,
      },
    },
    {
      field: 'number',
      label: '设备编号',
      component: 'Input',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请输入设备编号',
      },
    },
    {
      field: 'name',
      label: '设备名称',
      component: 'Input',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请输入设备名称',
      },
    },
    {
      field: 'model',
      label: '规格型号',
      component: 'Input',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请输入规格型号',
      },
    },
    {
      field: 'supplierId',
      label: '供应商',
      component: 'ApiSelect',
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请选择供应商',
        api: getSupplierList,
        labelField: 'name',
        valueField: 'id',
        getPopupContainer: () => document.body,
      },
    },
    {
      field: 'buyDate',
      label: '购买日期',
      component: 'DatePicker',
      colProps: { span: 24 },
      componentProps: {
        format: 'YYYY-MM-DD',
        placeholder: '请选择购买日期',
      },
    },
    {
      field: 'maintainDate',
      label: '维保日期',
      component: 'DatePicker',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        format: 'YYYY-MM-DD',
        placeholder: '请选择维保日期',
      },
    },
    {
      field: 'scrapDate',
      label: '报废日期',
      component: 'DatePicker',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        format: 'YYYY-MM-DD',
        placeholder: '请选择报废日期',
      },
    },
    {
      field: 'principalIds',
      label: '负责人',
      component: 'Input',
      slot: 'user',
      required: true,
      colProps: { span: 24 },
    },
    {
      field: 'address',
      label: '设备位置',
      component: 'Input',
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请输入设备位置',
      },
    },
  ];

  const { notification } = useMessage();
  const isUpdate = ref(true);
  const rowId = ref('');
  const emit = defineEmits(['success', 'register']);

  const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
    labelWidth: 100,
    schemas: FormSchema,
    layout: 'vertical',
    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 getDeviceInfo(data.id);
      setFieldsValue({
        ...record,
      });
    }
  });

  const getTitle = computed(() => (!unref(isUpdate) ? '新增设备' : '编辑设备'));

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

      if (!unref(isUpdate)) {
        await addDeviceInfo(values);
        notification.success({
          message: '新增设备',
          description: t('成功'),
        });
      } else {
        values.id = rowId.value;
        await updateDeviceInfo(values);
        notification.success({
          message: '编辑设备',
          description: t('成功'),
        });
      }

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