import Vue from 'vue'

// 获取当前具体时间
export function getStandardDatetime () {
  var now = new Date()
  var year = now.getFullYear()
  var month = now.getMonth() + 1
  if (month < 10) {
    month = '0' + month
  }
  var date1 = now.getDate()
  if (date1 < 10) {
    date1 = '0' + date1
  }
  var hour = now.getHours()
  if (hour < 10) {
    hour = '0' + hour
  }
  var min = now.getMinutes()
  if (min < 10) {
    min = '0' + min
  }
  var second = now.getSeconds()
  if (second < 10) {
    second = '0' + second
  }
  var Z = year + '-' + month + '-' + date1 + ' ' + hour + ':' + min + ':' + second
  return Z
}

export function toStandardDateString () {
  let dt = new Date()
  let month = dt.getMonth() + 1
  let date = dt.getDate()
  return dt.getFullYear() + '-' + (month < 10 ? '0' + month : month) + '-' + (date < 10 ? '0' + date : date)
}

export function toStandardTimeString () {
  let dt = new Date()
  let month = dt.getMonth() + 1
  let date = dt.getDate()
  let hour = dt.getHours()
  let min = dt.getMinutes()
  let sec = dt.getSeconds()
  return dt.getFullYear() + '-' + (month < 10 ? '0' + month : month) + '-' + (date < 10 ? '0' + date : date) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec)
}
export function toStandardTimeStrings (dt) {
  let month = dt.getMonth() + 1
  let date = dt.getDate()
  let hour = dt.getHours()
  let min = dt.getMinutes()
  let sec = dt.getSeconds()
  return dt.getFullYear() + '-' + (month < 10 ? '0' + month : month) + '-' + (date < 10 ? '0' + date : date) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec)
}

export function booleanFilter (paper) {
  for (let key in paper) {
    if (key === 'f_waterheater' || key === 'f_closevalve' || key === 'f_wallhangboiler' || key === 'f_kitchen' || key === 'f_lndheating' || key === 'f_lndheating' || key === 'f_lndjiarelu' || key === 'f_lndfurnace' || key === 'f_welfareboiler' || key === 'f_welfarekitchen' || key === 'f_businboiler' || key === 'f_businkitchen' || key === 'f_jtprice' || key === 'f_dibaohu') {
      if (!paper.hasOwnProperty(key)) {
        continue
      }
      if (typeof paper[key] === 'object' && paper[key] != null) {
        booleanFilter(paper[key])
      } else {
        if (paper[key] === 1 || paper[key] === 0) {
          paper[key] = (paper[key] === 1)
        }
      }
    }
  }
}

export function reBooleanFilter (paper) {
  for (let key in paper) {
    console.log(key + ':' + paper[key])
    if (!paper.hasOwnProperty(key)) {
      continue
    }
    if (typeof paper[key] === 'object' && paper[key] != null) {
      reBooleanFilter(paper[key])
    } else {
      if (paper[key] === true || paper[key] === false) {
        paper[key] = (paper[key] === true ? 1 : 0)
      }
    }
  }
}

export function guid () {
  let buf = new Uint16Array(8)
  window.crypto.getRandomValues(buf)
  let S4 = function (num) {
    let ret = num.toString(16)
    while (ret.length < 4) {
      ret = '0' + ret
    }
    return ret
  }
  return (S4(buf[0]) + S4(buf[1]) + S4(buf[2]) + S4(buf[3]) + S4(buf[4]) + S4(buf[5]) + S4(buf[6]) + S4(buf[7]))
}

// 通过地址栏的参数获取参数内容
export function getUrlParames (param) {
  var query = window.location.search
  var iLen = param.length
  var iStart = query.indexOf(param)
  if (iStart === -1) {
    return ''
  }
  iStart += iLen + 1
  var iEnd = query.indexOf('&', iStart)
  if (iEnd === -1) {
    return query.substring(iStart)
  }
  return query.substring(iStart, iEnd)
}

export function choices (param, hasBlank = false, blankHint = '请选择') {
  let result = []
  if (hasBlank) {
    result.push({label: blankHint, value: null})
  }
  if (!Vue.param) {
    result.push({label: 'aaa', value: 'bbb'})
  } else {
    let p = Vue.param[param]
    p.forEach(function (item) {
      result.push({label: item.name, value: item.name})
    })
  }
  return result
}
