import moment from "moment";
import { WeaLocaleProvider } from "ecCom";

const getLabel = WeaLocaleProvider.getLabel;

// 获取当前年月
export const getCurrentYearMonth = () => {
  return moment(new Date()).format("YYYY-MM");
};

// 获取当前月份
export const getCurrentMonth = () => {
  return (new Date()).getMonth() + 1;
};

// 获取前几个月的年月
export const getSubtractMonthYearMonth = (subtract = 1) => {
  return moment(new Date()).subtract(subtract, "months").startOf("month").format("YYYY-MM");
};

// 获取后几个月的年月
export const getAddMonthYearMonth = (add = 1) => {
  return moment(new Date()).add(add, "months").startOf("month").format("YYYY-MM");
};
/**
 * 数字前面补零
 * @param num
 * @param length
 */
export const prefixAddZero = (num, length) => {
  return (Array(length).join("0") + num).slice(-length);
};
/**
 * 获取月份天数
 * @param year
 * @param month
 */
export const getMonthDays = (year, month) => {
  const d = new Date(Number(year), Number(month), 0);
  return d.getDate();
};

export const generateBasicInfo = (monthCal, nowYear, nowMonth) => {
  let nowMonthStr = "", nextMonthStr = "";
  switch (monthCal) {
    case -2:
      nowMonthStr = getLabel(111, "上上月");
      nextMonthStr = getLabel(111, "上月");
      break;
    case -1:
      nowMonthStr = getLabel(111, "上月");
      nextMonthStr = getLabel(111, "本月");
      break;
    case 0:
      nowMonthStr = getLabel(111, "本月");
      nextMonthStr = getLabel(111, "下月");
      break;
    case 1:
      nowMonthStr = getLabel(111, "下月");
      nextMonthStr = getLabel(111, "下下月");
      break;
  }

  let year, month;
  if (monthCal < 0) {
    let monthCalAbs = Math.abs(monthCal);
    if (nowMonth <= monthCalAbs) {
      // 跨年
      year = (nowYear - 1).toString();
      month = (12 - (monthCalAbs - nowMonth)).toString();
    } else {
      // 未跨年
      year = nowYear.toString();
      month = (nowMonth - monthCalAbs).toString();
    }
  } else if (monthCal === 0) {
    year = nowYear.toString();
    month = nowMonth.toString();
  } else {
    if (nowMonth + monthCal > 12) {
      year = (nowYear + 1).toString();
      month = (nowMonth + monthCal - 12).toString();
    } else {
      year = nowYear.toString();
      month = (nowMonth + monthCal).toString();
    }
  }
  return { year, month, nowMonthStr, nextMonthStr };
};
