/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

// 手机号验证
export function isvalidPhone(str) {
  const reg = /^1[3|4|5|7|8|9][0-9]\d{8}$/
  return reg.test(str)
}

// 学生地址
export function vaildStudentUrl() {
  let url = window.location.href
  let returnUrl = ''
  if (/dev/.test(url)) { // dev 环境
    returnUrl = 'http://mstudev.dayaedu.com'
  } else if (/online/.test(url)) { //线上
    returnUrl = 'https://mstuonline.dayaedu.com'
  } else { // 默认dev环境
    returnUrl = 'http://mstudev.dayaedu.com'
  }
  return returnUrl
}

// 老师地址
export function vaildTeacherUrl() {
  let url = window.location.href
  let returnUrl = ''
  if (/dev/.test(url)) { // dev 环境
    returnUrl = 'http://mteadev.dayaedu.com'
  } else if (/online/.test(url)) { //线上
    returnUrl = 'https://mteaonline.dayaedu.com'
  } else { // 默认dev环境
    returnUrl = 'http://mteadev.dayaedu.com'
  }
  return returnUrl
}


export function nextMonthLastDay(year, month) { //日期显示为次月最后一天
  // var time = date ? new Date(date) : new Date();
  // var year = time.getFullYear();
  // //var year = 1900; //用于测试
  // var month = time.getMonth() + 2;
  if (month > 12) {

    month = month - 12;

    year = year + 1;

  }

  var day = nextMonthDay(year, month);

  return [year, month, day];

}

function nextMonthDay(year, month) { //判断每月多少天

  var day31 = [1, 3, 5, 7, 8, 10, 12];

  var day30 = [4, 6, 9, 11];

  if (day31.indexOf(month) > -1) {

    return 31;

  } else if (day30.indexOf(month) > -1) {

    return 30;

  } else {

    if (isLeapYear(year)) {

      return 29;

    } else {

      return 28;

    }

  }

}

function isLeapYear(year) { //判断是否为闰年
  return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);

}
