import { formatTime, formatDate, formatDateTime,  getHeaders } from './index';

const BASE_URL = 'http://localhost:8090' // 替换为你的实际后端地址
// 请求封装
const request = (url: string, options: any = {}) => {
  const token = uni.getStorageSync('token')
  return uni.request({
    url: `${BASE_URL}${url}`,
    method: options.method || 'GET',
    data: options.data,
    header: {
      'Content-Type': 'application/json',
      'Authorization': token ? `Bearer ${token}` : '',
      ...options.header
    }
  })
}

// 类型定义
export interface RequestItem {
  id?: string
  name: string
  spec?: string
  quantity: number
  unit?: string
  estimatedUnitCost?: number
  remark?: string
  catalogId?: string
  requestId?: string
  createdAt?: string
  updatedAt?: string
}

export interface ConsumableRequest {
  id?: string
  applicantName?: string
  applicantPhone: string
  department?: string
  reason?: string
  status?: 'PENDING' | 'APPROVED' | 'REJECTED' | 'CANCELLED' | 'COMPLETED'
  approverId?: string
  approvedAt?: string
  neededAt?: string
  remark?: string
  createdAt?: string
  updatedAt?: string
  items: RequestItem[]
}

export interface SKUResponse {
  id: number
  skuCode?: string
  specification?: string
  barcode: string
  supplierId?: number
  purchasePrice?: number
  sellingPrice?: number
  unit?: string
  minStock?: number
  maxStock?: number
  status?: number
}

export interface GoodsWithSKU {
  id: number
  goodsName: string
  categoryId?: number
  brand?: string
  status?: number
  hospId?: string
  skus: SKUResponse[]
}

export interface ListQuery {
  page?: number
  pageSize?: number
  status?: string
  applicantPhone?: string
  from?: string
  to?: string
}

export interface GoodsQuery {
  page?: number
  pageSize?: number
  status?: number
  hospId?: string
  categoryId?: number
  q?: string
}

// API 方法
export default {
  // 创建临时申请
  createRequest(data: Partial<ConsumableRequest>) {
    return request('/api/consumable-temp-requests', {
      method: 'POST',
      data
    })
  },

  // 查询申请列表
  getRequestList(params?: ListQuery) {
    return request('/api/consumable-temp-requests', {
      method: 'GET',
      data: params
    })
  },

  // 获取申请详情
  getRequestDetail(id: string) {
    return request(`/api/consumable-temp-requests/${id}`, {
      method: 'GET'
    })
  },

  // 更新申请
  updateRequest(id: string, data: Partial<ConsumableRequest>) {
    return request(`/api/consumable-temp-requests/${id}`, {
      method: 'PUT',
      data
    })
  },

  // 删除申请
  deleteRequest(id: string) {
    return request(`/api/consumable-temp-requests/${id}`, {
      method: 'DELETE'
    })
  },

  // 更新申请状态
  updateRequestStatus(id: string, data: {
    status: string
    approverId?: string
    remark?: string
  }) {
    return request(`/api/consumable-temp-requests/${id}/status`, {
      method: 'PATCH',
      data
    })
  },

  // 添加物料明细
  addRequestItem(requestId: string, data: Partial<RequestItem>) {
    return request(`/api/consumable-temp-requests/${requestId}/items`, {
      method: 'POST',
      data
    })
  },

  // 更新物料明细
  updateRequestItem(itemId: string, data: Partial<RequestItem>) {
    return request(`/api/consumable-temp-items/${itemId}`, {
      method: 'PUT',
      data
    })
  },

  // 删除物料明细
  deleteRequestItem(itemId: string) {
    return request(`/api/consumable-temp-items/${itemId}`, {
      method: 'DELETE'
    })
  },

  // 查询易耗品种类列表（含规格/价格）
  getConsumableGoods(params?: GoodsQuery) {
    return request('/api/consumable-goods', {
      method: 'GET',
      data: params
    })
  },

  // 获取易耗品种类详情（含规格/价格）
  getConsumableGoodsDetail(id: number) {
    return request(`/api/consumable-goods/${id}`, {
      method: 'GET'
    })
  }
}
