import { requestClient } from '#/api/request';

export namespace ClassroomApi {
  /** 教室列表查询参数 */
  export interface ListParams {
    page?: number;
    limit?: number;
    name?: string;
    building?: string;
    type?: 'normal' | 'multimedia' | 'lab';
    school_id?: number;
  }

  /** 教室位置信息 */
  export interface ClassroomLocation {
    room?: string;
    floor?: string;
    building?: string;
  }

  /** 教室座位布局单元格 */
  export interface ClassroomLayoutCell {
    col: number;
    row: number;
    type: 'empty' | 'pillar' | 'aisle' | 'seat' | 'door' | 'projector';
    number?: string;
    status?: number; // 座位状态：1=可用, 0=禁坐
    name?: string;
    colspan?: number; // 合并列数（默认1）
    rowspan?: number; // 合并行数（默认1）
    merged?: boolean; // 是否为被合并的单元格（用于标记被主单元格合并的单元格）
  }

  /** 教室座位布局 */
  export interface ClassroomLayout {
    cols: number;
    rows: number;
    cells: ClassroomLayoutCell[];
  }

  /** 教室信息 */
  export interface ClassroomInfo {
    id?: number;
    name?: string;
    location?: ClassroomLocation;
    teacher_id?: number;
    type?: 'normal' | 'multimedia' | 'lab' | string;
    capacity?: number;
    layout?: ClassroomLayout;
    school_id?: number;
    [key: string]: any;
  }

  /** 教室详情参数 */
  export interface DetailParams {
    id: number;
  }

  /** 保存教室参数 */
  export interface SaveParams extends ClassroomInfo {
    id?: number;
  }

  /** 删除教室参数 */
  export interface DeleteParams {
    id: number;
  }

  /** 批量删除教室参数 */
  export interface BatchDeleteParams {
    ids: number[];
  }

  /** 保存座位布局参数 */
  export interface SaveLayoutParams {
    id: number;
    layout: any;
  }

  /** 获取教室座位状态参数 */
  export interface StatusParams {
    id: number;
  }

  /** 分配座位参数 */
  export interface AssignSeatParams {
    classroomId: number;
    studentId: number;
    row: number;
    col: number;
    number: string;
  }

  /** 随机分配座位参数 */
  export interface RandomAssignSeatsParams {
    classroomId: number;
  }

  /** 获取未分配座位的学生列表参数 */
  export interface UnassignedStudentsParams {
    classroomId: number;
  }

  /** 取消选座参数 */
  export interface CancelBookingParams {
    id: number;
  }

  /** 批量取消选座参数 */
  export interface CancelBookingAutoParams {
    ids: number[];
  }

  /** 获取教室选座列表参数 */
  export interface GetSeatListParams {
    id: number;
  }

  /** 座位信息 */
  export interface SeatInfo {
    seat_number: string;
    is_selected: number; // 1已选，0未选
    student_name: string;
    student_mobile: string;
    select_time: string;
  }
}

/**
 * 获取教室列表
 * GET /api/admin/classroom/list
 */
export async function getClassroomListApi(params?: ClassroomApi.ListParams) {
  // 使用 responseReturn: 'body' 获取完整响应体，包含 code, count, data 等字段
  return requestClient.get<any>('/api/admin/classroom/list', {
    params,
    responseReturn: 'body',
  });
}

/**
 * 获取教室详情
 * GET /api/admin/classroom/detail
 */
export async function getClassroomDetailApi(params: ClassroomApi.DetailParams) {
  // 使用 responseReturn: 'body' 获取完整响应体
  return requestClient.get<any>('/api/admin/classroom/detail', {
    params,
    responseReturn: 'body',
  });
}

/**
 * 保存教室（新增/编辑）
 * POST /api/admin/classroom/save
 */
export async function saveClassroomApi(data: ClassroomApi.SaveParams) {
  return requestClient.post<ClassroomApi.ClassroomInfo>('/api/admin/classroom/save', data);
}

/**
 * 删除教室
 * POST /api/admin/classroom/delete
 */
export async function deleteClassroomApi(data: ClassroomApi.DeleteParams) {
  return requestClient.post('/api/admin/classroom/delete', data);
}

/**
 * 批量删除教室
 * POST /api/admin/classroom/batchDelete
 */
export async function batchDeleteClassroomApi(data: ClassroomApi.BatchDeleteParams) {
  return requestClient.post('/api/admin/classroom/batchDelete', data);
}

/**
 * 保存座位布局
 * POST /api/admin/classroom/saveLayout
 */
export async function saveClassroomLayoutApi(data: ClassroomApi.SaveLayoutParams) {
  return requestClient.post('/api/admin/classroom/saveLayout', data);
}

/**
 * 获取教室座位状态
 * GET /api/admin/classroom/status
 */
export async function getClassroomStatusApi(params: ClassroomApi.StatusParams) {
  return requestClient.get<any>('/api/admin/classroom/status', { params });
}

/**
 * 分配座位
 * POST /api/admin/classroom/assignSeat
 */
export async function assignSeatApi(data: ClassroomApi.AssignSeatParams) {
  return requestClient.post('/api/admin/classroom/assignSeat', data);
}

/**
 * 随机分配座位
 * POST /api/admin/classroom/randomAssignSeats
 */
export async function randomAssignSeatsApi(data: ClassroomApi.RandomAssignSeatsParams) {
  return requestClient.post('/api/admin/classroom/randomAssignSeats', data);
}

/**
 * 获取未分配座位的学生列表
 * GET /api/admin/classroom/getUnassignedStudents
 */
export async function getUnassignedStudentsApi(params: ClassroomApi.UnassignedStudentsParams) {
  return requestClient.get<any>('/api/admin/classroom/getUnassignedStudents', { params });
}

/**
 * 取消选座
 * POST /api/admin/classroom/cancelBooking
 */
export async function cancelBookingApi(data: ClassroomApi.CancelBookingParams) {
  return requestClient.post('/api/admin/classroom/cancelBooking', data);
}

/**
 * 批量取消选座
 * POST /api/admin/classroom/cancelBookingAuto
 */
export async function cancelBookingAutoApi(data: ClassroomApi.CancelBookingAutoParams) {
  return requestClient.post('/api/admin/classroom/cancelBookingAuto', data);
}

/**
 * 获取分校列表（用于教室管理）
 * GET /api/admin/classroom/getSchoolList
 */
export async function getClassroomSchoolListApi() {
  return requestClient.get<any[]>('/api/admin/classroom/getSchoolList');
}

/**
 * 上传图片
 * POST /api/admin/classroom/upload
 */
export async function uploadClassroomImageApi(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  return requestClient.post<string>('/api/admin/classroom/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}

/**
 * 获取教室选座列表
 * GET /api/admin/classroom/getSeatList
 */
export async function getClassroomSeatListApi(params: ClassroomApi.GetSeatListParams) {
  // 使用 responseReturn: 'body' 获取完整响应体
  return requestClient.get<any>('/api/admin/classroom/getSeatList', {
    params,
    responseReturn: 'body',
  });
}

