import request from '@/util/index.js'
export function timestampToTime(timestamp) {
    const date = new Date(timestamp) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    const Y = date.getFullYear() + '-'
    const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
    const D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    // const h = date.getHours() + ':'
    // const m = date.getMinutes() + ':'
    // const s = date.getSeconds()
    return Y + M + D
}

export function timestampToTimeMonth(timestamp) {
    const date = new Date(timestamp) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    const Y = date.getFullYear() + '-'
    const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
    const D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    // const h = date.getHours() + ':'
    // const m = date.getMinutes() + ':'
    // const s = date.getSeconds()
    return Y + M
}

export function getThisDay(value) {
    switch (value) {
        case 1:
            return '星期一'
        case 2:
            return '星期二'
        case 3:
            return '星期三'
        case 4:
            return '星期四'
        case 5:
            return '星期五'
        case 6:
            return '星期六'
        case 0:
            return '星期日'
    }
}

export function getYesterday() {
    const date = new Date()
    const y = date.getFullYear()
    const m = date.getMonth() + 1
    const d = date.getDate()
    return `${y}-${m}-${d - 1}`
}

//传入时间 拿到昨天的时间
export function handleYesterday(value) {
    const date = new Date(value)
    const y = date.getFullYear()
    const m = date.getMonth() + 1
    const d = date.getDate() - 1
    return `${y}-${m}-${d}`
}

// 处理树列表数据的方法
export function wrapTreeNode(data) {

    const wrapData = data.map((item) => {
        const node = { ...item.node };

        if (item.children && item.children.length > 0) {
            node.children = wrapTreeNode(item.children);
        }
        return node
    });
    return wrapData;
}

export function tableList(list) {
    return {
        data: list.List || [],
        current: list.PageIndex || 1,
        pageSize: list.pageSize || 10,
        total: list.TotalCount || 0,
        otherData: list?.OtherData || '',
        success: true,
    };
}
export function formateField(list) {
    const valueNumber = [];

    list.map((n) => {
        if (!isNaN(Number(n.value))) {
            valueNumber.push({
                label: n.label,
                value: numeral(n.value).value(),
            });
        }
    });
    return valueNumber.length > 0 ? valueNumber : list;
}
export async function getFieldEnum(params) {
    const data = await request.$webGet(`/EShangApiMain/FrameWork/GetFieldEnumByField`, params);
    if (data.Result_Code !== 100) {
        return [];
    }
    return data.Result_Data.List;
}



export function formatTime(ts) {
    let n = Number(ts);
    if (!Number.isFinite(n)) return '';

    // 兼容秒级/毫秒级时间戳
    if (n < 1e12) n *= 1000;

    const d = new Date(n);
    const pad = (x) => String(x).padStart(2, '0');

    const Y = d.getFullYear();
    const M = pad(d.getMonth() + 1);
    const D = pad(d.getDate());
    const h = pad(d.getHours());
    const m = pad(d.getMinutes());
    const s = pad(d.getSeconds());

    return `${Y}-${M}-${D} ${h}:${m}:${s}`;
}