import type { PageParam, PageResult } from '@vben/request';

import { requestClient } from '#/api/request';

export namespace HrmSupplyApi {
  /** 办公用品档案 */
  export interface Supply {
    id?: number; // 编号
    code?: string; // 用品编码
    name?: string; // 用品名称
    category?: number; // 用品类别：1 文具用品 2 打印耗材 3 清洁用品 4 劳保用品 5 其他
    unit?: string; // 单位
    specification?: string; // 规格
    stockQuantity?: number; // 当前库存
    minQuantity?: number; // 库存下限
    maxQuantity?: number; // 库存上限
    price?: number; // 参考单价
    status?: number; // 状态：0 启用 1 停用
    remark?: string; // 备注
    createTime?: Date; // 创建时间
  }

  /** 库存预警用品 */
  export interface SupplyAlert extends Supply {
    warningType?: number; // 预警类型：1 低于库存下限 2 高于库存上限
  }

  /** 采购记录 */
  export interface Purchase {
    id?: number; // 编号
    supplyId?: number; // 用品编号
    supplyCode?: string; // 用品编码
    supplyName?: string; // 用品名称
    unit?: string; // 单位
    quantity?: number; // 采购数量
    price?: number; // 采购单价
    totalPrice?: number; // 采购金额
    purchaseTime?: string; // 采购时间
    supplierName?: string; // 供应商名称
    remark?: string; // 备注
    createTime?: Date; // 创建时间
  }

  /** 领用申请 */
  export interface Issue {
    id?: number; // 编号
    supplyId?: number; // 用品编号
    supplyCode?: string; // 用品编码
    supplyName?: string; // 用品名称
    unit?: string; // 单位
    userId?: number; // 申请人编号
    userName?: string; // 申请人姓名
    deptId?: number; // 申请部门编号
    deptName?: string; // 申请部门名称
    quantity?: number; // 领用数量
    useReason?: string; // 领用事由
    status?: number; // 状态：1 待审批 2 已通过 3 已驳回 4 已发放
    approveUserId?: number; // 审批人编号
    approveUserName?: string; // 审批人姓名
    approveTime?: string; // 审批时间
    approveRemark?: string; // 审批意见
    issueTime?: string; // 发放时间
    remark?: string; // 备注
    createTime?: Date; // 创建时间
  }
}

/** 查询办公用品分页 */
export function getSupplyPage(params: PageParam) {
  return requestClient.get<PageResult<HrmSupplyApi.Supply>>(
    '/hrm/supply/page',
    { params },
  );
}

/** 查询启用的办公用品精简列表 */
export function getSupplySimpleList() {
  return requestClient.get<HrmSupplyApi.Supply[]>('/hrm/supply/simple-list');
}

/** 查询办公用品库存预警列表 */
export function getSupplyAlertList() {
  return requestClient.get<HrmSupplyApi.SupplyAlert[]>('/hrm/supply/alert-list');
}

/** 查询办公用品详情 */
export function getSupply(id: number) {
  return requestClient.get<HrmSupplyApi.Supply>(`/hrm/supply/get?id=${id}`);
}

/** 新增办公用品 */
export function createSupply(data: HrmSupplyApi.Supply) {
  return requestClient.post('/hrm/supply/create', data);
}

/** 修改办公用品 */
export function updateSupply(data: HrmSupplyApi.Supply) {
  return requestClient.put('/hrm/supply/update', data);
}

/** 删除办公用品 */
export function deleteSupply(id: number) {
  return requestClient.delete(`/hrm/supply/delete?id=${id}`);
}

/** 新增采购记录（采购入库，同时累加库存） */
export function createSupplyPurchase(data: HrmSupplyApi.Purchase) {
  return requestClient.post('/hrm/supply-purchase/create', data);
}

/** 查询采购记录分页 */
export function getSupplyPurchasePage(params: PageParam) {
  return requestClient.get<PageResult<HrmSupplyApi.Purchase>>(
    '/hrm/supply-purchase/page',
    { params },
  );
}

/** 发起领用申请 */
export function createSupplyIssue(data: HrmSupplyApi.Issue) {
  return requestClient.post('/hrm/supply-issue/create', data);
}

/** 查询领用申请分页 */
export function getSupplyIssuePage(params: PageParam) {
  return requestClient.get<PageResult<HrmSupplyApi.Issue>>(
    '/hrm/supply-issue/page',
    { params },
  );
}

/** 审批领用申请：approve=true 通过，false 驳回 */
export function approveSupplyIssue(id: number, approve: boolean, remark?: string) {
  return requestClient.put('/hrm/supply-issue/approve', null, {
    params: { id, approve, remark },
  });
}

/** 发放领用申请（扣减库存） */
export function issueSupply(id: number) {
  return requestClient.put('/hrm/supply-issue/issue', null, {
    params: { id },
  });
}
