const moudule = {};
import vue from "vue"
import excelImport from './index.vue'

import {
  getToken
} from '../../utils/auth'

let configUtil = {
  baseUrl: process.env.VUE_APP_BASE_API,
  getToken
};

const ExcelImportInstance = vue.extend(excelImport);
const initInstance = function (containter, flag) {
  // 实例化vue实例
  let currentInstance;
  currentInstance = new ExcelImportInstance({i18n: window.$vueRoot._i18n});

  let dom = currentInstance.$mount().$el;
  containter.appendChild(dom);
  return currentInstance;
};

function toDo(options) {
  // let containter = this && this.$el ? this.$el : document.body;
  let containter = document.body;
  let vueTarget = this && this.$el ? this : window.$vueRoot;
  let currentInstance = initInstance(containter, 1);
  currentInstance.param = {};
  Object.assign(currentInstance.param, {vue: vueTarget}, options);
  return currentInstance.exc();
};


// 定义插件对象
// vue的install方法，用于定义vue插件
moudule.install = function (Vue) {
  // 在Vue的原型上添加实例方法，以全局调用
  Vue.prototype.$langImport = toDo;

};

function downloadTxt(fileName, content) {
  // 创建Blob对象
  const blob = new Blob([content], {type: 'text/plain;charset=utf-8'});
  // 创建URL
  const url = URL.createObjectURL(blob);
  // 创建a标签
  const link = document.createElement('a');
  link.href = url;
  link.setAttribute('download', fileName + '.txt');
  document.body.appendChild(link);
  // 触发下载
  link.click();
  // 清理
  document.body.removeChild(link);
  URL.revokeObjectURL(url);
}

function download(ippaaapp, uuid) {
  let fileUrl = `${configUtil.baseUrl}${USER_PREFIX}/form_develop/download?ippaaapp=${ippaaapp}&uuid=${uuid}`;
  let params = {
    // n: fileName
  };
  params['access_token'] = getToken();

  let form = document.createElement('form');

  let randomNum = (new Date()).valueOf() + Math.floor(Math.random() * 1000000);
  let formId = "formId-" + randomNum;
  let formName = "formName-" + randomNum;

  form.id = formId;
  form.name = formName;
  form.target = "_blank";
  document.body.appendChild(form);
  for (let obj in params) {
    if (params.hasOwnProperty(obj)) {
      let input = document.createElement('input');
      input.tpye = 'hidden';
      input.name = obj;
      input.value = params[obj];
      form.appendChild(input);
    }
  }
  form.method = "post"; //请求方式
  form.action = fileUrl;
  form.submit();
  document.body.removeChild(form);
}

export default moudule;
