prepare($sql); $stmtRemote->execute([$newStatus, $id]); $count = $stmtRemote->rowCount(); error_log("ℹ️ [cancelRide.php] Remote DB Rows Affected: $count"); // التحقق: هل تم التحديث؟ if ($count > 0) { // --------------------------------------------------------- // 2. التحديث على السيرفر المحلي (Local DB) // --------------------------------------------------------- // نبدأ معاملة لضمان تكامل البيانات if (isset($con)) { $con->beginTransaction(); try { $stmtLocal = $con->prepare($sql); $stmtLocal->execute([$newStatus, $id]); // تحديث جدول driver_orders أيضاً لتوحيد الحالة (اختياري ولكنه مفضل) $stmtDriverOrder = $con->prepare("UPDATE driver_orders SET status = ? WHERE order_id = ?"); $stmtDriverOrder->execute([$newStatus, $id]); $con->commit(); } catch (Exception $eLocal) { $con->rollBack(); error_log("⚠️ Local DB Update Failed: " . $eLocal->getMessage()); } } // --------------------------------------------------------- // 3. 🔥 إشعار الراكب عبر السوكيت (القطعة المفقودة) 🔥 // --------------------------------------------------------- // أ. جلب معرف الراكب لإرسال الإشعار له // نستخدم connection الرحلات لضمان وجود البيانات $stmtPas = $con_ride->prepare("SELECT passenger_id FROM ride WHERE id = ?"); $stmtPas->execute([$id]); $passenger_id = $stmtPas->fetchColumn(); if ($passenger_id) { $payload = [ 'ride_id' => $id, 'status' => 'cancelled', // هذه الحالة يستقبلها الفلاتر ويغلق الواجهة 'msg' => 'للأسف، قام السائق بإلغاء الرحلة.' ]; // استدعاء الدالة المعرفة في functions.php/connect.php if (function_exists('notifyPassengerOnRideServer')) { notifyPassengerOnRideServer($passenger_id, $payload); error_log("📡 [cancelRide.php] Notification sent to Passenger ID: $passenger_id"); } else { error_log("⚠️ [cancelRide.php] Function notifyPassengerOnRideServer not found!"); } } // --------------------------------------------------------- // 4. إنهاء العملية // --------------------------------------------------------- error_log("✅ [cancelRide.php] Ride cancelled successfully."); jsonSuccess(null, "Ride cancelled successfully"); } else { // الفشل يعني أن الرحلة غير موجودة أو حالتها لا تسمح بالإلغاء (مثلاً بدأت بالفعل) error_log("⚠️ [cancelRide.php] Failed. ID invalid OR Status not allowed (maybe started?)."); jsonError("Cannot cancel ride. Status might be started or already completed."); } } catch (PDOException $e) { error_log("❌ [cancelRide.php] Database Error: " . $e->getMessage()); jsonError("Database Error: " . $e->getMessage()); } ?>