export function formatDate(time: any, cFormat: string) {
  const format = cFormat || "{y}-{m}-{d}";
  const date = new Date(time);
  const formatObj: any = {
    //年
    y: date.getFullYear(),
    //月
    m: date.getMonth() + 1,
    //日
    d: date.getDate(),
    //小时
    h: date.getHours(),
    //分钟
    i: date.getMinutes(),
    //秒
    s: date.getSeconds(),
    //星期
    a: date.getDay(),
  };
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === "a") {
      //如果key是a，就是星期，格式化成一~日
      //例如formatDate(new Date(), '{y}-{m}-{d}-{h}-{i}-{s}-{a}');
      //会输出2021-10-29-00-00-00-五
      //星期的value会返回0-6，['日', '一', '二', '三', '四', '五', '六'][2]代表周二
      return ["日", "一", "二", "三", "四", "五", "六"][value];
    }
    //padStart用于字符串头部补全，2个字符，如果不够前面补0
    return value.toString().padStart(2, "0");
  });
  return time_str;
}

export function getCurrentMonthStartAndEnd() {
  let now = new Date();
  let currentMonthStart = new Date(now.getFullYear(), now.getMonth(), 1);
  let currentMonthEnd = new Date(now.getFullYear(), now.getMonth() + 1, 0);

  return {
    start: currentMonthStart,
    end: currentMonthEnd,
  };
}

export function getCurrentYearStartAndEnd() {
  let now = new Date();
  let currentYearStart = new Date(now.getFullYear(), 0, 1); // 0 代表一月
  let currentYearEnd = (new Date(now.getFullYear() + 1, 0, 1) as any) - 1; // 下一年的一月一日减一毫秒

  return {
    start: currentYearStart,
    end: currentYearEnd,
  };
}

export function deleteBaseKey(obj: any) {
  delete obj.createTime;
  delete obj.createUserId;
  delete obj.createUserName;
  delete obj.updateTime;
  delete obj.updateUserId;
  delete obj.updateUserName;
  return obj;
}

export function moneyFormat(num: number, decimal = 2, split = ",") {
  /*
    parameter：
    num：格式化目标数字
    decimal：保留几位小数，默认2位
    split：千分位分隔符，默认为,
    moneyFormat(123456789.87654321, 2, ',') // 123,456,789.88
  */
  if (isFinite(num)) {
    // num是数字
    if (num === 0) {
      // 为0
      return num.toFixed(decimal);
    } else {
      // 非0
      var res = "";
      var dotIndex = String(num).indexOf(".");
      if (dotIndex === -1) {
        // 整数
        if (decimal === 0) {
          res = String(num).replace(/(\d)(?=(?:\d{3})+$)/g, `$1${split}`);
        } else {
          res =
            String(num).replace(/(\d)(?=(?:\d{3})+$)/g, `$1${split}`) +
            "." +
            "0".repeat(decimal);
        }
      } else {
        // 非整数
        // js四舍五入 Math.round()：正数时4舍5入，负数时5舍6入
        // Math.round(1.5) = 2
        // Math.round(-1.5) = -1
        // Math.round(-1.6) = -2
        // 保留decimals位小数
        const numStr = String(
          (
            Math.round(num * Math.pow(10, decimal)) / Math.pow(10, decimal)
          ).toFixed(decimal)
        ); // 四舍五入，然后固定保留2位小数
        const decimals = numStr.slice(dotIndex, dotIndex + decimal + 1); // 截取小数位
        res =
          String(numStr.slice(0, dotIndex)).replace(
            /(\d)(?=(?:\d{3})+$)/g,
            `$1${split}`
          ) + decimals;
      }
      return res;
    }
  } else {
    return "--";
  }
}

// 分页内容重置
export function pageListInit() {
  return {
    noMoreData: false,
    total: 0,
    list: [],
    pageNum: 1,
    pageSize: 10,
  };
}
