const getBeginDateAndEndDate = (timestamp) => {
  let date = new Date(timestamp)
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let yearAndMonth = year + '-' + (month >= 10 ? month : '0' + month);
  return { beginDate: yearAndMonth + '-01', endDate: yearAndMonth + '-' + new Date(year, month, 0).getDate() }
}

const formatDayTimeMoth = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = '01'
  return [year, month, day].map(n => {
    n = n.toString()
    return n[1] ? n : '0' + n
  }).join('-')
}

const parseFloatFixed2 = num => {
  return parseFloat(num).toFixed(2)
}

const calc10000 = (arr, keySet) => {
  arr.forEach(it => {
    keySet.forEach(kIt => {
      if (it[kIt]) {
        it[kIt] = parseFloat(it[kIt] / 10000).toFixed(2)
      }
    })
  })
  return arr
}

const getDateByDateType = (dateType) =>{
  let now = new Date(); //当前日期
  let nowDayOfWeek = now.getDay(); //今天本周的第几天
  let nowDay = now.getDate(); //当前日
  let nowMonth = now.getMonth(); //当前月
  let nowYear = now.getFullYear(); //当前年
  nowYear += (nowYear < 2000) ? 1900 : 0; //
  let lastMonthDate = new Date(); //上月日期
  lastMonthDate.setDate(1);
  lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
  let lastYear = lastMonthDate.getFullYear();
  let lastMonth = lastMonthDate.getMonth();

  const getMonthDays = (myMonth) => {
    var monthStartDate = new Date(nowYear, myMonth, 1);
    var monthEndDate = new Date(nowYear, myMonth + 1, 1);
    var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    return days;
  }

  if(dateType === 1){
    /*今天*/
    return [new Date().format('yyyy-MM-dd'),new Date().format('yyyy-MM-dd')]
  }

  if(dateType === 2){
    /* 本周 */
    return [new Date(nowYear, nowMonth, nowDay - nowDayOfWeek).format('yyyy-MM-dd'),new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek)).format('yyyy-MM-dd')]
  }

  if(dateType === 3){
    /* 本月 */
    return [new Date(nowYear, nowMonth, 1).format('yyyy-MM-dd'),new Date(nowYear, nowMonth, getMonthDays(nowMonth)).format('yyyy-MM-dd')]
  }

  if(dateType === 5){
    /* 本年 */
    if(nowMonth>=11){
      return [new Date(nowYear, 11, 1).format('yyyy-MM-dd'),new Date(nowYear+1, 10, 30).format('yyyy-MM-dd')]
    }else{
      return [new Date(nowYear-1, 11, 1).format('yyyy-MM-dd'),new Date(nowYear, 10, 30).format('yyyy-MM-dd')]
    }
  }
}

export default { getDateByDateType,getBeginDateAndEndDate,formatDayTimeMoth,parseFloatFixed2,calc10000 }
