import Vue from 'vue';
import axios from './router/axios';
import VueAxios from 'vue-axios';
import App from './App';
import router from './router/router';
import './permission'; // 权限
import './error'; // 日志
import './cache';//页面缓存
import store from './store';
import {loadStyle} from './util/util'
import * as urls from '@/config/env';
import Element from 'element-ui';
import lazySelect from '@/components/lazy-select/lazy-select';
import AmapVue from '@amap/amap-vue';
import {
  iconfontUrl,
  iconfontVersion
} from '@/config/env';
import i18n from './lang'; // Internationalization
import './styles/common.scss';
import basicBlock from './components/basic-block/main';
import basicContainer from './components/basic-container/main';
import thirdRegister from './components/third-register/main';
import AvueUeditor from 'avue-plugin-ueditor';
import website from '@/config/website';
import crudCommon from '@/mixins/crud';
import {select} from "./api/base/region";
import dataV from '@jiaminghi/data-view'
import deviceJob from "./components/job/job_solve.vue";
require('echarts');
import VideoPlayer from 'vue-video-player'
import 'video.js/dist/video-js.css' //videoJs的样式
import 'vue-video-player/src/custom-theme.css' //vue-video-player的样式
import 'videojs-flash'; //引入才能播放rtmp视屏
import 'videojs-contrib-hls' //引入才能播放m3u8文件
// 按需引入vue-awesome图标
import Icon from 'vue-awesome/components/Icon';
import 'vue-awesome/icons/chart-bar.js';
import 'vue-awesome/icons/chart-area.js';
import 'vue-awesome/icons/chart-pie.js';
import 'vue-awesome/icons/chart-line.js';
import 'vue-awesome/icons/align-left.js';
// 适配flex
import './views/grid/common/flexible.js';
// 引入全局css
import './views/grid/assets/scss/style.scss';
import alert from './views/grid/common/popup.js' //这里引入的是js文件
// import $ from 'jquery'
// import layer from "layui-layer";
Vue.prototype.$datavAlert = alert;
// 注册全局crud驱动
window.$crudCommon = crudCommon;
// 全局注册图标
Vue.component('icon', Icon);
Vue.prototype.$echarts = echarts;
// 加载Vue拓展
Vue.use(VideoPlayer);
Vue.use(router);
Vue.use(dataV);
Vue.use(VueAxios, axios);
Vue.use(Element, {
  i18n: (key, value) => i18n.t(key, value)
});
AmapVue.config.version = '2.0';
AmapVue.config.key = 'cbb265e2a60bbbc04a52eec4eecdeb12';
AmapVue.config.plugins = [
  'AMap.ToolBar',
  'AMap.MoveAnimation',
  'AMap.MouseTool',
  'AMap.DistrictSearch'
];
Vue.use(AmapVue);
Vue.use(window.AVUE, {
  size: 'small',
  tableSize: 'small',
  calcHeight: 60,
  i18n: (key, value) => i18n.t(key, value)
});
// 注册全局容器
Vue.component('basicContainer', basicContainer);
Vue.component('basicBlock', basicBlock);
Vue.component('thirdRegister', thirdRegister);
Vue.component('avueUeditor', AvueUeditor);
Vue.component('lazySelect', lazySelect);
Vue.component("device-job",deviceJob);
// 加载相关url地址
Object.keys(urls).forEach(key => {
  Vue.prototype[key] = urls[key];
});
// 加载website
Vue.prototype.website = website;
// 动态加载阿里云字体库
iconfontVersion.forEach(ele => {
  loadStyle(iconfontUrl.replace('$key', ele));
});

Vue.config.productionTip = false;

Vue.prototype.regionFunction = (node, resolve)=> {
  let stop_level = 3;
  let level = node.level;
  let data = node.data || {}
  let code = data.code;
  let list = [];
  let callback = () => {
    resolve((list || []).map(ele => {
      return Object.assign(ele, {
        leaf: level >= stop_level
      })
    }));
  }
  if (level == 0) {
    select().then(res => {
      list = res.data.data;
      callback()
    })
  }
  if (level == 1) {
    select(code).then(res => {
      list = res.data.data;
      callback()
    })
  } else if (level == 2) {
    select(code).then(res => {
      list = res.data.data;
      callback()
    })
  } else if (level == 3) {
    select(code).then(res => {
      list = res.data.data;
      callback()
    })
  }else{
    list=[]
    callback()
  }
}


Vue.prototype.$addStorageEvent = function (type, key, data) {
  if (type === 1) {
    // 创建一个StorageEvent事件
    var newStorageEvent = document.createEvent('StorageEvent');
    const storage = {
      setItem: function (k, val) {
        localStorage.setItem(k, val);
        // 初始化创建的事件
        newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
        // 派发对象
        window.dispatchEvent(newStorageEvent);
      }
    }
    return storage.setItem(key, data);
  } else {
    // 创建一个StorageEvent事件
    var newStorageEvent = document.createEvent('StorageEvent');
    const storage = {
      setItem: function (k, val) {
        sessionStorage.setItem(k, val);
        // 初始化创建的事件
        newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
        // 派发对象
        window.dispatchEvent(newStorageEvent);
      }
    }
    return storage.setItem(key, data);
  }
}


new Vue({
  router,
  store,
  i18n,
  render: h => h(App)
}).$mount('#app');
