// 获取合法
function getValidDate(year, month, date) {
  var maxDays = new Date(year, month + 1, 0).getDate();
  if (Number(date || 1) > maxDays) {
    return maxDays;
  }
  return date;
}

// 列表转日期
function listToValue(list, type) {
  if (type === 'year') {
    var year = list[0].id;
    var newDate = new Date();
    newDate.setFullYear(year, 0, 1);
    return newDate;
  }
  if (type === 'quarter') {
    var _year = list[0].id;
    var quarter = list[1].id;
    var month = (quarter - 1) * 3;
    var _newDate = new Date();
    _newDate.setFullYear(_year, month, 1);
    return _newDate;
  }
  if (type === 'month') {
    var _year2 = list[0].id;
    var _month = list[1].id - 1;
    var _newDate2 = new Date();
    _newDate2.setFullYear(_year2, _month, 1);
    return _newDate2;
  }
  if (type === 'date') {
    var _year3 = list[0].id;
    var _month2 = list[1].id - 1;
    var date = list[2].id;
    date = getValidDate(_year3, _month2, date);
    var _newDate3 = new Date();
    _newDate3.setFullYear(_year3, _month2, date);
    return _newDate3;
  }
  if (type === 'datetime') {
    var _year4 = list[0].id;
    var _month3 = list[1].id - 1;
    var _date = list[2].id;
    _date = getValidDate(_year4, _month3, _date);
    var hour = list[3].id;
    var minute = list[4].id;
    var _newDate4 = new Date();
    _newDate4.setFullYear(_year4, _month3, _date);
    _newDate4.setHours(hour, minute, 0);
    return _newDate4;
  }
  if (type === 'time') {
    var _hour = list[0].id;
    var _minute = list[1].id;
    var _newDate5 = new Date();
    _newDate5.setHours(_hour, _minute, 0);
    return _newDate5;
  }
  return null;
}
export default listToValue;