import request from '@/config/axios'

// 审批记录 VO
export interface ApprovalRecordVO {
  id?: number
  processInstanceId: string
  bizId: string
  bizTableName: string
  applicant: string
  applicantName?: string
  approver: string
  approverName?: string
  assigner?: string
  assignerName?: string
  assignReason?: string
  approvalTime?: Date
  comment?: string
  approvalResult: number
  flowType: number
  parentApprovalId?: number
  approvalLevel: number
  processStatus: number
  sort?: number
  createTime?: Date
  updateTime?: Date
}

// 提交审批 VO
export interface ApprovalSubmitVO {
  bizId: string
  bizTableName: string
  nextApprover: string
  remark?: string
}

// 处理审批 VO
export interface ApprovalProcessVO {
  id: number
  approvalResult: number
  comment?: string
  nextApprover?: string
  assignReason?: string
}

// 审批记录分页查询 VO
export interface ApprovalRecordPageReqVO extends PageParam {
  processInstanceId?: string
  bizId?: string
  bizTableName?: string
  approver?: string
  approvalResult?: number
  processStatus?: number
  createTime?: Date[]
}

// 审批记录 API
export const ApprovalRecordApi = {
  // 查询审批记录分页
  getApprovalRecordPage: async (params: ApprovalRecordPageReqVO) => {
    return await request.get({ url: `/erp/approval-record/page`, params })
  },

  // 查询审批记录详情
  getApprovalRecord: async (id: number) => {
    return await request.get({ url: `/erp/approval-record/get?id=` + id })
  },

  // 新增审批记录
  createApprovalRecord: async (data: ApprovalRecordVO) => {
    return await request.post({ url: `/erp/approval-record/create`, data })
  },

  // 修改审批记录
  updateApprovalRecord: async (data: ApprovalRecordVO) => {
    return await request.put({ url: `/erp/approval-record/update`, data })
  },

  // 删除审批记录
  deleteApprovalRecord: async (id: number) => {
    return await request.delete({ url: `/erp/approval-record/delete?id=` + id })
  },

  // 提交审批
  submitApproval: async (data: ApprovalSubmitVO) => {
    return await request.post({ url: `/erp/approval-record/submit`, data })
  },

  // 处理审批
  processApproval: async (data: ApprovalProcessVO) => {
    return await request.post({ url: `/erp/approval-record/process`, data })
  },

  // 获取业务单据的审批记录列表
  getApprovalRecordListByBiz: async (bizId: string, bizTableName: string) => {
    return await request.get({
      url: `/erp/approval-record/list-by-biz`,
      params: { bizId, bizTableName }
    })
  },

  // 获取用户的待办审批列表
  getPendingApprovalList: async (approver: string) => {
    return await request.get({
      url: `/erp/approval-record/pending-list`,
      params: { approver }
    })
  },

  // 获取用户的已审批列表
  getApprovedApprovalList: async (approver: string) => {
    return await request.get({
      url: `/erp/approval-record/approved-list`,
      params: { approver }
    })
  },

  // 获取用户的已审批分页列表
  getApprovedApprovalPage: async (params: ApprovalRecordPageReqVO) => {
    return await request.get({
      url: `/erp/approval-record/approved-page`,
      params
    })
  },

  // 终止审批流程
  terminateApproval: async (bizId: string, bizTableName: string) => {
    return await request.post({
      url: `/erp/approval-record/terminate`,
      params: { bizId, bizTableName }
    })
  }
}
