
export function selectDictLabel(datas, value) {
  var actions = [];
  Object.keys(datas).some((key) => {
    if (datas[key].dictValue == ('' + value)) {
      actions.push(datas[key].dictLabel);
      return true;
    }
  })
  return actions.join('');
}

export function selectDictScheme(datas, value) {
  var actions = [];
  Object.keys(datas).some((key) => {
    if (datas[key].id == ('' + value)) {
      actions.push(datas[key].schemeName);
      return true;
    }
  })
  return actions.join('');
}

//回退
export function onClickLeft(){
  history.back(-1);
}

export function getNowFormatDate(time) {
  var date;
  if (!time){
    date = new Date();
  }else{
    date = time;
  }
  var seperator1 = "-";
  var seperator2 = ":";
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var seconds = date.getSeconds();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  if (hours >= 0 && hours <= 9) {
    hours = "0" + hours;
  }
  if (minutes >= 0 && minutes <= 9) {
    minutes = "0" + minutes;
  }
  if (seconds >= 0 && seconds <= 9) {
    seconds = "0" + seconds;
  }
  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + day + " " + hours + seperator2 + minutes + seperator2 + seconds;

  return currentdate;
}

export function format(time, format) {
  var t = new Date(time);
  var tf = function (i) { return (i < 10 ? '0' : '') + i };
  return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
    switch (a) {
      case 'yyyy':
        return tf(t.getFullYear());
        break;
      case 'MM':
        return tf(t.getMonth() + 1);
        break;
      case 'mm':
        return tf(t.getMinutes());
        break;
      case 'dd':
        return tf(t.getDate());
        break;
      case 'HH':
        return tf(t.getHours());
        break;
      case 'ss':
        return tf(t.getSeconds());
        break;
    }
  })
}

/**
 * @author Rui.Zhang
 * @description 判断是否为银行卡号
 * @param {String} str_cardNo 待校验的数据
 * @returns {Boolean}, true:是银行卡号
 **/
export function isBankCard (str_cardNo) {
  str_cardNo = str_cardNo || String(this);
  if ("" == str_cardNo.trim() || undefined == str_cardNo) {
    return false;
  }
  var lastNum = str_cardNo.substr(str_cardNo.length - 1, 1);//取出最后一位（与luhm进行比较）

  var first15Num = str_cardNo.substr(0, str_cardNo.length - 1);//前15或18位
  var newArr=new Array();
  for(var i=first15Num.length-1;i>-1;i--){    //前15或18位倒序存进数组
    newArr.push(first15Num.substr(i,1));
  }
  var arrJiShu=new Array();  //奇数位*2的积 <9
  var arrJiShu2=new Array(); //奇数位*2的积 >9

  var arrOuShu=new Array();  //偶数位数组
  for(var j=0;j<newArr.length;j++){
    if((j+1)%2==1){//奇数位
      if(parseInt(newArr[j])*2<9)
        arrJiShu.push(parseInt(newArr[j])*2);
      else
        arrJiShu2.push(parseInt(newArr[j])*2);
    }
    else //偶数位
      arrOuShu.push(newArr[j]);
  }

  var jishu_child1=new Array();//奇数位*2 >9 的分割之后的数组个位数
  var jishu_child2=new Array();//奇数位*2 >9 的分割之后的数组十位数
  for(var h=0;h<arrJiShu2.length;h++){
    jishu_child1.push(parseInt(arrJiShu2[h])%10);
    jishu_child2.push(parseInt(arrJiShu2[h])/10);
  }

  var sumJiShu=0; //奇数位*2 < 9 的数组之和
  var sumOuShu=0; //偶数位数组之和
  var sumJiShuChild1=0; //奇数位*2 >9 的分割之后的数组个位数之和
  var sumJiShuChild2=0; //奇数位*2 >9 的分割之后的数组十位数之和
  var sumTotal=0;
  for(var m=0;m<arrJiShu.length;m++){
    sumJiShu=sumJiShu+parseInt(arrJiShu[m]);
  }

  for(var n=0;n<arrOuShu.length;n++){
    sumOuShu=sumOuShu+parseInt(arrOuShu[n]);
  }

  for(var p=0;p<jishu_child1.length;p++){
    sumJiShuChild1=sumJiShuChild1+parseInt(jishu_child1[p]);
    sumJiShuChild2=sumJiShuChild2+parseInt(jishu_child2[p]);
  }
  //计算总和
  sumTotal=parseInt(sumJiShu)+parseInt(sumOuShu)+parseInt(sumJiShuChild1)+parseInt(sumJiShuChild2);

  //计算Luhm值
  var k= parseInt(sumTotal)%10==0?10:parseInt(sumTotal)%10;
  var luhm= 10-k;

  if(lastNum==luhm){
    return true;
  }
  else{
    return false;
  }
}

export function is_not_number(val) {
  return (val === null || val === undefined || val === '');
}
