
import Vue from 'vue'
import { Message, MessageBox,Loading  } from 'element-ui'
import {setCache} from '@/utils/auth'
import axios from '@/utils/request'
// import FileSaver from '@/utils/FileSaver'

import VueRouter from 'vue-router'
var deriveJobId='';
var circulation=0;
var loadingInstance
export default function downloadFile(url, params,routerPath){
    MessageBox.confirm('确认导出排名？', '导出', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
          // loadingInstance = Loading.service({});
          // setCache('routerPath',routerPath)
          axios('get',url,params).then(res => {
              // deriveJobId = res.data.data.job_id;
              // downloadResult();
              Message.success('已导出，请留意消息通知');
          });
    }).catch(() => {});
}
function downloadResult() {
    axios('get', '/api/download/result', {job_id:deriveJobId}).then(res => {
        let urla=res.data.data.url
        if(url){
          loadingInstance.close();
          try{
            var oReq = new XMLHttpRequest();
            oReq.open("GET", urla, true);
            oReq.responseType = "blob";
            oReq.onload = function() {
                var file = new Blob([oReq.response], {type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
                // FileSaver.saveAs(file, "");
            };
            oReq.send();
            // var elemIF=document.createElement('iframe')
            // elemIF.src=url;
            // elemIF.style.display='none';
            // document.body.appendChild(elemIF)
          }catch(e){
            message.warning('导出异常');
          }
        }else{
          downloadResult();
        }
    });
};
