<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="批量入库"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    @ok="handleSubmit"
    :min-height="0"
  >
    <div class="pt-2px pr-3px">
      <BasicForm @register="registerForm" :model="model">
        <template #text="{ model, field }">
          {{ model[field] }}
        </template>
      </BasicForm>
      <a @click="getTemplate" style="padding: 20px 0 0 80px">下载批量入库模板</a>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, nextTick, onMounted, reactive } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  // import { sceneMove } from '/@/api/operate';
  import { companyUploadExcel, downTemplate } from '/@/api/customer';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { downloadByData } from '/@/utils/file/download'
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const modelRef = ref({});
      const fileFlow = reactive({
        file: null,
      });
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
        {
          field: 'file',
          component: 'Upload',
          label: t('routes.product.file'),
          required: true,
          rules: [{ required: true, message: t('common.uploadMessge') }],
          // helpMessage: t('routes.corporation.uploadHelp'),
          itemProps: {
            validateTrigger: 'onBlur',
          },
          componentProps: {
            maxNumber: 1,
            maxSize: 5,
            fileFlow: true,
            accept: ['xls', 'xlsx'],
            afterFetch: function (data) {
              // console.log('uploadApi', data);
              // Reflect.set(data, 'url', data.file);
              fileFlow.file = data.file;
              console.log(fileFlow.file);
              return data;
            },
          },

          colProps: {
            span: 22,
          },
        },
      ];
      const [registerForm, { validate, resetFields, setFieldsValue }] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        // console.log(data);
        data && onDataReceive(data);
      });

      function onDataReceive(data) {
        modelRef.value = data;
        resetFields();
        setFieldsValue({
          type: data.sceneName,
        });
      }
      const handleSubmit = async () => {
        try {
          const params = await validate();
          const apiData = {
            file: fileFlow.file,
            data:{
              type: 0,
            }
          };
          const res = await companyUploadExcel(apiData);
          closeModal();
          resetFields();
          createMessage.success( res || '导入成功');
        } catch (error) {
          console.log('not passing', error);
        }
      };
      function handleVisibleChange(v) {
        // console.log(v);
        // v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      async function getTemplate() {
        try {
          const res:BlobPart = await downTemplate({ type: 0 });
          console.log('downTemplate',res)
          downloadByData(res.data,'入库模板.xlsx')
        } catch (error) {
          console.log('not passing', error);
        }
      }
      return {
        register,
        schemas,
        registerForm,
        model: modelRef,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        resetFields,
        t,
        getTemplate,
      };
    },
  });
</script>
