import moment from "moment";

/* 
{    title: '批号',    dataIndex: 'manufacturerLotNum',  },
  {    title: '显示生产日期',    dataIndex: 'showProductionDate',  },
  {    title: '显示有效期至',    dataIndex: 'showExpiredDate',  },
  {    title: '生产日期',    dataIndex: 'batchProductionDate',  },
  {    title: '有效期至',    dataIndex: 'batchExpiredDate',  }, 
*/

export function handleShowProductionDate(val) {
    if (val.length !== 8) {
        return false;
    }
    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();
    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;
}