// 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 UsersListResponse { code: number; message: string; data: AdminUserResponse[]; } // Mock data for development const MOCK_USERS: AdminUserResponse[] = [ { id: 1, username: 'admin', email: 'admin@elearning.local', created_at: '2024-01-01T00:00:00Z', updated_at: '2024-01-01T00:00:00Z', role: { code: 'ADMIN', name: { en: 'Administrator', th: 'ผู้ดูแลระบบ' } }, profile: { prefix: { en: 'Mr.', th: 'นาย' }, first_name: 'Admin', last_name: 'User', avatar_url: null, birth_date: null, phone: '0812345678' } }, { id: 2, username: 'instructor', email: 'instructor@elearning.local', created_at: '2024-01-01T00:00:00Z', updated_at: '2024-01-01T00:00:00Z', role: { code: 'INSTRUCTOR', name: { en: 'Instructor', th: 'ผู้สอน' } }, profile: { prefix: { en: 'Mr.', th: 'นาย' }, first_name: 'John', last_name: 'Instructor', avatar_url: null, birth_date: null, phone: '0812345679' } }, { id: 3, username: 'student', email: 'student@elearning.local', created_at: '2024-01-01T00:00:00Z', updated_at: '2024-01-01T00:00:00Z', role: { code: 'STUDENT', name: { en: 'Student', th: 'นักเรียน' } }, profile: { prefix: { en: 'Ms.', th: 'นางสาว' }, first_name: 'Jane', last_name: 'Student', avatar_url: null, birth_date: null, phone: '0812345680' } } ]; // Helper function to get auth token from cookie or localStorage const getAuthToken = (): string => { const tokenCookie = useCookie('token'); return tokenCookie.value || ''; }; export const adminService = { async getUsers(): Promise { const config = useRuntimeConfig(); const useMockData = config.public.useMockData as boolean; if (useMockData) { await new Promise(resolve => setTimeout(resolve, 500)); return MOCK_USERS; } 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 useMockData = config.public.useMockData as boolean; if (useMockData) { await new Promise(resolve => setTimeout(resolve, 300)); const user = MOCK_USERS.find(u => u.id === id); if (!user) throw new Error('User not found'); return user; } 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 useMockData = config.public.useMockData as boolean; if (useMockData) { await new Promise(resolve => setTimeout(resolve, 500)); return; } const token = getAuthToken(); 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 } }); }, async deleteUser(userId: number): Promise { const config = useRuntimeConfig(); const useMockData = config.public.useMockData as boolean; if (useMockData) { await new Promise(resolve => setTimeout(resolve, 500)); return; } const token = getAuthToken(); await $fetch(`/api/admin/usermanagement/users/${userId}`, { method: 'DELETE', baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` } }); }, // ============ Categories ============ async getCategories(): Promise { const config = useRuntimeConfig(); const useMockData = config.public.useMockData as boolean; if (useMockData) { await new Promise(resolve => setTimeout(resolve, 500)); return MOCK_CATEGORIES; } 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 useMockData = config.public.useMockData as boolean; if (useMockData) { await new Promise(resolve => setTimeout(resolve, 500)); return { ...MOCK_CATEGORIES[0], id: Date.now() }; } 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 useMockData = config.public.useMockData as boolean; if (useMockData) { await new Promise(resolve => setTimeout(resolve, 500)); return { ...MOCK_CATEGORIES[0], id }; } 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 useMockData = config.public.useMockData as boolean; if (useMockData) { await new Promise(resolve => setTimeout(resolve, 500)); return; } const token = getAuthToken(); await $fetch(`/api/admin/categories/${id}`, { method: 'DELETE', baseURL: config.public.apiBaseUrl as string, headers: { Authorization: `Bearer ${token}` } }); } }; // 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; }; } // Mock categories const MOCK_CATEGORIES: CategoryResponse[] = [ { id: 1, name: { en: 'Web Development', th: 'การพัฒนาเว็บไซต์' }, slug: 'web-development', description: { en: 'Learn web development', th: 'หลักสูตรเกี่ยวกับการพัฒนาเว็บไซต์และเว็บแอปพลิเคชัน' }, icon: 'code', sort_order: 1, is_active: true, created_at: '2024-01-15T00:00:00Z', created_by: 1, updated_at: '2024-01-15T00:00:00Z', updated_by: null }, { id: 2, name: { en: 'Mobile Development', th: 'การพัฒนาแอปพลิเคชันมือถือ' }, slug: 'mobile-development', description: { en: 'Learn mobile app development', th: 'หลักสูตรเกี่ยวกับการพัฒนาแอปพลิเคชันบนมือถือ' }, icon: 'smartphone', sort_order: 2, is_active: true, created_at: '2024-01-20T00:00:00Z', created_by: 1, updated_at: '2024-01-20T00:00:00Z', updated_by: null }, { id: 3, name: { en: 'Database', th: 'ฐานข้อมูล' }, slug: 'database', description: { en: 'Learn database management', th: 'หลักสูตรเกี่ยวกับการออกแบบและจัดการฐานข้อมูล' }, icon: 'storage', sort_order: 3, is_active: true, created_at: '2024-02-01T00:00:00Z', created_by: 1, updated_at: '2024-02-01T00:00:00Z', updated_by: null } ];