<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="开票详情"
    height="500"
    :showOkBtn="false"
    :showCancelBtn="false"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    @ok="handleSubmit"
  >
    <div class="pt-2px pr-3px">
      <BasicForm @register="registerForm">
        <template #text="{ model, field }">
          {{ model[field]  }}
        </template>
        <template #img="{ model }">
          <TableImg v-if="model.invoiceImg" :size="200" :adaptations="true" :simpleShow="true" :imgList="[model.invoiceImg]" />
        </template>
      </BasicForm>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, nextTick, onMounted, reactive } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { TableImg } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { InvoiceRegister, InvoiceDetail } from '/@/api/order';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { uploadApi } from '/@/api/product/index';
  import { ResultEnum } from '/@/enums/httpEnum';

  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm, TableImg },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const fileFlow = reactive({
        file:null,
        type:0,//2-普通发票，3-专用发票
      })
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
          {
            field: 'invoiceEmail',
            component: 'Input',
            label: '邮箱',
            slot: 'text',
            ifShow:fileFlow.type == 0,
            colProps: {
              span: 24,
            },
          },{
            field: 'invoiceNo',
            component: 'Input',
            label: '发票编号',
            slot: 'text',
            required: true,
            colProps: {
              span: 24,
            },
          },{
            field: 'invoiceImg',
            component: 'Upload',
            label: '电子发票',
            ifShow:fileFlow.type == 0,
            required: true,
            slot: 'img',
            colProps: {
              span: 22,
            },
          },{
            field: 'trackingNum',
            component: 'Input',
            ifShow:fileFlow.type == 1,
            slot: 'text',
            label: '快递单号',
            required: true,
            colProps: {
              span: 24,
            },
          },
      ];

      const [registerForm, { validate, resetFields, setFieldsValue, updateSchema }] = useForm({
        labelWidth: 120,
        schemas:schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      async function onDataReceive(data) {
        resetFields();
        // let detail = await InvoiceDetail({id:data.id})
        console.log('data',data)
        fileFlow.type = data.invoiceType
        setFieldsValue({
          ...data,
          // ...detail,
        });
        updateSchema([
          {field: 'trackingNum',ifShow:fileFlow.type == 1,},
          {field: 'invoiceEmail',ifShow:fileFlow.type == 0,},
          {field: 'invoiceImg',ifShow:fileFlow.type == 0,},
        ])
      }
      const handleSubmit = async () => {
          closeModal();
          emit('update');
      };
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        registerForm,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        resetFields,
        t,
      };
    },
  });
</script>
