import request from '@/config/axios'

export type VoucherVO = {
  id: number
  no: string
  status: number
  type: number
  billType?: number
  voucherTime: string
  paymentDate?: string
  payeeName: string // 收款单位
  projectName: string // 项目名称
  bankName: string // 开户银行
  bankAccount: string // 开户账号
  contractNo: string // 合同号
  contractAmount: number // 合同总金额
  taxNo: string // 税号
  paidAmount: number // 累计已付金额
  contractFile: string // 合同附件
  invoiceFile: string // 发票附件
  totalAmount: number
  remark: string
  processInstanceId: string
  items: VoucherItemVO[]
  startUserSelectAssignees?: Record<string, number[]> // 发起人自选审批人
  createTime?: string
  creatorName?: string
}

export type VoucherItemVO = {
  id?: number
  voucherId?: number
  accountId: number
  accountName?: string // 会计科目名称
  summary: string
  debitAmount: number
  creditAmount: number
}

// 创建财务凭证
export const createVoucher = async (data: VoucherVO) => {
  return await request.post({ url: '/erp/finance-voucher/create', data: data })
}

// 更新财务凭证
export const updateVoucher = async (data: VoucherVO) => {
  return await request.put({ url: '/erp/finance-voucher/update', data: data })
}

// 删除财务凭证
export const deleteVoucher = async (ids: number | number[]) => {
  const data = Array.isArray(ids) ? ids : [ids]
  return await request.delete({ url: '/erp/finance-voucher/delete', data: data })
}

// 获取财务凭证详情
export const getVoucher = async (id: number) => {
  return await request.get({ url: '/erp/finance-voucher/get', params: { id } })
}

// 获取财务凭证分页
export const getVoucherPage = async (params: PageParam) => {
  return await request.get({ url: '/erp/finance-voucher/page', params })
}

// 导出财务凭证
export const exportVoucher = async (params: any) => {
  return await request.download({ url: '/erp/finance-voucher/export-excel', params })
}

// 更新财务凭证状态
export const updateVoucherStatus = async (id: number, status: number) => {
  return await request.put({ 
    url: '/erp/finance-voucher/update-status', 
    params: { id, status }
  })
} 
