// 返回两个日期之间的月份
export function getDiffDate(minDate, maxDate) {
  let startDate = new Date(minDate.replace(/-/g, '/'));
  let endDate = new Date(maxDate.replace(/-/g, '/'));
  let months = [];
  //把时间的天数都设置成当前月第一天
  startDate.setDate(1)
  endDate.setDate(1)
  // new Date(yyyy-MM-dd) 不知为何有时候小时是 08 有时候是00 
  endDate.setHours(0)
  startDate.setHours(0)
  while (endDate.getTime() >= startDate.getTime()) {
    let year = startDate.getFullYear();
    let month = startDate.getMonth() + 1;
    //加一个月
    startDate.setMonth(month)
    if (month.toString().length == 1) {
      month = "0" + month;
    }
    months.push(year + "-" + month)
  }
  return months;
}

// 将rgb颜色转成hex
export function RtoH(color) {
  var rgb = color.split(',');
  var r = parseInt(rgb[0].split('(')[1]);
  var g = parseInt(rgb[1]);
  var b = parseInt(rgb[2].split(')')[0]);

  var hex = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
  return hex;
}
// 将hex颜色转成rgb
export function HtoR(hex, opacity) {
  var RGBA = "rgba(" + parseInt("0x" + hex.slice(1, 3)) + "," + parseInt("0x" + hex.slice(3, 5)) + "," + parseInt("0x" + hex.slice(5, 7)) + "," + opacity + ")";
  // return { red: parseInt("0x" + hex.slice(1, 3)), green: parseInt("0x" + hex.slice(3, 5)), blue: parseInt("0x" + hex.slice(5, 7)), rgba: RGBA }
  return RGBA;
}

// 189 **** 0222
export function phoneNumA(value) {
  if (value && value.length > 8) {
    return `${value.substring(0, 3)} ${"*".repeat(value.length - 7).replace(/(.{4})/g, "$1 ")}${value.length % 4 ? " " : ""}${value.slice(-4)}`;
  }
  return value;
}

// goto
export function goto(url, json) {
  let params = "";
  if (json) {
    params = [];
    let da = Object.keys(json).map(function (key) {
      if (json[key]) return encodeURIComponent(key) + "=" + encodeURIComponent(json[key]);
    });
    da.forEach(e => {
      if (e) params.push(e)
    });
    params = params.join("&")
  }
  uni.navigateTo({
    url: url + '?' + params
  });
}

// tidyTpye
export function tidyTpye(va) {
  switch (va) {
    case '1': return "零售专区";
    case '2': return "批发专区";
    case '3': return "精品优选";
    case '4': return "甄选好物";
    case '5': return "茶宝兑换";
    case '6': return "天天捡漏";
    default: return ""
  }
}