<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model">

        <a-form-model-item label="下载模板" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-button
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="handleExportXls('开票内容表模板')">
            下载
          </a-button>
        </a-form-model-item>

        <a-form-model-item label="上传文件" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-upload name="file" :showUploadList="showUploadList" :fileList="fileList" :multiple="false" :headers="headers" :action="importExcelUrl()"  @change="handleImportExcel2">
            <a-button><a-icon type="upload"/>导入开票内容</a-button>
          </a-upload>
        </a-form-model-item>

      </a-form-model>
    </a-spin>
  </j-modal>

</template>

<script>
  import { httpAction } from '@/api/manage'
  import JDate from '@/components/jeecg/JDate'
  import JUpload from '@/components/jeecg/JUpload'
  import Vue from 'vue'
  import { ACCESS_TOKEN } from '@/store/mutation-types'
  import { getAction, deleteAction, putAction, postAction,downFile } from '@/api/manage'

  export default {
    name: 'HwUserExportModal',
    components: {
      JDate,
      JUpload,
    },
    data() {
      return {
        dataError:"",
        uploadAction: window._CONFIG['domianURL'] + '/hpSys/common/ossUpload',
        urlDownload: window._CONFIG['domianURL'] + '/hpSys/common/ossDownload',
        headers: {},
        fileList: [],
        type:0,
        showUploadList:false,
        title: '操作',
        width: 600,
        visible: false,
        disableSubmit: false,
        model: {
          collaborateProject: undefined,
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        defaultFileList:[],
        accept:'.pdf',
        confirmLoading: false,
        onBoardingCustomerOptions: [],
        subsidiaryOptions: [],
        collaborateProjectOptions: [],
        url: {
          exportXlsUrl: "/wisdom/hlwInvoiceCategory/exportDataXls",
          importExcelUrl: "wisdom/hlwInvoiceCategory/importExcel",
        }

      }
    },

    created() {
      const token = Vue.ls.get(ACCESS_TOKEN)
      this.headers = { 'X-Access-Token': token }
    },
    computed: {

    },
    methods: {
      add() {
        this.edit({})
      },
      edit(record) {
        this.fileList=[]
        this.model = Object.assign({}, record);
        this.visible = true
      },
      close() {
        this.visible = false
      },
      handleOk() {
        this.$emit('ok')
        this.visible = false
      },
      handleCancel() {
        this.close()
      },
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
      /* 导入 */
      handleImportExcel2(info){
        this.showUploadList = true;
        this.confirmLoading = true;
        if (info.file.status !== 'uploading') {
          console.log(info.file, info.fileList);
        }
        let fileList = info.fileList
        if (info.file.status === 'done') {
          if(info.file.response.success){
            this.$success({
              title: '导入成功',
              content: info.file.response.message,
              okText: '确定',
              onOk: () => {
                this.handleOk()
              }
            })
          } else {
            this.dataError=info.file.response.message
            this.$error({
              title: '导入失败',
              content: info.file.response.message,
              okText: '知道了',
              onOk: () => {
              }
            })
            fileList = [];
          }
          this.confirmLoading = false;
        } else if (info.file.status === 'error') {
          this.$message.error(`文件上传失败: ${info.file.msg} `);
          fileList = [];
          this.confirmLoading = false;
        }
        this.fileList = fileList
      },

      handleExportXls(fileName){
        if(!fileName || typeof fileName != "string"){
          fileName = "导出文件"
        }
        let param = {...this.queryParam};
        if(this.selectedRowKeys && this.selectedRowKeys.length>0){
          param['selections'] = this.selectedRowKeys.join(",")
        }
        console.log("导出参数",param)
        this.confirmLoading = true;
        downFile(this.url.exportXlsUrl,param).then((data)=>{
          if (!data) {
            this.$message.warning("文件下载失败")
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data]), fileName+'.xlsx')
          }else{
            let url = window.URL.createObjectURL(new Blob([data]))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName+'.xlsx')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link); //下载完成移除元素
            window.URL.revokeObjectURL(url); //释放掉blob对象
          }
        }).finally(() => {
          this.confirmLoading = false;
        })
      },

    }
  }
</script>