import request from '@/config/axios'

// ERP 库存盘点单 VO
export interface StockCheckVO {
  id: number // 出库编号
  no: string // 出库单号
  outTime: Date // 出库时间
  totalCount: number // 合计数量
  totalPrice: number // 合计金额，单位：元
  status: number // 状态
  remark: string // 备注
}

// 库存报告查询参数
export interface StockReportQueryVO {
  warehouseId?: number // 仓库ID
  productId?: number // 产品ID
  checkTime?: Date[] // 盘点时间范围
  status?: number // 状态
}

// 库存报告统计 VO
export interface StockReportSummaryVO {
  totalCheckCount: number // 总盘点次数
  totalCheckAmount: number // 总盘点金额
  totalProfitAmount: number // 盘盈金额
  totalLossAmount: number // 盘亏金额
  warehouseCount: number // 涉及仓库数
  productCount: number // 涉及产品数
  avgCheckAmount: number // 平均盘点金额
  checkAccuracyRate: number // 盘点准确率
}

// 仓库库存统计 VO
export interface WarehouseStockVO {
  warehouseId: number
  warehouseName: string
  checkCount: number // 盘点次数
  totalAmount: number // 库存金额
  profitAmount: number // 盘盈金额
  lossAmount: number // 盘亏金额
  productCount: number // 产品数量
}

// 产品库存统计 VO
export interface ProductStockVO {
  productId: number
  productName: string
  checkCount: number // 盘点次数
  totalCount: number // 库存数量
  totalAmount: number // 库存金额
  profitCount: number // 盘盈数量
  lossCount: number // 盘亏数量
}

// 库存趋势数据 VO
export interface StockTrendVO {
  date: string
  checkCount: number // 盘点次数
  totalAmount: number // 盘点金额
  profitAmount: number // 盘盈金额
  lossAmount: number // 盘亏金额
}

// ERP 库存盘点单 API
export const StockCheckApi = {
  // 查询库存盘点单分页
  getStockCheckPage: async (params: any) => {
    return await request.get({ url: `/erp/stock-check/page`, params })
  },

  // 查询库存盘点单详情
  getStockCheck: async (id: number) => {
    return await request.get({ url: `/erp/stock-check/get?id=` + id })
  },

  // 新增库存盘点单
  createStockCheck: async (data: StockCheckVO) => {
    return await request.post({ url: `/erp/stock-check/create`, data })
  },

  // 修改库存盘点单
  updateStockCheck: async (data: StockCheckVO) => {
    return await request.put({ url: `/erp/stock-check/update`, data })
  },

  // 更新库存盘点单的状态
  updateStockCheckStatus: async (id: number, status: number) => {
    return await request.put({
      url: `/erp/stock-check/update-status`,
      params: {
        id,
        status
      }
    })
  },

  // 删除库存盘点单
  deleteStockCheck: async (ids: number[]) => {
    return await request.delete({
      url: `/erp/stock-check/delete`,
      params: {
        ids: ids.join(',')
      }
    })
  },

  // 导出库存盘点单 Excel
  exportStockCheck: async (params) => {
    return await request.download({ url: `/erp/stock-check/export-excel`, params })
  },

  // ==================== 库存报告相关 API ====================
  
  // 获取库存报告汇总统计
  getStockReportSummary: async (params: StockReportQueryVO) => {
    return await request.get({ url: `/erp/stock-check/report/summary`, params })
  },

  // 获取仓库库存统计
  getWarehouseStockList: async (params: StockReportQueryVO) => {
    return await request.get({ url: `/erp/stock-check/report/warehouse`, params })
  },

  // 获取产品库存统计
  getProductStockList: async (params: StockReportQueryVO) => {
    return await request.get({ url: `/erp/stock-check/report/product`, params })
  },

  // 获取库存趋势数据
  getStockTrend: async (params: StockReportQueryVO) => {
    return await request.get({ url: `/erp/stock-check/report/trend`, params })
  },

  // 导出库存报告
  exportStockReport: async (params: StockReportQueryVO) => {
    return await request.download({ url: `/erp/stock-check/report/export`, params })
  },

  // ==================== 审批流程相关 API ====================

  // 提交库存盘点审批
  submitStockCheckApproval: async (id: number, nextApprover: string, remark?: string) => {
    return await request.post({
      url: `/erp/stock-check/submit-approval`,
      params: { id, nextApprover, remark }
    })
  },

  // 获取库存盘点单的审批记录
  getStockCheckApprovalRecords: async (id: number) => {
    return await request.get({
      url: `/erp/stock-check/approval-records`,
      params: { id }
    })
  }
}
