' SummaryPage2_QuickSearch_Summary

Option Explicit On
Option Strict On

' #################################################################
' Quick Search Summary

' Version: 1.03 (5/11/2018)

' Usage: Below Quick Search grid
' #################################################################

'#Const "MaxTabularDisplayRows",Integer,10,"The maximum number of rows to display in supplemental tabular data tables, e.g., Client Accounts. Set to 0 for unlimited."
'#Const "ShowActions",Boolean,True,"Indicates whether to include Actions such as 'Add Log' where applicable."
'#Const "ShowPasteIcons",Boolean,True,"Not used by this Script but can be used to specify whether when this summary is shown from a popup, e.g., DBComboBox, to not automatically add a 'Paste' icon to hyperlinks."

' Constants
Private MaxTabularDisplayRows As Integer = 10
Private ShowActions As Boolean = True

' Objects
Private QuickSearch As finQuickSearch
Private QuickSearchResult As finQuickSearchResult

' Other
Private mContext As isefinQuickSearchContext

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 Index As Integer

  ' Assume Success
  Main = True

  ' Initialise
  QuickSearch = DirectCast(source, finQuickSearch)
  Index = eventArgs.GetInteger("Index", -1)
  If Index <> -1 Then QuickSearchResult = QuickSearch.SearchResults(Index)
  mContext = DirectCast(eventArgs.GetEnum("Context", GetType(isefinQuickSearchContext), isefinQuickSearchContext.Unspecified), isefinQuickSearchContext)

  ' Get Constants
  With ScriptInfo.Constants
    MaxTabularDisplayRows = .GetInteger("MaxTabularDisplayRows", MaxTabularDisplayRows)
    ShowActions = .GetBoolean("ShowActions", ShowActions)
  End With

  Select Case eventId
    Case ""
      ' Main event, i.e., return main page content
      If Index = -1 Then
        Main = CreateHelp(text)
      Else
        Main = CreatePage(text)
      End If

      ' Indicate event was handled otherwise built-in Script will be called and override return values
      handled = True
  End Select

End Function

Private Function CreateHelp(ByRef html As String) As Boolean

  Dim Ok As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder

  ' Assume Success
  Ok = True

  ' Initialise
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()

  ' Initialise HTML
  With sb
    .Initialise(ScriptInfo)
    .HtmlHeaderBegin("", isefinSummaryPageStyle.None)
    .HtmlHeaderEnd()
  End With

  ' Generate Help text
  sb.Append(finBL.SummaryPageStandardBlocks.QuickSearch_Help(ScriptInfo))

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function

Private Function CreatePage(ByRef html As String) As Boolean

  Dim Ok As Boolean
  Dim OtherParameters As ISKeyValueList
  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim SummaryTables As ISSummaryTables

  ' Assume Success
  Ok = True

  ' Other Parameters
  OtherParameters = finBL.CreateKeyValueList()
  With OtherParameters
    .SetObject("Context", mContext)
  End With

  ' Initialise
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()

  ' Initialise HTML
  With sb
    .Initialise(ScriptInfo)
    .HtmlHeaderBegin("", isefinSummaryPageStyle.None)
    .HtmlHeaderEnd()
  End With

  ' Get Summary Data (in a non-HTML-specific ISSummaryTables collection)
  SummaryTables = finBL.SummaryPageStandardBlocks.QuickSearch_ResultSummary(ScriptInfo, QuickSearchResult, MaxTabularDisplayRows, ShowActions, OtherParameters)

  ' Render Summary Tables
  If SummaryTables IsNot Nothing Then
    sb.SummaryTablesAppend(SummaryTables, , , , MaxTabularDisplayRows)
  End If

  ' Additional Information on how to NOT search this type of item
  If QuickSearchResult IsNot Nothing Then
    Select Case QuickSearchResult.ObjectType
      Case isefinQuickSearchObjectType.Account, isefinQuickSearchObjectType.AccountArchive, isefinQuickSearchObjectType.Client, isefinQuickSearchObjectType.SecurityStmt
        ' Always searched

      Case isefinQuickSearchObjectType.MenuItem, isefinQuickSearchObjectType.Report, isefinQuickSearchObjectType.StandaloneDocument
        If finBL.IsRunningFromWeb Then
          ' finPOWER Connect Cloud always searches these
        Else
          sb.Append("<br/><p style='opacity:0.25'>{{icon|Information}} Exclude " & QuickSearchResult.ObjectTypeText(True) & " from Tools, User Preferences, Search.</p>")
        End If

      Case Else
        ' Probably an Admin Library which is execluded from searches by default so the User has explicitly opted to show these
    End Select
  End If

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function