<template>
  <BasicModal @register="registerModal" :title="title" width="720" v-bind="$attrs" destroyOnClose @ok="handleSubmit">
    <BasicForm @register="registerForm">
      <template #equipmentCategoryPicker="{ model, field }">
        <a-input-group compact style="display: flex; width: 100%">
          <a-input v-model:value="model[field]" read-only placeholder="请选择设备类别" style="flex: 1" />
          <a-button type="primary" :disabled="isDetail" @click="openCategorySelect">选择类别</a-button>
          <a-button v-if="model.equipmentCategoryId && !isDetail" @click="clearCategory">清除</a-button>
        </a-input-group>
      </template>
    </BasicForm>
    <MesXslEquipmentCategorySelectModal @register="registerCategoryModal" @select="onCategorySelect" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { computed, ref, unref } from 'vue';
  import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from '../MesXslEquipmentPart.data';
  import { saveOrUpdate } from '../MesXslEquipmentPart.api';
  import MesXslEquipmentCategorySelectModal from '/@/views/xslmes/mesXslEquipmentType/components/MesXslEquipmentCategorySelectModal.vue';

  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  const isDetail = ref(false);

  const [registerCategoryModal, { openModal: openCategoryModal }] = useModal();

  const [registerForm, { resetFields, setFieldsValue, validate, setProps, getFieldsValue }] = useForm({
    labelWidth: 120,
    schemas: formSchema,
    showActionButtonGroup: false,
  });

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
    isUpdate.value = !!data?.isUpdate;
    isDetail.value = !data?.showFooter;
    if (unref(isUpdate)) await setFieldsValue({ ...data.record });
    setProps({ disabled: !data?.showFooter });
  });

  const title = computed(() =>
    !unref(isUpdate) ? '新增设备部位' : unref(isDetail) ? '设备部位详情' : '编辑设备部位',
  );

  function openCategorySelect() {
    const vals = getFieldsValue();
    openCategoryModal(true, { equipmentCategoryId: vals.equipmentCategoryId });
  }

  function clearCategory() {
    setFieldsValue({ equipmentCategoryId: '', equipmentCategoryName: '' });
  }

  function onCategorySelect(payload: { equipmentCategoryId?: string; equipmentCategoryName?: string }) {
    setFieldsValue({
      equipmentCategoryId: payload.equipmentCategoryId || '',
      equipmentCategoryName: payload.equipmentCategoryName || '',
    });
  }

  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      await saveOrUpdate(values, isUpdate.value);
      closeModal();
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
