import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import '../../core/api/api_config.dart'; import '../../core/theme/app_theme.dart'; import '../../providers/auth_provider.dart'; class SettingsScreen extends ConsumerStatefulWidget { const SettingsScreen({super.key}); @override ConsumerState createState() => _SettingsScreenState(); } class _SettingsScreenState extends ConsumerState { final _urlController = TextEditingController(); @override void initState() { super.initState(); _urlController.text = ApiConfig.baseUrl; } @override void dispose() { _urlController.dispose(); super.dispose(); } Future _saveBaseUrl() async { final newUrl = _urlController.text.trim(); if (newUrl.isEmpty) return; ApiConfig.customBaseUrl = newUrl; final storage = ref.read(secureStorageProvider); await storage.saveCustomBaseUrl(newUrl); if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('API Server URL updated to: $newUrl'), backgroundColor: AppTheme.successColor, ), ); } } Future _resetBaseUrl() async { ApiConfig.resetToDefault(); final storage = ref.read(secureStorageProvider); await storage.saveCustomBaseUrl(''); setState(() { _urlController.text = ApiConfig.baseUrl; }); if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('API Server URL reset to default: ${ApiConfig.baseUrl}'), ), ); } } @override Widget build(BuildContext context) { final authState = ref.watch(authProvider); return Scaffold( appBar: AppBar( title: const Text('Settings & Configuration'), ), body: ListView( padding: const EdgeInsets.all(16), children: [ // Connection & API Server Card Card( child: Padding( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Row( children: [ Icon(Icons.dns_outlined, color: AppTheme.primaryColor), SizedBox(width: 8), Text( 'Backend Server Configuration', style: TextStyle( fontSize: 15, fontWeight: FontWeight.bold, color: AppTheme.textPrimary, ), ), ], ), const SizedBox(height: 8), const Text( 'Set the Odoo API Server address. Use your Mac IP (e.g. http://192.168.1.5:10008) when testing on a physical phone.', style: TextStyle(fontSize: 12, color: AppTheme.textSecondary), ), const SizedBox(height: 14), TextField( controller: _urlController, decoration: InputDecoration( labelText: 'API Base URL', hintText: 'http://localhost:10008', suffixIcon: IconButton( icon: const Icon(Icons.refresh), tooltip: 'Reset to default', onPressed: _resetBaseUrl, ), ), ), const SizedBox(height: 12), Row( children: [ ElevatedButton.icon( onPressed: _saveBaseUrl, icon: const Icon(Icons.save, size: 16), label: const Text('Apply & Save'), ), const SizedBox(width: 12), OutlinedButton( onPressed: () { // Quick preset: Android Emulator setState(() { _urlController.text = 'http://10.0.2.2:10008'; }); }, child: const Text('Android 10.0.2.2'), ), ], ), ], ), ), ), const SizedBox(height: 16), // User Session Info Card if (authState.user != null) Card( child: Padding( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Row( children: [ Icon(Icons.account_circle_outlined, color: AppTheme.primaryColor), SizedBox(width: 8), Text( 'Active Session', style: TextStyle( fontSize: 15, fontWeight: FontWeight.bold, color: AppTheme.textPrimary, ), ), ], ), const Divider(), ListTile( dense: true, contentPadding: EdgeInsets.zero, title: const Text('Logged in user'), subtitle: Text( '${authState.user!.name} (${authState.user!.email})', style: const TextStyle(fontWeight: FontWeight.bold), ), ), if (authState.student != null) ListTile( dense: true, contentPadding: EdgeInsets.zero, title: const Text('Linked Student Profile'), subtitle: Text( '${authState.student!.name} (ID: ${authState.student!.id})', style: const TextStyle(fontWeight: FontWeight.bold), ), ), const SizedBox(height: 12), SizedBox( width: double.infinity, child: ElevatedButton.icon( onPressed: () async { await ref.read(authProvider.notifier).logout(); if (context.mounted) { context.go('/login'); } }, icon: const Icon(Icons.logout), label: const Text('Sign Out'), style: ElevatedButton.styleFrom( backgroundColor: AppTheme.errorColor, ), ), ), ], ), ), ), const SizedBox(height: 16), // Application Info Card const Card( child: Padding( padding: EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( 'Application Info', style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold), ), SizedBox(height: 8), Text('Metatron Education ERP Mobile', style: TextStyle(fontSize: 13, color: AppTheme.textSecondary)), Text('Version 1.0.0+1 (Phase 4 Flutter Build)', style: TextStyle(fontSize: 12, color: AppTheme.textMuted)), SizedBox(height: 4), Text('Backend: Odoo 19 + education_mobile_api', style: TextStyle(fontSize: 12, color: AppTheme.textMuted)), ], ), ), ), ], ), ); } }