B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=12.8
@EndOfDesignText@
Sub Class_Globals
	Private Root As B4XView 'ignore
	Private xui As XUI 'ignore

	Dim g As GPS
	Dim clie_id As String
	Dim sDate,sTime As String
	Dim usuario As String
	
	Dim c As Cursor
	Dim ruta As String
	Dim Regresar As Button
	

	Dim ListView1 As ListView
	Dim L_CANT As Label
	Dim L_TOTAL As Label
	Dim borra As Button
	Dim Existe As String
	Dim result As String
	'    Dim lat_gps, lon_gps As String

	Private Titulo As Label
	Private b_desc As Button
	Private ListView2 As ListView
	Dim tgl As Toggle
	Private p_nota As Panel
	Private Panel1 As Panel
	Private l_productohist As Label
	Private l_s1 As Label
	Private l_s2 As Label
	Private l_s3 As Label
	Private l_s4 As Label
	Private CustomListView1 As CustomListView
	Private L_PROMEDIOHIST As Label
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("nota3")
	borra.Visible = False
	Titulo.Text = "Acumulado"
	b_desc.Visible = False
End Sub





Sub B4XPage_Appear
	Panel1.Visible = False
	If Not(Starter.gps.GPSEnabled) Then
		ToastMessageShow("Es necesario tener el GPS encendido", True)
		StartActivity(Starter.gps.LocationSettingsIntent)
	End If
	
	L_CANT.Text = ""
	L_TOTAL.Text = ""
	
	c = B4XPages.MainPage.skmt.ExecQuery("select count(*) as EXISTE from hist_ventastodos  WHERE HVD_CLIENTE IN (Select CUENTA from cuentaa) ORDER BY HVD_CODPROMO, HVD_COSTO_TOT ASC")
	c.Position = 0
	Existe = c.GetString("EXISTE")
	c.Close
	
	' =====================================================================
	' NUEVA LÓGICA DE LISTVIEW1: DISEÑO "TICKET MODERNO"
	' =====================================================================
	ListView1.Clear
	ListView1.Visible = True
	
	' 1. Fondo de la lista: Un gris muy claro/hueso para simular el papel
	ListView1.Color = xui.Color_RGB(245, 245, 245)
	
	' 2. Diseño del Encabezado (El Ticket y la Fecha)
	ListView1.SingleLineLayout.Label.TextSize = 16
	' Azul oscuro elegante para separar visualmente los tickets
	ListView1.SingleLineLayout.Label.TextColor = xui.Color_RGB(25, 118, 210)
	ListView1.SingleLineLayout.Label.Typeface = Typeface.DEFAULT_BOLD
	' Centrado en ambas direcciones para que parezca un divisor
	ListView1.SingleLineLayout.Label.Gravity = Bit.Or(Gravity.CENTER_VERTICAL, Gravity.CENTER_HORIZONTAL)
	ListView1.SingleLineLayout.ItemHeight = 50dip
	
	' 3. Diseño del Detalle (Los Productos)
	ListView1.TwoLinesLayout.Label.TextSize = 16
	ListView1.TwoLinesLayout.Label.TextColor = xui.Color_RGB(33, 33, 33) ' Gris casi negro (mejor lectura)
	ListView1.TwoLinesLayout.Label.Typeface = Typeface.DEFAULT_BOLD
	ListView1.TwoLinesLayout.Label.Gravity = Gravity.CENTER_VERTICAL
	
	ListView1.TwoLinesLayout.SecondLabel.TextSize = 15
	' Verde oscuro para destacar el dinero y la cantidad
	ListView1.TwoLinesLayout.SecondLabel.TextColor = xui.Color_RGB(56, 142, 60)
	ListView1.TwoLinesLayout.SecondLabel.Typeface = Typeface.DEFAULT_BOLD
	ListView1.TwoLinesLayout.ItemHeight = 70dip ' Espacio cómodo para que respire el texto

	' Query: Ordena de más antiguo a más reciente
	Dim queryVentas As String = $"
		SELECT HVD_NUM_TICKET, HVD_PRONOMBRE, HVD_CANT, HVD_COSTO_TOT, HVD_FECHA 
		FROM hist_ventastodos 
		WHERE HVD_CLIENTE IN (Select CUENTA from cuentaa) 
		ORDER BY 
			SUBSTR(HVD_FECHA, 7, 4) ASC,
			SUBSTR(HVD_FECHA, 4, 2) ASC,
			SUBSTR(HVD_FECHA, 1, 2) ASC,
			HVD_NUM_TICKET ASC,
			HVD_CODPROMO,
			HVD_COSTO_TOT ASC
	"$
	
	c = B4XPages.MainPage.skmt.ExecQuery(queryVentas)
	
	If c.RowCount > 0 Then
		Dim currentTicket As String = ""
		
		For i = 0 To c.RowCount - 1
			c.Position = i
			
			Dim ticket As String = c.GetString("HVD_NUM_TICKET")
			Dim fecha As String = c.GetString("HVD_FECHA")
			Dim producto As String = c.GetString("HVD_PRONOMBRE")
			Dim cant As String = c.GetString("HVD_CANT")
			Dim costo As String = c.GetString("HVD_COSTO_TOT")
'			Dim Costotodo As Double = 0
			
			' Agregamos el separador de Ticket
			If ticket <> currentTicket Then
'				Costotodo = Costotodo + costo
'				ListView1.AddSingleLine($"🎫 TICKET: ${ticket} | 📅 ${fecha} ${CRLF} 💵 Total: $${NumberFormat2(Costotodo,0,2,2,False)}"$)
				ListView1.AddSingleLine($"🎫 TICKET: ${ticket} | 📅 ${fecha}"$)
				currentTicket = ticket
			End If
			
			' Formateamos el texto del detalle usando Smart String Literal para que se vea más profesional
			Dim detalle As String = $"📦 Cantidad: ${cant}   |   💵 Total: $${costo}"$
			ListView1.AddTwoLines(producto, detalle)
		Next
	End If
	c.Close
	' =====================================================================

	' Lógica para totales
	If Existe <> "0" Then
		c = B4XPages.MainPage.skmt.ExecQuery("select SUM(HVD_CANT) AS PC_NOART, SUM(HVD_COSTO_TOT) AS PC_MONTO from hist_ventastodos where HVD_CLIENTE in (Select CUENTA from cuentaa)")
		c.Position = 0
		L_CANT.Text = c.GetString("PC_NOART")
		L_TOTAL.Text = c.GetString("PC_MONTO")
		c.Close
	End If

	' ...
	' (AQUÍ SIGUE TU CÓDIGO DEL CustomListView1 PARA EL PROMEDIO DE 4 SEMANAS)
	' CustomListView1.AsView.Height = Root.Height * 0.45
	' ...
End Sub


