import 'package:flutter/material.dart'; import '../../domain/project.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:lucide_icons_flutter/lucide_icons.dart'; import '../../../sales/presentation/add_customer_sheet.dart'; import '../../providers/project_provider.dart'; import '../../../sales/providers/customers_provider.dart'; import '../../../../core/widgets/premium_text_field.dart'; class AddProjectSheet extends ConsumerStatefulWidget { final Project? project; const AddProjectSheet({super.key, this.project}); @override ConsumerState createState() => _AddProjectSheetState(); } class _AddProjectSheetState extends ConsumerState { final _formKey = GlobalKey(); String _name = ''; String _description = ''; double _budget = 0; int? _selectedCustomerId; bool _isLoading = false; @override void initState() { super.initState(); if (widget.project != null) { _name = widget.project!.name; _description = widget.project!.description ?? ''; _budget = widget.project!.budget ?? 0; _selectedCustomerId = widget.project!.customerId; } } @override Widget build(BuildContext context) { final customersState = ref.watch(customersProvider); return Container( padding: EdgeInsets.only( bottom: MediaQuery.of(context).viewInsets.bottom, left: 24, right: 24, top: 24, ), decoration: const BoxDecoration( color: Colors.white, borderRadius: BorderRadius.vertical(top: Radius.circular(24)), ), child: SafeArea( child: Form( key: _formKey, child: SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text(widget.project == null ? 'New Project' : 'Edit Project', style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold)), IconButton(icon: const Icon(LucideIcons.x), onPressed: () => Navigator.pop(context)), ], ), const SizedBox(height: 16), PremiumTextField( labelText: 'Project Name', initialValue: _name, prefixIcon: const Icon(LucideIcons.folder), validator: (val) => val == null || val.isEmpty ? 'Required' : null, onSaved: (val) => _name = val!, ), const SizedBox(height: 16), PremiumTextField( maxLines: 3, labelText: 'Description (Optional)', initialValue: _description, prefixIcon: const Icon(LucideIcons.alignLeft), onSaved: (val) => _description = val ?? '', ), const SizedBox(height: 16), if (customersState.hasValue) Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: DropdownButtonFormField( value: _selectedCustomerId, decoration: InputDecoration( labelText: 'Client (Optional)', prefixIcon: const Icon(LucideIcons.users), border: OutlineInputBorder(borderRadius: BorderRadius.circular(12), borderSide: BorderSide.none), filled: true, fillColor: Theme.of(context).colorScheme.primary.withValues(alpha: 0.05), contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), ), items: [ const DropdownMenuItem(value: null, child: Text('None')), ...customersState.value!.map((c) => DropdownMenuItem( value: c.id, child: Text(c.name), )), ], onChanged: (val) { setState(() { _selectedCustomerId = val; }); }, ), ), const SizedBox(width: 8), Container( height: 52, // Match the height of the dropdown approximately decoration: BoxDecoration( color: Theme.of(context).colorScheme.primary.withOpacity(0.1), borderRadius: BorderRadius.circular(12), ), child: IconButton( icon: const Icon(LucideIcons.plus), color: Theme.of(context).colorScheme.primary, tooltip: 'Add New Client', onPressed: () { showModalBottomSheet( context: context, isScrollControlled: true, backgroundColor: Colors.transparent, builder: (context) => const AddCustomerSheet(), ); }, ), ), ], ), const SizedBox(height: 16), PremiumTextField( keyboardType: const TextInputType.numberWithOptions(decimal: true), labelText: 'Budget (Optional)', initialValue: _budget > 0 ? _budget.toString() : '', prefixIcon: const Icon(LucideIcons.indianRupee), onSaved: (val) => _budget = double.tryParse(val ?? '0') ?? 0, ), const SizedBox(height: 24), ElevatedButton( style: ElevatedButton.styleFrom( padding: const EdgeInsets.symmetric(vertical: 16), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), ), onPressed: _isLoading ? null : _submit, child: _isLoading ? const CircularProgressIndicator(color: Colors.white) : Text(widget.project == null ? 'Create Project' : 'Save Changes', style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold)), ), const SizedBox(height: 24), ], ), ), ), ), ); } Future _submit() async { if (!_formKey.currentState!.validate()) return; _formKey.currentState!.save(); setState(() => _isLoading = true); try { final repo = ref.read(projectRepositoryProvider); if (widget.project != null) { await repo.updateProject( projectId: widget.project!.id, name: _name, description: _description.isNotEmpty ? _description : null, customerId: _selectedCustomerId, budget: _budget, status: widget.project!.status, ); } else { await repo.createProject( name: _name, description: _description.isNotEmpty ? _description : null, customerId: _selectedCustomerId, budget: _budget, ); } ref.invalidate(projectsProvider); if (mounted) Navigator.pop(context); } catch (e) { ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Error: $e'))); } finally { if (mounted) setState(() => _isLoading = false); } } }