import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../../models/supplement.dart'; import '../../providers/supplement_provider.dart'; /// Shows the "Take supplement" dialog. /// - If [hideTime] is true, the time selection UI is hidden and intake is recorded as "now". Future showTakeSupplementDialog( BuildContext context, Supplement supplement, { bool hideTime = false, }) async { final unitsController = TextEditingController(text: supplement.numberOfUnits.toString()); final notesController = TextEditingController(); DateTime selectedDateTime = DateTime.now(); bool useCustomTime = false; await showDialog( context: context, builder: (context) => StatefulBuilder( builder: (context, setState) { return AlertDialog( title: Text('Take ${supplement.name}'), content: Column( mainAxisSize: MainAxisSize.min, children: [ Row( children: [ Expanded( child: TextField( controller: unitsController, keyboardType: const TextInputType.numberWithOptions(decimal: true), decoration: InputDecoration( labelText: 'Number of ${supplement.unitType}', border: const OutlineInputBorder(), suffixText: supplement.unitType, ), onChanged: (value) => setState(() {}), ), ), ], ), const SizedBox(height: 8), Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( color: Theme.of(context).colorScheme.surfaceVariant, borderRadius: BorderRadius.circular(4), ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( 'Total dosage:', style: TextStyle( fontSize: 12, color: Theme.of(context).colorScheme.onSurfaceVariant, ), ), Text( supplement.ingredientsDisplay, style: TextStyle( fontSize: 12, fontWeight: FontWeight.w600, color: Theme.of(context).colorScheme.onSurface, ), ), ], ), ), const SizedBox(height: 16), if (!hideTime) ...[ // Time selection section Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: Theme.of(context).colorScheme.primaryContainer.withOpacity(0.3), borderRadius: BorderRadius.circular(8), border: Border.all( color: Theme.of(context).colorScheme.primary.withOpacity(0.3), ), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Icon( Icons.access_time, size: 16, color: Theme.of(context).colorScheme.primary, ), const SizedBox(width: 6), Text( 'When did you take it?', style: TextStyle( fontSize: 12, fontWeight: FontWeight.w600, color: Theme.of(context).colorScheme.primary, ), ), ], ), const SizedBox(height: 8), Row( children: [ Expanded( child: RadioListTile( dense: true, contentPadding: EdgeInsets.zero, title: const Text('Just now', style: TextStyle(fontSize: 12)), value: false, groupValue: useCustomTime, onChanged: (value) => setState(() => useCustomTime = value!), ), ), Expanded( child: RadioListTile( dense: true, contentPadding: EdgeInsets.zero, title: const Text('Custom time', style: TextStyle(fontSize: 12)), value: true, groupValue: useCustomTime, onChanged: (value) => setState(() => useCustomTime = value!), ), ), ], ), if (useCustomTime) ...[ const SizedBox(height: 8), Container( width: double.infinity, padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), decoration: BoxDecoration( color: Theme.of(context).colorScheme.surface, borderRadius: BorderRadius.circular(6), border: Border.all( color: Theme.of(context).colorScheme.outline.withOpacity(0.5), ), ), child: Column( children: [ // Date picker Row( children: [ Icon( Icons.calendar_today, size: 14, color: Theme.of(context).colorScheme.onSurfaceVariant, ), const SizedBox(width: 8), Expanded( child: Text( 'Date: ${selectedDateTime.day}/${selectedDateTime.month}/${selectedDateTime.year}', style: const TextStyle(fontSize: 12), ), ), TextButton( onPressed: () async { final date = await showDatePicker( context: context, initialDate: selectedDateTime, firstDate: DateTime.now().subtract(const Duration(days: 7)), lastDate: DateTime.now(), ); if (date != null) { setState(() { selectedDateTime = DateTime( date.year, date.month, date.day, selectedDateTime.hour, selectedDateTime.minute, ); }); } }, child: const Text('Change', style: TextStyle(fontSize: 10)), ), ], ), const SizedBox(height: 4), // Time picker Row( children: [ Icon( Icons.access_time, size: 14, color: Theme.of(context).colorScheme.onSurfaceVariant, ), const SizedBox(width: 8), Expanded( child: Text( 'Time: ${selectedDateTime.hour.toString().padLeft(2, '0')}:${selectedDateTime.minute.toString().padLeft(2, '0')}', style: const TextStyle(fontSize: 12), ), ), TextButton( onPressed: () async { final time = await showTimePicker( context: context, initialTime: TimeOfDay.fromDateTime(selectedDateTime), ); if (time != null) { setState(() { selectedDateTime = DateTime( selectedDateTime.year, selectedDateTime.month, selectedDateTime.day, time.hour, time.minute, ); }); } }, child: const Text('Change', style: TextStyle(fontSize: 10)), ), ], ), ], ), ), ], ], ), ), const SizedBox(height: 16), ], TextField( controller: notesController, decoration: const InputDecoration( labelText: 'Notes (optional)', border: OutlineInputBorder(), ), maxLines: 2, ), ], ), actions: [ TextButton( onPressed: () => Navigator.of(context).pop(), child: const Text('Cancel'), ), ElevatedButton( onPressed: () { final unitsTaken = double.tryParse(unitsController.text) ?? supplement.numberOfUnits.toDouble(); // For now, we'll record 0 as total dosage since we're transitioning to ingredients // This will be properly implemented when we add the full ingredient tracking final totalDosageTaken = 0.0; context.read().recordIntake( supplement.id!, totalDosageTaken, unitsTaken: unitsTaken, notes: notesController.text.isNotEmpty ? notesController.text : null, takenAt: hideTime ? null : (useCustomTime ? selectedDateTime : null), ); Navigator.of(context).pop(); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('${supplement.name} recorded!'), backgroundColor: Colors.green, ), ); }, child: const Text('Record'), ), ], ); }, ), ); }