import Vue from 'vue'

import Cookies from 'js-cookie'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import Element from 'element-ui'
import './styles/element-variables.scss'

import '@/styles/index.scss' // global css

import App from './App'
import store from './store'
import router from './router'

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

import Print from '@/utils/print' // 打印
Vue.use(Print)

import ZkTable from 'vue-table-with-tree-grid'
Vue.use(ZkTable)

Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
})
Vue.prototype.msgSuccess = function (msg) {
  this.$message({ showClose: true, message: msg, type: 'success', duration: 2000 });
};

Vue.prototype.msgError = function (msg) {
  this.$message({ showClose: true, message: msg, type: 'error', duration: 2000 });
};

Vue.prototype.msgInfo = function (msg) {
  this.$message({ type: 'info', message: msg, duration: 2000 });
};
Vue.prototype.msgWarn = function (msg) {
  this.$message({ type: 'warning', message: msg, duration: 2000 });
};
// 公共确认弹窗
Vue.prototype.confirmModal = function (options, callback, cancelFun = () => { }) {
  const msg = options.msg;
  const title = options.title || '操作须知';
  const newOptions = {
    type: 'warning',
    ...options
  };
  this.$confirm(msg, title, newOptions).then(() => {
    callback();
  }).catch(() => {
    cancelFun();
  });
};

Vue.config.productionTip = false;


import { registerMicroApps, start } from 'qiankun';
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})



export const MICRO_CONF = [

  {
    name: 'sub-report',
    // entry: '//localhost:8082',
    entry: 'https://report.lifejingzhi.com/',
    container: '#qiankunapp1',
    activeRule: '/report',
    props: {
      parentStore: store,
    }
  },
  {
    name: 'app-mall',
    // entry: '//localhost:8083',
    entry: 'https://jzmall.lifejingzhi.com',//正式
    // entry: 'https://uat.jzmall.lifejingzhi.com',//测试
    container: '#qiankunapp2',
    activeRule: '/jzmall',
    props: {
      parentStore: store,
    }
  },
  {
    name: 'sub-worker',
    // entry: '//localhost:8081',
    entry: 'https://worker.lifejingzhi.com',//正式
    // entry: 'https://uat.servant.lifejingzhi.com',//测试
    container: '#qiankunapp3',
    activeRule: '/worker',
    props: {
      parentStore: store,
    }
  },{
    name: 'app-customer',
    // entry: '//localhost:8083',
    entry: 'https://jzcrm.lifejingzhi.com',//正式
    // entry: 'https://uat.jzcrm.lifejingzhi.com',//测试
    container: '#qiankunapp4',
    activeRule: '/customer',
    props: {
      parentStore: store,
    }
  },
]

// 手动控制应用加载
import {loadMicroApp} from 'qiankun';

// 缓存应用实例
const microList = new Map([])
// 当前应用配置
let current



router.beforeEach( async (to, from, next) =>{

  const conf = MICRO_CONF.find(item => to.path.indexOf(item.activeRule) !== -1)

  // 应用跳转
  if(conf){

    // 未切换子应用
    if(current &&  current.activeRule === conf.activeRule ){
      next()
      return
    }

    const cacheMicro = microList.get(conf.activeRule)

    // 已缓存应用
    if(cacheMicro){
      next()
      return
    }

    // 未缓存应用
    const micro = loadMicroApp({...conf, router})

    microList.set(conf.activeRule, micro)
    current = conf
    next()

  }

  // 主应用内跳转
  next()

})

window.addEventListener('app302', function (event) {
  // 当接收到事件时，刷新页面
  location.reload();
});


// registerMicroApps([

//   {
//     name: 'sub-report',
//     // entry: '//localhost:8082',
//     entry: 'https://report.lifejingzhi.com/',
//     // entry: { scripts: ['//localhost:8082/main.js'] },
//     container: '#qiankunapp',
//     activeRule: '/report',
//     props: {
//       parentStore: store,
//     }
//   },
//   {
//     name: 'app-mall',
//     // entry: { scripts: ['//localhost:8081/main.js'] },
//     // entry: '//localhost:8083',
//     entry: 'https://jzmall.lifejingzhi.com',
//     container: '#qiankunapp',
//     activeRule: '/jzmall',
//     props: {
//       parentStore: store,
//     }
//   },
//   {
//     name: 'sub-worker',
//     //entry: { scripts: ['//localhost:8081/main.js'] },
//     // entry: '//localhost:8082',
//     entry: 'https://worker.lifejingzhi.com',
//     container: '#qiankunapp',
//     activeRule: '/worker',
//     props: {
//       parentStore: store,
//     }
//   },{
//     name: 'app-customer',
//     // entry: { scripts: ['//localhost:8081/main.js'] },
//     // entry: '//localhost:8081',
//     entry: 'https://crm.lifejingzhi.com',
//     container: '#qiankunapp',
//     activeRule: '/customer',
//     props: {
//       parentStore: store,
//     }
//   },
// ]);

