function formatDate(value) {
  if (!value) return value;
  var timestring = value.toString();
  var index = timestring.lastIndexOf(":");
  return timestring.substring(0, index);
};

function formatDay(value) {
  if (!value) return value;
  var timestring = value.toString();
  console.log(timestring)
  return timestring.split(" ")[0]
};

function formatNumber(value) {
  if (!value) {
    return 0
  } else if (value > 99) {
    return '99+'
  } else {
    return value;
  }
};

function formatMoney(value) {
  if (!value) {
    return 0
  } else {
    return Number(value);
  }
};
function formateText(value){
   if(!value){
     return {}
   }else{
     return JSON.parse(value)
   }
}

function countDown(value) {
  var now = getDate().getTime();

  var reg = getRegExp("-", "g");
  var endTime=value.replace(reg, '/');
  var end = getDate(endTime).getTime() + 30 * 60 * 1000;
  var time = end - now > 0 ? end - now : 0;

  return Number(time)
};
function formatYear(value){
    if(!value){
      return 0
    }else if(value<=365){
      return value+ "天"
    }else{
      return Math.floor(value/365).toFixed(0)+"年"
    }
};
function transforDay(time, type) {
  if (time == null || type == '') {
    return ''
  }
  if (arguments.length === 0) {
    return null
  }
  var  time_cur=0
  if(typeof time=="string"){
    var reg = getRegExp("-", "g");
  var timeS=time.replace(reg, '/');
    time_cur = getDate(timeS).getTime();
  }else{
    time_cur=time*1000
  }
  var date = getDate(time_cur);//在wxs中不能使用new Date()来处理日期
  console.log("date", date);
  var y = date.getFullYear();
  var m = addZero(date.getMonth() + 1);
  var d = addZero(date.getDate());
  var h =addZero(date.getHours());
  var i = addZero(date.getMinutes());
  var s =addZero(date.getSeconds());
  var a = addZero(date.getDay());
  var time_str = "";
  if (type == 'month') {
    time_str = y + '-' + m;
  }else if(type=='day'){
    time_str = m + '-' + d;
  } else if(type=='HM'){
    time_str =h + ':' + s;
  }else if (type == 'date') {
    time_str = y + '-' + m + '-' + d;
  } else if(type == 'dateminute'){
    time_str = y + '-' + m + '-' + d + ' ' + h + ':' + i 
  }else if (type == 'datetime') {
    time_str = y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s;
  } else if (type == 'onlyMonth') {
    time_str = m;
  } else if (type == 'onlyYear') {
    time_str = y;
  }
  return time_str
};
function addZero(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
};
function toS(value){
  return JSON.stringify(value);
};
function numberTOstring(value){
   if(value==null) return null;
   return value.toString();
};
function customString(arr,id){
  if(arr.length>0){
    return arr[id].value; 
  }else{
    return ""
  } 
};
function formatReply(value){
  if(!value){
    return 0;
  }else{
    if(value/60<=1){
      return 1
    }else{
      return Math.ceil(value/60)
    }
    
  }

};

function formatInquiryType(arr){
  var  a='3';
  if(arr){
    for (var i = 0; i < arr.length; ++i) {
       if(arr[i].is_enable==1 && arr[i].inquiry_type==1){
          a='2'
      };
      if(arr[i].is_enable==1 && arr[i].inquiry_type==3){
        return '1'
      };
    }
 
  }
  return a
};
function formatChufang(arr){
   var flag=false
   if(arr){
    for (var i = 0; i < arr.length; ++i) {
      if(arr[i].is_enable==1 && arr[i].inquiry_type==4){
       return true
    };
    }
   }
   return flag
}

function formatwenzhenList(arr,type){
  var obj=null;
  for (var i = 0; i < arr.length; ++i) {
    if(arr[i]==type){
      return {NUMBER:i+1,ISHAS:true}
    }
   };
   return {ISHAS:false}
  }

module.exports = {
  formatwenzhenList:formatwenzhenList,
  formatChufang:formatChufang,
  formatInquiryType:formatInquiryType,
  formatDate: formatDate,
  formatNumber: formatNumber,
  formatDay: formatDay,
  countDown: countDown,
  formatMoney:formatMoney,
  formatYear:formatYear,
  transforDay:transforDay,
  formateText:formateText,
  toS:toS,
  customString:customString,
  numberTOstring:numberTOstring,
  formatReply:formatReply
};