import request from '@/utils/request'

// ==================== 服务周期管理 ====================

// 查询服务周期列表
export function listServicePeriod(query) {
  return request({
    url: '/credit/contract-employee/service-periods',
    method: 'get',
    params: query
  })
}

// 查询服务周期详情
export function getServicePeriod(servicePeriodId) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}`,
    method: 'get'
  })
}

// 新增服务周期
export function addServicePeriod(data) {
  return request({
    url: '/credit/contract-employee/service-periods',
    method: 'post',
    data: data
  })
}

// 修改服务周期
export function updateServicePeriod(data) {
  return request({
    url: '/credit/contract-employee/service-periods',
    method: 'put',
    data: data
  })
}

// 删除服务周期
export function delServicePeriod(servicePeriodId) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}`,
    method: 'delete'
  })
}

// ==================== 服务周期成本计算 ====================

// 计算服务周期人工成本
export function calculateServicePeriodCost(servicePeriodId) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/calculate-cost`,
    method: 'get'
  })
}

// 重新计算服务周期总成本
export function recalculateServicePeriodCost(servicePeriodId) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/recalculate-cost`,
    method: 'post'
  })
}

// ==================== 服务周期贷款管理 ====================

// 添加服务周期贷款信息
export function addServicePeriodLoan(servicePeriodId, data) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/loans`,
    method: 'post',
    data: data
  })
}

// 查询服务周期贷款信息
export function getServicePeriodLoans(servicePeriodId) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/loans`,
    method: 'get'
  })
}

// 更新服务周期贷款信息
export function updateServicePeriodLoan(loanId, data) {
  return request({
    url: `/credit/contract-employee/service-period-loans/${loanId}`,
    method: 'put',
    data: data
  })
}

// 删除服务周期贷款信息
export function deleteServicePeriodLoan(loanId) {
  return request({
    url: `/credit/contract-employee/service-period-loans/${loanId}`,
    method: 'delete'
  })
}

// 验证贷款金额并获取允许范围
export function validateLoanAmountAndGetRange(servicePeriodId, requestedAmount) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/validate-loan-amount`,
    method: 'post',
    data: { requestedAmount }
  })
}

// 根据出勤记录申请贷款
export function applyLoanBasedOnAttendance(servicePeriodId, loanRequest) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/apply-loan`,
    method: 'post',
    data: loanRequest
  })
}

// 计算基于出勤数据的贷款金额
export function calculateLoanAmountBasedOnAttendance(servicePeriodId) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/calculate-loan-amount`,
    method: 'get'
  })
}

// 根据合同期限选择银行融资条件
export function selectBankFinancingByContractTerm(servicePeriodId, contractTermMonths) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/bank-financing`,
    method: 'get',
    params: { contractTermMonths }
  })
}

// 生成服务周期贷款报表
export function generateServicePeriodLoanReport(servicePeriodId, reportType = 'COMPREHENSIVE') {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/loan-report`,
    method: 'post',
    params: { reportType }
  })
}

// 导出服务周期贷款与出勤关联报表
export function exportLoanAttendanceReport(servicePeriodId, format = 'EXCEL') {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/export-loan-attendance`,
    method: 'get',
    params: { format },
    responseType: 'blob'
  })
}

// ==================== 服务周期报表 ====================

// 生成服务周期报表
export function generateServicePeriodReport(servicePeriodId, reportType) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/report`,
    method: 'post',
    data: { reportType: reportType }
  })
}

// 导出服务周期报表
export function exportServicePeriodReport(servicePeriodId, format) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/export`,
    method: 'get',
    params: { format: format },
    responseType: 'blob'
  })
}

// ==================== 服务周期验证 ====================

// 验证服务周期数据
export function validateServicePeriodData(data) {
  return request({
    url: '/credit/contract-employee/service-periods/validate',
    method: 'post',
    data: data
  })
}

// 检查服务周期状态
export function checkServicePeriodStatus(servicePeriodId) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/status`,
    method: 'get'
  })
}

// ==================== 银行选择逻辑优化 ====================

// 创建包含银行选择的贷款申请
export function applyLoanWithBankSelection(servicePeriodId, loanRequest) {
  return request({
    url: `/credit/service-period-loan/apply-with-bank-selection`,
    method: 'post',
    data: {
      servicePeriodId: servicePeriodId,
      ...loanRequest
    }
  })
}

// 一键提交贷款到银行
export function quickSubmitLoanToBank(loanId) {
  return request({
    url: `/credit/service-period-loan/quick-submit/${loanId}`,
    method: 'post'
  })
}

// 修改银行选择和提交说明
export function updateBankSelection(loanId, updateRequest) {
  return request({
    url: `/credit/service-period-loan/update-bank-selection/${loanId}`,
    method: 'put',
    data: updateRequest
  })
}

// 获取贷款详情（包含银行选择状态）
export function getLoanDetails(loanId) {
  return request({
    url: `/credit/service-period-loan/${loanId}`,
    method: 'get'
  })
}