import request from '@/utils/request'

// ==================== 合同员工关联管理 ====================



// 关联单个员工到合同
export function linkIndividualEmployeeToContract(data) {
  return request({
    url: '/credit/contract-employee/link-employee',
    method: 'post',
    data: data
  })
}

// 批量关联员工到合同
export function linkMultipleEmployeesToContract(data) {
  return request({
    url: '/credit/contract-employee/link-multiple-employees',
    method: 'post',
    data: data
  })
}

// 查询合同关联的员工
export function getContractEmployeeLinks(contractId) {
  return request({
    url: `/credit/contract-employee/contract/${contractId}/links`,
    method: 'get'
  })
}

// 取消合同员工关联
export function unlinkContractEmployee(linkId) {
  return request({
    url: `/credit/contract-employee/unlink/${linkId}`,
    method: 'delete'
  })
}

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

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

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

// 查询合同的服务周期列表
export function getServicePeriodsByContract(contractId) {
  return request({
    url: `/credit/contract-employee/contracts/${contractId}/service-periods`,
    method: 'get'
  })
}

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

// ==================== 服务周期出勤员工选择 ====================

// 为服务周期选择出勤员工
export function selectAttendanceEmployees(servicePeriodId, data) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/select-attendance`,
    method: 'post',
    data: data
  })
}



// 查询服务周期的员工分配
export function getServicePeriodEmployees(servicePeriodId) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/employees`,
    method: 'get'
  })
}

// 更新员工分配信息
export function updateServicePeriodEmployee(data) {
  return request({
    url: '/credit/contract-employee/service-period-employees',
    method: 'put',
    data: data
  })
}

// 添加员工分配信息
export function addServicePeriodEmployee(data) {
  return request({
    url: '/credit/contract-employee/service-period-employees',
    method: 'post',
    data: data
  })
}

// 移除服务周期员工分配
export function removeServicePeriodEmployee(assignmentId) {
  return request({
    url: `/credit/contract-employee/service-period-employees/${assignmentId}`,
    method: 'delete'
  })
}

// 批量移除服务周期员工分配
export function batchRemoveServicePeriodEmployees(servicePeriodId, data) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/batch-remove-employees`,
    method: 'post',
    data: data
  })
}

// 清空服务周期所有员工分配
export function clearAllServicePeriodEmployees(servicePeriodId) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/clear-all-employees`,
    method: 'delete'
  })
}

// ==================== OCR信息提取 ====================

// 通过OCR自动提取员工信息
export function extractEmployeeInfoFromOCR(file) {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/credit/contract-employee/ocr-extract',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 手动调整薪资标准
export function adjustSalaryStandard(servicePeriodId, data) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/adjust-salary`,
    method: 'post',
    data: data
  })
}

// 批量调整薪资标准
export function batchAdjustSalaryStandards(servicePeriodId, data) {
  return request({
    url: `/credit/contract-employee/service-periods/${servicePeriodId}/batch-adjust-salary`,
    method: 'post',
    data: data
  })
}

// ==================== 人工成本计算 ====================

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

// 计算单个员工成本
export function calculateEmployeeCost(employeeId, workDays, salaryType) {
  return request({
    url: `/credit/contract-employee/employees/${employeeId}/calculate-cost`,
    method: 'get',
    params: {
      workDays: workDays,
      salaryType: salaryType
    }
  })
}

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

// ==================== 数据验证 ====================

// 验证员工是否可以分配到服务周期
export function validateEmployeeAssignment(employeeId, servicePeriodId) {
  return request({
    url: '/credit/contract-employee/validate-employee-assignment',
    method: 'get',
    params: {
      employeeId: employeeId,
      servicePeriodId: servicePeriodId
    }
  })
}




// ==================== 员工库基础查询 ====================

// 获取员工库列表（仅用于查询，不用于分配）
export function getEmployeeLibraries(query) {
  return request({
    url: '/system/employee-library/list',
    method: 'get',
    params: query
  })
}

// 获取员工库详情（仅用于查询，不用于分配）
export function getEmployeeLibrary(libraryId) {
  return request({
    url: `/system/employee-library/${libraryId}`,
    method: 'get'
  })
}

// 获取员工库中的员工列表（仅用于查询，不用于分配）
export function getEmployeesInLibrary(libraryId, query) {
  return request({
    url: `/system/employee-library/${libraryId}/employees`,
    method: 'get',
    params: query
  })
}