import Vue from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store/index'
import request from '@/benyun/utils/request'
import {download} from '@/benyun/utils/request'
import Cookies from 'js-cookie'
import lodash from 'lodash'
import plugins from '@/benyun/plugins'
import directive from './directive' // directive
import {getDicts} from "@/api/system/dict/data";
import {getToken} from '@/benyun/utils/auth'
import {getQueryObject} from '@/benyun/utils'

import '@/assets/styles/index.scss' // global css
import '@/assets/styles/benyuntech.scss' // young css

import './assets/icons' // icon
import './permission' // permission control
// import './permission2'

import Element from 'element-ui'
import './assets/styles/element-variables.scss'
import { Message,MessageBox } from 'element-ui';

// import VXETable from 'vxe-table'
import XEUtils from 'xe-utils'
import 'vxe-table/lib/style.css'
// import { VXETable, Column,Table,Modal,Button,Input,Validator,Filter } from 'vxe-table'
import VXETable from 'vxe-table'
import zhCN from 'vxe-table/lib/locale/lang/zh-CN'
 // 按需加载的方式默认是不带国际化的，自定义国际化需要自行解析占位符 '{0}'，例如：
VXETable.setup({
  i18n: (key, args) => XEUtils.toFormatString(XEUtils.get(zhCN, key), args)
})
import {addDateRange, handleTree, parseTime, resetForm, selectDictLabel, selectDictLabels, getQueryString} from "@/benyun/utils/benyuntech";
import {getConfigKey} from "@/api/system/config";
import gmComponent from './benyun/plugins/componentRegister'

// 字典数据组件
import DictData from '@/components/DictData'
DictData.install()

// 分页组件
import Pagination from "@/components/Pagination/index.vue";
// 自定义表格工具组件
import RightToolbar from "@/components/RightToolbar/index.vue"
// 字典标签组件
import DictTag from '@/components/DictTag/index.vue'

// 全局组件挂载
Vue.component('DictTag', DictTag)
Vue.component('Pagination', Pagination)
Vue.component('RightToolbar', RightToolbar)

//组件注册
import productDialog from './components/productDialog/productDialog.vue'
Vue.component('productDialog', productDialog);
import productStore from './components/productStore/productStore.vue'
Vue.component('productStore', productStore);
import buyer from './components/buyer/buyer.vue'
Vue.component('by-buyer', buyer);
import productSkuModal from './components/skuModal/productModal.vue';
Vue.component('product-sku-modal', productSkuModal);
import supplierModal from './components/supplierModal/supplierModal.vue';
Vue.component('supplier-modal', supplierModal);
import warehouse from './components/warehouse/warehouse.vue';
Vue.component('warehouse', warehouse);
import warehousePosition from './components/warehousePosition/warehousePosition.vue';
Vue.component('warehousePosition', warehousePosition);
import byLog from './components/byLog/byLog.vue';
Vue.component('byLog', byLog);
import userModal from './components/userModal/userModal.vue';
Vue.component('user-modal', userModal);

Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
})

Vue.use(VXETable)
// Vue.use(Validator).use(Column).use(Table).use(Modal).use(Button).use(Input);
Vue.use(gmComponent);
Vue.use(plugins);
Vue.use(directive);

Vue.config.productionTip = false

// 全局方法挂载
Vue.prototype.getDicts = getDicts
Vue.prototype.$request = request;
Vue.prototype.$lodash = lodash;
// Vue.prototype.$download = download; //下载
Vue.prototype.$XModal = VXETable.modal
// 全局方法挂载
Vue.prototype.getDicts = getDicts
Vue.prototype.getConfigKey = getConfigKey
Vue.prototype.parseTime = parseTime
Vue.prototype.resetForm = resetForm
Vue.prototype.addDateRange = addDateRange
Vue.prototype.selectDictLabel = selectDictLabel
Vue.prototype.selectDictLabels = selectDictLabels
Vue.prototype.download = download
Vue.prototype.handleTree = handleTree

function importJS() { // 引入 "天地图"
  return new Promise((resolve) => {
    const importHandle = (url:String) => {
      const head = document.getElementsByTagName('head')[0]
      const script:any = document.createElement('script')
      script.type = 'text/javascript'
      script.src = url
      script.onload = function() {
        resolve(true)
      }
      script.onerror = () => {
        resolve(false)
      }
      head.appendChild(script)
    }

    // importHandle('http://api.tianditu.gov.cn/api?v=4.0&tk=873e4a15f0f80945cd0ba56f069c19bc')
    importHandle('/static/js/xlsx.full.min.js')

  })
}

function omsLogin() {
  let params = getQueryObject();
  const ticket = params.ticket;
  const ssoToken = params.ssoToken;
  if(ticket || ssoToken){
    let data:any = {}
    const url = ticket ? '/sso/doLoginByTicket' : '/sso/doLoginByToken'
    if(ticket) {
      data.ticket = ticket
    }
    if(ssoToken) {
      data.token = ssoToken
    }
    store.dispatch('GetToken',{
      url,data
    }).then(() => {
      unitVue()
      
    }).catch((err:any) => {
      let msg = '未知错误，请联系管理员！'
      if(err && err.msg) {
        msg = err.msg
      }
      MessageBox.alert(msg, '提示', {
        confirmButtonText: '确定',
        callback: () => {
          store.dispatch('LogOut').then(() => {})
        }
      });
      
    })
  }else if(getToken()){
    unitVue()
  }else{
    const redirect = location.origin;
    location.href = `${process.env.VUE_APP_LOGIN_URL}?&redirect=${encodeURIComponent(redirect)}`
  }
}

function unitVue() {
  Promise.all([importJS()]).then(result => {
    new Vue({
      router,
      store,
      render: h => h(App),
      data: {
        eventHub: new Vue()
      }
    }).$mount('#app')
  })
}
// unitVue()
omsLogin()


