import 'package:flutter/material.dart'; import 'package:url_launcher/url_launcher.dart'; import '../services/odoo_service.dart'; import '../theme/app_theme.dart'; class BranchesScreen extends StatefulWidget { const BranchesScreen({super.key}); @override State createState() => _BranchesScreenState(); } class _BranchesScreenState extends State { List _branches = []; bool _isLoading = true; @override void initState() { super.initState(); _fetchBranches(); } Future _fetchBranches() async { try { final branches = await OdooService.getPublicBranches(); if (mounted) { setState(() { _branches = branches; _isLoading = false; }); } } catch (e) { if (mounted) { setState(() => _isLoading = false); ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Error loading branches. Check connection.'))); } } } Future _launchMaps(String queryTerm) async { final query = Uri.encodeComponent(queryTerm); final url = Uri.parse('https://www.google.com/maps/search/?api=1&query=$query'); if (await canLaunchUrl(url)) { await launchUrl(url, mode: LaunchMode.externalApplication); } else { if (mounted) ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Could not open map.'))); } } Future _launchWhatsApp(String phone) async { final cleanPhone = phone.replaceAll(RegExp(r'\D'), ''); String waPhone = cleanPhone; if (waPhone.startsWith('0')) { waPhone = '62${waPhone.substring(1)}'; } final url = Uri.parse('https://wa.me/$waPhone'); if (await canLaunchUrl(url)) { await launchUrl(url, mode: LaunchMode.externalApplication); } else { if (mounted) ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Could not open WhatsApp.'))); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Find Branch')), body: _isLoading ? const Center(child: CircularProgressIndicator()) : _branches.isEmpty ? const Center(child: Text('No branches available.', style: TextStyle(fontSize: 16))) : ListView.builder( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16), itemCount: _branches.length, itemBuilder: (context, index) { final branch = _branches[index]; final street = branch['street'] != null && branch['street'] != false ? branch['street'] : ''; final city = branch['city'] != null && branch['city'] != false ? branch['city'] : ''; final phone = branch['phone'] != null && branch['phone'] != false ? branch['phone'] : ''; final addressParts = [street, city].where((e) => e.toString().isNotEmpty).join(', '); return Container( margin: const EdgeInsets.only(bottom: 16), decoration: BoxDecoration( color: AppTheme.surfaceContainerLow, borderRadius: BorderRadius.circular(16), // Spec rules: "Don't use standard drop shadows" ), child: ListTile( contentPadding: const EdgeInsets.all(16), onTap: () => _launchMaps('${branch['name']} ${addressParts}'), leading: Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: AppTheme.secondaryContainer.withOpacity(0.2), shape: BoxShape.circle, ), child: const Icon(Icons.storefront, color: AppTheme.secondary), ), title: Text( branch['name'] ?? 'Mapan Branch', style: Theme.of(context).textTheme.titleMedium, ), subtitle: Padding( padding: const EdgeInsets.only(top: 8.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( addressParts.isEmpty ? 'No address specified' : addressParts, style: Theme.of(context).textTheme.bodyMedium, ), if (phone.isNotEmpty) ...[ const SizedBox(height: 4), Row( children: [ const Icon(Icons.phone, size: 14, color: AppTheme.primary), const SizedBox(width: 4), Text(phone, style: Theme.of(context).textTheme.bodySmall?.copyWith(color: AppTheme.primary)), ], ), ] ], ), ), trailing: phone.isNotEmpty ? IconButton( icon: const Icon(Icons.chat_bubble_outline, color: Colors.green), onPressed: () => _launchWhatsApp(phone), tooltip: 'Chat on WhatsApp', ) : const Icon(Icons.chevron_right, color: AppTheme.onSurfaceVariant), ), ); }, ), ); } }