import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:http/http.dart' as http; import '../../constant/links.dart'; import '../../controller/admin/get_all_invoice_controller.dart'; import '../../controller/functions/crud.dart'; import '../../print.dart'; import 'add_invoice_page.dart'; class InvoiceListPage extends StatefulWidget { @override _InvoiceListPageState createState() => _InvoiceListPageState(); } class _InvoiceListPageState extends State { List invoices = []; int totalCount = 0; double totalAmount = 0.0; bool isLoading = true; @override void initState() { super.initState(); fetchInvoices(); } Future fetchInvoices() async { // لإظهار مؤشر التحديث بشكل جيد if (!isLoading) { setState(() {}); } final response = await CRUD().post(link: AppLink.getInvoices, payload: {}); final data = (response); Log.print('data: $data'); if (mounted) { if (data != 'failure' && data['status'] == 'success') { setState(() { invoices = List.from(data['data']) .map((item) => InvoiceModel.fromJson(item)) .toList(); totalCount = data['summary']['count']; totalAmount = double.tryParse(data['summary']['total'].toString()) ?? 0.0; isLoading = false; }); } else { setState(() { isLoading = false; }); Get.snackbar("خطأ", "فشل في تحميل الفواتير. حاول التحديث مرة أخرى.", backgroundColor: Colors.red.withOpacity(0.8), colorText: Colors.white); } } } // --- دالة لعرض الصورة في نافذة منبثقة --- void _showImageDialog(BuildContext context, String imageUrl) { showDialog( context: context, builder: (BuildContext context) { return Dialog( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12.0), ), child: GestureDetector( // لإغلاق الصورة عند الضغط عليها onTap: () => Navigator.of(context).pop(), child: Container( padding: EdgeInsets.all(12), child: InteractiveViewer( // لإتاحة التكبير والتصغير panEnabled: true, minScale: 0.5, maxScale: 4, child: Image.network( imageUrl, fit: BoxFit.contain, // إظهار مؤشر تحميل أثناء جلب الصورة loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent? loadingProgress) { if (loadingProgress == null) return child; return Center( child: CircularProgressIndicator( value: loadingProgress.expectedTotalBytes != null ? loadingProgress.cumulativeBytesLoaded / loadingProgress.expectedTotalBytes! : null, ), ); }, // إظهار أيقونة خطأ في حال فشل تحميل الصورة errorBuilder: (context, error, stackTrace) { return Icon(Icons.broken_image, size: 100, color: Colors.red); }, ), ), ), ), ); }, ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("قائمة الفواتير"), centerTitle: true, elevation: 2, actions: [ IconButton( icon: Icon(Icons.add_a_photo), onPressed: () { // يمكنك إضافة إجراء الطباعة هنا Get.to(() => AddInvoicePage()); }, ), ], ), body: isLoading ? Center(child: CircularProgressIndicator()) : RefreshIndicator( onRefresh: fetchInvoices, // خاصية السحب للتحديث child: Column( children: [ Expanded( child: ListView.builder( padding: EdgeInsets.symmetric(vertical: 8.0, horizontal: 12.0), itemCount: invoices.length, itemBuilder: (context, index) { final invoice = invoices[index]; return Card( elevation: 4, margin: EdgeInsets.symmetric(vertical: 8.0), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(15.0), ), child: InkWell( borderRadius: BorderRadius.circular(15.0), onTap: () { // التحقق من وجود رابط للصورة قبل محاولة عرضه if (invoice.imageLink != null && invoice.imageLink!.isNotEmpty) { _showImageDialog(context, invoice.imageLink!); } else { Get.snackbar("لا توجد صورة", "هذه الفاتورة لا تحتوي على صورة مرفقة."); } }, child: Padding( padding: const EdgeInsets.all(16.0), child: Row( children: [ // أيقونة الفاتورة الرئيسية Icon(Icons.receipt_long, color: Theme.of(context).primaryColor, size: 40), SizedBox(width: 16), // تفاصيل الفاتورة Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "فاتورة رقم: ${invoice.invoiceNumber}", style: TextStyle( fontWeight: FontWeight.bold, fontSize: 16, ), ), SizedBox(height: 8), Text( "الاسم: ${invoice.name}", style: TextStyle( color: Colors.green.shade700, fontWeight: FontWeight.w500, ), ), SizedBox(height: 8), Text( "المبلغ: ${invoice.amount} د.أ", style: TextStyle( color: Colors.green.shade700, fontWeight: FontWeight.w500, ), ), SizedBox(height: 4), Text( "التاريخ: ${invoice.date}", style: TextStyle( color: Colors.grey.shade600, fontSize: 12, ), ), ], ), ), // أيقونة توضح وجود صورة if (invoice.imageLink != null && invoice.imageLink!.isNotEmpty) Icon(Icons.image_outlined, color: Colors.blueAccent, size: 30), ], ), ), ), ); }, ), ), _buildSummaryCard(), // بطاقة الملخص السفلية ], ), ), ); } Widget _buildSummaryCard() { return Card( margin: EdgeInsets.all(0), elevation: 8, shape: RoundedRectangleBorder( borderRadius: BorderRadius.only( topLeft: Radius.circular(20), topRight: Radius.circular(20), ), ), child: Container( padding: EdgeInsets.symmetric(vertical: 20, horizontal: 25), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "إجمالي الفواتير", style: TextStyle(color: Colors.grey.shade600, fontSize: 14), ), Text( "$totalCount", style: TextStyle( fontWeight: FontWeight.bold, fontSize: 20, ), ), ], ), Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ Text( "المبلغ الإجمالي", style: TextStyle(color: Colors.grey.shade600, fontSize: 14), ), Text( "${totalAmount.toStringAsFixed(2)} د.أ", style: TextStyle( fontWeight: FontWeight.bold, fontSize: 20, color: Colors.green.shade800, ), ), ], ), ], ), ), ); } }