<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 { downloadByData } from '/@/utils/file/download'
  // import { sceneMove } from '/@/api/operate';
  import { companyUploadExcel, downTemplate } from '/@/api/customer';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { uploadApi, AddAndUpload } from '/@/api/product/index';
  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: {
            api: uploadApi,
            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: 1,
            }
          };
          const res = await companyUploadExcel(apiData);
          console.log('res', res);
          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: 1 });
          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>
