Imports CAMS_ADMIN
Imports CAMS_BL
Imports System.Data
Imports System.Text

Public Class frmGrantTypes
    Inherits CAMSBasePage

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object
    Protected WithEvents txtGrantType As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtAbbreviation As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtThreshold As System.Web.UI.WebControls.TextBox
    Protected WithEvents dgResults As System.Web.UI.WebControls.DataGrid
    Protected WithEvents lblTotalRecords As System.Web.UI.WebControls.Label
    Protected WithEvents lblTotalPages As System.Web.UI.WebControls.Label
    Protected WithEvents txtRecordsPerPage As System.Web.UI.WebControls.TextBox
    Protected WithEvents imgBtnRefresh As System.Web.UI.WebControls.ImageButton
    Protected WithEvents pnlResults As System.Web.UI.WebControls.Panel
    Protected WithEvents lblMsgNoResults As System.Web.UI.WebControls.Label
    Protected WithEvents pnlNoResults As System.Web.UI.WebControls.Panel
    Protected WithEvents hdEvent As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdAction As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdRowId As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents lblValAbbrev As System.Web.UI.WebControls.Label
    Protected WithEvents PagingBar1 As PACNET.WebControls.PagingBar
    Protected WithEvents mnuCams As Menu

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Const FIRST_PAGE As Integer = 1

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Access Validation to page
        If Not Me.IsPostBack Then
            Dim oEC_User As EC_User = Me.GetUserFromSession

            If Not oEC_User Is Nothing Then
                If Me.HasAccessTo(Me.GetControlID("frmGrantTypes.aspx"), oEC_User.AccessGroup) = False Then
                    Me.RedirectToMsgPage(Constants.CONST_ERROR_ACCESSDENIED_TOPAGE)
                End If
            End If

        End If
        'End Access Validation

        'Menu
        mnuCams.CurrentGroup = Constants.MENU_MAINTENANCE_ID
        mnuCams.CurrentItem = Constants.MENU_MAINTENANCE_GRANTTYPE_ID
        'End Menu

        'Attributes for some controls
        txtRecordsPerPage.Attributes.Add("onKeyPress", "fnOnlyDigits(event,'decNo')")
        lblValAbbrev.Visible = False

        If txtRecordsPerPage.Text.Trim.Equals(String.Empty) OrElse Convert.ToInt32(txtRecordsPerPage.Text) < 1 Then
            txtRecordsPerPage.Text = "100"
        End If
        'End attributes

        'Initial values
        If Not Me.IsPostBack Then
            hdAction.Value = "I" 'setting as default action, I = Insert
            RefreshResults(FIRST_PAGE, txtRecordsPerPage.Text)
        End If
        'End Initial values

        'Postback
        If IsPostBack Then
            If hdEvent.Value = "save" Then
                Save()
            ElseIf hdEvent.Value = "selrow" Then
                SelectRow(Convert.ToInt32(hdRowId.Value))
                ShowItem(Convert.ToInt32(hdRowId.Value))
            ElseIf hdEvent.Value = "cancel" Then
                SetCancel()
            ElseIf hdEvent.Value = "add" Then
                btnAdd_Click()
            End If

        End If
        'End postback

        'Added by Raul Callupe
        If Me.IsPostBack = False Then
            Dim stbOutput As New StringBuilder
            stbOutput.Append("<script language='Javascript'>")
            stbOutput.Append("var pag = window.parent.frames(0);")
            stbOutput.Append("pag.document.all.lblPath.innerHTML='Maintenance/Grant Types'")
            stbOutput.Append("</script>")
            Response.Write(stbOutput.ToString)
        End If
        'End Added

    End Sub

#Region "Refresh datagrid"

    Private Sub setNoRecordsFounds(ByVal bValue As Boolean)
        pnlNoResults.Visible = bValue
        pnlResults.Visible = Not bValue
    End Sub

    Private Sub RefreshResults(ByVal nPage As Integer, ByVal nRecordsPerPage As Integer)
        Dim oCC_GrantType As New CC_GrantType
        Dim dsResults As DataSet

        Try
            dsResults = oCC_GrantType.GetGrantTypes(nPage, txtRecordsPerPage.Text)

            If Not dsResults Is Nothing Then
                If dsResults.Tables(0).Rows.Count = 0 Then
                    setNoRecordsFounds(True)
                ElseIf dsResults.Tables(0).Rows(0)("record_count") < Convert.ToInt32(txtRecordsPerPage.Text) Then
                    PagingBar1.TotalNPages = FIRST_PAGE
                Else
                    Dim dDec = dsResults.Tables(0).Rows(0)("record_count") / Convert.ToInt32(txtRecordsPerPage.Text)
                    Dim nEnt = Decimal.Round(dDec, 0)
                    Dim nPages As Integer

                    If dDec > nEnt Then
                        nPages = nEnt + 1
                    Else
                        nPages = nEnt
                    End If
                    PagingBar1.TotalNPages = nPages
                End If

                If dsResults.Tables(0).Rows.Count > 0 Then
                    dgResults.CurrentPageIndex = 0
                    dgResults.DataSource = dsResults
                    dgResults.DataBind()

                    PagingBar1.PagesDisplayed = Convert.ToInt32(txtRecordsPerPage.Text)
                    PagingBar1.CurrentPage = nPage
                    lblTotalRecords.Text = dsResults.Tables(0).Rows(0)("record_count").ToString()
                    lblTotalPages.Text = PagingBar1.TotalNPages

                    setNoRecordsFounds(False)
                Else
                    setNoRecordsFounds(True)
                End If
            Else
                setNoRecordsFounds(True)
            End If

        Catch ex As Exception
            dsResults = Nothing
            setNoRecordsFounds(True)
        End Try

    End Sub

#End Region

#Region "Controls' events"

    Private Sub btnAdd_Click()
        txtGrantType.Text = String.Empty
        txtAbbreviation.Text = String.Empty
        txtThreshold.Text = String.Empty

        'setting actions
        hdEvent.Value = ""
        hdAction.Value = "I"
        hdRowId.Value = ""
    End Sub

    Private Sub SetCancel()
        Me.RedirectToHome()
    End Sub

#End Region

#Region "Save"

    Private Sub Save()
        If hdAction.Value.Equals("I") Then
            Insert()
        ElseIf hdAction.Value.Equals("U") Then
            Update()
        ElseIf hdAction.Value.Equals("D") Then
            Delete()
        End If

    End Sub

    Private Sub Insert()
        Dim oEC_GrantType As EC_GrantType = SetProperties()
        Dim oCC_GrantType As New CC_GrantType
        Dim strMsg As String

        If Not oEC_GrantType Is Nothing Then
            If oCC_GrantType.SaveGrantType(oEC_GrantType, strMsg) Then
                Me.btnAdd_Click()
                RefreshResults(FIRST_PAGE, Convert.ToInt32(txtRecordsPerPage.Text))
            Else
                If strMsg = "EXISTS" Then
                    lblValAbbrev.Text = "Already exists in database"
                    lblValAbbrev.Visible = True
                End If
            End If

        End If

    End Sub

    Private Sub Update()
        Dim oEC_GrantType As EC_GrantType = SetProperties()
        Dim oCC_GrantType As New CC_GrantType
        Dim strMsg As String

        If Not oEC_GrantType Is Nothing Then
            If oCC_GrantType.UpdateGrantType(oEC_GrantType, strMsg) = False Then
                If strMsg = "NOEXISTS" Then
                    lblValAbbrev.Text = "Doesn't exist in database"
                    lblValAbbrev.Visible = True
                End If
            Else
                RefreshResults(FIRST_PAGE, Convert.ToInt32(txtRecordsPerPage.Text))

                If hdRowId.Value.Trim.Equals(String.Empty) = False Then
                    SelectRow(Convert.ToInt32(hdRowId.Value))
                    ShowItem(Convert.ToInt32(hdRowId.Value))
                End If
            End If

        End If
    End Sub

    Private Sub Delete()
        Dim oEC_GrantType As EC_GrantType = SetProperties()
        Dim oCC_GrantType As New CC_GrantType
        Dim strMsg As String

        If Not oEC_GrantType Is Nothing Then
            If oCC_GrantType.DeleteGrantOffice(oEC_GrantType, strMsg) = False Then
                If strMsg = "NOEXISTS" Then
                    lblValAbbrev.Text = "Doesn't exist in database"
                    lblValAbbrev.Visible = True
                End If
            Else
                btnAdd_Click()
                RefreshResults(FIRST_PAGE, Convert.ToInt32(txtRecordsPerPage.Text))
            End If

        End If
    End Sub

    Private Function SetProperties() As EC_GrantType
        Dim oEC_GrantType As New EC_GrantType
        Dim oEC_User As EC_User = Me.GetUserFromSession

        With oEC_GrantType
            .GrantTypeID = txtAbbreviation.Text.Trim.ToUpper
            .Description = txtGrantType.Text.Trim.ToUpper
            .Threshold = txtThreshold.Text.Trim

            If Not oEC_User Is Nothing Then
                .AddedBy = oEC_User.Login
                .ChangedBy = oEC_User.Login
            End If

        End With

        Return oEC_GrantType

    End Function

#End Region

#Region "Common methods"

    Private Sub dgResults_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgResults.ItemDataBound
        If e.Item.ItemType <> ListItemType.Header Then
            e.Item.Cells(0).Text = "<a href=""#"" onclick=""fnSelRow(" & e.Item.ItemIndex.ToString & ")""><img src=""../images/view.gif"" border=0></a>"
        End If

    End Sub

    Private Sub SelectRow(ByVal rowID As Integer)

        Try
            If dgResults.Items.Count > 0 Then
                'Setting the action as an updating
                hdAction.Value = "U"

                For i As Integer = 0 To dgResults.Items.Count - 1
                    If i Mod 2 = 0 Then
                        dgResults.Items(i).CssClass = "PAGE_GRID_ITEM"
                    Else
                        dgResults.Items(i).CssClass = "PAGE_GRID_ALTERNATING"
                    End If
                Next

                dgResults.Items(rowID).CssClass = "PAGE_GRID_ITEM_SELECTED"
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub ShowItem(ByVal rowID As Integer)
        Dim strItemID As String

        Try
            If dgResults.Items.Count > 0 Then

                strItemID = dgResults.Items(rowID).Cells(2).Text.Trim

                If strItemID.Equals(String.Empty) = False Then
                    LoadItem(strItemID)
                End If
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub LoadItem(ByVal prmItemID As String)
        Dim oCC_GrantType As New CC_GrantType
        Dim oEC_GrantType As EC_GrantType = oCC_GrantType.GetGrantTypeByID(prmItemID)

        If Not oEC_GrantType Is Nothing Then
            With oEC_GrantType
                txtGrantType.Text = .Description
                txtAbbreviation.Text = .GrantTypeID
                txtThreshold.Text = .Threshold
            End With
        End If
    End Sub

#End Region

#Region "Pagination"

    Private Sub txtRecordsPerPage_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRecordsPerPage.TextChanged
        PagingBar1.CurrentPage = FIRST_PAGE
        PagingBar1.TotalNPages = FIRST_PAGE
        PagingBar1.PagesDisplayed = txtRecordsPerPage.Text
        RefreshResults(FIRST_PAGE, txtRecordsPerPage.Text)
    End Sub

    Private Sub PagingBar1_ChangePage(ByVal sender As Object, ByVal e As PACNET.WebControls.ChangePageEventArgs) Handles PagingBar1.ChangePage
        RefreshResults(e.SelectedPage, txtRecordsPerPage.Text)
    End Sub

#End Region

End Class
