Option Explicit On
Option Strict On

' ##################################################################
' Custom Service Summary Page Script
'
' (c) Copyright Intersoft Systems Limited 2026
'
' Version: 1.00 (29/04/2026)
'
' Usage: Service Log form (when ServiceId = "CustomService")
' ##################################################################
' Version        Date  User     Job       Description
' -------  ----------  -------  --------  -------------------------
'    1.00  29/04/2026  XX       J036835   Created
' #################################################################

' Special Constants
Const TestMode As Boolean = False

Public Function Main(source As Object,
                     eventId As String,
                     eventArgs As ISKeyValueList,
                     ByRef handled As Boolean,
                     ByRef returnValues As ISKeyValueList,
                     ByRef text As String) As Boolean

  Dim ServiceLogs As ISServiceLogs

  ' Assume Success
  Main = True

  ' Get Service Logs
  If Main Then
    Main = finBL.CustomService.GetServiceLogs(CInt(source), ServiceLogs)
  End If

  ' Validate
  If Main Then
    If ServiceLogs.Count = 0 Then
      Main = False
      finBL.Error.ErrorBegin("Invalid Service Log. Not a Custom Service request.")
    ElseIf StrComp(ServiceLogs(0).ServiceId, "CustomService", CompareMethod.Text) <> 0 Then
      Main = False
      finBL.Error.ErrorBegin("Invalid Service Log. Service Id does not match.")
    End If
  End If

  ' Handle Events
  If Main Then
    Select Case eventId
      Case ""
        Main = CreatePage(ServiceLogs, text)
    End Select
  End If

End Function

Private Function CreatePage(serviceLogs As ISServiceLogs,
                            ByRef html As String) As Boolean

  Dim HtmlSummaryPage2StringBuilder As finHtmlSummaryPage2StringBuilder
  Dim ServiceLog As ISServiceLog
  Dim Success As Boolean

  ' Assume Success
  Success = True

  ' Initialise ByRef
  html = ""

  ' Initialise
  ServiceLog = serviceLogs(0)
  HtmlSummaryPage2StringBuilder = finBL.CreateHtmlSummaryPage2StringBuilder()

  ' Initialise HTML
  With HtmlSummaryPage2StringBuilder
    If ServiceLog.TestMode Then .Watermark = "Test Mode"

    ' Embed images (and CSS) so the page can be exported to PDF
    .Initialise(ScriptInfo, False, True)

    .HtmlHeaderBegin(String.Format("Custom Service {0}", ServiceLog.ProductId), isefinSummaryPageStyle.ServiceReport)
    .HtmlHeaderInsertServiceReportTheme("#006599", "#C0C0C0", "", "#E8E8E8")
    .HtmlHeaderInsertPdfGenerationHints("A4", "Portrait", 880, "1.5cm", "1.5cm", "1cm", "1cm", "font-size:0.9em")
    .HtmlHeaderEnd()
  End With

  ' Body
  With HtmlSummaryPage2StringBuilder
    If Len(ServiceLog.EnquiryReference) <> 0 Then
      .AppendH1(ServiceLog.EnquiryReference)
    Else
      .AppendH1(String.Format("Service Log {0}", ServiceLog.Pk))
    End If
    .AppendH2("Custom Service Request")

    .SummaryTablesAppend(EnquiryDetails(ServiceLog))
    .SummaryTablesAppend(Request(ServiceLog))
    .SummaryTablesAppend(Response(ServiceLog))
    '.SummaryTablesAppend(History(serviceLogs))
  End With

  ' Disclaimer
  HtmlSummaryPage2StringBuilder.Append("<hr/>")
  HtmlSummaryPage2StringBuilder.AppendLine("{{disclaimer|This report was produced by finPOWER Connect version " & finBL.VersionMajorMinorBuild & " using information retrieved from a Custom Service request on " & finBL.FormatDateTime(ServiceLog.CreatedUtcDate, True, True, True) & "}}")

  ' Finalise
  HtmlSummaryPage2StringBuilder.HtmlFinalise()
  html = HtmlSummaryPage2StringBuilder.ToString(True)

  Return Success

End Function

Private Function EnquiryDetails(serviceLog As ISServiceLog) As ISSummaryTables

  Dim SummaryTable As ISSummaryTable
  Dim SummaryTables As ISSummaryTables

  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  SummaryTable = SummaryTables.CreateSummaryTableInformation("", "EnquiryDetails", , , , TestMode)
  With SummaryTable
    .AddRowCaptionUtcDateTimeWithTimeZone("Date:", serviceLog.CreatedUtcDate, finBL.Settings.TimeZoneId)
    .AddRowCaptionText("Status:", serviceLog.ExStatus)
    .AddRowCaptionText("Product:", serviceLog.ProductId)
    If Len(serviceLog.EnquiryReference) <> 0 Then
      .AddRowCaptionText("Reference:", serviceLog.EnquiryReference)
    End If
    .AddRowCaptionTick("Test Mode:", serviceLog.TestMode)
    If Len(serviceLog.FailedReason) <> 0 Then
      .AddRowCaptionWiki("Failed Reason:", "{{Warning|" & finBL.HtmlEncode(serviceLog.FailedReason) & "}}")
    End If
  End With
  SummaryTables.Add("EnquiryDetails", SummaryTable)

  Return SummaryTables

End Function

Private Function Request(serviceLog As ISServiceLog) As ISSummaryTables

  Dim SummaryTable As ISSummaryTable
  Dim SummaryTables As ISSummaryTables

  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  SummaryTable = SummaryTables.CreateSummaryTableInformation("Request", "Request", 2, , , TestMode)
  With SummaryTable
    .AddRowCaptionText("URL:", serviceLog.RequestUrl)
    If Len(serviceLog.Request) <> 0 Then
      .AddRowCaptionText("Body:", serviceLog.Request)
    End If
  End With
  SummaryTables.Add("Request", SummaryTable)

  Return SummaryTables

End Function

Private Function Response(serviceLog As ISServiceLog) As ISSummaryTables

  Dim SummaryTable As ISSummaryTable
  Dim SummaryTables As ISSummaryTables

  ' Initialise
  SummaryTables = finBL.CreateSummaryTables()

  SummaryTable = SummaryTables.CreateSummaryTableInformation("Response", "Response", 2, , , TestMode)
  With SummaryTable
    If Len(serviceLog.Response) <> 0 Then
      .AddRowCaptionText("Body:", serviceLog.Response)
    Else
      .AddRowCaptionText("Body:", "(no response body)")
    End If
  End With
  SummaryTables.Add("Response", SummaryTable)

  Return SummaryTables

End Function

' History block - retained but not currently rendered. Re-enable by uncommenting both this function and the call in CreatePage.
'Private Function History(serviceLogs As ISServiceLogs) As ISSummaryTables
'
'  Dim ServiceLog As ISServiceLog
'  Dim SummaryTable As ISSummaryTable
'  Dim SummaryTables As ISSummaryTables
'
'  ' Initialise
'  SummaryTables = finBL.CreateSummaryTables()
'
'  SummaryTable = SummaryTables.CreateSummaryTableTabular("History", "History", 2, TestMode)
'  With SummaryTable
'    With .Columns
'      .AddText("Service Log")
'      .AddDate("Date")
'      .AddText("User")
'      .AddText("Status")
'    End With
'
'    With .Rows
'      For Each ServiceLog In serviceLogs
'        With .Add
'          If Len(ServiceLog.ExStatusHtmlColour) <> 0 Then
'            .BackgroundColour = ServiceLog.ExStatusHtmlColour
'            If finBL.Runtime.ColourUtilities.HtmlColourIsDark(ServiceLog.ExStatusHtmlColour) Then .ForegroundColour = "white"
'          End If
'          With .Cells
'            .AddInteger(ServiceLog.Pk).HyperlinkSetFormShow("ServiceLog", CStr(ServiceLog.Pk))
'            .AddDateTime(ServiceLog.CreatedDate, , True)
'            .AddCodeDescription(ServiceLog.CreatedUserPk, finBL.Users)
'            .AddText(ServiceLog.ExStatus)
'          End With
'        End With
'      Next
'    End With
'  End With
'  SummaryTables.Add("History", SummaryTable)
'
'  Return SummaryTables
'
'End Function
