import 'package:flutter/material.dart'; import 'package:get/get.dart'; import '../../../constant/links.dart'; import '../../../controller/functions/crud.dart'; import '../../widgets/snackbar.dart'; class PendingAdminsPage extends StatefulWidget { const PendingAdminsPage({super.key}); @override State createState() => _PendingAdminsPageState(); } class _PendingAdminsPageState extends State { final CRUD _crud = CRUD(); bool _isLoading = true; List _pendingAdmins = []; @override void initState() { super.initState(); _fetchPendingAdmins(); } Future _fetchPendingAdmins() async { setState(() => _isLoading = true); try { final response = await _crud.post( link: '${AppLink.server}/Admin/auth/list_pending.php', ); if (response != 'failure') { setState(() { _pendingAdmins = response['message'] ?? []; }); } } catch (e) { mySnackbarError('فشل في جلب البيانات: $e'); } finally { setState(() => _isLoading = false); } } Future _handleAction(String adminId, String action) async { try { final response = await _crud.post( link: '${AppLink.server}/Admin/auth/approve_admin.php', payload: { 'admin_id': adminId, 'action': action, }, ); if (response != 'failure') { mySnackbarSuccess('تم تنفيذ الإجراء بنجاح'); _fetchPendingAdmins(); } } catch (e) { mySnackbarError('حدث خطأ: $e'); } } @override Widget build(BuildContext context) { final cs = Theme.of(context).colorScheme; return Scaffold( backgroundColor: cs.surface, body: Column( children: [ Container( padding: const EdgeInsets.fromLTRB(16, 50, 16, 16), child: Row( children: [ GestureDetector( onTap: () => Get.back(), child: Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( color: cs.surfaceContainerHighest, borderRadius: BorderRadius.circular(10), border: Border.all(color: cs.outline), ), child: Icon(Icons.arrow_back_ios_new_rounded, color: cs.onSurfaceVariant, size: 16), ), ), const SizedBox(width: 12), Text('طلبات الانضمام المعلقة', style: TextStyle(color: cs.onSurface, fontSize: 18, fontWeight: FontWeight.w700)), ], ), ), Expanded( child: _isLoading ? Center(child: CircularProgressIndicator(color: cs.primary)) : _pendingAdmins.isEmpty ? _buildEmptyState(cs) : _buildList(cs), ), ], ), ); } Widget _buildEmptyState(ColorScheme cs) { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(Icons.person_search_rounded, size: 80, color: cs.outline), const SizedBox(height: 16), Text('لا توجد طلبات معلقة حالياً', style: TextStyle(color: cs.onSurfaceVariant)), ], ), ); } Widget _buildList(ColorScheme cs) { return ListView.builder( padding: const EdgeInsets.all(16), itemCount: _pendingAdmins.length, itemBuilder: (context, index) { final admin = _pendingAdmins[index]; return Container( margin: const EdgeInsets.only(bottom: 16), padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: cs.surfaceContainerHighest, borderRadius: BorderRadius.circular(16), border: Border.all(color: cs.outline), ), child: Column( children: [ ListTile( contentPadding: EdgeInsets.zero, leading: CircleAvatar( backgroundColor: cs.outline, child: Icon(Icons.person, color: cs.primary), ), title: Text(admin['name'] ?? 'بدون اسم', style: TextStyle(color: cs.onSurface, fontWeight: FontWeight.bold)), subtitle: Text(admin['phone'] ?? 'بدون رقم', style: TextStyle(color: cs.onSurfaceVariant)), trailing: Text( admin['created_at']?.split(' ')[0] ?? '', style: TextStyle(color: cs.onSurfaceVariant, fontSize: 12), ), ), Divider(color: cs.outline, height: 24), Row( mainAxisAlignment: MainAxisAlignment.end, children: [ TextButton( onPressed: () => _handleAction(admin['id'], 'rejected'), style: TextButton.styleFrom(foregroundColor: cs.error), child: const Text('رفض'), ), const SizedBox(width: 8), ElevatedButton( onPressed: () => _handleAction(admin['id'], 'approved'), style: ElevatedButton.styleFrom( backgroundColor: cs.primary, foregroundColor: cs.onPrimary, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10), ), ), child: const Text('موافقة', style: TextStyle(fontWeight: FontWeight.bold)), ), ], ), ], ), ); }, ); } }