import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:sefer_admin1/controller/functions/crud.dart'; import 'package:sefer_admin1/controller/functions/wallet.dart'; // تأكد من المسار // --- Controller: المسؤول عن المنطق (البحث، الفحص، الإضافة) --- class DriverGiftCheckerController extends GetxController { // للتحكم في حقل النص final TextEditingController phoneController = TextEditingController(); // لعرض النتائج وحالة التحميل var statusLog = "".obs; var isLoading = false.obs; // قائمة السائقين (سنقوم بتحميلها للبحث عن الـ ID) List driversCache = []; @override void onInit() { super.onInit(); // fetchDriverCache(); // تحميل البيانات عند فتح الصفحة } // 1. تحميل قائمة السائقين لاستخراج الـ ID منها Future fetchDriverCache() async { try { final response = await CRUD().post( link: 'https://api.intaleq.xyz/intaleq/Admin/driver/getDriverGiftPayment.php', payload: {'phone': phoneController.text.trim()}, ); // print('response: ${response}'); if (response != 'failure') { driversCache = (response['message']); } } catch (e) { debugPrint("Error loading cache: $e"); } } // --- الدالة الرئيسية التي تنفذ العملية المطلوبة --- Future processDriverGift() async { String phoneInput = phoneController.text.trim(); if (phoneInput.isEmpty) { Get.snackbar("تنبيه", "يرجى إدخال رقم الهاتف", backgroundColor: Colors.orange); return; } await fetchDriverCache(); isLoading.value = true; statusLog.value = "جاري البحث عن السائق..."; try { // الخطوة 1: استخراج الـ ID بناءً على رقم الهاتف var driver = driversCache.firstWhere( (d) => d['phone'].toString().contains(phoneInput), orElse: () => null, ); if (driver == null) { statusLog.value = "❌ لم يتم العثور على سائق بهذا الرقم في الكاش."; isLoading.value = false; return; } String driverId = driver['id'].toString(); String driverName = driver['name_arabic'] ?? 'بدون اسم'; statusLog.value = "✅ تم العثور على السائق: $driverName (ID: $driverId)\nجاري فحص رصيد الهدايا..."; // الخطوة 2: فحص السيرفر هل الهدية موجودة؟ // bool hasGift = await _checkIfGiftExistsOnServer(driverId); // if (hasGift) { // statusLog.value += // "\n⚠️ هذا السائق لديه هدية الافتتاح (30,000) مسبقاً. لم يتم اتخاذ إجراء."; // } else { // الخطوة 3: إضافة الهدية statusLog.value += "\n🎁 الهدية غير موجودة. جاري الإضافة..."; await _addGiftToDriver(driverId, phoneInput, "30000"); // } } catch (e) { statusLog.value = "حدث خطأ غير متوقع: $e"; } finally { isLoading.value = false; } } // دالة إضافة الهدية باستخدام WalletController الموجود عندك Future _addGiftToDriver( String driverId, String phone, String amount) async { final wallet = Get.put(WalletController()); // استخدام الدالة الموجودة في نظامك await wallet.addDrivergift3000('new driver', driverId, amount, phone); // statusLog.value += "\n✅ تمت إضافة مبلغ $amount ل.س بنجاح!"; // إضافة تنبيه مرئي // Get.snackbar("تم بنجاح", "تمت إضافة هدية الافتتاح للسائق", // backgroundColor: Colors.green, colorText: Colors.white); } } // --- View: واجهة المستخدم --- class DriverGiftCheckPage extends StatelessWidget { const DriverGiftCheckPage({super.key}); @override Widget build(BuildContext context) { // حقن الكنترولر final controller = Get.put(DriverGiftCheckerController()); return Scaffold( backgroundColor: const Color(0xFFF8FAFC), appBar: AppBar( title: const Text("فحص ومنح هدية الافتتاح", style: TextStyle(fontWeight: FontWeight.bold)), backgroundColor: const Color(0xFF0F172A), // نفس لون الهيدر السابق foregroundColor: Colors.white, ), body: Padding( padding: const EdgeInsets.all(20.0), child: Column( children: [ // كارد الإدخال Container( padding: const EdgeInsets.all(20), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(15), boxShadow: [ BoxShadow(color: Colors.grey.withOpacity(0.1), blurRadius: 10) ], ), child: Column( children: [ const Icon(Icons.card_giftcard, size: 50, color: Colors.amber), const SizedBox(height: 10), const Text( "أدخل رقم الهاتف للتحقق", style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold), ), const SizedBox(height: 20), // حقل الإدخال TextField( controller: controller.phoneController, keyboardType: TextInputType.phone, decoration: InputDecoration( hintText: 'مثال: 0912345678', prefixIcon: const Icon(Icons.phone), border: OutlineInputBorder( borderRadius: BorderRadius.circular(10)), filled: true, fillColor: Colors.grey[50], ), ), const SizedBox(height: 20), // زر التنفيذ Obx(() => SizedBox( width: double.infinity, height: 50, child: ElevatedButton( onPressed: controller.isLoading.value ? null : () => controller.processDriverGift(), style: ElevatedButton.styleFrom( backgroundColor: const Color(0xFF0F172A), foregroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10)), ), child: controller.isLoading.value ? const CircularProgressIndicator( color: Colors.white) : const Text("تحقق ومنح الهدية (30,000)", style: TextStyle(fontSize: 16)), ), )), ], ), ), const SizedBox(height: 30), // منطقة عرض النتائج (Log) Expanded( child: Container( width: double.infinity, padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.black87, borderRadius: BorderRadius.circular(12), ), child: SingleChildScrollView( child: Obx(() => Text( controller.statusLog.value.isEmpty ? "بانتظار العملية..." : controller.statusLog.value, style: const TextStyle( color: Colors.greenAccent, fontFamily: 'monospace', height: 1.5), )), ), ), ), ], ), ), ); } }