import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../customAppBar/customAppBar.dart'; import '../customAppBar/customFooter.dart'; import '../customAppBar/toastHelper.dart'; import '../service/token_storage_service.dart'; import 'branch_card_widget.dart'; class BranchSelectionPage extends StatefulWidget { const BranchSelectionPage({Key? key}) : super(key: key); @override State createState() => _BranchSelectionPageState(); } class _BranchSelectionPageState extends State { List> branches = []; int? selectedIndex; final tokenStorage = TokenStorageService(); @override void initState() { super.initState(); _loadBranches(); } void _loadBranches() { setState(() { branches = tokenStorage.getCombinedBranches(); }); } void _selectBranch(int index) { setState(() { selectedIndex = index; }); } Future _handleNext() async { if (selectedIndex == null) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('Please select a branch'), backgroundColor: Colors.orange, ), ); return; } final selectedBranch = branches[selectedIndex!]; // Save selected branch and decode token await tokenStorage.saveSelectedBranch(selectedBranch); // Debug: Print decoded token final decodedToken = tokenStorage.getDecodedToken(); print('Selected Branch: $selectedBranch'); print('Decoded Token: $decodedToken'); // Navigate to home if (mounted) { final SharedPreferences prefs = await SharedPreferences.getInstance(); //Post Token Decode Details final empClientBranchId = decodedToken?['post_branch_id']; await prefs.setString('empClientBranchId', empClientBranchId); final empPrimaryId = decodedToken?['post_hr_id']; await prefs.setString('empPrimaryId', empPrimaryId); final empClientId = decodedToken?['post_client_id']; await prefs.setString('empClientId', empClientId); final empHrId = decodedToken?['post_hr_id']; await prefs.setString('empHrId', empHrId); dynamic allowedModules = decodedToken?['allowed_modules']; // If it's a String, decode it again if (allowedModules is String) { allowedModules = jsonDecode(allowedModules); } final empAllowedModules = allowedModules['post']; await prefs.setString('empAllowed_modules', jsonEncode(empAllowedModules)); // final empAllowed_modules = decodedToken?['allowed_modules'][0]['post']; // await prefs.setString('empAllowed_modules', jsonEncode(empAllowed_modules)); //Pre Token Decode Details final enrollmentEmpClientBranchId = decodedToken?['pre_branch_id']; await prefs.setString('enrollmentEmpClientBranchId', enrollmentEmpClientBranchId); final enrollmentEmpPrimaryId = decodedToken?['pre_hr_id']; await prefs.setString('enrollmentEmpPrimaryId', enrollmentEmpPrimaryId); final enrollmentClient_id = decodedToken?['pre_client_id']; await prefs.setString('enrollmentClient_id', enrollmentClient_id); final enrollmentHrId = decodedToken?['pre_hr_id']; await prefs.setString('enrollmentHrId', enrollmentHrId); final enrollmentAllowedModules = allowedModules['pre']; await prefs.setString('enrollmentAllowed_modules', jsonEncode(enrollmentAllowedModules)); // final enrollmentAllowed_modules = decodedToken?['allowed_modules'][1]['pre']; // await prefs.setString('enrollmentAllowed_modules', jsonEncode(enrollmentAllowed_modules)); final token = await tokenStorage.getCurrentToken(); print('tokenbranch - $token'); await prefs.setString('token', token!); ToastHelper.showSuccessToast(context, 'Successfully Login'); Navigator.pushReplacementNamed(context, 'hrDashboard'); } } @override Widget build(BuildContext context) { final screenHeight = MediaQuery.of(context).size.height; final screenWidth = MediaQuery.of(context).size.width; // Calculate dynamic horizontal padding (10% of screen width) final horizontalPadding = screenWidth * 0.1; // Calculate dynamic grid height double gridHeight = screenHeight * 0.45; if (gridHeight < 300) gridHeight = 300; if (gridHeight > 600) gridHeight = 600; // Determine crossAxisCount with multiple breakpoints int crossAxisCount; double childAspectRatio; if (screenWidth < 600) { // Mobile phones crossAxisCount = 2; childAspectRatio = 2.5; } else if (screenWidth < 900) { // Small tablets crossAxisCount = 2; childAspectRatio = 2.8; } else if (screenWidth < 1200) { // Large tablets crossAxisCount = 3; childAspectRatio = 3; } else { // Desktop crossAxisCount = 3; childAspectRatio = 4; } return Scaffold( backgroundColor: Color(0xFFEFF3F6), appBar: CustomAppBar(), body: Column( children: [ Expanded( child: SingleChildScrollView( child: Padding( padding: EdgeInsets.symmetric( horizontal: horizontalPadding , // Inner horizontal padding vertical: 20, // Inner vertical padding ), // Outer padding child: Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(12), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.05), blurRadius: 10, offset: Offset(0, 4), ), ], ), padding: const EdgeInsets.symmetric( horizontal: 24.0, // Inner horizontal padding vertical: 28.0, // Inner vertical padding ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( 'Select Client', style: TextStyle( fontSize: 24, fontWeight: FontWeight.bold, color: Colors.black, ), ), SizedBox(height: 25), Container( height: gridHeight, // Shows approximately 3 rows child: branches.isEmpty ? Center( child: Text( 'No branches available', style: TextStyle(fontSize: 16, color: Colors.grey), ), ) : GridView.builder( // Enable scrolling if content exceeds height physics: ClampingScrollPhysics(), padding: EdgeInsets.zero, gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: crossAxisCount, childAspectRatio: childAspectRatio, crossAxisSpacing: 15, mainAxisSpacing: 15, ), itemCount: branches.length, itemBuilder: (context, index) { final branch = branches[index]; return BranchCard( clientName: branch['client_name'] ?? 'Unknown Client', branchName: branch['branch_name'] ?? 'Unknown Branch', isSelected: selectedIndex == index, onTap: () => _selectBranch(index), ); }, ), ), SizedBox(height: 24), Center( child: SizedBox( width: 120, height: 48, child: ElevatedButton( onPressed: _handleNext, style: ElevatedButton.styleFrom( backgroundColor: Color(0xFFFF6B35), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), ), elevation: 0, ), child: Text( 'Next', style: TextStyle( fontSize: 16, fontWeight: FontWeight.w600, color: Colors.white, ), ), ), ), ), ], ), ), ), ), ), Container( width: double.infinity, child: CustomFooter(), ), ], ), ); } }