import 'package:flutter/material.dart'; import 'package:gap/gap.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:intl/intl.dart'; import 'package:pharmacy_mobile/tables/stocks.dart'; import 'package:pharmacy_mobile/tables/storage.dart'; import 'package:pharmacy_mobile/widgets/datatable_widget.dart'; import 'package:pharmacy_mobile/widgets/page_background_widget.dart'; import 'package:pharmacy_mobile/widgets/text_widget.dart'; import 'package:pharmacy_mobile/widgets/title_widget.dart'; class ListStocksPage extends StatefulWidget { const ListStocksPage({super.key}); @override State createState() => _ListStocksPageState(); } class _ListStocksPageState extends State { final _stocks = Stocks(); final _storage = Storage(); late List _stockList = []; bool _isLoading = false; List _createRows() { final today = DateTime.now().toUtc(); return _stockList.map((item) { final dateString = item['expiration_date']; final date = DateTime.parse(dateString).toUtc(); final formattedDate = DateFormat('MMMM d, yyyy').format(date); if (date.isBefore(today)) { return DataRow( cells: [ DataCell(Text(item['medicine_name'], style: const TextStyle(color: Color.fromRGBO(188, 59, 50, 1)))), DataCell(Text(item['quantity'].toString(), style: const TextStyle(color: Color.fromRGBO(188, 59, 50, 1)))), DataCell( Text(formattedDate, style: const TextStyle(color: Color.fromRGBO(188, 59, 50, 1))), // Highlight expired items ), ], ); } else { return DataRow( cells: [ DataCell(Text(item['medicine_name'])), DataCell(Text(item['quantity'].toString())), DataCell(Text(formattedDate)), ], ); } }).toList(); } List _createColumns() { return [ const DataColumn(label: Text('Medicine')), const DataColumn(label: Text('Quantity')), const DataColumn(label: Text('Expiration')) ]; } void autoRun() async { setState(() { _isLoading = true; }); _stockList = await _stocks.getList(); setState(() { _isLoading = false; }); } @override void initState() { autoRun(); super.initState(); } @override void dispose() { _stockList = []; super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( body: PageBackgroundWidget( child: Column( children: [ const Gap(96), const TitleWidget( firstTextSize: 14, secondTextSize: 24, logoSize: 90, ), const Gap(32), const TextWidget(text: 'List of Stocks'), const Gap(16), if (_isLoading) const Center( child: CircularProgressIndicator( color: Color.fromRGBO(255, 255, 255, 1), )) else if (_stockList.isEmpty) Container( decoration: BoxDecoration( border: Border.all(color: const Color.fromRGBO(205, 59, 208, 0.702), width: 2), borderRadius: BorderRadius.circular(12), ), padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 16.0), child: Text( 'No Stock Listed', style: GoogleFonts.outfit( color: const Color.fromRGBO(255, 255, 255, 1), fontSize: 14, fontWeight: FontWeight.normal, ), ), ) else DataTableWidget( column: _createColumns(), row: _createRows(), ), ], ), ), ); } }