' SummaryPage2_AccountApp_DetailView
Option Explicit On
Option Strict On

' #################################################################
' Account Application Detail View Summary
'
' Version: 1.04 (14/05/2021)
'
' Usage: Account Applications form, Data Entry page if no Page Set
'        defined and no Summary Page specified.
'
'        Can also be used as a Key Details type page if required.
' #################################################################

'#const "ShowApplicationUserDefined",Boolean,True,"Show the Application's User Defined data."
'#const "ShowApplicantUserDefined",Boolean,True,"Show Applicant User Defined data."
'#const "ShowCollateralUserDefined",Boolean,True,"Show Collateral User Defined data."

' Constants
Private ShowApplicationUserDefined As Boolean = True
Private ShowApplicantUserDefined As Boolean = True
Private ShowCollateralUserDefined As Boolean = True

' Objects
Private mAccountApp As finAccountApp

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

  ' Assume Success
  Main = True

  ' Initialise
  mAccountApp = DirectCast(source, finAccountApp)

  ' Get Constants
  With ScriptInfo.Constants
    ShowApplicationUserDefined = .GetBoolean("ShowApplicationUserDefined", ShowApplicationUserDefined)
    ShowApplicantUserDefined = .GetBoolean("ShowApplicantUserDefined", ShowApplicantUserDefined)
    ShowCollateralUserDefined = .GetBoolean("ShowCollateralUserDefined", ShowCollateralUserDefined)
  End With

  ' Handle Events
  Select Case eventId
    Case ""
      ' Main event, i.e., return main page content
      Main = CreatePage(text)
  End Select

End Function

Private Function CreatePage(ByRef html As String) As Boolean

  Dim AccountAppApplicant As finAccountAppApplicant
  Dim Collapsed As Boolean
  Dim HeadingStyle As isefinSummaryPageBlockHeadingStyle
  Dim Ok As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim Title As String
  Dim TitleSuffix As String

  ' Assume Success
  Ok = True

  ' Initialise
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()

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

  ' Begin Section
  sb.SectionColumnsBegin()

  ' Begin Column 1
  sb.ColumnBegin("50%")

  ' Summary
  sb.BlockBegin("Application")
  sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountApp_DetailSummaryApplication(ScriptInfo, mAccountApp))
  sb.BlockEnd()

  ' User Defined (Application)
  If ShowApplicationUserDefined AndAlso mAccountApp.UserData.Count <> 0 Then
    sb.BlockBegin("User Defined", , , True, True)
    sb.SummaryTablesAppend(finBL.Utilities.KeyValueListToSummaryTables(mAccountApp.UserData))
    sb.BlockEnd()
  End If

  ' Collateral Items
  If finBL.Settings.AddOns.SecurityRegister AndAlso mAccountApp.CollateralItems.Count <> 0 Then
    sb.BlockBegin("Collateral Items")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountApp_DetailSummaryCollateralItems(ScriptInfo, mAccountApp))
    sb.BlockEnd()
  End If

  ' End Column 1
  sb.ColumnEnd()

  ' Begin Column 2
  sb.ColumnBegin("50%")

  ' Applicants (one block per Applicant)
  For Each AccountAppApplicant In mAccountApp.Applicants
    ' Unused should be collapsed and have a warning title
    If AccountAppApplicant.Used Then
      Collapsed = False
      HeadingStyle = isefinSummaryPageBlockHeadingStyle.Normal
      TitleSuffix = ""
    Else
      Collapsed = True
      HeadingStyle = isefinSummaryPageBlockHeadingStyle.WarningLow
      TitleSuffix = "(Not Used)"
    End If

    ' Title
    If Len(AccountAppApplicant.Name) = 0 Then
      Title = "Applicant #" & (mAccountApp.Applicants.IndexOf(AccountAppApplicant) + 1)
    Else
      Title = AccountAppApplicant.Name
    End If

    sb.BlockBegin(Title, TitleSuffix, HeadingStyle, True, Collapsed)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountApp_DetailSummaryApplicant(ScriptInfo, AccountAppApplicant))

    ' User Defined (in a collapsed table)
    If ShowApplicantUserDefined AndAlso AccountAppApplicant.UserData.Count <> 0 Then
      sb.Append("<br/>")
      sb.SummaryTableAppend(finBL.Utilities.KeyValueListToSummaryTable(AccountAppApplicant.UserData, False, True, "User Defined"))
    End If
    sb.BlockEnd()
  Next

  ' End Column 2
  sb.ColumnEnd()

  ' End Section
  sb.SectionColumnsEnd()

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function