import request from '@/utils/request'

// 员工管理API服务

// ==================== 员工库管理 ====================

/**
 * 获取员工库列表
 * @param {Object} query - 查询参数
 * @returns {Promise} API响应
 */
export function listEmployeeLibraries(query) {
  return request({
    url: '/credit/employee/library',
    method: 'get',
    params: query
  })
}

/**
 * 创建员工库
 * @param {Object} data - 员工库数据 {companyId, libraryName, description}
 * @returns {Promise} API响应
 */
export function createEmployeeLibrary(data) {
  return request({
    url: '/credit/employee/library',
    method: 'post',
    data: data
  })
}

/**
 * 获取员工库详细信息
 * @param {string} libraryId - 员工库ID
 * @returns {Promise} API响应
 */
export function getEmployeeLibrary(libraryId) {
  return request({
    url: `/credit/employee/library/${libraryId}`,
    method: 'get'
  })
}

/**
 * 根据公司ID获取员工库
 * @param {string} companyId - 公司ID
 * @returns {Promise} API响应
 */
export function getEmployeeLibraryByCompany(companyId) {
  return request({
    url: `/credit/employee/library/company/${companyId}`,
    method: 'get'
  })
}

/**
 * 更新员工库
 * @param {Object} data - 员工库数据
 * @returns {Promise} API响应
 */
export function updateEmployeeLibrary(data) {
  return request({
    url: '/credit/employee/library',
    method: 'put',
    data: data
  })
}

/**
 * 删除员工库
 * @param {string} libraryId - 员工库ID
 * @returns {Promise} API响应
 */
export function deleteEmployeeLibrary(libraryId) {
  return request({
    url: `/credit/employee/library/${libraryId}`,
    method: 'delete'
  })
}

// ==================== 员工信息管理 ====================

/**
 * 批量导入ERP格式的员工数据
 * @param {FormData} formData - 包含file, libraryId, format的表单数据
 * @returns {Promise} API响应
 */
export function importErpFile(formData) {
  return request({
    url: '/credit/employee/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 批量导入员工数据 (别名，保持向后兼容)
 * @param {FormData} formData - 包含file, libraryId, format的表单数据
 * @returns {Promise} API响应
 */
export function importEmployees(formData) {
  return importErpFile(formData);
}

/**
 * 创建员工信息
 * @param {Object} data - 员工数据
 * @returns {Promise} API响应
 */
export function createEmployee(data) {
  return request({
    url: '/credit/employee',
    method: 'post',
    data: data
  })
}

/**
 * 获取员工信息列表
 * @param {Object} query - 查询参数
 * @returns {Promise} API响应
 */
export function listEmployees(query) {
  return request({
    url: '/credit/employee',
    method: 'get',
    params: query
  })
}

/**
 * 获取员工详情
 * @param {string} employeeId - 员工ID
 * @returns {Promise} API响应
 */
export function getEmployee(employeeId) {
  return request({
    url: `/credit/employee/${employeeId}`,
    method: 'get'
  })
}

/**
 * 根据员工库ID查询员工信息列表
 * @param {string} libraryId - 员工库ID
 * @returns {Promise} API响应
 */
export function getEmployeesByLibrary(libraryId) {
  return request({
    url: `/credit/employee/library/${libraryId}/employees`,
    method: 'get'
  })
}

/**
 * 根据岗位查询员工信息列表
 * @param {string} position - 岗位
 * @returns {Promise} API响应
 */
export function getEmployeesByPosition(position) {
  return request({
    url: `/credit/employee/position/${position}`,
    method: 'get'
  })
}

/**
 * 更新员工信息
 * @param {Object} data - 员工数据
 * @returns {Promise} API响应
 */
export function updateEmployee(data) {
  return request({
    url: '/credit/employee',
    method: 'put',
    data: data
  })
}

/**
 * 删除员工信息
 * @param {string} employeeId - 员工ID
 * @returns {Promise} API响应
 */
export function deleteEmployee(employeeId) {
  return request({
    url: `/credit/employee/${employeeId}`,
    method: 'delete'
  })
}

/**
 * 添加临时员工
 * @param {Object} data - {employeeInfo, syncToLibrary}
 * @returns {Promise} API响应
 */
export function addTemporaryEmployee(data) {
  return request({
    url: '/credit/employee/temporary',
    method: 'post',
    data: data
  })
}

/**
 * 同步临时员工到正式员工库
 * @param {Array} employeeIds - 员工ID数组
 * @returns {Promise} API响应
 */
export function syncTempEmployees(employeeIds) {
  return request({
    url: '/credit/employee/temporary/sync',
    method: 'post',
    data: { employeeIds }
  })
}

/**
 * 导出员工信息
 * @param {Object} query - 查询参数
 * @returns {Promise} API响应
 */
export function exportEmployees(query) {
  return request({
    url: '/credit/employee/export',
    method: 'post',
    params: query,
    responseType: 'blob'
  })
}

// ==================== 员工批量操作 ====================

/**
 * 批量修改员工信息
 * @param {Object} data - {employeeIds: Array, updateFields: Object}
 * @returns {Promise} API响应
 */
export function batchUpdateEmployees(data) {
  return request({
    url: '/credit/employee/batch',
    method: 'put',
    data: data
  })
}

/**
 * 批量删除员工信息
 * @param {Object} data - {employeeIds: Array}
 * @returns {Promise} API响应
 */
export function batchDeleteEmployees(data) {
  return request({
    url: '/credit/employee/batch',
    method: 'delete',
    data: data
  })
}

/**
 * 批量转移员工到其他员工库
 * @param {Object} data - {employeeIds: Array, targetLibraryId: Number}
 * @returns {Promise} API响应
 */
export function batchTransferEmployees(data) {
  return request({
    url: '/credit/employee/batch/transfer',
    method: 'put',
    data: data
  })
}

// ==================== OCR和数据提取 ====================

/**
 * 通过OCR提取员工相关信息
 * @param {FormData} data - 文件数据
 * @returns {Promise} API响应
 */
export function extractEmployeeByOcr(data) {
  return request({
    url: '/credit/employee/ocr-extract',
    method: 'post',
    data: data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 通过OCR提取员工信息 (别名，保持向后兼容)
 * @param {FormData} data - 文件数据
 * @returns {Promise} API响应
 */
export function extractEmployeeInfoFromOCR(data) {
  return extractEmployeeByOcr(data);
}

/**
 * 验证员工数据完整性
 * @param {Object} data - 验证数据
 * @returns {Promise} API响应
 */
export function validateEmployeeData(data) {
  return request({
    url: '/credit/employee/validate',
    method: 'post',
    data: data
  })
}

/**
 * 生成员工工号
 * @param {string} libraryId - 员工库ID
 * @returns {Promise} API响应
 */
export function generateEmployeeNumber(libraryId) {
  return request({
    url: `/credit/employee/generate-number/${libraryId}`,
    method: 'get'
  })
}

// ==================== 系统数据获取 ====================

/**
 * 获取部门列表
 * @returns {Promise} API响应
 */
export function getDeptList() {
  return request({
    url: '/system/dept/list',
    method: 'get'
  })
}

/**
 * 获取职位列表
 * @param {Object} query - 查询参数
 * @returns {Promise} API响应
 */
export function getPostList(query = { pageNum: 1, pageSize: 100 }) {
  return request({
    url: '/system/post/list',
    method: 'get',
    params: query
  })
}