/*
 * @Author: zhaohongyun1@jd.com
 * @Date: 2019-10-10 15:07:20
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-02-24 18:04:32
 */

/**
 * @description: 获取元素在页面距离顶部的偏移量
 * @param {element} 元素节点
 * @return:  Number
 */
export var getElementTop = function getElementTop(element) {
  // 获取 element 元素距离父元素的 offsetTop 像素;
  // console.log(element);
  if (!element) return 200;
  var actualTop = element.offsetTop;
  var current = element.offsetParent; // console.log(element, actualTop, current);
  // 判断当前元素是都循环到 HTML 根元素了

  while (current !== null) {
    // offsetTop 循环相加
    actualTop += current.offsetTop; // 当 current 为 HTML 根元素是， current.offsetParent 的值为 null

    current = current.offsetParent; // console.log(element, actualTop, current);
  } // console.log(actualTop);


  return actualTop;
}; // 写cookies

export function setCookie(name, value) {
  var Days = 30;
  var exp = new Date();
  exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  document.cookie = "".concat(name, "=").concat(escape(value), ";expires=").concat(exp.toUTCString());
} // 读取cookies

export function getCookie(cname) {
  var name = "".concat(cname, "=");
  var ca = document.cookie.split(';');

  for (var i = 0; i < ca.length; i++) {
    var c = ca[i].trim();
    if (c.indexOf(name) === 0) return c.substring(name.length, c.length);
  }

  return '';
} // 删除cookies

export function delCookie(name) {
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  var cval = getCookie(name);
  if (cval != null) document.cookie = "".concat(name, "=").concat(cval, ";expires=").concat(exp.toUTCString());
} // 事件触发函数

export function triggerEvent(el, type) {
  if ('createEvent' in document) {
    // modern browsers, IE9+
    var e = document.createEvent('HTMLEvents');
    e.initEvent(type, false, true);
    el.dispatchEvent(e);
  }
}
/**
 * @description: 生成唯一的id
 * @param {number} length 长度
 * @return:
 */

export var genID = function genID() {
  var id = "".concat(Math.random(), "-").concat(Date.now());
  console.log(id, 'id---');
  return id;
};
/**
 * @description: 给数据加一个模拟的id，在刚刚获取数据或者新建一条数据的时候
 * @param {type}
 * @return:
 */

export var addId = function addId(data) {
  return data.map(function (item) {
    return Object.assign(Object.assign({}, item), {
      id: item.id || genID()
    });
  });
};
/*
 * @param: hex { string} 例如:"#23ff45"
 * @param: opacity  { string || number } 透明度
 * @return: { string } rgba格式
 */

export function hexToRgba(hex, opacity) {
  return 'rgba(' + parseInt('0x' + hex.slice(1, 3)) + ',' + parseInt('0x' + hex.slice(3, 5)) + ',' + parseInt('0x' + hex.slice(5, 7)) + ',' + opacity + ')';
}
export function getMinInteger(min) {
  if (min.toString().includes('.')) {
    return Math.floor(min * 1000) / 1000;
  }

  var data = min.toString().split('');
  var dataLen = data.length;

  switch (dataLen) {
    case 1:
      return 0;

    case 2:
    case 3:
      return min - 5;

    case 4:
      return Math.round(Number(data[0] + data[1] + '00'));

    case 5:
      return Math.round(Number(data[0] + data[1] + data[2] + '00'));

    case 6:
      return Math.round(Number(data[0] + data[1] + data[2] + data[3] + '0'));

    default:
      return min;
  }
}
export function getMaxInteger(max) {
  if (max.toString().includes('.')) {
    return Math.ceil(max * 1000) / 1000;
  }

  var data = max.toString().split('');
  var dataLen = data.length;

  switch (dataLen) {
    case 1:
      return 10;

    case 2:
    case 3:
      return max + 2;

    case 4:
      return Math.round(Number(data[0] + data[1] + '00') + 100);

    case 5:
      return Math.round(Number(data[0] + data[1] + data[2] + '00') + 100);

    case 6:
      return Math.round(Number(data[0] + data[1] + data[2] + '00') + 100);

    default:
      return max;
  }
}
export var formdataNumber = function formdataNumber(n) {
  if (n === null || n === undefined) return null;
  if (typeof n === 'number') return n.toLocaleString();
  return Number(n).toLocaleString();
}; // ECharts 使用的颜色

export var colors = ['#12CCFF', '#FA5AAD', '#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc'];
export var calDate = function calDate() {
  var base = +new Date();
  var oneDay = 24 * 3600 * 1000;
  var date = [];

  for (var i = 0; i < 7; i++) {
    var now = new Date(base -= oneDay);
    date.unshift([now.getMonth() + 1, now.getDate()].join('/'));
  }

  return date;
};
export var unitDeal = function unitDeal(num, unit) {
  var resNum = num;
  var resUnit = unit;

  if (num >= 10000) {
    resNum = num / 10000;
    resUnit = "\u4E07".concat(unit);
  }

  return {
    num: resNum,
    unit: resUnit
  };
};
export var getUrlParams = function getUrlParams(url) {
  // 定义一个 parse url.search 的方法
  function parse(url) {
    var obj = {};
    url.replace(/([^?&=]+)=([^&]+)/g, function (_, k, v) {
      console.log();

      if (v.includes('#')) {
        v = v.split('#').shift();
      }

      return obj[k] = v;
    });
    return obj;
  }

  return parse(url);
};