// Vue
import Vue from 'vue'
import App from './App'
// 核心插件
import d2Admin from '@/plugin/d2admin'
// store
import store from '@/store/index'
// 多国语
import i18n from './i18n'

// 菜单和路由设置
import router from './router'
import menuHeader from '@/menu/header'
import menuAside from '@/menu/aside'
import { frameInRoutes } from '@/router/routes'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import D2Crud from '@d2-projects/d2-crud'
 
 //图表
 import VeLine from 'v-charts/lib/line.common'
 import VePie from 'v-charts/lib/pie.common'
 Vue.component(VeLine.name, VeLine)
 Vue.component(VePie.name, VePie)
 import NP from 'number-precision'
 Vue.prototype.NP = NP
// 核心插件
Vue.use(d2Admin)
Vue.use(ElementUI)
Vue.use(D2Crud)

function calcTableHeight (options,headerHeight) {
  let tempOptions = JSON.parse(JSON.stringify(options))
  tempOptions.height = document.body.clientHeight - 260 - headerHeight
  return tempOptions
}

Vue.prototype.$calcTableHeight = calcTableHeight

 function setInitTime () {
   let startDate = new Date()
   let endDate = new Date()
   startDate.setHours(0)
   startDate.setMinutes(0)
   startDate.setSeconds(0)
   endDate.setHours(23)
   endDate.setMinutes(59)
   endDate.setSeconds(59)
 
   return [startDate,endDate]
 }
 Vue.prototype.$setInitTime = setInitTime
 
 	function timestampToTime(timestamp) {
     let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
     let Y = date.getFullYear() + '-';
     let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
     let D = (date.getDate()<10 ? '0'+date.getDate():date.getDate()) + ' ';
     let h = (date.getHours()<10 ? '0'+date.getHours():date.getHours()) + ':';
     let m = (date.getMinutes()<10 ? '0'+date.getMinutes():date.getMinutes()) + ':';
     let s = (date.getSeconds()<10 ? '0'+date.getSeconds():date.getSeconds());
     return Y+M+D+h+m+s;
     }
 Vue.prototype.$timestampToTime = timestampToTime
 
 Date.prototype.format = function(fmt) {
   let o = {
     "M+" : this.getMonth()+1,                 //月份
     "d+" : this.getDate(),                    //日
     "h+" : this.getHours(),                   //小时
     "m+" : this.getMinutes(),                 //分
     "s+" : this.getSeconds(),                 //秒
     "q+" : Math.floor((this.getMonth()+3)/3), //季度
     "S"  : this.getMilliseconds()             //毫秒
   };
   if(/(y+)/.test(fmt)) {
     fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length))
   }
   for(let k in o) {
     if(new RegExp("("+ k +")").test(fmt)){
       fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)))
     }
   }
   return fmt
 }
 
new Vue({
  router,
  store,
  i18n,
  render: h => h(App),
  created () {
    // 处理路由 得到每一级的路由设置
    this.$store.commit('d2admin/page/init', frameInRoutes)
    // 设置顶栏菜单
    this.$store.commit('d2admin/menu/headerSet', menuHeader)
    // 设置侧边栏菜单
    this.$store.commit('d2admin/menu/asideSet', menuAside)
    // 初始化菜单搜索功能
    this.$store.commit('d2admin/search/init', menuHeader)
  },
  mounted () {
    // 展示系统信息
    this.$store.commit('d2admin/releases/versionShow')
    // 用户登录后从数据库加载一系列的设置
    this.$store.dispatch('d2admin/account/load')
    // 获取并记录用户 UA
    this.$store.commit('d2admin/ua/get')
    // 初始化全屏监听
    this.$store.dispatch('d2admin/fullscreen/listen')
  }
}).$mount('#app')
