import vue from "vue";
import {
  MessageBox
} from 'element-ui';

vue.prototype.$makeSure = function (fn, message, title) {
  try {
    message = message || '您确定要执行此操作吗';
    title = title || '温馨提示';
    MessageBox.confirm(message, title).then(() => {
      fn();
    }).catch(() => {});
  } catch (e) {
    debugger
  }
};
//vue.prototype.$upload = http_upload_picture; //global variable
vue.prototype.$getPageSize = function (num) {
  var winHeight = document.documentElement.clientHeight;
  // 菜单高62  分页72  page_content的上下padding合30  表头高44 其他内容占据高度num
  var size = parseInt((winHeight - 62 - 44 - 72 - 30 - num) / 44);
  size = size <= 1 ? 1 : size
  //三目运算的判断
  return size;
};
vue.prototype.$getStyleHeight = function (num, flag=true) {
  var winHeight = document.documentElement.clientHeight;

  // 菜单高62  page_content的上下padding合30  其他内容占据高度num
  var size = parseInt(winHeight - 56 - 15 - num)
  if (flag) {
    return {
      'height': size + 'px',
    }
  } else {
    return {
      'min-height': size + 'px',
    }
  }
};

vue.prototype.$getHeight = function (num, flag) {
  var winHeight = document.documentElement.clientHeight;
  var size = parseInt(winHeight - 62 - 30 - num)
  size < 0 && (size = 0);
  return size + 'px';
};

vue.prototype.$addstyle = function (arr, id) {
  arr.forEach(element => {
    var link = document.createElement('link');
    link.setAttribute("rel", "stylesheet");
    link.setAttribute("id", id);
    link.href = element;
    document.getElementsByTagName("head")[0].appendChild(link)
  }, this);
};

vue.prototype.$addscript = function (url, callback) {
  var head = document.getElementsByTagName("head")[0];
  // arr.forEach(element => {
  var script = document.createElement('script');
  script.setAttribute('style', 'type/javascript');
  script.setAttribute('id', 'echarts2');
  script.src = url;
  head.appendChild(script)
  if (typeof (callback) != "undefined") {
    if (script.readyState) {
      script.onreadystatechange = function () {
        if (script.readyState == "loaded" || script.readyState == "complete") {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else {
      script.onload = function () {
        callback();
      };
    }
  }
  // }, this)
};
//获取本日、本周、本月、本年的时间
vue.prototype.$getTime = (idx) => {
  !idx && (idx = 0);
  let date = new Date(new Date().setHours(23, 59, 59, 0));
  let year = date.getFullYear(),
    month = date.getMonth() + 1,
    day = date.getDate();
  switch (idx) {
    case 0: //本日
      return [new Date(year, month, day), date]
    case 1: //本周
      let week = date.getDay();
      week == 0 && (week = 7);
      let time = new Date(new Date().setHours(0, 0, 0, 0));
      time.setDate(day - week + 1);
      return [new Date(time), date]
    case 2: //本月
      return [new Date(year, month, '1'), date]
    case 3: //本年
      return [new Date(year + '-01-01'), date]
  }
};
vue.prototype.empty = '--';
vue.prototype.maxPageSize = 2000;
vue.prototype.camelCase=function(str='none',smallCamelCase=true){
  str=str.toString();
  if(str.toLowerCase()=='id') return smallCamelCase?'id':'ID';
  return smallCamelCase?str[0].toLowerCase()+str.slice(1):str[0].toUpperCase()+str.slice(1);
};
vue.prototype.camelCaseData=function(data,smallCamelCase=true){
  let agent={};
  let that=this;
  Object.keys(data).forEach(function(key){
    agent[that.camelCase(key,smallCamelCase)]=data[key];
  });
  return agent;
}
vue.prototype.$uuid=function(length){
  return Number(Math.random().toString().substr(3,length) + Date.now()).toString(36);
}

String.prototype.toJson = function () {
  return JSON.parse(this);
}
String.prototype.isEmpty = function () {
  if (this == null || this == undefined || this.trim() == "") {
    return true;
  }
  return false;
}
String.prototype.DateFormat = function (fmt) {
  var dt = new Date(this);
  return dt.Format(fmt);
}
Date.prototype.Format = function (fmt) { //author: meizz
  var 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 (var 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;
}
Window.prototype.clone=function(data){
 return JSON.parse(JSON.stringify(data));
}
