// import  CountUp from 'countup.js'
import {Message} from 'view-design'
import  Echart from 'echarts'
//数字滚动插件 countUp.js
// export const countUp = (el,num)=>{
//   let dom = document.getElementById(el);
//   let options = {
//     useEasing: true,
//     useGrouping: false,//是否使用分割符
//     separator: ',',//千位分隔符
//     decimal: '.',
//   };
//   let demo = new CountUp(dom, 0, num, 0, 3, options);//html元素id 开始值 结束值 小数位数（默认0）  持续时间（默认2，单位S）
//   if (!demo.error) {
//     demo.start();
//   } else {
//     console.error(demo.error);
//   }
// }

export const uniteTable = (tableId,colLength)=>{
  //colLength-- 需要合并单元格的列1开始
  let tb=document.getElementById(tableId);
  tb.style.display='';
  let i = 0;
  let j = 0;
  let rowCount = tb.rows.length; //   行数
  let colCount = tb.rows[0].cells.length; //   列数
  let obj1 = null;
  let obj2 = null;
  //为每个单元格命名
  for (i = 0; i < rowCount; i++) {
    for (j = 0; j < colCount; j++) {
      tb.rows[i].cells[j].id = "tb__" + i.toString() + "_" + j.toString();
    }
  }
  //合并行
  for (i = 0; i < colCount; i++) {
    if (i === colLength) break;
    obj1 = document.getElementById("tb__0_" + i.toString())
    for (j = 1; j < rowCount; j++) {
      obj2 = document.getElementById("tb__" + j.toString() + "_" + i.toString());
      if (obj1.innerText == obj2.innerText) {
        obj1.rowSpan++;
        obj2.parentNode.removeChild(obj2);
      } else {
        obj1 = document.getElementById("tb__" + j.toString() + "_" + i.toString());
      }
    }
  }

  //合并列
  for (i = 0; i < rowCount; i++) {
    colCount = tb.rows[i].cells.length;
    obj1 = document.getElementById(tb.rows[i].cells[0].id);
    for (j = 1; j < colCount; j++) {
      if (j >= colLength) break;
      if (obj1.colSpan >= colLength) break;
      obj2 = document.getElementById(tb.rows[i].cells[j].id);
      if (obj1.innerText == obj2.innerText) {
        obj1.colSpan++;
        obj2.parentNode.removeChild(obj2);
        j = j - 1;
      } else {
        obj1 = obj2;
        j = j + obj1.rowSpan;
      }
    }
  }
}

/*
     * 格式化日期 create by guoxingwang 2017-07-15
     * type 01 '-' 02 '/' 03  '.'
     * format  01 年月日  02 年月日 时分 03 年月日时分秒
     * */
export const formatDate = (date,type,format)=>{
  if (date == '' || date == null) {
    return null
  }
  let nowDate = new Date(date);
  let y = nowDate.getFullYear();
  let m = nowDate.getMonth() + 1;
  m = m<10?"0" + m:m;
  let d = nowDate.getDate();
  d = d<10?"0" + d:d;
  let h = nowDate.getHours();
  h = h<10?"0" + h:h;
  let min = nowDate.getMinutes();
  min = min<10?"0" + min:min;
  let s = nowDate.getSeconds();
  s = s<10?"0" + s:s;
  let separatorType = "";
  switch(type){
    case '01':
      separatorType = '-';
      break;
    case '02':
      separatorType = '/';
      break;
    case '03':
      separatorType = '.';
      break;
    default:
      separatorType = '-';
      break;
  }
  if(format == '01'){//年月日
    return y + separatorType + m + separatorType + d;
  }else if(format == '02'){//年月日 时分
    return y + separatorType + m + separatorType + d + " " + h+ ":"+ min;
  }else if(format == '03'){//年月日  时分秒
    return y + separatorType + m + separatorType + d + " " + h+ ":"+ min + ":" +s;
  }else if(format == '04'){//年月
    return y + separatorType + m;
  } else{
    alert('时间格式输入有误')
  }
}

export const getTableHeight = (n = 0)=> {
    let height = (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight) - n
    return height
}

/**
 	 * 比较2个时间的大小，以确保用户选择的结束时间大于开始时间
 	 * beginDate 开始时间
 	 * endDate   结束时间 格式为 YYYY-MM-DD
 	 * */
export const compareDate = (beginDate, endDate) => {
  let d1 = ''
  let d2  =''
    if (beginDate) {
      d1 = new Date(beginDate.replace(/\-/g, "\/"));
    }
    if (endDate) {
      d2 = new Date(endDate.replace(/\-/g, "\/"));
    }
    if (beginDate && endDate && d1 > d2) {
      Message.error("开始时间不能大于结束时间！");
      return false;
    }
   }

   //字典项传参
   export const dictionaryValue= (value,array)=>{
    if(array.length>0){
      let index = array.findIndex(item=>item.code==value+'');
      if(index!=-1){
        return array[index].name;
        console.log(array[index].name);
      }
    }
  }

/*
 *格式化中间有T的替换为空格
 * */
export const replaceT = (val) => {
	if (!val) return '';
	val = val.toString();
	return val.replace(/T/g," ");
}
