/**
 * @description  tree的处理
 * @param {Array} data
 * @returns
 */

export function buildTree(data) {
  const tree = {};
  data.forEach(item => {
    const code = item.type.code;
    const node = {
      icon: item.icon,
      id: item.id,
      name: item.name,
    };

    if (!tree[code]) {
      tree[code] = {
        code: code,
        description: item.type.description,
        children: [node],
      };
    } else {
      tree[code].children.push(node);
    }
  });

  return Object.values(tree);
}

/**
 * @description 电话号码加密   18285564342 转化为 182****4342
 * @param {String} phone
 * @returns
 */

export function phoneEncryption(phone) {
  return phone.replace(/^(\d{3})\d{4}(\d+)/, '$1****$2');
}

/**
 * @description 身份证号码加密
 * @param {*} idCard
 * @returns
 */

export function idCardEncryption(idCard) {
  if (!idCard) return '';
  let reg = /^(.{6})(?:\d+)(.{4})$/;
  // 身份证号脱敏，将中间8位替换为“*”
  let result = idCard.replace(reg, '$1******$2');
  return result;
}

// 处理时间戳距离当前时间戳
export  function GetDateToNewData(diffValue){	
    var minute = 60000;
    var hour = minute * 60;
    var day = hour * 24;
    var month = day * 30;
    var nowTime = (new Date()).getTime(); //获取当前时间戳
    var ShiJianCha = nowTime - diffValue;
    var monthC = ShiJianCha / month;
    var weekC = ShiJianCha / (7 * day);
    var dayC = ShiJianCha / day;
    var hourC = ShiJianCha / hour;
    var minC = ShiJianCha / minute;
    var res = '';
    if(monthC>=12){
        oldTime = nowTime - diffValue      // 获取记录的时间戳
        var date = new Date(oldTime);
        res = date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
    }else if(monthC>=1){
        res = parseInt(monthC) + "个月前";
    }
    else if(weekC>=1){
        res = parseInt(weekC) + "周前"
    }
    else if(dayC>=1){
        res = parseInt(dayC) +"天前"
    }
    else if(hourC>=1){
        res = parseInt(hourC) +"个小时前"
    }
    else if(minC>=1){
        res = parseInt(minC) +"分钟前"
    }else{
        res = "刚刚"
    }
    return res;  
}
