import moment from "moment";
import { revertSeconds } from '@/utils/dateUtils'
// 时间格式化
export const timeFormatter = (date) => {
    if (date === null || date === undefined || date === '') {
        return ''
    }
    return moment(date).format('yyyy-MM-DD HH:mm:ss');
}
// 日期格式化
export const dateFormatter = (date) => {
    if (date === null || date === undefined) {
        return ''
    }
    return moment(date).format('yyyy-MM-DD');
}
// 运行时长格式化
export const dueFormatter = (date) => {
    if (date === null || date === undefined) {
        return ''
    }
    return revertSeconds(moment(new Date().getTime()).diff(moment(date), 'seconds'));
}

// 库位格式化
export const locationFormatter = (locationId) => {
    if (locationId === null || locationId === undefined || locationId === '') {
        return ''
    }
    const locationArray = locationId.split('-')
    if (locationArray.length === 3) {
        return locationArray[0] + '排' + locationArray[1] + '列' + locationArray[2] + '层'
    } else if (locationArray.length === 4) {
        return locationArray[0] + '排' + locationArray[1] + '列' + locationArray[2] + '层' + locationArray[3] + '深度'
    } else {
        return ''
    }
}
// 配置类型格式化
export const configTypeFormatter = (value) => {
    switch (value) {
        case 1:
            return '输入框'
        case 2:
            return '下拉多选'
        case 3:
            return '开关'
        default:
            return '任务类型异常'
    }
}
// 文件大小格式化
export const sizeFormatter = (value) => {
    if (typeof value != 'number') {
        return 'error'
    }
    const oneByte = 1
    const oneKB = oneByte * 1024
    const oneMB = oneKB * 1024
    const oneGB = oneMB * 1024
    const oneTB = oneGB * 1024
    const onePB = oneTB * 1024
    const oneEB = onePB * 1024
    const oneZB = oneEB * 1024
    const oneYB = oneZB * 1024
    if (value < oneKB) {
        return value + 'B'
    } else if (value < oneMB) {
        return (value / oneKB).toFixed(2) + 'KB'
    } else if (value < oneGB) {
        return (value / oneMB).toFixed(2) + 'MB'
    } else if (value < oneTB) {
        return (value / oneGB).toFixed(2) + 'GB'
    } else if (value < onePB) {
        return (value / oneTB).toFixed(2) + 'TB'
    } else if (value < oneEB) {
        return (value / onePB).toFixed(2) + 'PB'
    } else if (value < oneZB) {
        return (value / oneEB).toFixed(2) + 'EB'
    } else if (value < oneYB) {
        return (value / oneZB).toFixed(2) + 'ZB'
    } else {
        return (value / oneYB).toFixed(2) + 'YB'
    }
}
// json格式化
export const jsonFormatter = (value) => {
    if (value == null) {
        return ""
    }
    if (typeof value == 'string') {
        try {
            return JSON.stringify(JSON.parse(value), null, 4)
        } catch (e) {
            return "error"
        }
    } else {
        return JSON.stringify(value, null, 4)
    }
}
// 去除英文和数字之外的字符
export const replaceEnglishAndNumberIGAI = (value) => {
    return value.replace(/[^\u4e00-\u9fa5a-zA-Z0-9]/g, '')
}
// 是否格式化
export const yesOrNoFormatter = (value) => {
    return value === 1 ? '是' : '否'
}
// 反转是否格式化
export const converseYesOrNoFormatter = (value) => {
    return value === 0 ? '是' : '否'
}
// 任务类型格式化
export const wmsTaskTypeFormatter = (value) => {
    switch (value) {
        case 1:
            return '入库'
        case 2:
            return '出库'
        case 3:
            return '盘点'
        default:
            return '未知类型'
    }
}
// wms任务状态格式化
export const wmsTaskStatusFormatter = (value) => {
    switch (value) {
        case -1:
            return '暂存'
        case 0:
            return '初始化'
        case 1:
            return '已解析'
        case 2:
            return '已下发'
        case 3:
            return '正在执行'
        case 4:
            return '执行完成'
        case 98:
            return '已取消'
        case 99:
            return '执行异常'
        default:
            return '未知状态'
    }
}
// 料箱状态
export const vehicleStatusFormatter = (value) => {
    switch (value) {
        case 1:
            return '入库中'
        case 2:
            return '在库中'
        case 3:
            return '出库中'
        case 9:
            return '锁定中'
        default:
            return '未知状态'
    }
}
// 拣选任务状态
export const pickTaskStatusFormatter = (value) => {
    switch (value) {
        case -2:
            return '入库用'
        case -1:
            return '暂存中'
        case 0:
            return '待下发'
        case 1:
            return '已下发'
        case 2:
            return '执行中'
        case 3:
            return '已到达'
        case 4:
            return '已离开'
        case 5:
            return '拣选取消'
        default:
            return '已取消'
    }
}
// 出库单类型格式化
export const outTaskTypeFormatter = (value) => {
    switch (value) {
        case 1:
            return '空箱出库'
        case 2:
            return '普通出库'
        case 4:
            return '完整出库'
        case 5:
            return '入库使用'
        case 9:
            return '紧急出库'
        default:
            return '未知类型'
    }
}