import request from '@/utils/request'

// 购物车商品项接口
export interface CartItem {
    id: number
    sku: string
    name: string
    spec_model: string
    source_table: string
    stock_quantity: number
    available_quantity: number
    barcode: string
    price: number      // 单价
    out_quantity: number // 本次出库数量
}

// 提交出库单的数据结构
export interface OutboundSubmitData {
    items: Array<{
        sku: string
        source_table: string
        stock_id: number
        barcode: string
        quantity: number
        price: number
    }>
    outbound_type: string
    consumer_name: string
    operator_name: string
    signature_path: string // 上传后返回的图片路径
    remark?: string
}

export interface ScanResult {
    id: number
    sku: string
    name: string
    spec_model: string
    source_table: string // 'stock_buy' | 'stock_product' ...
    stock_quantity: number
    available_quantity: number
    batch_number?: string
    warehouse_location?: string
    barcode?: string
    price?: number // 扫描返回的价格
}

/**
 * 根据条码获取库存物品详情
 * @param barcode 扫描到的条码
 */
export function getStockByBarcode(barcode: string) {
    return request<any, ScanResult>({
        url: '/v1/outbound/scan',
        method: 'get',
        params: { barcode }
    })
}

/**
 * 提交出库单 (批量)
 */
export function submitOutbound(data: OutboundSubmitData) {
    return request({
        url: '/v1/outbound',
        method: 'post',
        data
    })
}

/**
 * 获取出库记录列表
 */
export function getOutboundList(params: any) {
    return request({
        url: '/v1/outbound',
        method: 'get',
        params
    })
}