import Cookies from 'js-cookie'
import store from '@/store'

/**
 * 权限
 * @param {*} key
 */
export function hasPermission(key) {
  return window.SITE_CONFIG['permissions'].indexOf(key) !== -1 || false
}
/**
 * 获取字典数据列表
 * @param dictType  字典类型
 */
export function getDictDataList(dictType, subtype) {
  const type = window.SITE_CONFIG['dictList'].find((element) => (element.dictType === dictType))
  if (type) {
    if (dictType == 'PeopleType') {
      if (subtype == "touch") {
        return type.dataList.slice(0, 2)
      } else {
        return type.dataList
      }
    } else {
      return type.dataList
    }
  } else {
    return []
  }
}

/**
 * 获取字典名称
 * @param dictType  字典类型
 * @param dictValue  字典值
 */
export function getDictLabel(dictType, dictValue) {
  const type = window.SITE_CONFIG['dictList'].find((element) => (element.dictType === dictType))
  if (type) {
    const val = type.dataList.find((element) => (element.dictValue === dictValue + ''))
    if (val) {
      return val.dictLabel
    } else {
      return dictValue
    }
  } else {
    return dictValue
  }
}


/**
 * 清除登录信息
 */
export function clearLoginInfo() {
  store.commit('resetStore')
  Cookies.remove('token')
  window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] = false
}

/**
 * 获取uuid
 */
export function getUUID() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
  })
}

/**
 * 获取svg图标(id)列表
 */
export function getIconList() {
  var res = []
  var list = document.querySelectorAll('svg symbol')
  for (var i = 0; i < list.length; i++) {
    res.push(list[i].id)
  }

  return res
}

/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function treeDataTranslate(data, id = 'id', pid = 'pid') {
  var res = []
  var temp = {}
  for (var i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (var k = 0; k < data.length; k++) {
    if (!temp[data[k][pid]] || data[k][id] === data[k][pid]) {
      res.push(data[k])
      continue
    }
    if (!temp[data[k][pid]]['children']) {
      temp[data[k][pid]]['children'] = []
    }
    temp[data[k][pid]]['children'].push(data[k])
    data[k]['_level'] = (temp[data[k][pid]]._level || 0) + 1
  }
  return res
}

//获取当前日期
export function getCurrentDate() {
  var date = new Date();
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  return year + "-" + formatZero(month) + "-" + formatZero(day);
}

//格式化日期时间
export function formatZero(n) {
  if (n >= 0 && n <= 9) {
    return "0" + n;
  } else {
    return n;
  }
}
//获取是周几
export function getWeekDate(date) {
  var now
  if (date) {
    now = new Date(date);
  } else {
    now = new Date()
  }
  var day = now.getDay();
  var weeks = new Array(
    "周日",
    "周一",
    "周二",
    "周三",
    "周四",
    "周五",
    "周六"
  );
  var week = weeks[day];
  return week;
}

export function getCurrMonthDays(year,month){
  var date = new Date();
  var d = new Date(year, month, 0);
  return d.getDate();
}

export function isEmpty(v) {
  switch (typeof v) {
  case 'undefined':
      return true;
  case 'string':
      if (v.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true;
      break;
  case 'boolean':
      if (!v) return true;
      break;
  case 'number':
      if (isNaN(v)) return true;
      break;
  case 'object':
      if (null === v || v.length === 0) return true;
      for (var i in v) {
          return false;
      }
      return true;
  }
  return false;
}

export function convertCurrency(money) {
  //汉字的数字
  var cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
  //基本单位
  var cnIntRadice = new Array('', '拾', '佰', '仟');
  //对应整数部分扩展单位
  var cnIntUnits = new Array('', '万', '亿', '兆');
  //对应小数部分单位
  var cnDecUnits = new Array('角', '分', '毫', '厘');
  //整数金额时后面跟的字符
  var cnInteger = '整';
  //整型完以后的单位
  var cnIntLast = '元';
  //最大处理的数字
  var maxNum = 999999999999999.9999;
  //金额整数部分
  var integerNum;
  //金额小数部分
  var decimalNum;
  //输出的中文金额字符串
  var chineseStr = '';
  //分离金额后用的数组，预定义
  var parts;
  // 传入的参数为空情况 
  if(money == '') {
      return '';
  }
  money = parseFloat(money)
  if(money >= maxNum){
      return ''
  }
  // 传入的参数为0情况 
  if(money == 0) {
      chineseStr = cnNums[0] + cnIntLast + cnInteger;
      return chineseStr
  }
  // 转为字符串
  money = money.toString();
  // indexOf 检测某字符在字符串中首次出现的位置 返回索引值（从0 开始） -1 代表无
  if(money.indexOf('.') == -1){
      integerNum = money;
      decimalNum = ''
  }else{
      parts = money.split('.');
      integerNum = parts[0];
      decimalNum = parts[1].substr(0,4);
  }
  //转换整数部分
  if(parseInt(integerNum,10) > 0){
      let zeroCount  = 0;
      let IntLen = integerNum.length
      for(let i = 0; i < IntLen; i++){
          let n = integerNum.substr(i,1);
          let p = IntLen - i - 1;
          let q = p / 4;
          let m = p % 4;
          if( n == '0'){
              zeroCount ++ ;
          }else{
              if(zeroCount > 0){
                  chineseStr += cnNums[0]
              }
              zeroCount = 0;
              chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
          }
          if(m == 0 && zeroCount < 4){
              chineseStr += cnIntUnits[q]; 
          }
      }
      // 最后+ 元
      chineseStr += cnIntLast;
  }
  // 转换小数部分
  if(decimalNum != ''){
      let decLen = decimalNum.length;
      for(let i = 0; i <decLen; i++){
          let n = decimalNum.substr(i,1);
          if(n != '0'){
              chineseStr += cnNums[Number(n)] + cnDecUnits[i]
          }
      }
  }
  if(chineseStr == ''){
      chineseStr += cnNums[0] + cnIntLast + cnInteger;
  }else if(decimalNum == ''){
      chineseStr += cnInteger;
  }

  return chineseStr
}
