/*
 * @Description: 工具函数
 */

/*
 * @description 获取地址栏hash参数
 */
export const getUrlHash = (paramName) => {
  const urlParams = {};
  const url = window.location.href;
  const idx = url.indexOf("#");
  if (idx === -1) {
    return false;
  }
  let params = url.substring(url.indexOf("#") + 1);
  params = params.split("&");
  params.forEach((item) => {
    const param = item.split("=");
    urlParams[param[0]] = decodeURIComponent(param[1]);
  });
  if (paramName) {
    return urlParams[paramName];
  }
  return urlParams;
};

/**
 * 倒计时
 * @param {String} target 目标时间
 */
export const countDown = (target) => {
  const oDate = new Date(); // 获取日期对象
  const oldTime = oDate.getTime(); // 现在距离1970年的毫秒数
  const newDate = new Date(
    isIE() && String(target).match("-") ? target.replace("-", "/") : target
  );
  const newTime = newDate.getTime(); // 2019年距离1970年的毫秒数
  if (newTime >= oldTime) {
    let second = Math.floor((newTime - oldTime) / 1000); // 未来时间距离现在的秒数
    const day = Math.floor(second / 86400); // 整数部分代表的是天；一天有24*60*60=86400秒 ；
    second %= 86400; // 余数代表剩下的秒数；
    const hour = Math.floor(second / 3600); // 整数部分代表小时；
    second %= 3600; // 余数代表 剩下的秒数；
    const minute = Math.floor(second / 60);
    second %= 60;
    return {
      day,
      hour,
      minute,
      second,
    };
  }
  return false;
};

/**
 * 文件流导出
 */
export const downloadBlob = (res, filename) => {
  const blob = new Blob([res], {
    type: "application/vnd.ms-excel",
  });
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(blob, filename);
  } else {
    const oA = document.createElement("a");
    oA.href = URL.createObjectURL(blob);
    oA.download = filename;
    oA.click();
  }
};

// 计算数组对象的总数量和总价格,传列表，num，price可传可不传
export const countNumPrice = (list, num, price) => {
  if (!num) {
    num = 'ordCnt'
  }
  if (!price) {
    price = 'purcAmt'
  }
  var amount = { num: 0, price: 0 }
  list.forEach((item) => {
    if (item[num]) {
      amount.num += parseFloat(item[num]);
    }
    if (item[price]) {
      amount.price += parseFloat(item[price]);
    }
  });
  amount.price = amount.price.toFixed(2);

  return amount;
}

// 计算采购金额=数量*单价，传数量和单价
export const getTotalAmount = (num, price) => {
  if (num && price) {
    let prices = 0
    prices += parseFloat(num) * parseFloat(price)
    prices = prices.toFixed(2)
    return prices
  } else {
    return ''
  }
}
// 获取截止日期前三个月的日期
export const getThreeMonthAgoDate = (deadline) => {
  const year = deadline.getFullYear();
  const month = deadline.getMonth() + 1;
  const day = deadline.getDate();
  const days =
    parseInt(month) < 3
      ? new Date(year - 1, month + 12 - 3, 0)
      : new Date(year, month - 3, 0);
  const maxDays = days.getDate(); //对应的三个月前的month，这个月的最大天数
  const yearAgo = parseInt(month) < 3 ? year - 1 : year;
  const monthAgo = parseInt(month) < 3 ? month + 9 : month - 3;
  const dayAgo = parseInt(maxDays) < parseInt(day) ? maxDays : day;
  const threeMonthsAgoDate = yearAgo + "-" + monthAgo + "-" + dayAgo;
  return threeMonthsAgoDate;
}
//表单验证错误直接滚动到错误位置
export const moveToError = () => {
  let isError = document.getElementsByClassName('is-error');
     if(isError.length){
      isError[0].scrollIntoView({
        block: 'center',
        behavior: 'smooth'

     })
    }
};



