import request from '@/utils/request'

// 获取BOM列表
export function getBomList(params?: any) {
    return request({
        url: '/v1/bom/list',
        method: 'get',
        params
    })
}

// 获取BOM详情
export function getBomDetail(bomNo: string, version?: string) {
    // 去除首尾斜杠，保留中间斜杠并进行 URL 编码
    const trimmed = bomNo.replace(/^\/+|\/+$/g, '');
    const encoded = encodeURIComponent(trimmed);
    const url = version 
        ? `/v1/bom/detail/${encoded}?version=${encodeURIComponent(version)}`
        : `/v1/bom/detail/${encoded}`;
    return request({
        url,
        method: 'get'
    })
}

// 保存BOM
export function saveBom(data: any) {
    return request({
        url: '/v1/bom/save',
        method: 'post',
        data
    })
}

// 删除BOM
export function deleteBom(bomNo: string, version: string) {
    // 去除首尾斜杠，保留中间斜杠并进行 URL 编码
    const trimmed = bomNo.replace(/^\/+|\/+$/g, '');
    const encoded = encodeURIComponent(trimmed);
    return request({
        url: `/v1/bom/${encoded}?version=${encodeURIComponent(version)}`,
        method: 'delete'
    })
}
