import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:lucide_icons/lucide_icons.dart'; import '../../providers/business_provider.dart'; class BusinessSettingsScreen extends ConsumerStatefulWidget { const BusinessSettingsScreen({super.key}); @override ConsumerState createState() => _BusinessSettingsScreenState(); } class _BusinessSettingsScreenState extends ConsumerState { bool _taxIncludedInPrice = false; bool _salesEnabled = true; bool _inventoryEnabled = true; @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) { final profile = ref.read(businessProfileProvider).value; if (profile != null) { setState(() { _taxIncludedInPrice = profile.taxIncludedInPrice ?? false; }); } final feature = ref.read(businessFeatureProvider).value; if (feature != null) { setState(() { _inventoryEnabled = feature.inventoryManagement; _salesEnabled = feature.salesManagement; }); } }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Business Settings'), elevation: 0, backgroundColor: Colors.transparent, ), body: ListView( padding: const EdgeInsets.all(24.0), children: [ const Text('Module Configuration', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.grey)), const SizedBox(height: 16), SwitchListTile( title: const Text('Inventory Management'), subtitle: const Text('Track stock levels, multiple locations, and products'), value: _inventoryEnabled, onChanged: (val) { setState(() => _inventoryEnabled = val); _saveFeatures(); }, secondary: const Icon(LucideIcons.package), ), SwitchListTile( title: const Text('Sales & POS'), subtitle: const Text('Enable point of sale, invoicing, and receivables'), value: _salesEnabled, onChanged: (val) { setState(() => _salesEnabled = val); _saveFeatures(); }, secondary: const Icon(LucideIcons.shoppingCart), ), const SizedBox(height: 32), const Text('Inventory Strategy', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.grey)), const SizedBox(height: 16), Consumer( builder: (context, ref, child) { final featureState = ref.watch(businessFeatureProvider); final currentStrategy = featureState.value?.bomReductionStrategy ?? 'COMPONENTS_ONLY'; final deductParentStock = currentStrategy == 'PARENT_AND_COMPONENTS'; final stockDeductionOnInvoice = featureState.value?.stockDeductionOnInvoice ?? true; return Column( children: [ SwitchListTile( title: const Text('Deduct Parent Stock on BOM Sale'), subtitle: const Text('If enabled, selling an assembled product reduces both parent and component stock.'), value: deductParentStock, secondary: const Icon(LucideIcons.gitMerge), onChanged: (val) { if (featureState.value != null) { final newStrategy = val ? 'PARENT_AND_COMPONENTS' : 'COMPONENTS_ONLY'; final updated = featureState.value!.copyWith(bomReductionStrategy: newStrategy); ref.read(businessFeatureProvider.notifier).updateFeatures(updated); } }, ), SwitchListTile( title: const Text('Stock Deduction on Invoice'), subtitle: const Text('If enabled, finalizing an invoice automatically deducts product stock. Turn off for a two-step fulfillment process.'), value: stockDeductionOnInvoice, secondary: const Icon(LucideIcons.boxes), onChanged: (val) { if (featureState.value != null) { final updated = featureState.value!.copyWith(stockDeductionOnInvoice: val); ref.read(businessFeatureProvider.notifier).updateFeatures(updated); } }, ), SwitchListTile( title: const Text('Use EAN/Barcode for Scanner'), subtitle: const Text('If disabled, the barcode scanner will search using the SKU field instead.'), value: (featureState.value?.barcodeSource ?? 'SKU') == 'BARCODE', secondary: const Icon(LucideIcons.scanLine), onChanged: (val) { if (featureState.value != null) { final updated = featureState.value!.copyWith(barcodeSource: val ? 'BARCODE' : 'SKU'); ref.read(businessFeatureProvider.notifier).updateFeatures(updated); } }, ), ], ); } ), const SizedBox(height: 32), const Text('Pricing & Taxation', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.grey)), const SizedBox(height: 16), SwitchListTile( title: const Text('Selling Price includes Tax'), subtitle: const Text('If enabled, GST is assumed to be inclusive in the entered selling price.'), value: _taxIncludedInPrice, onChanged: (val) { setState(() => _taxIncludedInPrice = val); _saveSettings(); }, secondary: const Icon(LucideIcons.receipt), ), ], ), ); } void _saveSettings() { final profileState = ref.read(businessProfileProvider).value; if (profileState != null) { final updated = profileState.copyWith(taxIncludedInPrice: _taxIncludedInPrice); ref.read(businessProfileProvider.notifier).updateProfile(updated); } } void _saveFeatures() { final featureState = ref.read(businessFeatureProvider).value; if (featureState != null) { final updated = featureState.copyWith( inventoryManagement: _inventoryEnabled, salesManagement: _salesEnabled, ); ref.read(businessFeatureProvider.notifier).updateFeatures(updated); } } }