Imports System.Web.Configuration
Imports Superbowl.sqlDB

Partial Public Class PricipalDetails
  Inherits System.Web.UI.Page

  Public Shared PrincipalID As Integer = 0
  Public Shared dwPrincipal As String = ""
  Private db As New sqlDB
  Private ds As DataSet
  Private dr, drIni As DataRow

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not Page.IsPostBack Then
      WebConfigurationManager.AppSettings("strPage") = "PrincipalDetails"
      If WebConfigurationManager.AppSettings("ULEVEL") <> 5 Then
        btnSave.Enabled = False
      End If
      Try
        PrincipalID = Request.QueryString("PID")
      Catch ex As Exception
        PrincipalID = 0
      End Try

      If PrincipalID <> 0 Then
        Try
          drIni = sqlDB.GetRow(db.sql_get_Principal(PrincipalID))
          strPrincipal.Text = drIni!strPrincipal
          dwPrincipal = drIni!strPrincipal
          txtFYStart.Text = drIni!strFYStart
          txtFYEnd.Text = drIni!strFYEnd

          lblLastEdit.Text = Nz(drIni!strLastUser, "unknown") & " - " & SqlDate(Nz(drIni!dtStamp, ""), "d MMMM yyyy HH:mm")

        Catch ex As IndexOutOfRangeException                'dr = ds.tables(0).rows(0) doesn't exist
          lblMessage.Text = "Principal ID, <font color=black>" & PrincipalID & "</font>, was not found!"
          drIni = Nothing
        End Try
      Else
        dwPrincipal = ""
      End If
    End If
  End Sub

  Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
    lblMessage.Text = ""

    If strPrincipal.Text.Trim() = "" Then lblMessage.Text &= "Principal name required.<BR>"
    If txtFYStart.Text.Trim() = "" Then lblMessage.Text &= "Fin Year Start required.<BR>"
    If txtFYEnd.Text.Trim() = "" Then lblMessage.Text &= "Fin Year End required.<BR>"

    If txtFYStart.Text.Trim.Length < 2 Then txtFYStart.Text = "0" & txtFYStart.Text.Trim
    If txtFYEnd.Text.Trim.Length < 2 Then txtFYStart.Text = "0" & txtFYEnd.Text.Trim

    If sqlDB.CountRows(db.sql_get_Principal(strPrincipal.Text.Trim, PrincipalID)) > 0 Then lblMessage.Text &= "Principal """ & strPrincipal.Text.Trim & """ already exists!"

    If Not lblMessage.Text.Length > 0 Then

      If PrincipalID <> 0 Then
            sqlDB.doQueryDS(db.sql_update_Principal(PrincipalID, strPrincipal.Text.Trim, txtFYStart.Text.Trim, txtFYEnd.Text.Trim, Session.Item("User")))
        '2-Update into DW
        sqlDB.doQueryDS(db.sql_update_Principal_dw(dwPrincipal, strPrincipal.Text.Trim, txtFYStart.Text.Trim, txtFYEnd.Text.Trim))

      Else
            sqlDB.doQueryDS(db.sql_insert_Principal(strPrincipal.Text.Trim, txtFYStart.Text.Trim, txtFYEnd.Text.Trim, Session.Item("User")))
      End If

      Response.Redirect("PrincipalList.aspx")
    End If

  End Sub

  Protected Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
    Response.Redirect("PrincipalList.aspx")
  End Sub

End Class