<template>
  <div class="modals">
    <BasicModal
      v-bind="$attrs"
      destroyOnClose
      @register="registerModal"
      :title="getTitle"
      @ok="handleSubmit"
      :width="800"
      @cancel="handleCancel"
    >
      <div class="!pl-8 !pt-4">
        <BasicForm @register="registerForm">
          <template #uniqueCode>
            <span>{{ deviceInfo?.uniqueCode }}</span>
          </template>
          <template #manufacturer>
            <span>{{ deviceInfo?.manufacturer }}</span>
          </template>
          <template #model>
            <span>{{ deviceInfo?.model }}</span>
          </template>
        </BasicForm>
      </div>
    </BasicModal>
  </div>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { upkeepDataFormSchema } from './data';
  import {
    upkeepAdd,
    upkeepDetail,
    upkeepEdit,
    engineerDialysisDeviceDetail,
  } from '/@/api/biz/engineer/dialysisDeviceApi';
  const emit = defineEmits(['success', 'cancel', 'register']);

  const getTitle = computed(() => (!unref(isUpdate) ? '保养设备' : '保养设备'));
  const isUpdate = ref(false);
  const deviceInfo = ref();
  const rowId = ref();

  const { createMessage } = useMessage();
  const [registerForm, { resetFields, validate, setFieldsValue }] = useForm({
    layout: 'vertical',
    showResetButton: true,
    labelWidth: 100,
    schemas: upkeepDataFormSchema,
    showActionButtonGroup: false,
    actionColOptions: {
      span: 12,
    },
    baseColProps: {
      span: 12,
    },
    wrapperCol: {
      span: 22,
    },
  });
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async data => {
    await resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;
    if (!data.record.name) {
      deviceInfo.value = await engineerDialysisDeviceDetail(data.record?.id);
    } else {
      deviceInfo.value = data.record;
    }
    if (unref(isUpdate)) {
      rowId.value = data.upkeepRecord.id;
      const resData = await upkeepDetail(data.upkeepRecord.id);
      console.log('resData::::', resData);
      await setFieldsValue({
        ...resData,
      });
    }
  });

  // 提交按钮事件
  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      values.picture = values.files && values.files.map(ele => ele.id);
      if (!isUpdate.value) {
        values.deviceIds = [deviceInfo.value.id];
      } else {
        values.deviceId = deviceInfo.value.id;
      }
      !unref(isUpdate)
        ? await upkeepAdd({ ...values })
        : await upkeepEdit({ ...values, id: rowId.value });
      !unref(isUpdate) ? createMessage.success('新增成功！') : createMessage.success('编辑成功！');
      closeModal();
      emit('success', { isUpdate: unref(isUpdate), values: { ...values, id: rowId.value } });
    } finally {
      setModalProps({ confirmLoading: false, canFullscreen: false });
    }
  }

  async function handleCancel() {
    closeModal();
    emit('cancel');
  }
</script>
<style lang="less" scoped>
  ::v-deep(.ant-modal) {
    background-color: #000;
  }
</style>
