<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>
      <template #equipmentPartPicker="{ 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="openPartSelect">选择大部位</a-button>
          <a-button v-if="model.equipmentPartId && !isDetail" @click="clearPart">清除</a-button>
        </a-input-group>
      </template>
    </BasicForm>
    <MesXslEquipmentCategorySelectModal @register="registerCategoryModal" @select="onCategorySelect" />
    <MesXslEquipmentPartSelectModal @register="registerPartModal" @select="onPartSelect" />
  </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 { useMessage } from '/@/hooks/web/useMessage';
  import { formSchema } from '../MesXslEquipmentSubPart.data';
  import { saveOrUpdate } from '../MesXslEquipmentSubPart.api';
  import MesXslEquipmentCategorySelectModal from '/@/views/xslmes/mesXslEquipmentType/components/MesXslEquipmentCategorySelectModal.vue';
  import MesXslEquipmentPartSelectModal from './MesXslEquipmentPartSelectModal.vue';

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

  const [registerCategoryModal, { openModal: openCategoryModal }] = useModal();
  const [registerPartModal, { openModal: openPartModal }] = 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: '', equipmentPartId: '', equipmentPartName: '' });
  }

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

  function openPartSelect() {
    const vals = getFieldsValue();
    if (!vals.equipmentCategoryId) {
      createMessage.warning('请先选择所属设备类别');
      return;
    }
    openPartModal(true, {
      equipmentCategoryId: vals.equipmentCategoryId,
      equipmentPartId: vals.equipmentPartId,
    });
  }

  function clearPart() {
    setFieldsValue({ equipmentPartId: '', equipmentPartName: '' });
  }

  function onPartSelect(payload: { equipmentPartId?: string; equipmentPartName?: string }) {
    setFieldsValue({
      equipmentPartId: payload.equipmentPartId || '',
      equipmentPartName: payload.equipmentPartName || '',
    });
  }

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