<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="开票登记"
    :okText="okText"
    @visible-change="handleVisibleChange"
    @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, nextTick, 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 { InvoiceOpen } 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 },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const fileFlow = reactive({
        file:null,
        type:2,//2-普通发票，3-专用发票
      })
      const okText = ref('发送')
      const loading = ref(false)
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
          {
            field: 'invoiceId',
            component: 'Input',
            show:false,
            label: '发票编号',
          },
          {
            field: 'invoiceEmail',
            component: 'Input',
            label: '邮箱',
            slot: 'text',
            ifShow:fileFlow.type == 0,
            componentProps: {
              maxLength: 50,
            },
            colProps: {
              span: 18,
            },
          },{
            field: 'invoiceNo',
            component: 'Input',
            label: '发票编号',
            required: true,
            componentProps: {
              maxLength: 50,
            },
            colProps: {
              span: 18,
            },
          },{
            field: 'invoiceImg',
            component: 'Upload',
            label: '电子发票',
            ifShow:fileFlow.type == 0,
            required: true,
            rules: [{ required: true, message: t('common.uploadMessge') }],
            itemProps: {
              validateTrigger: 'blur',
            },
            componentProps: {
              api: uploadApi,
              maxNumber: 1,
              maxSize: 1000,
              accept: ['jpeg','jpg','png'],
            },

            colProps: {
              span: 22,
            },
          },{
            field: 'trackingNum',
            component: 'Input',
            ifShow:fileFlow.type == 1,
            label: '快递单号',
            componentProps: {
              maxLength: 50,
            },
            required: true,
            colProps: {
              span: 18,
            },
          },
      ];

      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);
      });
      function onDataReceive(data) {
        resetFields();
        fileFlow.type = data.invoiceType
        setFieldsValue(data);
        okText.value = fileFlow.type == 0?'发送':'确认'
        updateSchema([
          {field: 'trackingNum',ifShow:fileFlow.type == 1,},
          {field: 'invoiceEmail',ifShow:fileFlow.type == 0,},
          {field: 'invoiceImg',ifShow:fileFlow.type == 0,},
        ])
      }
      const handleSubmit = async () => {
        loading.value = true
        try {
          const params = await validate();
          await InvoiceOpen({
            ...params,
            invoiceImg:fileFlow.type == 0?params.invoiceImg[0]:''
          });
          closeModal();
          resetFields();
          createMessage.success(t('common.optSuccess'));
          emit('update');
          loading.value = false
        } catch (error) {
          loading.value = false
          console.log('not passing', error);
        }
      };
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        registerForm,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        resetFields,
        okText,
        loading,
        t,
      };
    },
  });
</script>
