<template>
  <a-modal
    ref="modal"
    title='导出历史'
    :visible="testShow"
    @cancel="handleCancel"
    destroyOnClose
    footer=""
    width=580
  >
  <a-card :bordered="false" v-show='testShow'>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="downloadFile(record)">下载</a>
        </span>

      </a-table>
    </div>

  </a-card>
  </a-modal>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { downFile } from '@/api/manage'


  export default {
    name: 'GtExportHisList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {

    },
    watch:{
      show:function(newVal,oldVal) {
        console.info(newVal)
        this.testShow = newVal;
      },
      exportId:function(newVal,oldVal) {
        this._exportId = newVal;
        //this.loadData();
        //console.info("py123123123123")
      }
    },
    props:{
      show:{
        type:Boolean,
        default:false
      },
      exportId:{
        type:String,
        default:""
      }
    },
    data () {
      return {
        description: '导出管理页面',
        testShow:false,
        _exportId:"",
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            hidden:true,
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },


          {
            title:'导出文件',
            align:"center",
            dataIndex: 'exportFile'
          },
          {
            title:'运行时间',
            align:"center",
            dataIndex: 'runTime'
          },
          {
            title:'导出路径',
            align:"center",
            hidden : true,
            dataIndex: 'exportPath'
          },
          {
            title:'导出时间',
            align:"center",
            dataIndex: 'createTime'
          },
          {
            title:'导出状态',
            align:"center",
            dataIndex: 'status_dictText'
          },
          {
            title:'报错信息',
            align:"center",
            dataIndex: 'errorMsg'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/export/gtExportHis/list",
          downloadUrl:"/sys/common/static/",
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      // this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      downloadFile(record){
        let filePath = record.exportPath;
        let fileName = record.exportFile;
        downFile(this.url.downloadUrl+filePath,{}).then((data)=>{
          if (!data) {
            this.$message.warning("文件下载失败")
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName)
          }else{
            //判断当前后缀是生成什么文件
            let downloadType = record.downloadType;

            if (downloadType == 'zip') {
              let url = window.URL.createObjectURL(new Blob([data],{type: 'application/zip'}))
              let link = document.createElement('a')
              link.style.display = 'none'
              link.href = url
              link.setAttribute('download', fileName)
              document.body.appendChild(link)
              link.click()
              document.body.removeChild(link); //下载完成移除元素
              window.URL.revokeObjectURL(url); //释放掉blob对象
            }else{
              let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
              let link = document.createElement('a')
              link.style.display = 'none'
              link.href = url
              link.setAttribute('download', fileName)
              document.body.appendChild(link)
              link.click()
              document.body.removeChild(link); //下载完成移除元素
              window.URL.revokeObjectURL(url); //释放掉blob对象
            }

          }
        })
      },
      setUrl(_url){
        this.url.list = _url;
      },
      close() {
        this.$emit("_123cloase");
      },
      handleCancel() {
        this.close()
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'int',value:'exportId',text:'exportId'})
        fieldList.push({type:'string',value:'exportPath',text:'exportPath'})
        fieldList.push({type:'string',value:'exportFile',text:'exportFile'})
        fieldList.push({type:'string',value:'processRate',text:'processRate'})
        fieldList.push({type:'string',value:'errorMsg',text:'errorMsg'})
        fieldList.push({type:'string',value:'runTime',text:'runTime'})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>