$r) { $num = $i + 1; $date = $r['date'] ?? ''; $time = $r['time'] ?? ''; $from = $r['start_location'] ?? '---'; $to = $r['end_location'] ?? '---'; $price = $r['price'] ?? '0'; $status = $r['status'] ?? ''; $rideList .= "{$num}. رحلة #{$r['id']} | {$date} {$time}\n" . " من: {$from} → إلى: {$to}\n" . " السعر: {$price} | الحالة: {$status}\n\n"; } $rideList .= "الرجاء إرسال رقم الرحلة من القائمة (1-{$num})\n" . "أو اكتب رقم الرحلة كاملاً (مثال: 831):"; return new FlowResult($rideList, "await_ride"); // ───────────────────────────────────────────────── // AWAIT_RIDE: let user pick a ride // ───────────────────────────────────────────────── case 'await_ride': $selectedRide = null; $rides = $context['rides'] ?? []; // Check if user entered a list number (1, 2, 3...) $cleanNum = preg_replace('/[^0-9]/', '', $text); if (!empty($cleanNum) && is_numeric($cleanNum)) { $index = (int)$cleanNum - 1; if (isset($rides[$index])) { $selectedRide = $rides[$index]; } // If not found in list, try as ride_id directly if (!$selectedRide) { foreach ($rides as $r) { if ((string)$r['id'] === $cleanNum) { $selectedRide = $r; break; } } } // If still not found, try the number as ride_id if (!$selectedRide) { $selectedRide = ['id' => $cleanNum]; } } if (!$selectedRide) { return new FlowResult( "لم نتعرف على الرقم. الرجاء إرسال رقم الرحلة من القائمة:", "await_ride" ); } $context['ride_id'] = $selectedRide['id']; $context['ride_details'] = $selectedRide; $locFrom = $selectedRide['start_location'] ?? '---'; $locTo = $selectedRide['end_location'] ?? '---'; $date = $selectedRide['date'] ?? '---'; $time = $selectedRide['time'] ?? '---'; $price = $selectedRide['price'] ?? '---'; $status = $selectedRide['status'] ?? '---'; return new FlowResult( "🚖 تفاصيل الرحلة المحددة:\n" . "• رقم الرحلة: {$selectedRide['id']}\n" . "• التاريخ: {$date} {$time}\n" . "• من: {$locFrom}\n" . "• إلى: {$locTo}\n" . "• السعر: {$price}\n" . "• الحالة: {$status}\n\n" . "📋 وصف المشكلة:\n" . "{$context['complaint_text']}\n\n" . "هل تريد تأكيد إرسال الشكوى؟\n" . "✅ أرسل: تأكيد\n" . "🔄 أرسل: تعديل (لإعادة كتابة الوصف)\n" . "🟡 أرسل: إلغاء", "await_confirmation" ); // ───────────────────────────────────────────────── // AWAIT_CONFIRMATION: confirm and submit // ───────────────────────────────────────────────── case 'await_confirmation': $clean = trim(mb_strtolower($text)); if (in_array($clean, ['تعديل', 'edit', 'تعديل الوصف'])) { return new FlowResult( "الرجاء إرسال وصف المشكلة الجديد:", "await_description" ); } if (!in_array($clean, ['تأكيد', 'نعم', 'اكيد', 'ok', 'yes', 'confirm', 'تاكيد', 'okay'])) { return new FlowResult( "❌ لم يتم التأكيد.\n" . "✅ للتأكيد أرسل: تأكيد\n" . "🔄 للتعديل أرسل: تعديل\n" . "🟡 للإلغاء أرسل: إلغاء", "await_confirmation" ); } // Submit complaint via Siro try { $result = SiroService::submitComplaint( $country, $phone, $context['ride_id'], $context['complaint_text'] ); if ($result && ($result['status'] ?? '') === 'success') { $aiResult = $result['ai_result'] ?? []; $report = $result['report'] ?? []; $reportTitle = $report['title'] ?? ''; $reportBody = $report['body'] ?? ''; $reply = "✅ تم إرسال شكواك بنجاح!\n\n" . "📋 نتيجة التحليل:\n" . "• تصنيف الشكوى: " . ($aiResult['complaint_type'] ?? '---') . "\n" . "• الطرف المخطئ: " . ($aiResult['fault_determination'] ?? '---') . "\n" . "• طبيعة الشكوى: " . ($aiResult['complaint_nature'] ?? '---') . "\n\n"; if ($reportBody) { $reply .= "📄 {$reportTitle}\n{$reportBody}\n\n"; } $reply .= "سيتم التواصل معك من قبل فريق الدعم إذا لزم الأمر."; return new FlowResult($reply, "finished", true); } return new FlowResult( "⚠️ حدث خطأ في إرسال الشكوى. يرجى المحاولة مرة أخرى أو التواصل مع الدعم الفني.", "finished", true ); } catch (\Exception $e) { error_log("[ComplaintFlow] Submit error: " . $e->getMessage()); return new FlowResult( "⚠️ تعذر إرسال الشكوى حالياً. يرجى المحاولة لاحقاً.", "finished", true ); } default: return new FlowResult("حدث خطأ في المسار.", "finished", true); } } }