/**
 * @Author: NanKeYang
 * @Description: 全局过滤器定义
 * @Data: 2019/12/27 15:40
 */

// import parseTime, formatTime and set to filter
export { parseTime, formatTime } from '@/utils'
import store from '@/store/index'
import { dateUtils } from 'happy-utils'

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

/**
 * @param {number} time
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}

/**
 * 数据字典过滤器
 * @param value
 * @param type
 * @return {*}
 */
export function dataDictionaryFilter(value, type) {
  const dataDict = store.getters.getDataDictItemByTypeAndValue(type, value)
  if (dataDict) {
    return dataDict.dictKey
  } else {
    return ''
  }
}

/**
 * @Author: NanKeYang
 * @Description: 日期过滤器
 * @Data: 2019/7/29 23:02
 * @type 类型       0=>年-月-日 ,
 *                  1=>年-月-日 时:分 ,
 *                  2=>年-月-日 时:分:秒
 */
export function dateFilter(value, type = 0) {
  if (value === null) {
    return ''
  }
  if (type === 0) {
    return dateUtils.dateFormat(new Date(value), 'yyyy-MM-dd')
  } else if (type === 1) {
    return dateUtils.dateFormat(new Date(value), 'yyyy-MM-dd hh:mm')
  } else if (type === 2) {
    return dateUtils.dateFormat(new Date(value), 'yyyy-MM-dd hh:mm:ss')
  }
}

/**
 * @Author: NanKeYang
 * @Description: 性别过滤器
 * @Data: 2019/10/8 11:21
 */
export function sexFilter(value) {
  if (value === 'M') {
    return '男'
  } else if (value === 'F') {
    return '女'
  }
  return ''
}

/**
 * @Author: NanKeYang
 * @Description: 用户状态过滤器
 * @Data: 2019/10/8 11:21
 */
export function userStatusFilter(value) {
  if (value === 'ENABLE') {
    return '启用'
  } else if (value === 'LOCKED') {
    return '冻结'
  }
  return ''
}

/**
 * @Author: NanKeYang
 * @Description: 菜单类型过滤器
 * @Data: 2019/10/8 11:21
 */
export function menuFlagFilter(value) {
  if (value === 'Y') {
    return '是'
  } else if (value === 'N') {
    return '否'
  }
  return ''
}

/**
 * @Author: NanKeYang
 * @Description: 菜单类型过滤器
 * @Data: 2019/10/8 11:21
 */
export function menuStatusFilter(value) {
  if (value === 'ENABLE') {
    return '启用'
  } else if (value === 'DISABLE') {
    return '禁用'
  }
  return ''
}


/**
*时间:2020/1/4
*作者:zm
*功能: 身份证中间几位用*表示  frontLen 前面保留的位数 endLen 后面保留的位数
*/
export function strCenterHide(str,frontLen,endLen) {

    var len = str.length-frontLen-endLen;
    var xing = '';
    for (var i=0;i<len;i++) {
        xing+='*';
    }
    return str.substring(0,frontLen)+xing+str.substring(str.length-endLen);
}