// API Response structure for user list export interface AdminUserResponse { id: number; username: string; email: string; created_at: string; updated_at: string; role: { code: string; name: { en: string; th: string; }; }; profile: { prefix: { en: string; th: string; }; first_name: string; last_name: string; avatar_url: string | null; birth_date: string | null; phone: string | null; }; } export interface ApiResponse { code: number; message: string; data: T; } export interface UsersListResponse { code: number; message: string; data: AdminUserResponse[]; } // Pending Course interfaces export interface PendingCourseUser { id: number; email: string; username: string; } export interface PendingCourseInstructor { user_id: number; is_primary: boolean; user: PendingCourseUser; } export interface PendingCourseSubmission { id: number; submitted_by: number; created_at: string; submitter: PendingCourseUser; } export interface PendingCourse { id: number; title: { th: string; en: string; }; slug: string; description: { th: string; en: string; }; thumbnail_url: string | null; status: string; created_at: string; updated_at: string; created_by: number; creator: PendingCourseUser; instructors: PendingCourseInstructor[]; chapters_count: number; lessons_count: number; latest_submission: PendingCourseSubmission | null; } export interface PendingCoursesListResponse { code: number; message: string; data: PendingCourse[]; total: number; } // Course Detail interfaces for admin review export interface CourseCategory { id: number; name: { th: string; en: string; }; } export interface CourseLesson { id: number; title: { th: string; en: string; }; type: string; sort_order: number; is_published: boolean; } export interface CourseChapter { id: number; title: { th: string; en: string; }; sort_order: number; is_published: boolean; lessons: CourseLesson[]; } export interface ApprovalHistory { id: number; action: string; comment: string | null; created_at: string; submitter: PendingCourseUser; reviewer: PendingCourseUser | null; } export interface CourseDetailForReview { id: number; title: { th: string; en: string; }; slug: string; description: { th: string; en: string; }; thumbnail_url: string | null; price: number; is_free: boolean; have_certificate: boolean; status: string; created_at: string; updated_at: string; category: CourseCategory; creator: PendingCourseUser; instructors: PendingCourseInstructor[]; chapters: CourseChapter[]; approval_history: ApprovalHistory[]; } export interface CourseDetailForReviewResponse { code: number; message: string; data: CourseDetailForReview; } // Category interfaces export interface CategoryResponse { id: number; name: { en: string; th: string; }; slug: string; description: { en: string; th: string; }; icon: string; sort_order: number; is_active: boolean; created_at: string; created_by: number; updated_at: string; updated_by: number | null; } export interface CategoriesListResponse { code: number; message: string; data: { categories: CategoryResponse[]; }; } export interface CreateCategoryRequest { name: { en: string; th: string; }; slug: string; description: { en: string; th: string; }; created_by?: number; } export interface UpdateCategoryRequest { id: number; name: { en: string; th: string; }; slug: string; description: { en: string; th: string; }; } // Helper function to get auth token from cookie const getAuthToken = (): string => { const tokenCookie = useCookie('token'); return tokenCookie.value || ''; }; export const adminService = { async getUsers(): Promise { const config = useRuntimeConfig(); const token = getAuthToken(); const response = await $fetch('/api/admin/usermanagement/users', { baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` } }); return response.data; }, async getUserById(id: number): Promise { const config = useRuntimeConfig(); const token = getAuthToken(); const response = await $fetch(`/api/admin/usermanagement/users/${id}`, { baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` } }); return response; }, async updateUserRole(userId: number, roleId: number): Promise> { const config = useRuntimeConfig(); const token = getAuthToken(); const response = await $fetch>(`/api/admin/usermanagement/role/${userId}`, { method: 'PUT', baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` }, body: { id: userId, role_id: roleId } }); return response; }, async deleteUser(userId: number): Promise> { const config = useRuntimeConfig(); const token = getAuthToken(); const response = await $fetch>(`/api/admin/usermanagement/users/${userId}`, { method: 'DELETE', baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` } }); return response; }, // ============ Pending Courses ============ async getPendingCourses(): Promise { const config = useRuntimeConfig(); const token = getAuthToken(); const response = await $fetch('/api/admin/courses/pending', { baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` } }); return response.data; }, async getCourseForReview(courseId: number): Promise { const config = useRuntimeConfig(); const token = getAuthToken(); const response = await $fetch(`/api/admin/courses/${courseId}`, { baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` } }); return response.data; }, async approveCourse(courseId: number, comment?: string): Promise> { const config = useRuntimeConfig(); const token = getAuthToken(); const response = await $fetch>(`/api/admin/courses/${courseId}/approve`, { method: 'POST', baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` }, body: { comment: comment || '' } }); return response; }, async rejectCourse(courseId: number, comment: string): Promise> { const config = useRuntimeConfig(); const token = getAuthToken(); const response = await $fetch>(`/api/admin/courses/${courseId}/reject`, { method: 'POST', baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` }, body: { comment } }); return response; }, // ============ Categories ============ async getCategories(): Promise { const config = useRuntimeConfig(); const token = getAuthToken(); const response = await $fetch('/api/categories', { baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` } }); return response.data.categories; }, async createCategory(data: CreateCategoryRequest): Promise> { const config = useRuntimeConfig(); const token = getAuthToken(); const response = await $fetch>('/api/admin/categories', { method: 'POST', baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` }, body: data }); return response; }, async updateCategory(id: number, data: UpdateCategoryRequest): Promise> { const config = useRuntimeConfig(); const token = getAuthToken(); const response = await $fetch>(`/api/admin/categories/${id}`, { method: 'PUT', baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` }, body: data }); return response; }, async deleteCategory(id: number): Promise> { const config = useRuntimeConfig(); const token = getAuthToken(); const response = await $fetch>(`/api/admin/categories/${id}`, { method: 'DELETE', baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` } }); return response; } };