<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="付款登记"
    @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, onMounted, reactive, ref, h } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { payRegister } 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 fileFlow = reactive({
        file:null,
        type:2,//2-普通发票，3-专用发票
      })
      const loading = ref(false)
      const { createMessage, createConfirm } = useMessage();
      const schemas: FormSchema[] = [
          {
            field: 'repairId',
            slot: 'text',
            component: 'Input',
            label: '维修单号',
          },{
            field: 'deviceInfo',
            component: 'Input',
            label: '设备信息',
            slot: 'text',
            colProps: {
              span: 18,
            },
          },
          {
            field: 'payAmount',
            component: 'Input',
            label: '应收金额',
            slot: 'text',
            colProps: {
              span: 24,
            },
          },{
            field: 'payType',
            component: 'Select',
            label: '付款方式',
            required: true,
            colProps: {
              span: 18,
            },
            defaultValue:0,
            componentProps: {
              options: [
                {
                  label: '银行转账',
                  value: 0,
                  key: 0,
                }
              ],
            },
          },{
            field: 'payImg',
            component: 'Upload',
            label: '付款凭证',
            required: true,
            rules: [{ required: true, message: t('common.uploadMessge') }],
            itemProps: {
              validateTrigger: 'blur',
            },
            componentProps: {
              api: uploadApi,
              maxNumber: 5,
              maxSize: 10,
              accept: ['jpeg','jpg','png'],
            },
            colProps: {
              span: 22,
            },
          },
      ];

      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();
        setFieldsValue({
          ...data,
          deviceInfo:t(`routes.scene.tableType.${data.cameraType}`)+data.cameraSnCode
        });
      }
      const handleSubmit = async () => {
        const params = await validate();
        createConfirm({
          iconType: 'warning',
          title: () => h('span', '温馨提示'),
          content: '确定提交付款登记吗?',
          onOk: async () => {
            loading.value = true
            try {
              await payRegister(params);
              closeModal();
              resetFields();
              createMessage.success(t('common.optSuccess'));
              emit('update');
              loading.value = false
            } catch (error) {
              loading.value = false
              console.log('not passing', error);
            }
          },
          onCancel: () => {
            loading.value = false
          }
        });
      };
      return {
        register,
        registerForm,
        fileFlow,
        handleSubmit,
        addListFunc,
        resetFields,
        loading,
        t,
      };
    },
  });
</script>
