<template>
  <BasicModal
    v-bind="$attrs"
    destroyOnClose
    @register="registerModal"
    :title="getTitle"
    :width="width"
    @ok="handleSubmit"
    :showFooter="true"
  >
    <BasicForm @register="registerForm" layout="vertical" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { dataDisinfectMaintenanceFormSchema } from './data';

  import {
    disinfectMaintenanceDetail,
    disinfectMaintenaceEdit,
  } from '/@/api/biz/visit/deviceDisinfectApi';

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

  const getTitle = '机器消毒维护';
  const width = '35%';
  const rowRes = ref();

  const { createMessage } = useMessage();
  const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
    labelWidth: 150,
    schemas: dataDisinfectMaintenanceFormSchema,
    showActionButtonGroup: false,
    actionColOptions: {
      span: 23,
    },
    baseColProps: {
      span: 12,
    },
    wrapperCol: {
      span: 23,
    },
  });
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async data => {
    await resetFields();
    setModalProps({ confirmLoading: false });
    if (data) {
      const resData = await disinfectMaintenanceDetail(data?.record?.deviceId);
      rowRes.value = resData;
      if (resData && resData.disinfection.length > 0) {
        const resVal = {
          hot_disinfectant: '',
          hot_disinfectionTime: '',
          chemistry_disinfectant: '',
          chemistry_disinfectionTime: '',
          chemistryHot_disinfectant: '',
          chemistryHot_disinfectionTime: '',
          other_disinfectant: '',
          other_disinfectionTime: '',
        };
        resData.disinfection.forEach(item => {
          switch (item.disinfectionType) {
            case 'ddt_disinfectant_hot':
              resVal.hot_disinfectant = item.disinfectant;
              resVal.hot_disinfectionTime = item.disinfectionTime;
              break;
            case 'ddt_disinfectant_chemistry':
              resVal.chemistry_disinfectant = item.disinfectant;
              resVal.chemistry_disinfectionTime = item.disinfectionTime;
              break;
            case 'ddt_disinfectant_chemistry_hot':
              resVal.chemistryHot_disinfectant = item.disinfectant;
              resVal.chemistryHot_disinfectionTime = item.disinfectionTime;
              break;
            case 'ddt_disinfectant_other':
              resVal.other_disinfectant = item.disinfectant;
              resVal.other_disinfectionTime = item.disinfectionTime;
              break;
          }
        });
        await setFieldsValue({
          ...resVal,
        });
      }
    }
  });

  // 提交按钮事件
  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      const params = rowRes.value;
      params.disinfection.forEach(item => {
        switch (item.disinfectionType) {
          case 'ddt_disinfectant_hot':
            item.disinfectant = values.hot_disinfectant;
            item.disinfectionTime = values.hot_disinfectionTime + '';
            break;
          case 'ddt_disinfectant_chemistry':
            debugger;
            item.disinfectant = values.chemistry_disinfectant;
            item.disinfectionTime = values.chemistry_disinfectionTime + '';
            break;
          case 'ddt_disinfectant_chemistry_hot':
            item.disinfectant = values.chemistryHot_disinfectant;
            item.disinfectionTime = values.chemistryHot_disinfectionTime + '';
            break;
          case 'ddt_disinfectant_other':
            item.disinfectant = values.other_disinfectant;
            item.disinfectionTime = values.other_disinfectionTime + '';
            break;
        }
      });
      await disinfectMaintenaceEdit({ ...params });
      createMessage.success('机器消毒维护成功！');
      closeModal();
      emit('success', {
        values: { ...values },
      });
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
