// -------倒计时--------
/**
 * @params {number} endtime
 * 参数是结束时间的毫秒数
 * eg: endtime = (new Date()).getTime() + 1 * 60 * 60 * 1000  当前时间的1个小时后
 * 
 * @returns {string} '00:59:59'
 */
export function showtime(endtime) {
  var nowtime = new Date()  //获取当前时间

  var lefttime = endtime - nowtime.getTime()  //距离结束时间的毫秒数

  // var leftd = Math.floor(lefttime / (1000 * 60 * 60 * 24))  //计算天数

  var lefth = Math.floor(lefttime / (1000 * 60 * 60) % 24)  //计算小时数
  lefth = lefth < 10 ? '0' + lefth : lefth

  var leftm = Math.floor(lefttime / (1000 * 60) % 60)  //计算分钟数
  leftm = leftm < 10 ? '0' + leftm : leftm

  var lefts = Math.floor(lefttime / 1000 % 60);  //计算秒数
  lefts = lefts < 10 ? '0' + lefts : lefts

  // return leftd + "天" + lefth + ":" + leftm + ":" + lefts;  //返回倒计时的字符串
  return lefth + ":" + leftm + ":" + lefts;  //返回倒计时的字符串
}


const PublicMethods = {
  // 设置cookie
  setCookie: function (name, value, seconds) {
    seconds = seconds || 0; //seconds有值就直接赋值，没有为0，这个根php不一样。
    var expires = "";
    if (seconds != 0) {
      //设置cookie生存时间
      var date = new Date();
      date.setTime(date.getTime() + seconds * 1000);
      expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name + "=" + escape(value) + expires + "; path=/"; //转码并赋值
  },

  // 取得cookie
  getCookie: function (name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(";"); // 把cookie分割成组
    for (var i = 0; i < ca.length; i++) {
      var c = ca[i]; // 取得字符串
      while (c.charAt(0) == " ") {
        // 判断一下字符串有没有前导空格
        c = c.substring(1, c.length); // 有的话，从第二位开始取
      }
      if (c.indexOf(nameEQ) == 0) {
        // 如果含有我们要的name
        return unescape(c.substring(nameEQ.length, c.length)); // 解码并截取我们要值
      }
    }
    return false;
  },

  // 清除cookie
  clearCookie: function (name) {
    setCookie(name, "", -1);
  },

  // //获取cookie
  // getCookie: function(name) {
  //   var arr = "";
  //   var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  //   if ((arr = document.cookie.match(reg))) {
  //     return unescape(arr[2]);
  //   } else {
  //     return "";
  //   }
  // },

  //获取链接里的参数
  getUrlParams() {
    let obj = {};
    let is_parameter = window.location.href.split("?");

    if (is_parameter.length <= 1) {
      return obj;
    }

    let parList = is_parameter[1].split("&");
    for (var i = 0; i < parList.length; i++) {
      var arr = parList[i].split("=");
      var key = arr[0];
      var value = arr[1];
      obj[key] = value;
    }
    return obj;
  },

  //获取系统当前时间
  getToday() {
    var nowdate = new Date();
    var y = nowdate.getFullYear();
    var m = nowdate.getMonth() + 1;
    var d = nowdate.getDate();

    m = m < 10 ? "0" + m : m;
    d = d < 10 ? "0" + d : d;

    var formatToday = y + "-" + m + "-" + d;
    return formatToday;
  },

  //获取系统前一周的时间
  getWeek() {
    var nowdate = new Date();
    var oneweekdate = new Date(nowdate - 7 * 24 * 3600 * 1000);
    var y = oneweekdate.getFullYear();
    var m = oneweekdate.getMonth() + 1;
    var d = oneweekdate.getDate();

    m = m < 10 ? "0" + m : m;
    d = d < 10 ? "0" + d : d;

    var formatWeek = y + "-" + m + "-" + d;
    return formatWeek;
  },

  //获取系统前一个月的时间
  getMonth() {
    var nowdate = new Date();
    var oneweekdate = new Date(nowdate - 30 * 24 * 3600 * 1000);
    var y = oneweekdate.getFullYear();
    var m = oneweekdate.getMonth() + 1;
    var d = oneweekdate.getDate();

    m = m < 10 ? "0" + m : m;
    d = d < 10 ? "0" + d : d;

    var formatMonth = y + "-" + m + "-" + d;
    return formatMonth;
  },

  //获取系统前一个月的时间
  getDynamicDay(agoDay) {
    var nowdate = new Date();
    var oneweekdate = new Date(nowdate - agoDay * 24 * 3600 * 1000);
    var y = oneweekdate.getFullYear();
    var m = oneweekdate.getMonth() + 1;
    var d = oneweekdate.getDate();

    m = m < 10 ? "0" + m : m;
    d = d < 10 ? "0" + d : d;

    var formatAgo = y + "-" + m + "-" + d;
    return formatAgo;
  },

  //获取某天对应的周几
  getweekday(date) {
    var weekArray = new Array("7", "1", "2", "3", "4", "5", "6");
    var week = weekArray[new Date(date).getDay()]; //注意此处必须是先new一个Date
    return week;
  },

  //将秒数转为时分秒
  getHoursMinutesSeconds(time) {
    time = Math.floor(time / 1000);
    let hours = Math.floor(time / 3600);
    let minutes = Math.floor((time % 3600) / 60);
    let seconds = (time % 3600) % 60;

    hours = ("0" + hours).slice(-2);
    minutes = ("0" + minutes).slice(-2);
    seconds = ("0" + seconds).slice(-2);
    return hours + ":" + minutes + ":" + seconds;
  },

  //判断当前App环境
  appSource() {
    const u = navigator.userAgent;
    const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
    if (isiOS) {
      return "ios";
    } else {
      return "android";
    }
  },

  //判断是否在微信环境里
  isWeiXin() {
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
      return true;
    } else {
      return false;
    }
  }
};

export default PublicMethods; //封装所有公共方法
