' SummaryPage2_CreditBureauDefaults_ItemSummary
Option Explicit On
Option Strict On

' #################################################################
' Account information to diplay in the Credit Bureau Defaults wizard
'
' Version: 2.02 (26/04/2016)
'
' Usage: Credit Bureau Defaults and Account Credit Bureau Default wizards
' #################################################################

'#const "IncludeAllServices",Boolean,False,"Indicates whether to include all Services in the Previous Uploads table."
'#const "LastPaymentDaysMaximum",Integer,120,"Maximum Days for warning since last Reversed Payment."
'#const "ShowClientAddressInSummary",Boolean,False,"Show the Main Client's postal address in the Summary Block."

' Constants
Private IncludeAllServices As Boolean = False
Private LastPaymentDaysMaximum As Integer = 120
Private ShowClientAddressInSummary As Boolean = False

' Objects
Private mAccount As finAccount
Private mCreditBureauDefault As finCreditBureauDefault
Private mCreditBureauDefaults As finCreditBureauDefaults

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
  If TypeOf source Is finCreditBureauDefaults Then
    ' Summarising via the Credit Bureau Defaults wizard
    mCreditBureauDefaults = DirectCast(source, finCreditBureauDefaults)
    Index = eventArgs.GetInteger("Index", -1)
    If Index = -1 Then
      mCreditBureauDefault = Nothing
      mAccount = Nothing
    Else
      mCreditBureauDefault = mCreditBureauDefaults.Item(Index)
      mAccount = mCreditBureauDefault.Account
    End If
  Else
    ' Summarising via the Account Credit Bureau Default wizard
    mCreditBureauDefault = DirectCast(source, finCreditBureauDefault)
    mAccount = mCreditBureauDefault.Account
  End If

  ' Get Constants
  With ScriptInfo.Constants
    IncludeAllServices = .GetBoolean("IncludeAllServices", IncludeAllServices)
    LastPaymentDaysMaximum = .GetInteger("LastPaymentDaysMaximum", LastPaymentDaysMaximum)
    ShowClientAddressInSummary = .GetBoolean("ShowClientAddressInSummary", ShowClientAddressInSummary)
  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

  ' Assume Success
  Ok = True

  ' Initialise
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()

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

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

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

    ' Summary
    sb.BlockBegin("Summary", "", If(Len(mAccount.AlertMessage) = 0, isefinSummaryPageBlockHeadingStyle.Main, isefinSummaryPageBlockHeadingStyle.Warning), False)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_Summary(ScriptInfo, mAccount, True, ShowClientAddressInSummary))
    sb.BlockEnd()

    ' Financial Status
    If mAccount.Status = isefinAccountStatus.Open Then
      sb.BlockBegin("Financial Status", "", isefinSummaryPageBlockHeadingStyle.Normal, True)
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_FinancialStatus(ScriptInfo, mAccount))
      sb.BlockEnd()
    End If

    ' Monitoring
    If mAccount.Status = isefinAccountStatus.Open AndAlso finBL.Settings.MonitorCategoriesUsed Then
      sb.BlockBegin("Monitoring", "", If(mAccount.MonitorCategoryAPk = 0 AndAlso mAccount.MonitorCategoryBPk = 0 AndAlso mAccount.MonitorCategoryCPk = 0, isefinSummaryPageBlockHeadingStyle.Normal, isefinSummaryPageBlockHeadingStyle.Warning))
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_Monitoring(ScriptInfo, mAccount, False, False))
      sb.BlockEnd()
    End If

    ' End Column 1
    sb.ColumnEnd()

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

    ' Credit Bureau Defaults
    sb.BlockBegin("Credit Bureau Default - " & mCreditBureauDefault.CreditBureauService.ServiceName)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Account_CreditBureauDefaultDetails(ScriptInfo, mCreditBureauDefault, IncludeAllServices))
    sb.BlockEnd()

    ' 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