// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

import VueAMap from 'vue-amap'
Vue.use(VueAMap);
VueAMap.initAMapApiLoader({
  key:'e5ad4c00a0c473727165a82cfeaaddff',
  Plugin:['AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.MarkerClusterer']
});

// import Echarts from 'echarts'
var Echarts = require('echarts');
Vue.prototype.dateFormatA = function (row, column) {
  let date = row[column.property];
  if (Vue.prototype.isEmptyObjVue(date)) {
    return '';
  }
  return moment(date).format('YYYY-MM-DD HH:mm');
}
Vue.prototype.$echarts = Echarts
// console.log(window.location);
import util from './script/util'
// let token=util.getUrlKey('token');
// console.log(token);
import Axios from 'axios';
import ElementUI from 'element-ui';
import moment from 'moment'
import {
  Message
} from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css'; // 默认主题
Vue.use(ElementUI, {
  size: 'small'
});

//列表斑马线显示
ElementUI.Table.props.stripe={type:Boolean,default:true}
//判空
Vue.prototype.isEmptyObjVue = function (obj) {
  if (obj == null || obj == undefined || ("" + obj) == "") {
    return true;
  }
  return false;
};
Vue.prototype.formateDay = function (date) {
  if (Vue.prototype.isEmptyObjVue(date)) {
    return '';
  }
  return moment(date).format('YYYY-MM-DD');
}

Vue.prototype.formateMonth = function (date) {
  if (Vue.prototype.isEmptyObjVue(date)) {
    return '';
  }
  return moment(date).format('YYYY-MM');
}
Vue.prototype.$axios = Axios;

Vue.config.productionTip = false
let apiUrl =process.env.baseUrl;
// console.log(apiUrl);
Axios.defaults.timeout = 300000; //设置请求时间
Axios.defaults.baseURL = apiUrl;
var loading;
function startLoading() {
    loading = (new Vue()).$loading({
        lock: true,
        text: "拼命加载中...",
        background: "rgba(0,0,0,0.4)"
    });
}

function endLoading() {
    loading.close();
}
Vue.prototype.formatMinute=function(duration){
  if(!duration) return "00小时00分00秒";
 

    let day=parseInt(duration/(60*60*24))
    let h=parseInt((duration%(60*60*24))/(60*60))
    let m=parseInt((duration-h*60*60-day*(60*60*24))/60)
    let s=(duration%60).toFixed(0);

    h=h.toString().length<2?"0"+h.toString():h.toString();
    m=m.toString().length<2?"0"+m.toString():m.toString();
    s=s.toString().length<2?"0"+s.toString():s.toString();

    return h+'小时'+m.toString()+'分'+s.toString()+'秒';

}
Axios.interceptors.request.use(
  config => {

    startLoading();
    config.headers.token = window.localStorage.getItem('token');
    // config.headers.token = token;
    return config;
  },
  err => {
    Message.error('请求超时!');
    return Promise.reject(err);
  });

  //axios统一错误处理
Axios.interceptors.response.use(data => {

  endLoading();
  // if (data.status && data.status == 200) {
  // }

  if (data.data.errno == 501) {
    Message.error({ message: '服务端错误: ' + data.data.errmsg });
  }
  if (data.data.errno == 502) {
    Message.error({ message: '服务端错误: ' + data.data.data + data.data.errmsg });
  }
  if (data.data.errno == 503) {
    Message.error({ message: '服务器异常，请联系相关人员！' })
  }
  if (data.data.errno == 400) {
    // Message.error({ message: '权限不够' });
    Message.error({ message: data.data.errmsg });
  }
  
  if (data.data.errno == 401) {
    if (window.localStorage.getItem('token') == "") {
      Message.error({
        message: '身份过期，请重新登陆'
      });
      // router.push('/login');
      // window.localStorage.removeItem('token')
    }
       router.push('/login');
      window.localStorage.removeItem('token')

  }
  // return data;
  if(data.data.errno==0){
    return data
  }else if(data.data.errno==-1){
    Message.error({
      message: data.data.data
    });
    return Promise.reject(data);
  }else{
    if(data.status==200&&data.request.responseType=='blob')
         return data;

    return Promise.reject(data);
  }
}, err => {
  endLoading();
  (new Vue()).$message({
    showClose: true,
    message: '接口错误,请稍后重试',
    type: 'error'
});

  return Promise.reject(err);
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
