/**
 *格式化时间
 *yyyy-MM-dd hh:mm:ss
 */
export function formatDate(time, fmt) {
  if (time === undefined || '') {
    return;
  }
  const date = new Date(time);
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  const o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  };
  for (const k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      const str = o[k] + '';
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str));
    }
  }
  return fmt;
}

function padLeftZero(str) {
  return ('00' + str).substr(str.length);
}
/*
 * 隐藏用户手机号中间四位
 */
export function hidePhone(phone) {
  return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
}
/**
 *
 * @param {*} height
 * @param {*} weight
 * @returns
 */
export function calcBMI(height, weight) {
  const realHeight = height > 100 ? height / 100 : height;
  const bmiObj = {};
  // 计算 BMI 指数
  const bmi = weight / (realHeight * realHeight);
  bmiObj.bmi = bmi;
  if (bmi < 18.5) {
    bmiObj.result = '体重过轻';
  } else if (bmi >= 18.5 && bmi < 24.9) {
    bmiObj.result = '正常体重';
  } else if (bmi >= 25 && bmi < 29.9) {
    bmiObj.result = '超重';
  } else {
    bmiObj.result = '肥胖';
  }
  return bmiObj;
}

/**
 *
 * @param {*} numDays
 */
export function getRangeStartAndEnd(numDays) {
  // 获取当前日期对象
  const currentDate = new Date();
  const startDate = new Date(currentDate);
  startDate.setDate(currentDate.getDate() - numDays + 1); // 减去numDays-1天以得到numDays天前的日期作为开始日期

  const endDate = new Date(currentDate);
  endDate.setDate(currentDate.getDate()); // 当前日期为结束日期

  return {
    start: startDate,
    end: endDate
  };
}
