<template>
  <div>
    <!--引用表格-->
   <BasicTable @register="registerTable" :rowSelection="rowSelection">
     <!--插槽:table标题-->
      <template #tableTitle>
      </template>
      <template #templateFile="{ record }">
        <a :href="baseUrl+record.templateFile">{{record.templateFile}}</a>
      </template>
       <!--操作栏-->
      <template #action="{ record }">
        <!-- <TableAction :actions="getTableAction(record)" /> -->
        <span slot="upload" >
          <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="uploadUrl"  @change="value => handleImportExcel(value, record)">
            <a>上传</a>
          </a-upload>
          </span>
      </template>
      <!--字段回显插槽-->
      <template v-slot:bodyCell="{ column, record, index, text }">
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <BaseTemplatesModal @register="registerModal" @success="handleSuccess"></BaseTemplatesModal>
  </div>
</template>

<script lang="ts" name="baseCode-baseTemplates" setup>
  import {ref, reactive, computed, unref} from 'vue';
  import {BasicTable, useTable, TableAction} from '/@/components/Table';
  import {useModal} from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage'
  import BaseTemplatesModal from './components/BaseTemplatesModal.vue'
  import {columns, searchFormSchema,} from './BaseTemplates.data';
  import {list, getImportUrl,getExportUrl} from './BaseTemplates.api';
  import { getToken } from '/@/utils/auth';
  import { message } from 'ant-design-vue';
  import { useGlobSetting } from '/@/hooks/setting';
  import { defHttp } from '/@/utils/http/axios';
  const queryParam = reactive<any>({});
  const { domainUrl } = useGlobSetting();
  const baseUrl = domainUrl + '/sys/common/static/';
  //注册model
  const [registerModal] = useModal();
  const tokenHeader = { 'X-Access-Token': getToken() };
  //注册table数据
  const { prefixCls,tableContext,onExportXls,onImportXls } = useListPage({
      tableProps:{
           title: '模板',
           api: list,
           columns,
           canResize:false,
           formConfig: {
              //labelWidth: 120,
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              showAdvancedButton:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
              ],
            },
           actionColumn: {
               width: 120,
               fixed:'right'
            },
            beforeFetch: (params) => {
              return Object.assign(params, queryParam);
            },
      },
       exportConfig: {
            name:"模板",
            url: getExportUrl,
            params: queryParam,
          },
          importConfig: {
            url: getImportUrl,
            success: handleSuccess
          },
  })

  const [registerTable, {reload},{ rowSelection, selectedRowKeys }] = tableContext
   /**
    * 成功回调
    */
  function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
   }
   /**
      * 操作栏
      */
  function getTableAction(record){
       return [
         {
           label: '上传',
           scopedSlots: { customRender: 'upload' }
          //  auth: 'baseCode:base_templates:edit'
         }
       ]
   }

   function handleImportExcel(info,record){
        if (info.file.status !== 'uploading') {
          // console.log(info.file, info.fileList);
        }
        if (info.file.status === 'done') {
          if (info.file.response.success) {
            var row = JSON.parse(JSON.stringify(record))
            row.templateFile = info.file.response.message
            editData(row)
          } else {
            message.error(`${info.file.name} ${info.file.response.message}.`);
          }
        } else if (info.file.status === 'error') {
          message.error(`文件上传失败: ${info.file.msg} `);
        }
      }
      function editData(record){
        let params = record,
        url = '/baseCode/baseTemplates/edit'
        defHttp.post({url,params}, { isTransformResponse: false })
        .then((res) => {
            if (res.success) {
               reload()
               message.success('上传成功');
            } else {
               message.error(res.message);
            }
        })
        .finally(() => {
           
        });
      }
</script>

<style lang="less" scoped>
  :deep(.ant-picker),:deep(.ant-input-number){
    width: 100%;
  }
</style>