// 长度不足补0
export function fillZero(num, length) {
  if (!length) length = 2;
  let chaLen = length - num.toString().length;
  if (chaLen) {
    return Array(chaLen + 1).join('0') + num.toString();
  }
  return num;
}

// 日期转字符串
export function dateToStr(datetime, format) {
  if (!datetime) datetime = new Date();
  if (!format) format = 'yyyy-MM-dd';
  let formateArr = ['yyyy', 'MM', 'dd', 'hh', 'mm', 'ss'];
  let returnArr = [];
  returnArr.push(datetime.getFullYear());
  returnArr.push(fillZero(datetime.getMonth() + 1));
  returnArr.push(fillZero(datetime.getDate()));
  returnArr.push(fillZero(datetime.getHours()));
  returnArr.push(fillZero(datetime.getMinutes()));
  returnArr.push(fillZero(datetime.getSeconds()));
  for (let i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

// 时间字符串2018-12-12转换成日期
export function strToDate(str) {
  if (!str) return null;
  let datetime = new Date();
  datetime.setFullYear(parseInt(str.split('-')[0], 10));
  datetime.setMonth(parseInt(str.split('-')[1], 10) - 1);
  datetime.setDate(parseInt(str.split('-')[2], 10));
  return datetime;
}

// 日期加减 var newDate = dateAdd("2018-12-12", "dd", -3)
export function dateAdd(mydate, type, add) {
  let d = new Date(mydate);
  if (type === 'dd') {
    // 天
    d.setDate(d.getDate() + add);
  }
  if (type === 'MM') {
    // 月
    d.setMonth(d.getMonth() + add);
  }
  if (type === 'yyyy') {
    // 年
    d.setFullYear(d.getFullYear() + add);
  }
  return d;
}

/**
 * 滚动到指定位置
 * scrollDom: 滚动容器
 * type: top（默认顶部） || bottom（底部）|| dom（滚动到指定元素位置）
 * */
export function scrollTo(scrollDom, type) {
  if (scrollDom) {
    let top = 0;
    if (type) {
      if (type === 'top') {
      } else if (type === 'bottom') {
        top = scrollDom.scrollHeight;
      } else {
        top = type.offsetTop - scrollDom.offsetTop;
      }
    }
    scrollDom.scrollTo({
      top: top,
      behavior: 'smooth'
    });
  }
}

/**
 * 打开全屏窗口页面
 * url: 页面地址
 * title: 页面标题
 * close: 打开窗口页面关闭时触发事件
 * time: 实时监听关闭时间间隔，默认800
 * */
export function openMax(url, title, close, time) {
  let width = window.screen.availWidth;
  let height = window.screen.availHeight;
  let Left_size = 0;
  let Top_size = 0;
  const win = window.open(
    url,
    '_blank',
    'width=' +
      width +
      'px, height=' +
      height +
      'px, left=' +
      Left_size +
      'px, top=' +
      Top_size +
      'px,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes'
  );
  if (title) win.document.title = title;
  if (Object.prototype.toString.apply(close) === '[object Function]') {
    var loop = setInterval(function() {
      if (win.closed) {
        clearInterval(loop);
        close();
      } else {
        win.document.title = title;
      }
    }, time || 800);
  }
  return win;
}

/**
 * 打开自定义窗口页面
 * url: 页面地址
 * title: 页面标题
 * config: 页面属性
 * close: 打开窗口页面关闭时触发事件
 * time: 实时监听关闭时间间隔，默认800
 * */
export function openWindow(url, title, config, close, time) {
  let width = window.screen.availWidth - 10;
  let height = window.screen.availHeight - 30;
  let Left_size = screen.width ? (screen.width - width) / 2 : 0;
  let Top_size = screen.height ? (screen.height - height) / 2 : 0;
  const win = window.open(
    url,
    '_blank',
    config ||
      'width=' +
        width +
        'px, height=' +
        height +
        'px, left=' +
        Left_size +
        'px, top=' +
        Top_size +
        'px,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes'
  );
  if (title) win.document.title = title;
  if (Object.prototype.toString.apply(close) === '[object Function]') {
    var loop = setInterval(function() {
      if (win.closed) {
        clearInterval(loop);
        close();
      } else {
        win.document.title = title;
      }
    }, time || 800);
  }
  return win;
}

/**
 * 布局拖拽改变高度**/
export function layoutDragHeight(e, type, dom, wrapDom, callback) {
  e.preventDefault();
  let dragBox = dom;
  let maxHeight = wrapDom.clientHeight - e.target.clientHeight;
  let oldHeight = dom.clientHeight;
  let oldPointerY = e.clientY;
  let newHeight = null;

  document.onmousemove = (e) => {
    e.preventDefault();
    if (!dragBox) return false;
    let currentPointerY = e.clientY;
    newHeight =
      type === 'bottom'
        ? oldPointerY - currentPointerY + oldHeight
        : currentPointerY - oldPointerY + oldHeight;
    if (newHeight < 0) newHeight = 0;
    if (newHeight > maxHeight) newHeight = maxHeight;
    dragBox.style.height = newHeight + 'px';
  };

  document.onmouseup = () => {
    if (dragBox && callback) callback(newHeight === 0);
    dragBox = null;
    document.onmousemove = null;
  };
}

/**
 * 布局拖拽改变宽度**/
export function layoutDragWidth(e, type, dom, wrapDom, callback) {
  e.preventDefault();
  let dragBox = dom;
  let maxWidth = wrapDom.clientWidth - e.target.clientWidth;
  let oldWidth = dom.clientWidth;
  let oldPointerX = e.clientX;
  let newWidth = null;

  document.onmousemove = (e) => {
    e.preventDefault();
    if (!dragBox) return false;
    let currentPointerX = e.clientX;
    newWidth =
      type === 'right'
        ? oldPointerX - currentPointerX + oldWidth
        : currentPointerX - oldPointerX + oldWidth;
    if (newWidth < 0) newWidth = 0;
    if (newWidth > maxWidth) newWidth = maxWidth;
    dragBox.style.width = newWidth + 'px';
  };

  document.onmouseup = () => {
    if (dragBox && callback) callback(newWidth === 0);
    dragBox = null;
    document.onmousemove = null;
  };
}
