import crypto from "crypto";

//个位数字补零
export function numberB0(n) {
  return n < 10 ? "0" + n : n;
}
/**
 * 
 * 传入时间戳，返回 hh-MM-ss
 */
export  function getBztime(data = 1){
  let myDate;

  if(data !== 1){
    myDate = new Date(data * 1000);
  }else{
    myDate = new Date();
  }

  let H = myDate.getHours(),
      Min = myDate.getMinutes(),
      S = myDate.getSeconds();


  if(H < 10){
    H = '0' + H ;
  }
  if(Min < 10){
    Min = '0' + Min ;
  }
  if(S < 10){
    S = '0' + S ;
  }
  return H + ':' + Min;
}
/**
 * 通过传入的时间戳获取所需的一个标准格式的日期
 * 参数：
 * time: 时间戳
 * tag: 0,今天；1，明天，-1，昨天
 * type: 输出格式，date：标准日期1999-01-10；datetime：标准日期时间：1999-01-10 10:10:10。
 * 返回：1999-01-10
 */
export function getBzDate(time, tag = 0, type = "date") {
  let dateTime = new Date(time).getTime() + 3600*1000*24*tag;
  let newDate = new Date(dateTime);
  let result = newDate.getFullYear() + "-" + numberB0((newDate.getMonth() + 1)) + "-" + numberB0(newDate.getDate());
  switch(type){
    case "date":
      return result;
      break;
    case "datetime":
      return result + " " + numberB0(newDate.getHours()) + ":" + numberB0(newDate.getMinutes()) + ":" + numberB0(newDate.getSeconds());
      break;
    default:
      return result;
  }
}

/**
 * 传入[开始日期, 结束日期]
 * 返回格式[2020-10-01 00:00:00, 2020-10-01 23:59:59]
 * tag: 1，补时分秒
 *      2，补分秒
 *      3，补秒
 * */
export function dateQjChange(list, tag) {
  if (list[0] === "" || list[0] === null) return list;
  let d1 = new Date(list[0]);
  let d2 = new Date(list[1]);
  if (tag === undefined) {
    tag = 1;
  }
  switch (tag) {
    case 1:
      return [
        d1.getFullYear() +
          "-" +
          numberB0(d1.getMonth() + 1) +
          "-" +
          numberB0(d1.getDate()) +
          " 00:00:00",
        d2.getFullYear() +
          "-" +
          numberB0(d2.getMonth() + 1) +
          "-" +
          numberB0(d2.getDate()) +
          " 23:59:59",
      ];
    case 2:
      return [
        d1.getFullYear() +
          "-" +
          numberB0(d1.getMonth() + 1) +
          "-" +
          numberB0(d1.getDate()) +
          " " +
          numberB0(d1.getHours()) +
          ":00:00",
        d2.getFullYear() +
          "-" +
          numberB0(d2.getMonth() + 1) +
          "-" +
          numberB0(d2.getDate()) +
          " " +
          numberB0(d2.getHours()) +
          ":59:59",
      ];
      break;
    case 3:
      return [
        d1.getFullYear() +
          "-" +
          numberB0(d1.getMonth() + 1) +
          "-" +
          numberB0(d1.getDate()) +
          " " +
          numberB0(d1.getHours()) +
          ":" +
          numberB0(d1.getMinutes()) +
          ":00",
        d2.getFullYear() +
          "-" +
          numberB0(d2.getMonth() + 1) +
          "-" +
          numberB0(d2.getDate()) +
          " " +
          numberB0(d2.getHours()) +
          ":" +
          numberB0(d2.getMinutes()) +
          ":59",
      ];
      break;
  }
}

//金额分割符,4位分割（仅数字）
export function getNewNum(nStr) {
  nStr += "";
  var x = nStr.split(".");
  var x1 = x[0];
  var x2 = x.length > 1 ? "." + x[1] : "";
  var rgx = /(\d+)(\d{4})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, "$1" + "," + "$2");
  }
  return x1 + x2;
}

/*
 *  对一个数字上取舍，如1234，取舍结果是2000。
 *  适配万亿级别的数字，无上限
 *  num: 数字
 * */
export function numberCeil(num) {
  num = Math.ceil(num);
  let indexNum = (num+"").length;
  let result = 0;
  let w = Math.pow(10,(indexNum - 1));
  result = Math.ceil(num/w)*w;
  return result;
}

/*
 *  Ant和element-ui 级联相同bug - 底层存在children的时候会显示空模块的问题，此方法可以完成任意级别的递归找children。
 *  data:原型数据
 * */
export function cascaderBugRepair(data) {
  if (data == null) return false;
  let nums = data.length;
  for (var i = 0; i < nums; i++) {
    if (data[i].children.length <= 0) {
      delete data[i].children;
    } else {
      if (data[i].children.length > 0) {
        data[i].children = cascaderBugRepair(data[i].children);
      }
    }
  }
  return data;
}

export default {
  numberCeil,
  getBzDate,
  cascaderBugRepair,
  getNewNum,
  dateQjChange,
  getBztime,
  numberB0,
};
