' Form module: Menu
' Description: Main application menu with refactored code

Private Sub b_cargar_logs_Click()
    ' Process logs and update data
    RunPythonLogProcessor
    RefreshLogData
    Unload Me
End Sub

Private Sub b_gen_factura_Click()
    ' Show the date selection form
    ShowDateSelectionForm
    Unload Me
End Sub

Public Sub b_gen_hoja_comessas_Click()
    ' Export Comesse to Excel
    ExportComessaToExcel
    Unload Me
End Sub

' Helper method to display the date selection form
Private Sub ShowDateSelectionForm()
    Dim wsHoras As Worksheet
    Dim fechaDesde As Date, fechaHasta As Date
    Dim rangoSeleccionado As Range
    
    ' Set reference to Horas sheet
    Set wsHoras = ThisWorkbook.Sheets(SHEET_HORAS)
    
    ' Check if there's a selected range in Horas sheet and column E
    If Not Application.Selection Is Nothing Then
        Set rangoSeleccionado = Application.Selection
        If rangoSeleccionado.Worksheet.Name = SHEET_HORAS And _
           rangoSeleccionado.EntireColumn.Address = wsHoras.Columns("E").Address Then
            ' Use dates from selected range
            fechaDesde = rangoSeleccionado.Cells(1, 1).Value2
            fechaHasta = rangoSeleccionado.Cells(rangoSeleccionado.Rows.Count, 1).Value2
        End If
    End If
    
    ' If no appropriate selection, use default date calculation
    If fechaDesde = 0 Or fechaHasta = 0 Then
        fechaHasta = GetDefaultEndDate()
        fechaDesde = GetDefaultStartDate(fechaHasta)
    End If
    
    ' Set default values in the form
    ConsultarFechas.t_hasta.Value = Format(fechaHasta, "dd/mm/yyyy")
    ConsultarFechas.t_desde.Value = Format(fechaDesde, "dd/mm/yyyy")
    
    ' Set invoice number
    ConsultarFechas.frm_factnro.Value = ThisWorkbook.Sheets(SHEET_NOTA).Cells(6, 3).Value
    
    ' Show form
    ConsultarFechas.Show
End Sub