B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=11.5
@EndOfDesignText@
Sub Class_Globals
	Private Root As B4XView 'ignore
	Private xui As XUI 'ignore
	
	Dim GPS As GPS
	Dim rp As RuntimePermissions
	Dim ruta As String
	Dim skmt As SQL
	Dim c As Cursor
	Dim c2 As Cursor
	Dim c22 As Cursor
	Dim c3 As Cursor
	
	Private gmap As GoogleMap
	Private MapFragment1 As MapFragment
	Dim Latitud As Double = 0
	Dim Longitud As Double = 0
	Dim Lat2 As Double = 0
	Dim Lon2 As Double = 0
	Dim p1, p2 As Location
	Dim Distance As Float
	Dim boton1 As Button
	Dim HUE_BLUE As Float
	Dim HUE_RED As Float
	Dim HUE_GREEN As Float
	Private B_AZUL As Button
	Private B_ROJO As Button
	Private B_VERDE As Button
	Private B_TODOS As Button
	Dim Tienda As String
	Dim LatitudRu As Double
	Dim LongitudRU As Double
	Dim LIST_AZUL As List
	Dim LIST_ROJO As List
	Dim LIST_VERDE As List
	Dim MARK_AZUL As Marker
	Dim MARK_ROJO As Marker
	Dim MARK_VERDE As Marker
	Dim MARK_CEDIS As Marker
	Dim rojo As String
	Dim azul As String
	Dim verde As String
	Dim todos As String
	Dim NumSerie As Int
	Dim OnInfoWindowClickListener1 As OnInfoWindowClickListener
	Dim GoogleMapEXTRA As GoogleMapsExtras
	Dim CODIGO As String
	Private SEMANA As String
	Private p_mapaRutas As Panel
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
	Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
	Root = Root1
	'load the layout to Root
	Root.LoadLayout("mapaRutas")
	ruta = File.DirInternal
	If File.Exists(ruta, "kmt.db") = False Then
		File.Copy(File.DirAssets, "kmt.db", ruta, "kmt.db")
	End If
	skmt.Initialize(ruta,"kmt.db", True)
	GPS.Initialize("GPS")
	If MapFragment1.IsGooglePlayServicesAvailable = False Then
		ToastMessageShow("Please install Google Play Services.", True)
	End If
	MARK_AZUL.IsInitialized
	MARK_ROJO.IsInitialized
	MARK_VERDE.IsInitialized
	LIST_AZUL.Initialize
	LIST_ROJO.Initialize
	LIST_VERDE.Initialize
	verde = 0
	azul = 0
	rojo = 0
	todos = 1
	c=B4XPages.MainPage.skmt.ExecQuery2("select count(*) AS CUANTOS from CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = ?", Array As String ("SEMANA"))
	c.Position =0
	SEMANA = c.GetString("CUANTOS")
	c.Close
	If SEMANA > 0 Then
		c=B4XPages.MainPage.skmt.ExecQuery2("select CAT_VA_VALOR from CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = ?", Array As String ("SEMANA"))
		c.Position =0
		SEMANA = c.GetString("CAT_VA_VALOR")
		c.Close
	End If
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Sub MapFragment1_Ready
	Log("ENTRANDO")
	gmap = MapFragment1.GetMap
	gmap.IsInitialized
	rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
	Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
	gmap.MyLocationEnabled = Result
	Dim JavaMapsObject As JavaObject
	JavaMapsObject = gmap.GetUiSettings
	JavaMapsObject.RunMethod("setMapToolbarEnabled", Array As Object(True))
	'''''''----------------------------MARKER AZUL - POR VISITAR
	If azul = 1 Or todos = 1  Then
		c.IsInitialized
		c=B4XPages.MainPage.skmt.ExecQuery("select CAT_CL_CODIGO,CAT_CL_NOMBRE,CAT_CL_LAT,CAT_CL_LONG from kmt_info where gestion = 0 and CAT_CL_LAT is not null and CAT_CL_LONG is not null and CAT_CL_LAT <> 0 and CAT_CL_LONG <> 0 and CAT_CL_LAT <> ' ' and CAT_CL_LONG <> ' ' ")
		skmt.Initialize(ruta,"kmt.db", True)
		If c.RowCount > 0 Then
			For i = 0 To c.RowCount - 1
				c.Position = i
'				Log(i&"|"&c.GetString("CAT_CL_LAT")&"|")
				LatitudRu = 0
				If c.GetString("CAT_CL_LAT") <> "" Then LatitudRu = c.GetString("CAT_CL_LAT")
				LongitudRU = 0
				If c.GetString("CAT_CL_LONG") <> "" Then LongitudRU = c.GetString("CAT_CL_LONG")
				CODIGO=c.GetString("CAT_CL_CODIGO")
				Tienda= c.GetString("CAT_CL_NOMBRE")
				MARK_AZUL = gmap.AddMarker2(LatitudRu,LongitudRU,CODIGO, gmap.HUE_BLUE)
				MARK_AZUL.Snippet = Tienda
			Next
		End If
		c .Close
		If MARK_AZUL.IsInitialized Then LIST_AZUL.Add(MARK_AZUL)
	End If
	'''''''----------------------------MARKER VERDE - VENTA
	If verde =1 Or todos = 1  Then
		c2.IsInitialized
		c2=B4XPages.MainPage.skmt.ExecQuery("select CAT_CL_CODIGO, CAT_CL_NOMBRE,CAT_CL_LONG,CAT_CL_LAT from kmt_info where gestion = 2 and CAT_CL_LAT is not null and CAT_CL_LONG is not null and CAT_CL_LAT <> 0 and CAT_CL_LONG <> 0")
		For i = 0 To c2.RowCount -1
			c2.Position = i
			LongitudRU = c2.GetString("CAT_CL_LONG")
			LatitudRu = c2.GetString("CAT_CL_LAT")
			CODIGO=c2.GetString("CAT_CL_CODIGO")
			Tienda= c2.GetString("CAT_CL_NOMBRE")
			MARK_VERDE = gmap.AddMarker2(LatitudRu,LongitudRU,CODIGO,gmap.HUE_GREEN)
			MARK_VERDE.Snippet = Tienda
		Next
	Else
		If verde =1 Or todos = 1  Then
			c2.IsInitialized
			c2=B4XPages.MainPage.skmt.ExecQuery("select CAT_CL_CODIGO, CAT_CL_NOMBRE,CAT_CL_LONG,CAT_CL_LAT from kmt_info where gestion = 2 and CAT_CL_LAT is not null and CAT_CL_LONG is not null and CAT_CL_LAT <> 0 and CAT_CL_LONG <> 0")
			For i = 0 To c2.RowCount -1
				c2.Position = i
				LongitudRU = c2.GetString("CAT_CL_LONG")
				LatitudRu = c2.GetString("CAT_CL_LAT")
				CODIGO=c2.GetString("CAT_CL_CODIGO")
				Tienda= c2.GetString("CAT_CL_NOMBRE")
				MARK_VERDE = gmap.AddMarker2(LatitudRu,LongitudRU,CODIGO,gmap.HUE_GREEN)
				MARK_VERDE.Snippet = Tienda
			Next
			c2 .Close
			If MARK_VERDE.IsInitialized Then LIST_VERDE.Add(MARK_VERDE)
		End If
	End If
'	'''''''----------------------------MARKER ROJO - NO VENTA
	If rojo =1 Or todos = 1 Then
		c3.IsInitialized
		c3=B4XPages.MainPage.skmt.ExecQuery("select CAT_CL_CODIGO, CAT_CL_NOMBRE,CAT_CL_LONG,CAT_CL_LAT from kmt_info where gestion = 3 and CAT_CL_LAT is not null and CAT_CL_LONG is not null and CAT_CL_LAT <> 0 and CAT_CL_LONG <> 0")
		For i = 0 To c3.RowCount -1
			c3.Position = i
			LongitudRU = c3.GetDouble("CAT_CL_LONG")
			LatitudRu = c3.GetDouble("CAT_CL_LAT")
			Tienda= c3.GetString("CAT_CL_NOMBRE")
			MARK_ROJO = gmap.AddMarker2(LatitudRu,LongitudRU,Tienda, gmap.HUE_RED)
		Next
	Else
		If rojo =1 Or  todos = 1  Then
			c3.IsInitialized
			c3=B4XPages.MainPage.skmt.ExecQuery("select CAT_CL_CODIGO, CAT_CL_NOMBRE,CAT_CL_LONG,CAT_CL_LAT from kmt_info where gestion = 3 and CAT_CL_LAT is not null and CAT_CL_LONG is not null and CAT_CL_LAT <> 0 and CAT_CL_LONG <> 0")
			For i = 0 To c3.RowCount -1
				c3.Position = i
				LongitudRU = c3.GetDouble("CAT_CL_LONG")
				LatitudRu = c3.GetDouble("CAT_CL_LAT")
				Tienda= c3.GetString("CAT_CL_NOMBRE")
				MARK_ROJO = gmap.AddMarker2(LatitudRu,LongitudRU,Tienda, gmap.HUE_RED)
			Next
			If MARK_ROJO.IsInitialized Then LIST_ROJO.Add(MARK_ROJO)
			c3.Close
		End If
	End If
	''------------------------------
	Dim aa As CameraPosition
	aa.Initialize(LatitudRu,LongitudRU,15)''' RECOMENDABLE CAMBIAR A 10 PARA QUE SE VEAN MAS MARCADORES
	gmap.AnimateCamera(aa)
	
	'''''---------------------- ESTO ES PARA LOS CLICK EN LAS VENTANAS D INFORMACION-----------
	Dim OnInfoWindowClickListener1 As OnInfoWindowClickListener
	OnInfoWindowClickListener1.Initialize("OnInfoWindowClickListener1")
	GoogleMapEXTRA.SetOnInfoWindowClickListener(gmap, OnInfoWindowClickListener1)

End Sub

Sub OnInfoWindowClickListener1_click(Marker1 As Marker)
	Log("mapclicked")
	Log("borramos cuentaa")
	B4XPages.MainPage.skmt.ExecNonQuery("delete from CUENTAA")
	Log("insertamos cuentaa")
	B4XPages.MainPage.skmt.ExecNonQuery2("INSERT INTO CUENTAA VALUES (?) ", Array As Object(Marker1.Title))
	Log("vamos a cliente")
	B4XPages.ShowPage("Cliente")
	B4XPages.ClosePage("MapaRutas")
	B4XPages.GetManager.ClosePage(Me)
End Sub

Sub GPS_LocationChanged (Parametro As Location)
	MARK_CEDIS.IsInitialized
End Sub

Sub B4XPage_Appear
	If GPS.GPSEnabled = False Then
		ToastMessageShow("Debe Activar el GPS del Equipo.", True)
		StartActivity(GPS.LocationSettingsIntent)
	Else
		GPS.Start(0, 0)
	End If
End Sub

Sub B_TODOS_Click
	todos  =1
	verde = 0
	azul = 0
	rojo = 0
	MapFragment1_Ready
End Sub

Sub B_VERDE_Click
	verde = 1
	azul = 0
	rojo = 0
	todos = 0
	gmap.Clear
	MapFragment1_Ready
End Sub

Sub B_ROJO_Click
	rojo = 1
	verde = 0
	azul = 0
	todos = 0
	gmap.Clear
	MapFragment1_Ready
End Sub

Sub B_AZUL_Click
	azul = 1
	verde = 0
	rojo = 0
	todos = 0
	gmap.Clear
	MapFragment1_Ready
End Sub

Private Sub p_mapaRutas_Click
	
End Sub