' SummaryPage2_AccountApp_Summary
Option Explicit On
Option Strict On

' #################################################################
' Account Application Summary

' Version: 1.05 (23/11/2023)

' Usage: Account Applications form
' #################################################################
' Version        Date  User     Job       Description
' -------  ----------  -------  --------  -------------------------
'    1.05  23/11/2023  ISS      J030836   Added the ability to show "Related via Hashtags" block
' #################################################################

'#const "ShowApplicantAddressInSummary",Boolean,False,"Show the Main Applicant's postal address in the Summary Block."
'#const "ShowApplicantCity",Boolean,True,"Show the City (or suburb) in the Applicant Block."
'#const "ShowApplicantAddressFull",Boolean,True,"Show the full Address in the Applicants Block."
'#const "ShowApplicantContactMethods",Boolean,False,"Show Contact Methods in the Applicants Block."
'#const "ShowApplicantDateOfBirth",Boolean,False,"Show the Applicant's date of birth in the Applicants Block."
'#const "ShowRelatedViaHashtagsBlock",Boolean,True,"Show Related via Hashtags block."
'#const "ShowUnusedApplicants",Boolean,False,"Show Unused Applicant in the Applicants Block."

'#Const "ShowCollateralItemReference",Boolean,False,"Show Reference column in Collateral Items table."
'#const "ShowUnusedCollateralItems",Boolean,False,"Show Unused Collateral Items in the Collateral Items table."

'#const "EventNumberOfDays",Integer,30,"Number of Days of Events to display."
'#const "EventAuditInclude",Boolean,False,"Include Audit Events?"
'#const "ShowEventBlock",Boolean,False,"Show Event block?"

' Constants
Private ShowApplicantAddressInSummary As Boolean = False
Private ShowApplicantCity As Boolean = True
Private ShowApplicantAddressFull As Boolean = False
Private ShowApplicantContactMethods As Boolean = False
Private ShowApplicantDateOfBirth As Boolean = False
Private ShowRelatedViaHashtagsBlock As Boolean = True
Private ShowUnusedApplicants As Boolean = False

Private ShowCollateralItemReference As Boolean = False
Private ShowUnusedCollateralItems As Boolean = False

Private EventAuditInclude As Boolean = False
Private EventNumberOfDays As Integer = 30
Private ShowEventBlock As Boolean = False

' 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
  If TypeOf source Is String Then
    mAccountApp = finBL.CreateAccountApp()
    mAccountApp.Load(CStr(source))

    ' Check not filtered out
    If finBL.CurrentUserInformation.IsAccountAppFilteredOut(mAccountApp.AccountAppId) Then
      Throw New ApplicationException(finBL.Error.Message)
    End If
  Else
    mAccountApp = DirectCast(source, finAccountApp)
  End If

  ' Get Constants
  With ScriptInfo.Constants
    ShowApplicantAddressInSummary = .GetBoolean("ShowApplicantAddressInSummary", ShowApplicantAddressInSummary)
    ShowApplicantCity = .GetBoolean("ShowApplicantCity", ShowApplicantCity)
    ShowApplicantAddressFull = .GetBoolean("ShowApplicantAddressFull", ShowApplicantAddressFull)
    ShowApplicantContactMethods = .GetBoolean("ShowApplicantContactMethods", ShowApplicantContactMethods)
    ShowApplicantDateOfBirth = .GetBoolean("ShowApplicantDateOfBirth", ShowApplicantDateOfBirth)
    ShowRelatedViaHashtagsBlock = .GetBoolean("ShowRelatedViaHashtagsBlock", ShowRelatedViaHashtagsBlock)
    ShowUnusedApplicants = .GetBoolean("ShowUnusedApplicants", ShowUnusedApplicants)

    ShowUnusedCollateralItems = .GetBoolean("ShowUnusedCollateralItems", ShowUnusedCollateralItems)
    ShowCollateralItemReference = .GetBoolean("ShowCollateralItemReference", ShowCollateralItemReference)
    
    ShowEventBlock = .GetBoolean("ShowEventBlock", ShowEventBlock)
    EventAuditInclude = .GetBoolean("EventAuditInclude", EventAuditInclude)
    EventNumberOfDays = .GetInteger("EventNumberOfDays", EventNumberOfDays)
  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 Ok As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim SummaryTable As ISSummaryTable
  Dim Warnings As finWarnings
  Dim WorkflowIncompleteCount As Integer

  ' Assume Success
  Ok = True

  ' Initialise
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()

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

  If mAccountApp.IsNew Then
    ' No Account Application loaded
    sb.SectionColumnsAddQuickBlock("Summary", "{{info|Please enter a code to view an Account Application record.}}")
  Else
    ' Client
    ' Begin Section
    sb.SectionColumnsBegin()

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

    ' Summary
    sb.BlockBegin("Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountApp_Summary(ScriptInfo, mAccountApp, Me.ScriptInfo.FormKey <> "AccountApps", ShowApplicantAddressInSummary))
    sb.BlockEnd()

    ' Applicants
    sb.BlockBegin("Applicants")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountApp_Applicants(ScriptInfo, mAccountApp, ShowUnusedApplicants, ShowApplicantCity, ShowApplicantAddressFull, ShowApplicantContactMethods, ShowApplicantDateOfBirth, True, True, True, 20, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()

    ' Collateral Items
    If finBL.Settings.AddOns.SecurityRegister AndAlso mAccountApp.CollateralItems.Count <> 0 Then
      sb.BlockBegin("Collateral")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountApp_CollateralItems(ScriptInfo, mAccountApp, ShowUnusedCollateralItems, ShowCollateralItemReference, False, False, False, False, True, 20, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()
    End If

    ' Workflows
    If finBL.Settings.AddOns.Workflow AndAlso Not mAccountApp.IsNew Then
      mAccountApp.GetIncompleteWorkflowsCount(WorkflowIncompleteCount)
      sb.BlockBegin("Workflows", "", If(WorkflowIncompleteCount = 0, isefinSummaryPageBlockHeadingStyle.Normal, isefinSummaryPageBlockHeadingStyle.WarningLow))
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountApp_Workflows(ScriptInfo, mAccountApp, True, True, True, , ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()
    End If

    ' Related via Hashtags
    If ShowRelatedViaHashtagsBlock AndAlso finBL.Settings.QuickSearchUseVersion2 AndAlso finBL.Settings.QuickSearch2SupportHashtags Then
      SummaryTable = finBL.Utilities.GetSummaryTableOfHashTagReferences(isefinSearchSourceType.AccountApp, mAccountApp.Pk, mAccountApp.AccountAppId, mAccountApp.Notes, "Related via Hashtags")

      If SummaryTable IsNot Nothing Then
        sb.BlockBegin("Related via Hashtags")
        sb.SummaryTableAppend(SummaryTable)
        sb.BlockEnd()
      End If
    End If

    ' End Column 1
    sb.ColumnEnd()

    ' Begin Column 2
    sb.ColumnBegin("40%", "25em", "50em")

    ' Warnings
    Warnings = mAccountApp.GetWarnings()
    sb.BlockBegin("Warnings", "", If(Warnings.Count = 0, isefinSummaryPageBlockHeadingStyle.Normal, isefinSummaryPageBlockHeadingStyle.Warning))
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.WarningsSummary(ScriptInfo, Warnings))
    sb.BlockEnd()

    ' Monitoring
    If finBL.Settings.MonitorCategoriesUsedAccountApp AndAlso Not mAccountApp.IsNew Then
      sb.BlockBegin("Monitoring", "", If(mAccountApp.MonitorCategoryAPk = 0 AndAlso mAccountApp.MonitorCategoryBPk = 0 AndAlso mAccountApp.MonitorCategoryCPk = 0, isefinSummaryPageBlockHeadingStyle.Normal, isefinSummaryPageBlockHeadingStyle.Warning))
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountApp_Monitoring(ScriptInfo, mAccountApp, False, False))
      sb.BlockEnd()
    End If

    ' Events
    If ShowEventBlock Then
      sb.BlockBegin("Events")
      sb.SummaryTableAppend(finBL.SummaryPageStandardBlocks.Events_Summary(ScriptInfo, mAccountApp.GetEvents(EventNumberOfDays, EventAuditInclude), Nothing))
      sb.BlockEnd()
    End If
    
    ' End Column 2
    sb.ColumnEnd()

    ' End Section
    sb.SectionColumnsEnd()
  End If

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function