import request from '@/config/axios'

export interface SaleOutwarehouseVO {
  id?: number
  no?: string
  status?: number
  customerId?: number
  customerName?: string // 添加客户名称字段
  accountId?: number
  saleUserId?: number
  outTime?: string | number
  warehouseId?: number
  totalCount?: number
  totalPrice?: number
  receiptPrice?: number
  receiptStatus?: number // 添加收款状态字段
  totalProductPrice?: number
  totalTaxPrice?: number
  discountPercent?: number
  discountPrice?: number
  otherPrice?: number
  fileUrl?: string
  remark?: string
  items?: SaleOutwarehouseItemVO[]
  itemList?: SaleOutwarehouseItemVO[] // 后端返回的字段名
  stockRecordIds?: number[] // 关联的库存明细ID
  stockRecordText?: string // 关联的库存明细显示文本
  warehouseNames?: string // 仓库名称，多个用逗号分隔
}

export interface SaleOutwarehouseItemVO {
  id?: number
  outId?: number
  warehouseId?: number
  warehouseName?: string
  productId?: number
  productName?: string
  productUnitId?: number
  productUnitName?: string
  productBarCode?: string
  batchCode?: string
  productPrice?: number
  count?: number
  stockCount?: number
  totalPrice?: number
  taxPercent?: number
  taxPrice?: number
  remark?: string
}

// 收款记录接口
export interface SaleOutwarehouseReceiptVO {
  id?: number
  outId?: number
  outNo?: string
  amount: number
  date: string | Date
  remainAmount?: number
  remark?: string
  creator?: string
  createTime?: number
}

// ERP 销售出库 API
export const SaleOutwarehouseApi = {
  // 查询销售出库分页
  getSaleOutwarehousePage: async (params: any) => {
    return await request.get({ url: `/erp/sale-outwarehouse/page`, params })
  },

  // 查询销售出库详情
  getSaleOutwarehouse: async (id: number) => {
    return await request.get({ url: `/erp/sale-outwarehouse/get?id=` + id })
  },

  // 新增销售出库
  createSaleOutwarehouse: async (data: SaleOutwarehouseVO) => {
    return await request.post({ url: `/erp/sale-outwarehouse/create`, data })
  },

  // 修改销售出库
  updateSaleOutwarehouse: async (data: SaleOutwarehouseVO) => {
    return await request.put({ url: `/erp/sale-outwarehouse/update`, data })
  },

  // 更新销售出库的状态
  updateSaleOutwarehouseStatus: async (id: number, status: number) => {
    return await request.put({
      url: `/erp/sale-outwarehouse/update-status`,
      params: {
        id,
        status
      }
    })
  },

  // 删除销售出库
  deleteSaleOutwarehouse: async (ids: number[]) => {
    return await request.delete({
      url: `/erp/sale-outwarehouse/delete`,
      params: {
        ids: ids.join(',')
      }
    })
  },

  // 导出销售出库 Excel
  exportSaleOutwarehouse: async (params: any) => {
    return await request.download({ url: `/erp/sale-outwarehouse/export-excel`, params })
  },

  // 获取收款记录列表
  getReceiptList: async (outId: number) => {
    return await request.get({ 
      url: `/erp/sale-outwarehouse-receipt/list`, 
      params: { outId } 
    })
  },

  // 创建收款记录
  createReceipt: async (data: SaleOutwarehouseReceiptVO) => {
    return await request.post({ 
      url: `/erp/sale-outwarehouse-receipt/create`, 
      data 
    })
  },

  // 查询销售出库详情
  getIdByStockRecordId: async (id: number) => {
    return await request.get({ url: `/erp/sale-outwarehouse/getIdByStockRecordId?stockRecordId=` + id })
  },
}
