import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import '../cubit/dashboard_cubit.dart'; import '../cubit/dashboard_state.dart'; class AddContactScreen extends StatefulWidget { const AddContactScreen({super.key}); @override State createState() => _AddContactScreenState(); } class _AddContactScreenState extends State { // English: Form key to execute field validations. // Arabic: مفتاح النموذج لتنفيذ عمليات التحقق من صحة الحقول. final _formKey = GlobalKey(); final _nameController = TextEditingController(); final _phoneController = TextEditingController(); @override void dispose() { _nameController.dispose(); _phoneController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: const Color(0xFF0F0C20), appBar: AppBar( backgroundColor: const Color(0xFF15102A), elevation: 0, title: const Text( 'إضافة جهة اتصال جديدة', style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold), ), ), body: BlocConsumer( listener: (context, state) { if (state.errorMessage != null) { // English: Show API failure messages as SnackBar alert. // Arabic: عرض رسائل فشل واجهة برمجة التطبيقات كشريط تنبيه. ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( state.errorMessage!, style: const TextStyle(color: Colors.white), ), backgroundColor: Colors.redAccent, ), ); } }, builder: (context, state) { return SingleChildScrollView( padding: const EdgeInsets.all(24.0), child: Form( key: _formKey, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text( 'تفاصيل جهة الاتصال', style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold), ), const SizedBox(height: 20), // English: Input field for contact name. // Arabic: حقل إدخال اسم جهة الاتصال. TextFormField( controller: _nameController, style: const TextStyle(color: Colors.white), decoration: InputDecoration( labelText: 'الاسم الكامل', labelStyle: TextStyle(color: Colors.white.withOpacity(0.6)), prefixIcon: const Icon(Icons.person_outline, color: Colors.purpleAccent), filled: true, fillColor: Colors.white.withOpacity(0.05), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: BorderSide(color: Colors.white.withOpacity(0.1)), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide(color: Colors.purpleAccent, width: 2), ), ), validator: (value) { if (value == null || value.trim().isEmpty) { return 'الرجاء إدخال اسم جهة الاتصال'; } return null; }, ), const SizedBox(height: 20), // English: Input field for phone number. // Arabic: حقل إدخال رقم الهاتف. TextFormField( controller: _phoneController, keyboardType: TextInputType.phone, style: const TextStyle(color: Colors.white), decoration: InputDecoration( labelText: 'رقم الهاتف (مع رمز الدولة)', labelStyle: TextStyle(color: Colors.white.withOpacity(0.6)), prefixIcon: const Icon(Icons.phone_outlined, color: Colors.purpleAccent), filled: true, fillColor: Colors.white.withOpacity(0.05), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: BorderSide(color: Colors.white.withOpacity(0.1)), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide(color: Colors.purpleAccent, width: 2), ), ), validator: (value) { if (value == null || value.trim().isEmpty) { return 'الرجاء إدخال رقم الهاتف'; } return null; }, ), const SizedBox(height: 40), // English: Save button. Shows progress bar or calls Cubit and Pops on success. // Arabic: زر الحفظ. يعرض شريط تقدم التحميل أو يستدعي الكيوبيت ويغلق الصفحة عند النجاح. if (state.isLoading) const Center(child: CircularProgressIndicator(color: Colors.purpleAccent)) else ElevatedButton( style: ElevatedButton.styleFrom( backgroundColor: Colors.purpleAccent, minimumSize: const Size(double.infinity, 56), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), ), onPressed: () async { if (_formKey.currentState!.validate()) { // English: Dispatch addContact action to DashboardCubit. // Arabic: استدعاء إجراء إضافة جهة اتصال في الكيوبيت. final success = await context.read().addContact( _nameController.text.trim(), _phoneController.text.trim(), ); if (success && mounted) { // English: Use Navigator.pop to return to previous contacts screen. // Arabic: استخدام ميزة الموجه لإغلاق الشاشة والرجوع لقائمة جهات الاتصال. Navigator.pop(context); } } }, child: const Text( 'حفظ جهة الاتصال', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.white), ), ), ], ), ), ); }, ), ); } }