import request from '@/utils/request'

// ============ 用户管理 ============

export function getUsers(params: { page: number; page_size: number; keyword?: string }) {
  return request.Get('/v1/admin/users', { params })
}

export function updateUser(id: number, data: Record<string, any>) {
  return request.Put(`/v1/admin/users/${id}`, data)
}

export function getUserRoles(id: number) {
  return request.Get(`/v1/admin/users/${id}/roles`)
}

// ============ Banner 管理 ============

export function getBanners() {
  return request.Get('/v1/admin/banners')
}

export function createBanner(data: Record<string, any>) {
  return request.Post('/v1/admin/banners', data)
}

export function updateBanner(id: number, data: Record<string, any>) {
  return request.Put(`/v1/admin/banners/${id}`, data)
}

export function deleteBanner(id: number) {
  return request.Delete(`/v1/admin/banners/${id}`)
}

// ============ 通知管理 ============

export function getNotices() {
  return request.Get('/v1/admin/notices')
}

export function createNotice(data: Record<string, any>) {
  return request.Post('/v1/admin/notices', data)
}

export function updateNotice(id: number, data: Record<string, any>) {
  return request.Put(`/v1/admin/notices/${id}`, data)
}

export function deleteNotice(id: number) {
  return request.Delete(`/v1/admin/notices/${id}`)
}

// ============ 认证审核 ============

export function getCertifications(params?: Record<string, any>) {
  return request.Get('/v1/admin/certifications', { params })
}

export function auditCertification(id: number, data: Record<string, any>) {
  return request.Put(`/v1/admin/certifications/${id}/audit`, data)
}



// ============ 员工列表 ============

/** 获取员工列表（含部门信息） */
export function getEmployees() {
  return request.Get('/v1/admin/employees')
}

// ============ 员工-部门关联 ============

/** 设置员工所属部门（可选传 position） */
export function setEmployeeDepartment(userId: number, departmentId: number, position?: string) {
  const data: Record<string, any> = { department_id: departmentId }
  if (position !== undefined) data.position = position
  return request.Put(`/v1/admin/employees/${userId}/department`, data)
}

/** 清除员工所属部门 */
export function clearEmployeeDepartment(userId: number) {
  return request.Delete(`/v1/admin/employees/${userId}/department`)
}

/** 获取所有员工-部门关联 */
export function getEmployeeDepartments() {
  return request.Get('/v1/admin/employee-departments')
}

// ============ 角色 ============

export function getRoles() {
  return request.Get('/v1/admin/roles')
}

// ============ 部门管理 ============

export function getDepartmentTree() {
  return request.Get('/v1/admin/department-tree')
}

export function getDepartments() {
  return request.Get('/v1/admin/departments')
}

export function createDepartment(data: Record<string, any>) {
  return request.Post('/v1/admin/departments', data)
}

export function updateDepartment(id: number, data: Record<string, any>) {
  return request.Put(`/v1/admin/departments/${id}`, data)
}

export function deleteDepartment(id: number) {
  return request.Delete(`/v1/admin/departments/${id}`)
}

export function listAllDepartments() {
  return request.Get('/v1/admin/departments-all')
}

// ============ 微信素材上传 ============

/** 通过文件上传图片到微信服务器 */
export function uploadImage(file: File) {
  const formData = new FormData()
  formData.append('file', file)
  // 不手动设置 Content-Type：浏览器/ fetch 会自动追加 boundary
  return request.Post('/v1/upload/image', formData)
}

/** 通过链接上传图片到微信服务器 */
export function uploadImageByURL(url: string) {
  return request.Post<{ url: string }>('/v1/upload/image-by-url', { url })
}

// ============ 审批流配置（新） ============

/** 设置部门的分管领导用户ID列表 */
export function setDepartmentVPs(departmentId: number, vpUserIds: number[]) {
  return request.Put(`/v1/admin/departments/${departmentId}/vp-users`, { vp_user_ids: vpUserIds })
}

/** 设置部门指定审批人（可多人） */
export function setDepartmentApprover(departmentId: number, approverUserIds: number[]) {
  return request.Put(`/v1/admin/departments/${departmentId}/approver`, { approver_user_ids: approverUserIds })
}

// ============ 员工通讯录导入 ============

/** 批量导入待确认员工 */
export function importPendingEmployees(data: { employees: { real_name: string; phone: string; department_id: number; position?: string }[] }) {
  return request.Post('/v1/admin/employees/import', data)
}

/** 待确认员工列表 */
export function getPendingEmployees(params?: { page?: number; page_size?: number; status?: string; keyword?: string; department_id?: number }) {
  return request.Get('/v1/admin/employees/pending', { params })
}

/** 手动匹配待确认员工到用户 */
export function matchPendingEmployee(id: number) {
  return request.Post(`/v1/admin/employees/pending/${id}/match`)
}

/** 待确认员工统计 */
export function getPendingEmployeeStats() {
  return request.Get('/v1/admin/employees/pending/stats')
}

/** 一键同步所有待确认员工信息到用户 */
export function syncAllPendingEmployees() {
  return request.Post('/v1/admin/employees/pending/sync-all')
}

// ============ 全局兜底审批人 ============

/** 获取全局兜底审批人列表 */
export function getGlobalApprovers() {
  return request.Get('/v1/admin/global-approvers')
}

/** 添加全局兜底审批人 */
export function createGlobalApprover(userId: number) {
  return request.Post('/v1/admin/global-approvers', { user_id: userId })
}

/** 删除全局兜底审批人 */
export function deleteGlobalApprover(id: number) {
  return request.Delete(`/v1/admin/global-approvers/${id}`)
}
