43 lines
1.7 KiB
PHP
43 lines
1.7 KiB
PHP
<?php
|
|
// transit/trip/delay.php — السائق يبلغ عن تأخير
|
|
|
|
require_once __DIR__ . '/../../transit/connect_app.php';
|
|
|
|
if ($transit_user_role !== 'driver') jsonError('Only drivers can access this endpoint', 403);
|
|
|
|
$tripId = filterRequest('trip_id', 'int');
|
|
$delay = min((int)filterRequest('delay_minutes'), 120);
|
|
$reason = filterRequest('reason');
|
|
if (!$tripId || $delay <= 0) jsonError('trip_id and delay_minutes are required', 400);
|
|
|
|
// استخرج سجل السائق من JWT
|
|
$driverRow = $transit_con->prepare(
|
|
"SELECT id FROM transit_drivers WHERE main_driver_id=? AND status='active' LIMIT 1"
|
|
);
|
|
$driverRow->execute([(string)$transit_user_id]);
|
|
$driverRec = $driverRow->fetch();
|
|
if (!$driverRec) jsonError('Driver not found or not active', 403);
|
|
$driverId = (int)$driverRec['id'];
|
|
|
|
$st = $transit_con->prepare(
|
|
"SELECT t.id, t.route_id, r.name_ar AS route_name
|
|
FROM transit_trips t JOIN transit_routes r ON r.id=t.route_id
|
|
WHERE t.id=? AND t.driver_id=? AND t.status='started' LIMIT 1"
|
|
);
|
|
$st->execute([$tripId, $driverId]);
|
|
$trip = $st->fetch();
|
|
if (!$trip) jsonError('Active trip not found', 404);
|
|
|
|
$transit_con->prepare(
|
|
"UPDATE transit_trips SET delay_minutes=?, delay_reason=?, updated_at=NOW() WHERE id=?"
|
|
)->execute([$delay, $reason, $tripId]);
|
|
|
|
transitSendTopicNotification(
|
|
transitRouteTopic((int)$trip['route_id']),
|
|
'تأخير في الباص',
|
|
'خط ' . $trip['route_name'] . ' متأخر ' . $delay . ' دقيقة' . ($reason ? " — $reason" : ''),
|
|
['type' => 'transit_delay', 'trip_id' => (string)$tripId, 'delay' => (string)$delay]
|
|
);
|
|
|
|
jsonSuccess(['delay_minutes' => $delay], 'Delay reported and passengers notified');
|