<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.invoiceUrl" :size="200" :adaptations="true" :simpleShow="true" :imgList="[model.invoiceUrl]" />
        </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:2,//2-普通发票，3-专用发票
      })
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
          {
            field: 'id',
            component: 'Input',
            show:false,
            label: '发票编号',
            required: true,
          },
          {
            field: 'email',
            component: 'Input',
            label: '邮箱',
            slot: 'text',
            ifShow:fileFlow.type == 2,
            colProps: {
              span: 24,
            },
          },{
            field: 'invoiceNum',
            component: 'Input',
            label: '发票编号',
            slot: 'text',
            required: true,
            colProps: {
              span: 24,
            },
          },{
            field: 'invoiceUrl',
            component: 'Upload',
            label: '电子发票',
            ifShow:fileFlow.type == 2,
            required: true,
            slot: 'img',
            colProps: {
              span: 22,
            },
          },{
            field: 'invoiceNum',
            component: 'Input',
            label: '发票编号',
            required: true,
            slot: 'text',
            colProps: {
              span: 24,
            },
          },{
            field: 'shipNum',
            component: 'Input',
            ifShow:fileFlow.type == 3,
            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})
        fileFlow.type = data.type
        setFieldsValue({
          ...data,
          ...detail,
        });
        updateSchema([
          {field: 'shipNum',ifShow:fileFlow.type == 3,},
          {field: 'email',ifShow:fileFlow.type == 2,},
          {field: 'invoiceUrl',ifShow:fileFlow.type == 2,},
        ])
      }
      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>
