* Adaugam controale noi in clasa RarAutoPassForm pentru tab-ul Log

* Form pentru cautare avansata
DEFINE CLASS SearchLogForm AS Form
    Caption = "Cautare avansata in log"
    Width = 600
    Height = 400
    AutoCenter = .T.
    MaxButton = .F.
    BorderStyle = 2
    
    ADD OBJECT lblDateFrom AS Label WITH ;
        Caption = "De la data:", ;
        Left = 20, ;
        Top = 20
        
    ADD OBJECT txtDateFrom AS TextBox WITH ;
        Left = 100, ;
        Top = 20, ;
        Width = 100, ;
        Value = DATE() - 30
        
    ADD OBJECT lblDateTo AS Label WITH ;
        Caption = "Pana la:", ;
        Left = 220, ;
        Top = 20
        
    ADD OBJECT txtDateTo AS TextBox WITH ;
        Left = 280, ;
        Top = 20, ;
        Width = 100, ;
        Value = DATE()
        
    ADD OBJECT lblVin AS Label WITH ;
        Caption = "VIN:", ;
        Left = 20, ;
        Top = 60
        
    ADD OBJECT txtVin AS TextBox WITH ;
        Left = 100, ;
        Top = 60, ;
        Width = 150
        
    ADD OBJECT lblComanda AS Label WITH ;
        Caption = "Nr. Comanda:", ;
        Left = 270, ;
        Top = 60
        
    ADD OBJECT txtComanda AS TextBox WITH ;
        Left = 350, ;
        Top = 60, ;
        Width = 100
        
    ADD OBJECT lblStatus AS Label WITH ;
        Caption = "Status:", ;
        Left = 20, ;
        Top = 100
        
    ADD OBJECT cboStatus AS ComboBox WITH ;
        Left = 100, ;
        Top = 100, ;
        Width = 150, ;
        Style = 2
        
    ADD OBJECT chkHasError AS Checkbox WITH ;
        Caption = "Doar cu erori", ;
        Left = 270, ;
        Top = 100
        
    ADD OBJECT btnSearch AS CommandButton WITH ;
        Caption = "Caută", ;
        Left = 20, ;
        Top = 140, ;
        Width = 100, ;
        Height = 30
        
    ADD OBJECT btnCancel AS CommandButton WITH ;
        Caption = "Anulează", ;
        Left = 130, ;
        Top = 140, ;
        Width = 100, ;
        Height = 30

    * Constructor
    FUNCTION Init
        THIS.cboStatus.AddItem("Toate")
        THIS.cboStatus.AddItem("SUCCESS")
        THIS.cboStatus.AddItem("ERROR")
        THIS.cboStatus.ListIndex = 1
        RETURN .T.
    
    * Handler pentru butonul Cauta
    FUNCTION btnSearch.Click
        LOCAL lcFilter
        lcFilter = THIS.BuildFilter()
        THISFORM.Parent.ApplyLogFilter(lcFilter)
        THISFORM.Release()
    
    * Handler pentru butonul Anuleaza
    FUNCTION btnCancel.Click
        THISFORM.Release()
    
    * Construieste filtrul SQL
    FUNCTION BuildFilter
        LOCAL lcFilter, lcAnd
        lcFilter = ""
        lcAnd = ""
        
        * Filtru data
        IF !EMPTY(THIS.txtDateFrom.Value)
            lcFilter = lcFilter + "data_prezentare >= CTOD('" + ;
                      DTOC(THIS.txtDateFrom.Value) + "')"
            lcAnd = " AND "
        ENDIF
        
        IF !EMPTY(THIS.txtDateTo.Value)
            lcFilter = lcFilter + lcAnd + "data_prezentare <= CTOD('" + ;
                      DTOC(THIS.txtDateTo.Value) + "')"
            lcAnd = " AND "
        ENDIF
        
        * Filtru VIN
        IF !EMPTY(THIS.txtVin.Value)
            lcFilter = lcFilter + lcAnd + "UPPER(vin) LIKE '" + ;
                      UPPER(ALLTRIM(THIS.txtVin.Value)) + "%'"
            lcAnd = " AND "
        ENDIF
        
        * Filtru comanda
        IF !EMPTY(THIS.txtComanda.Value)
            lcFilter = lcFilter + lcAnd + "UPPER(nr_comanda) LIKE '" + ;
                      UPPER(ALLTRIM(THIS.txtComanda.Value)) + "%'"
            lcAnd = " AND "
        ENDIF
        
        * Filtru status
        IF THIS.cboStatus.ListIndex > 1
            lcFilter = lcFilter + lcAnd + "status = '" + ;
                      THIS.cboStatus.Value + "'"
            lcAnd = " AND "
        ENDIF
        
        * Filtru erori
        IF THIS.chkHasError.Value
            lcFilter = lcFilter + lcAnd + "!EMPTY(error_msg)"
        ENDIF
        
        RETURN lcFilter
ENDDEFINE

* Adaugam metodele noi in RarAutoPassForm
* In functia Init, adaugam butoanele noi:
ADD OBJECT btnAdvSearch AS CommandButton OF tabLog WITH ;
    Caption = "Căutare avansată", ;
    Left = 320, ;
    Top = 20, ;
    Width = 120, ;
    Height = C_BTN_HEIGHT

ADD OBJECT btnExportExcel AS CommandButton OF tabLog WITH ;
    Caption = "Export Excel", ;
    Left = 450, ;
    Top = 20, ;
    Width = 120, ;
    Height = C_BTN_HEIGHT

* Handler pentru cautare avansata
FUNCTION btnAdvSearch.Click
    LOCAL loSearch
    loSearch = CREATEOBJECT("SearchLogForm")
    loSearch.Show(1)
ENDFUNC

* Aplicare filtru de cautare
FUNCTION ApplyLogFilter
PARAMETERS tcFilter
    SELECT rar_log
    IF !EMPTY(tcFilter)
        SET FILTER TO &tcFilter
    ELSE
        SET FILTER TO
    ENDIF
    GO TOP
    THIS.grdLog.Refresh()
ENDFUNC

* Handler pentru export Excel
FUNCTION btnExportExcel.Click
    LOCAL lcFile, loExcel, loWorkbook, loWorksheet
    LOCAL lnRow, lnCol, lcValue
    
    * Alegem locatia fisierului
    lcFile = PUTFILE("Excel files|*.xlsx", "RAR_Log_Export.xlsx", "", 0)
    IF EMPTY(lcFile)
        RETURN
    ENDIF
    
    THIS.AddStatus("Export Excel în " + lcFile + "...")
    
    TRY
        * Cream obiectul Excel
        loExcel = CREATEOBJECT("Excel.Application")
        loExcel.Visible = .F.
        
        * Adaugam un workbook nou
        loWorkbook = loExcel.Workbooks.Add()
        loWorksheet = loWorkbook.Sheets(1)
        
        * Setam headerele
        WITH loWorksheet
            .Cells(1,1).Value = "Data prezentare"
            .Cells(1,2).Value = "Nr. comandă"
            .Cells(1,3).Value = "VIN"
            .Cells(1,4).Value = "Nr. înmatriculare"
            .Cells(1,5).Value = "Km final"
            .Cells(1,6).Value = "Km initial"
            .Cells(1,7).Value = "Status"
            .Cells(1,8).Value = "Eroare"
            .Cells(1,9).Value = "Data trimitere"
            
            * Formatare header
            .Range(.Cells(1,1), .Cells(1,9)).Font.Bold = .T.
            .Range(.Cells(1,1), .Cells(1,9)).Interior.Color = RGB(200,200,200)
        ENDWITH
        
        * Populam datele
        SELECT rar_log
        lnRow = 2
        SCAN
            loWorksheet.Cells(lnRow, 1).Value = data_prezentare
            loWorksheet.Cells(lnRow, 2).Value = nr_comanda
            loWorksheet.Cells(lnRow, 3).Value = vin
            loWorksheet.Cells(lnRow, 4).Value = nr_inm
            loWorksheet.Cells(lnRow, 5).Value = km_final
            loWorksheet.Cells(lnRow, 6).Value = km_initial
            loWorksheet.Cells(lnRow, 7).Value = status
            loWorksheet.Cells(lnRow, 8).Value = error_msg
            loWorksheet.Cells(lnRow, 9).Value = data_trimitere
            
            * Coloram randurile cu erori in rosu deschis
            IF status = "ERROR"
                loWorksheet.Range(loWorksheet.Cells(lnRow,1), ;
                                loWorksheet.Cells(lnRow,9)).Interior.Color = RGB(255,200,200)
            ENDIF
            
            lnRow = lnRow + 1
        ENDSCAN
        
        * Auto-fit coloane
        loWorksheet.Range("A:I").Columns.AutoFit()
        
        * Salvam si inchidem
        loWorkbook.SaveAs(lcFile)
        loWorkbook.Close()
        loExcel.Quit()
        
        THIS.AddStatus("Export Excel finalizat cu succes")
        
        * Deschidem fisierul
        RUN /N "explorer.exe" &lcFile
        
    CATCH TO loError
        THIS.AddStatus("EROARE la export Excel: " + loError.Message)
        
        TRY
            loWorkbook.Close()
            loExcel.Quit()
        CATCH
        ENDTRY
    ENDTRY
ENDFUNC
