'Unauthorized access. Admin role required.']); exit; } $alerts = []; try { // 1. شكاوى جديدة غير محلولة (مفتوحة) $stmt = $con->prepare("SELECT id, ride_id, complaint_type, date_filed FROM complaint WHERE statusComplaint = 'Open' ORDER BY date_filed DESC LIMIT 10"); $stmt->execute(); $open_complaints = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach($open_complaints as $c) { $alerts[] = [ 'type' => 'complaint', 'severity' => 'high', 'title' => 'شكوى جديدة (' . $c['complaint_type'] . ')', 'description' => "يوجد شكوى جديدة للرحلة رقم " . $c['ride_id'] . " تحتاج للمراجعة.", 'date' => $c['date_filed'], 'action_id' => $c['id'] ]; } // 2. رحلات عالقة (في الانتظار لأكثر من 15 دقيقة) $stmt = $con->prepare("SELECT id, created_at FROM ride WHERE status = 'wait' AND created_at < DATE_SUB(NOW(), INTERVAL 15 MINUTE) LIMIT 10"); $stmt->execute(); $stuck_rides = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach($stuck_rides as $r) { $alerts[] = [ 'type' => 'ride', 'severity' => 'medium', 'title' => 'رحلة عالقة قيد الانتظار', 'description' => "الرحلة رقم " . $r['id'] . " عالقة في حالة انتظار لأكثر من 15 دقيقة.", 'date' => $r['created_at'], 'action_id' => $r['id'] ]; } // 3. رخص قيادة شارفت على الانتهاء (خلال 15 يوم القادمة) $stmt = $con->prepare("SELECT id, first_name, last_name, phone, expiry_date FROM driver WHERE expiry_date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 15 DAY) LIMIT 10"); $stmt->execute(); $expiring_drivers = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach($expiring_drivers as $d) { $alerts[] = [ 'type' => 'license', 'severity' => 'warning', 'title' => 'رخصة كابتن قاربت على الانتهاء', 'description' => "رخصة الكابتن " . $d['first_name'] . " " . $d['last_name'] . " ستنتهي بتاريخ " . $d['expiry_date'] . ".", // We use current time for sorting purposes, but display the expiry date 'date' => date('Y-m-d H:i:s'), 'action_id' => $d['id'] ]; } // ترتيب التنبيهات حسب الأحدث usort($alerts, function($a, $b) { return strtotime($b['date']) - strtotime($a['date']); }); echo json_encode([ 'status' => 'success', 'message' => $alerts ]); } catch (Exception $e) { http_response_code(500); echo json_encode(['status' => 'error', 'message' => $e->getMessage()]); } ?>