import 'package:sysfarma_mobile/src/app/widgets/app_panel.dart'; import 'package:sysfarma_mobile/src/core/network/api_client.dart'; import 'package:sysfarma_mobile/src/core/theme/app_tokens.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; class InventoryReportScreen extends ConsumerStatefulWidget { const InventoryReportScreen({super.key}); @override ConsumerState createState() => _InventoryReportScreenState(); } class _InventoryReportScreenState extends ConsumerState { bool _isLoading = true; Map _data = const {}; @override void initState() { super.initState(); _refresh(); } Future _refresh() async { if (mounted) setState(() => _isLoading = true); final response = await ref.read(apiClientProvider).inventoryReport(); if (!mounted) return; setState(() { _data = response.data ?? const {}; _isLoading = false; }); } String _fmtCurrency(Object? value) { if (value == null) return 'S/ 0.00'; final n = double.tryParse(value.toString()) ?? 0.0; return 'S/ ${n.toStringAsFixed(2)}'; } @override Widget build(BuildContext context) { final totals = Map.from(_data['totals'] as Map? ?? const {}); final items = List>.from(_data['items'] as List? ?? const []); return SafeArea( child: RefreshIndicator( onRefresh: _refresh, child: ListView( padding: AppTokens.pagePadding, children: [ if (_isLoading) const Padding( padding: EdgeInsets.symmetric(vertical: 32), child: Center(child: CircularProgressIndicator()), ) else ...[ AppPanel( title: 'Resumen consolidado', child: Column( children: [ _Line(label: 'Stock total', value: '${totals['stock'] ?? 0} u'), const Divider(height: 22), _Line(label: 'Costo', value: _fmtCurrency(totals['stock_cost_value'])), const Divider(height: 22), _Line(label: 'Valor de venta', value: _fmtCurrency(totals['stock_sale_value'])), const Divider(height: 22), _Line(label: 'Utilidad potencial', value: _fmtCurrency(totals['potential_profit'])), ], ), ), const SizedBox(height: 14), AppPanel( title: 'Top valorizados', child: Column( children: [ for (final item in items) ...[ _ItemRow(item: item, formatter: _fmtCurrency), if (item != items.last) const Divider(height: 16), ], ], ), ), ], ], ), ), ); } } class _Line extends StatelessWidget { const _Line({required this.label, required this.value}); final String label; final String value; @override Widget build(BuildContext context) { return Row( children: [ Expanded( child: Text( label, style: Theme.of(context).textTheme.bodyMedium?.copyWith(color: AppTokens.secondary), ), ), Text( value, style: Theme.of(context).textTheme.titleSmall?.copyWith(fontWeight: FontWeight.w700), ), ], ); } } class _ItemRow extends StatelessWidget { const _ItemRow({required this.item, required this.formatter}); final Map item; final String Function(Object?) formatter; @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.symmetric(vertical: 4), child: Row( children: [ Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( item['description']?.toString() ?? '-', maxLines: 1, overflow: TextOverflow.ellipsis, style: Theme.of(context).textTheme.titleSmall, ), const SizedBox(height: 2), Text( '${item['stock'] ?? 0} u', style: Theme.of(context).textTheme.labelSmall?.copyWith( color: AppTokens.secondary, ), ), ], ), ), Text( formatter(item['potential_profit']), style: Theme.of(context).textTheme.titleSmall?.copyWith( color: AppTokens.primary, fontWeight: FontWeight.w700, ), ), ], ), ); } }