<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="fileFlow.title"
    width="700px"
    @cancel="resetFields"
    :confirmLoading="loading"
    @ok="handleSubmit"
  >
    <div class="pt-2px pr-3px">
      <BasicForm @register="registerForm">
        <template #text="{ model, field }">
          {{ model[field] }}
        </template>
      </BasicForm>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, h, onMounted, reactive, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import {
    partAllList,
    faultAllList,
    checkRegister,
    partInfo,
    partInfo,
    checkRegisterInfo,
    u8Send,
  } from '/@/api/spares';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { uploadApi } from '/@/api/product/index';
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const n = ref(1);
      const repairId = ref('');
      const fileFlow = reactive({
        file: null,
        title: '发件登记',
        repairId: null,
        cameraType: 0,
        type: 2, //2-普通发票，3-专用发票
        faultList: [],
      });
      const loading = ref(false);
      const { createMessage, createConfirm } = useMessage();
      let schemas: FormSchema[] = [
        {
          field: 'repairId',
          component: 'Input',
          label: '维修单号',
          slot: 'text',
          colProps: {
            span: 24,
          },
        },
        {
          field: 'deviceInfo',
          component: 'Input',
          label: '设备信息',
          slot: 'text',
          colProps: {
            span: 18,
          },
        },
        {
          field: 'trackingNum',
          component: 'Input',
          label: '发货单号',
          required: true,
          componentProps: {
            maxLength: 50,
          },
          colProps: {
            span: 16,
          },
        },
      ];
      const [registerForm, { validate, resetFields, setFieldsValue }] = useForm({
        labelWidth: 100,
        schemas: schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      // getFaultList();
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        repairId.value = data?.repairId;
        data && onDataReceive(data);
      });
      async function getFaultList() {
        const res = await faultAllList({});
        fileFlow.faultList = res.map((item) => {
          return {
            ...item,
            label: item.faultMsg,
            value: item.faultId,
          };
        });
      }

      function onDataReceive(data) {
        console.log('openOutModal', data, repairId.value);
        resetFields();
        // updateSchema({
        //   field: 'faultIds',
        //   componentProps: {
        //     options: fileFlow.faultList,
        //   },
        // });
        fileFlow.type = data.type;
        fileFlow.cameraType = data.cameraType;
        setFieldsValue({
          ...data,
          remark: '',
          deviceInfo: t(`routes.scene.tableType.${data.cameraType}`) + data.cameraSnCode,
        });
      }
      const handleSubmit = async () => {
        const params = await validate();
        try {
          createConfirm({
            iconType: 'warning',
            title: () => h('span', '温馨提示'),
            content: '确定提交发件登记吗?',
            onOk: async () => {
              loading.value = true;
              await u8Send(params);
              loading.value = false;
              createMessage.success(t('common.optSuccess'));
              closeModal();
              emit('update');
            },
            onCancel: () => {
              loading.value = false;
            },
          });
        } catch (error) {
          loading.value = false;
          console.log('not passing', error);
        }
      };
      return {
        register,
        registerForm,
        fileFlow,
        handleSubmit,
        addListFunc,
        resetFields,
        loading,
        t,
      };
    },
  });
</script>
