// import Vue from 'vue'
import App from './App'

import VueRouter from 'vue-router'

import './assets/js/rem.js'

import Mint from 'mint-ui';
import 'mint-ui/lib/style.css';

import store from './vuex/store'
// import Vuex from 'vuex'
import routes from './routes'

import http from './http'
Vue.prototype.http = http

import port from './port'
Vue.prototype.port = port

// import echarts from 'echarts'
Vue.prototype.echarts = echarts

import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)

import 'font-awesome/css/font-awesome.min.css'
import './assets/iconfont/iconfont.css'

import picker from "./assets/js/custom-picker.js"
import "./assets/css/custom-picker.css"
Vue.use(picker)

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(Mint);
Vue.use(VueRouter)
Vue.use(Vuex)

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    NProgress.configure({showSpinner: false}); 
    NProgress.start();

    if(to.meta.parentPath == "/tab_assets"){
        document.title = '资产'
    } else if(to.meta.parentPath == "/tab_data") {
        document.title = '数据'
    } else if(to.meta.parentPath == "/tab_my") {
        document.title = '我的'
    } else {
        document.title = '数据中心'
    }

    if (to.path == '/login') {
        sessionStorage.clear();
    }

    let user = JSON.parse(sessionStorage.getItem('user'));
    if (!user && to.path != '/login' && to.path != '/404') {
        next({ path: '/login' })
    } else {
        if(to.path == '/login'){
            var url = location.hash;
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.split("?")[1].substr(1);
                var strs = str.split("&");
                for(var i = 0; i < strs.length; i ++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
                // Vue.prototype.$indicator.open();
                Vue.prototype.http.post(Vue.prototype.port.manage.login, {
                    "loginName": theRequest.sername,
                    "password": theRequest.password,
                } , res => {
                    // Vue.prototype.$indicator.close();
                    if (res.code == "ok") {
                        sessionStorage.setItem('user', JSON.stringify(res.data));
                        var str = '';
                        for(var i in res.data.functions) {
                            str += res.data.functions[i].code + ",";
                        }
                        sessionStorage.setItem('power',str);
                        sessionStorage.setItem('assetsTab',-1);
                        next({ path: '/assets' });
                    } else {
                        Vue.prototype.$toast({
                            message: res.msg,
                            duration: 2000
                        });
                        next({ path: '/404' });
                    }
                }, error => {
                    // Vue.prototype.$indicator.close();
                    Vue.prototype.$toast({
                        message: error,
                        duration: 2000
                    });
                    next({ path: '/404' });
                })
            } else {
                next();
            }
            return false;
        }

        if(to.path == '/assets' || to.path == '/data' || to.path == '/my') {
            $(".tabber").show();
        } else {
            $(".tabber").hide();
        }

        next()
    }
})

router.afterEach(() => {
    NProgress.done()
})

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

