/*
 * @Date: 2022-08-04 09:39:56
 * @LastEditors: 陈禹琨 cyk910622@163.com
 * @LastEditTime: 2022-08-26 11:53:37
 * @FilePath: \upcloud-wms-hnsy-web\src\views\kzyWMS\inBound\receive\rcvEditorRepairReceipt\script\lineHandleLogic.js
 * @Description: cyk
 */
import moment from "moment";

export function handleShowProductionDate(val) {
  if (val.length === 6) {
    return moment(transDateFromString(val + "01")).format("YYYY-MM-DD");
  }
  if (val.length === 8) {
    return moment(transDateFromString(val)).format("YYYY-MM-DD");
  }
}

export function handleShowExpiredDate(val, unit, num) {
  if (val.length === 6) {
    let d = transDateFromString(val + "01");
    // if (!unit || !num) {
    //     return moment(d).format('YYYY-MM-DD');
    // }
    // if (unit == '日') {
    //     d.setDate(num);
    // }
    // if (unit == '月') {
    //     d.setMonth(d.getMonth() + num);
    // }
    // if (unit == '年') {
    //     d.setFullYear(d.getFullYear() + num);
    // }
    return moment(d).format("YYYY-MM-DD");
  }
  if (val.length === 8) {
    return moment(transDateFromString(val)).format("YYYY-MM-DD");
  }
}

// 判断有效期，返回值 0通过 1警告 2 拦截
export function handleExpiredDays(date, expiredDays) {
  var x = new moment();
  var y = new moment(date);
  var duration = moment.duration(y.diff(x)).asDays();
  console.log(duration);
  if (duration <= expiredDays.intercept) return 2;
  if (duration <= expiredDays.alert) return 1;
  return 0;
}

function transDateFromString(str) {
  let y = str.substring(0, 4);
  let m = str.substring(4, 6);
  let d = str.substring(6);
  let n = new Date(y + "-" + m + "-" + d);
  return n;
}
