B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Activity
Version=13.4
@EndOfDesignText@

' RouteSelectionFunctions - Helper module for route selection logic
' This module contains the API communication logic for the Route system.

Sub Process_Globals
	' Keep track of the active route name for display purposes
	Public ActiveRouteName As String = ""
	Public ActiveRouteID As Int = 0
End Sub

Sub Globals
	' Not used in Static Code Modules, but kept for compatibility
End Sub

' Fetch available routes from server for the driver's bus
Public Sub LoadRoutes(TargetActivity As Object)
	Dim job As HttpJob
	job.Initialize("get_routes", TargetActivity)
	job.Download(APIModule.ServerURL & "/get_routes.php")
	APIModule.SetAuthHeader(job, APIModule.GetToken)
End Sub

' Set the active route ID on the server for the current driver session
Public Sub SetActiveRoute(TargetActivity As Object, RouteID As Int, RouteName As String)
	' Store locally for easy access
	ActiveRouteID = RouteID
	ActiveRouteName = RouteName
    
	Dim payload As Map
	payload.Initialize
	payload.Put("route_id", RouteID)
    
	Dim gen As JSONGenerator
	gen.Initialize(payload)
    
	Dim job As HttpJob
	job.Initialize("set_active_route", TargetActivity)
	job.PostString(APIModule.ServerURL & "/set_active_route.php", gen.ToString)
	APIModule.SetAuthHeader(job, APIModule.GetToken)
End Sub

' Helper to parse the routes list and populate a ListView
' Call this from Main.bas JobDone handler
Public Sub PopulateRoutesList(Root As Map, lv As ListView)
	lv.Clear
	If Root.ContainsKey("routes") Then
		Dim routes As List = Root.Get("routes")
        
		For Each colRoute As Map In routes
			Dim rName As String = colRoute.Get("route_name")
			Dim shift As String = colRoute.Get("shift")
			Dim studentCount As Int = colRoute.Get("student_count")
            
			Dim secondLine As String = shift.ToUpperCase & " • " & studentCount & " students"
			' AddTwoLines2 allows storing the Map object for retrieving the ID on click
			lv.AddTwoLines2(rName, secondLine, colRoute)
		Next
        
		If routes.Size = 0 Then
			Log("RouteSelection: No routes found for this bus.")
		End If
	End If
End Sub
