Imports CAMS_ADMIN
Imports CAMS_BL
Imports System.Data
Imports System.Text

Public Class frmReminders
    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 btnSave As System.Web.UI.WebControls.Button
    Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
    Protected WithEvents txtThresholdM As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtThresholdW As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtTTTRBRT As System.Web.UI.WebControls.TextBox
    Protected WithEvents dgGTT As System.Web.UI.WebControls.DataGrid
    Protected WithEvents dgRS As System.Web.UI.WebControls.DataGrid
    Protected WithEvents hdEvent As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdRowId_GTT As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdItemID_GTT As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdRowId_RS As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hdItemID_RS As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents lblMsgGTT As System.Web.UI.WebControls.Label
    Protected WithEvents lblThresholdGTT As System.Web.UI.WebControls.Label
    Protected WithEvents lblThresholdRS As System.Web.UI.WebControls.Label
    Protected WithEvents lblFooterRS As System.Web.UI.WebControls.Label
    Protected WithEvents lblMsgRS As System.Web.UI.WebControls.Label
    Protected WithEvents lblTTTRBRT As System.Web.UI.WebControls.Label
    Protected WithEvents btnRefreshGTT As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents btnRefreshRS As System.Web.UI.HtmlControls.HtmlInputButton
    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 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("frmReminders.aspx"), oEC_User.AccessGroup) = False Then
                    Me.RedirectToMsgPage(Constants.CONST_ERROR_ACCESSDENIED_TOPAGE)
                End If
            End If

            ValidateUser()

        End If
        'End Access Validation

        mnuCams.CurrentGroup = Constants.MENU_SETTINGS_ID
        mnuCams.CurrentItem = Constants.MENU_SETTINGS_THRESHOLD_ID

        txtThresholdM.Attributes.Add("onKeyPress", "fnOnlyDigits(event,'decNo')")
        txtThresholdW.Attributes.Add("onKeyPress", "fnOnlyDigits(event,'decNo')")
        txtTTTRBRT.Attributes.Add("onKeyPress", "fnOnlyDigits(event,'decNo')")

        If IsPostBack Then
            If hdEvent.Value = "selrow_GTT" Then
                SelectRow_GTT(Convert.ToInt32(hdRowId_GTT.Value))
                'Load selected item 
                ShowItem_GTT(Convert.ToInt32(hdRowId_GTT.Value))
            ElseIf hdEvent.Value = "selrow_RS" Then
                SelectRow_RS(Convert.ToInt32(hdRowId_RS.Value))
                'Load selected item 
                ShowItem_RS(Convert.ToInt32(hdRowId_RS.Value))
            ElseIf hdEvent.Value = "update_GTT" Then
                UpdateGridGTT()
                hdEvent.Value = ""
            ElseIf hdEvent.Value = "update_RS" Then
                UpdateGridRS()
                hdEvent.Value = ""
            End If
        Else
            'Load datagrids
            LoadDatagrids()

        End If

        '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='Settings/Thresholds Reminders'")
            stbOutput.Append("</script>")
            Response.Write(stbOutput.ToString)
        End If
        'End Added

    End Sub

    Private Sub ValidateUser()
        Dim oUser As EC_User = Me.GetUserFromSession

        If Not oUser Is Nothing Then

            If oUser.AccessGroup = Constants.CONST_ACCESSGROUP_MA Then
                btnSave.Visible = True
                btnRefreshGTT.Visible = True
                txtThresholdM.Visible = True
                btnRefreshRS.Visible = True
                txtTTTRBRT.Visible = True
                txtThresholdW.Visible = True
                lblThresholdGTT.Visible = True
                lblThresholdRS.Visible = True
                lblTTTRBRT.Visible = True
            Else
                btnSave.Visible = False
                btnRefreshGTT.Visible = False
                txtThresholdM.Visible = False
                btnRefreshRS.Visible = False
                txtTTTRBRT.Visible = False
                txtThresholdW.Visible = False
                lblThresholdGTT.Visible = False
                lblThresholdRS.Visible = False
                lblTTTRBRT.Visible = False
            End If
        End If

    End Sub

    Private Sub SetNoRowFoundGTT(ByVal booValue As Boolean)
        dgGTT.Visible = Not booValue
        lblThresholdGTT.Visible = Not booValue
        txtThresholdM.Visible = Not booValue
        btnRefreshGTT.Visible = Not booValue
        lblMsgGTT.Visible = booValue

        ValidateUser()
    End Sub

    Private Sub SetNoRowFoundRS(ByVal booValue As Boolean)
        dgRS.Visible = Not booValue
        lblThresholdRS.Visible = Not booValue
        txtThresholdW.Visible = Not booValue
        lblTTTRBRT.Visible = Not booValue
        txtTTTRBRT.Visible = Not booValue
        btnRefreshRS.Visible = Not booValue
        lblFooterRS.Visible = Not booValue
        lblMsgRS.Visible = booValue

        ValidateUser()
    End Sub

    Private Sub LoadDatagrids()
        Dim oCC_Rem As New CC_Reminder
        Dim dsGTT As DataSet
        Dim dsRS As DataSet

        Try
            dsGTT = oCC_Rem.GetGrantTypes

            If Not dsGTT Is Nothing Then
                If dsGTT.Tables(0).Rows.Count > 0 Then
                    dgGTT.DataSource = dsGTT.Tables(0).DefaultView
                    dgGTT.DataBind()
                    SetNoRowFoundGTT(False)
                Else
                    SetNoRowFoundGTT(True)
                End If
            Else
                SetNoRowFoundGTT(True)
            End If
        Catch ex As Exception
            SetNoRowFoundGTT(True)
        End Try

        Try
            dsRS = oCC_Rem.GetReminders

            If Not dsRS Is Nothing Then
                If dsRS.Tables(0).Rows.Count > 0 Then
                    dgRS.DataSource = dsRS.Tables(0).DefaultView
                    dgRS.DataBind()
                    SetNoRowFoundRS(False)
                Else
                    SetNoRowFoundRS(True)
                End If
            Else
                SetNoRowFoundRS(True)
            End If
        Catch ex As Exception
            SetNoRowFoundRS(True)
        End Try

    End Sub

    Private Sub SelectRow_GTT(ByVal rowID As Integer)

        Try
            If dgGTT.Items.Count > 0 Then
                'Setting the action as an updating
                hdItemID_GTT.Value = dgGTT.Items(rowID).Cells(1).Text

                For i As Integer = 0 To dgGTT.Items.Count - 1
                    If i Mod 2 = 0 Then
                        dgGTT.Items(i).CssClass = "PAGE_GRID_ITEM"
                    Else
                        dgGTT.Items(i).CssClass = "PAGE_GRID_ALTERNATING"
                    End If
                Next

                dgGTT.Items(rowID).CssClass = "PAGE_GRID_ITEM_SELECTED"
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub SelectRow_RS(ByVal rowID As Integer)

        Try
            If dgRS.Items.Count > 0 Then
                'Setting the action as an updating
                hdItemID_RS.Value = dgRS.Items(rowID).Cells(1).Text

                For i As Integer = 0 To dgRS.Items.Count - 1
                    If i Mod 2 = 0 Then
                        dgRS.Items(i).CssClass = "PAGE_GRID_ITEM"
                    Else
                        dgRS.Items(i).CssClass = "PAGE_GRID_ALTERNATING"
                    End If
                Next

                dgRS.Items(rowID).CssClass = "PAGE_GRID_ITEM_SELECTED"
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub ShowItem_GTT(ByVal rowID As Integer)
        txtThresholdM.Text = String.Empty

        Dim lblHTm As Label = CType(dgGTT.Items(rowID).Cells(2).FindControl("lblTHm"), Label)

        Try
            If dgGTT.Items.Count > 0 Then

                If Not lblHTm Is Nothing Then
                    txtThresholdM.Text = Convert.ToInt32(lblHTm.Text)
                Else
                    txtThresholdM.Text = String.Empty
                End If

            End If
        Catch ex As Exception
            txtThresholdM.Text = String.Empty
        End Try

    End Sub

    Private Sub ShowItem_RS(ByVal rowID As Integer)
        txtThresholdW.Text = String.Empty
        txtTTTRBRT.Text = String.Empty

        Dim lblTHw As Label = CType(dgRS.Items(rowID).Cells(3).FindControl("lblTHw"), Label)
        Dim lblTTTRBRTw As Label = CType(dgRS.Items(rowID).Cells(4).FindControl("lblTTTRBRTw"), Label)

        If dgRS.Items(rowID).Cells(1).Text.Trim.Equals("2") Or dgRS.Items(rowID).Cells(1).Text.Trim.Equals("4") Then
            txtThresholdW.ReadOnly = True
            txtThresholdW.CssClass = "PAGE_CONTROL_READONLY"
        Else
            txtThresholdW.ReadOnly = False
            txtThresholdW.CssClass = "PAGE_CONTROL_TEXT"
        End If

        Try
            If dgRS.Items.Count > 0 Then
                Try
                    If Not lblTHw Is Nothing Then
                        txtThresholdW.Text = Convert.ToInt32(lblTHw.Text)
                    Else
                        txtThresholdW.Text = String.Empty
                    End If
                Catch
                    txtThresholdW.Text = String.Empty
                End Try


                Try
                    If Not lblTTTRBRTw Is Nothing Then
                        txtTTTRBRT.Text = Convert.ToInt32(lblTTTRBRTw.Text)
                    Else
                        txtTTTRBRT.Text = String.Empty
                    End If
                Catch
                    txtTTTRBRT.Text = String.Empty
                End Try

            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub UpdateGridGTT()
        If hdRowId_GTT.Value.Equals(String.Empty) = False Then
            Dim lblHTm As Label = CType(dgGTT.Items(hdRowId_GTT.Value).Cells(2).FindControl("lblTHm"), Label)

            If Not lblHTm Is Nothing Then
                lblHTm.Text = txtThresholdM.Text
            End If
        End If
    End Sub

    Private Sub UpdateGridRS()
        If hdRowId_RS.Value.Equals(String.Empty) = False Then
            Dim lblHTw As Label = CType(dgRS.Items(hdRowId_RS.Value).Cells(3).FindControl("lblTHw"), Label)
            Dim lblTTTRBRTw As Label = CType(dgRS.Items(hdRowId_RS.Value).Cells(4).FindControl("lblTTTRBRTw"), Label)

            If Not lblHTw Is Nothing Then
                lblHTw.Text = txtThresholdW.Text
                lblTTTRBRTw.Text = txtTTTRBRT.Text
            End If
        End If
    End Sub

    Private Sub dgGTT_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgGTT.ItemDataBound
        If e.Item.ItemType <> ListItemType.Header Then
            e.Item.Cells(0).Text = "<a href=""#"" onclick=""fnSelRow_GTT(" & e.Item.ItemIndex.ToString & ")""><img src=""../images/view.gif"" border=0></a>"

            Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
            Dim lblTHm As Label = CType(e.Item.FindControl("lblTHm"), Label)

            If Not drv Is Nothing Then
                If Not lblTHm Is Nothing Then
                    lblTHm.Text = drv("THRESHOLD")
                End If

            Else
                If Not lblTHm Is Nothing Then
                    lblTHm.Text = ""
                End If
            End If

        End If
    End Sub

    Private Sub dgRS_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgRS.ItemDataBound
        If e.Item.ItemType <> ListItemType.Header Then
            e.Item.Cells(0).Text = "<a href=""#"" onclick=""fnSelRow_RS(" & e.Item.ItemIndex.ToString & ")""><img src=""../images/view.gif"" border=0></a>"

            Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
            Dim lblTHm As Label = CType(e.Item.FindControl("lblTHw"), Label)
            Dim lblTTTRBRTw As Label = CType(e.Item.FindControl("lblTTTRBRTw"), Label)

            If Not drv Is Nothing Then
                If Not lblTHm Is Nothing Then
                    lblTHm.Text = drv("THRESHOLD")
                End If

                If Not lblTTTRBRTw Is Nothing Then
                    lblTTTRBRTw.Text = drv("TTTRBRT")
                End If

            Else
                If Not lblTHm Is Nothing Then
                    lblTHm.Text = ""
                End If

                If Not lblTTTRBRTw Is Nothing Then
                    lblTTTRBRTw.Text = ""
                End If
            End If

        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.RedirectToHome()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        saveReminders()
        saveGrantTypes()

        LoadDatagrids()
    End Sub

    Private Sub saveReminders()
        Dim oCC_Reminder As New CC_Reminder
        Dim oEC_Rem As EC_Reminder
        Dim lblTHw As Label
        Dim lblTTTRBRTw As Label

        For i As Integer = 0 To dgRS.Items.Count - 1
            oEC_Rem = New EC_Reminder

            Try
                lblTHw = CType(dgRS.Items(i).Cells(3).FindControl("lblTHw"), Label)
                lblTTTRBRTw = CType(dgRS.Items(i).Cells(4).FindControl("lblTTTRBRTw"), Label)

                oEC_Rem.ReminderID = Convert.ToInt32(dgRS.Items(i).Cells(1).Text.Trim)

                If Not lblTHw Is Nothing Then
                    oEC_Rem.Threshold = lblTHw.Text.Trim
                End If

                If Not lblTTTRBRTw Is Nothing Then
                    oEC_Rem.TTTRBRT = lblTTTRBRTw.Text.Trim
                End If

                oCC_Reminder.Update(oEC_Rem)
            Catch

            End Try

        Next
    End Sub

    Private Sub saveGrantTypes()
        Dim oCC_GT As New CC_GrantType
        Dim oEC_GT As EC_GrantType
        Dim lblTHm As Label

        For i As Integer = 0 To dgGTT.Items.Count - 1
            oEC_GT = New EC_GrantType

            Try
                lblTHm = CType(dgGTT.Items(i).Cells(2).FindControl("lblTHm"), Label)

                oEC_GT.GrantTypeID = dgGTT.Items(i).Cells(1).Text.Trim
                oEC_GT.Description = dgGTT.Items(i).Cells(3).Text.Trim

                If Not lblTHm Is Nothing Then
                    oEC_GT.Threshold = lblTHm.Text.Trim
                End If

                oCC_GT.UpdateGrantType(oEC_GT)
            Catch

            End Try

        Next
    End Sub

End Class
