Imports System.Data.OleDb
Imports AcggDoc.Acgg.dbClass

Namespace Acgg

    Public Class ControlPanel
        Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub
        Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
        Protected WithEvents optOpen As System.Web.UI.WebControls.RadioButton
        Protected WithEvents optClosed As System.Web.UI.WebControls.RadioButton
        Protected WithEvents optAll As System.Web.UI.WebControls.RadioButton
        Protected WithEvents optCancelled As System.Web.UI.WebControls.RadioButton
        Protected WithEvents Label4 As System.Web.UI.WebControls.Label
        Protected WithEvents SortExp As System.Web.UI.WebControls.Label
        Protected WithEvents Label9 As System.Web.UI.WebControls.Label
        Protected WithEvents cmbDevelopment As System.Web.UI.WebControls.DropDownList
        Protected WithEvents Label1 As System.Web.UI.WebControls.Label
        Protected WithEvents lnkUpload As System.Web.UI.WebControls.LinkButton

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        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
        Dim sConnString As String
        Dim strStatus As String

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            sConnString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Database Password=;Data Source=" & Server.MapPath("AcggDoc.mdb") & ";Password=;Jet OLEDB:Engine Type=5;Jet OLEDB:Global Bulk Transactions=1;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=False"

            If Session("UserId") = "" Then
                Response.End()
            End If

            If optOpen.Checked = True Then
                strStatus = "OPEN"
            ElseIf optCancelled.Checked = True Then
                strStatus = "CANCELLED"
            ElseIf optClosed.Checked = True Then
                strStatus = "CLOSED"
            ElseIf optAll.Checked = True Then
                strStatus = "OPEN','CLOSED','CANCELLED"
            End If

            If Not Page.IsPostBack Then
                SortExpression = "A.FileNo"
                BindDevCombo()
                BindGrid(strStatus)
            End If

            If Session("UserCat") = 1 Or Session("UserCat") = 2 Then
                lnkUpload.Visible = True
                lnkUpload.Attributes.Add("onclick", "javascript:window.open('AddOrder.aspx');")
            End If

        End Sub

        Private Sub BindDevCombo()
            Dim objDBClass As New Acgg.dbClass
            Dim strQuery As String = "SELECT DevID,DevName FROM Developments"
            Try
                'objDBClass.strConn = sConnString
                objDBClass.OpenDB()
                cmbDevelopment.DataSource = objDBClass.retReader(strQuery)
                cmbDevelopment.DataMember = "DevId"
                cmbDevelopment.DataTextField = "DevName"
                cmbDevelopment.DataValueField = "DevId"
                cmbDevelopment.DataBind()
                objDBClass.CloseDB()
            Catch Exc As Exception
                Label4.Text = "Error: " & Exc.Message
            Finally
                objDBClass = Nothing
            End Try
        End Sub

        Private Sub BindGrid(ByVal strFileStatus As String)
            Dim objDBClass As New Acgg.dbClass
            Dim strQuery As String
            Dim TotalRow As Integer

            If Session("UserCat") = 1 Or Session("UserCat") = 2 Then
                strQuery = "SELECT A.OrdId,A.FileNo,mid(B.PropDesc,1,60)+'...' as PropDesc1,A.OpenDate,A.DueDate,A.PrjClDate,A.ClDate,C.DevName,A.Status FROM NewOrders A, Property B , Developments C WHERE A.Status IN ('" & strFileStatus & "') AND A.PropId=B.PropId AND B.DevId=C.DevId AND C.DevId=" & cmbDevelopment.SelectedValue
            Else
                strQuery = "SELECT A.OrdId,A.FileNo,mid(B.PropDesc,1,60)+'...' as PropDesc1,A.OpenDate,A.DueDate,A.PrjClDate,A.ClDate,C.DevName,A.Status FROM NewOrders A, Property B , Developments C WHERE A.Status IN ('" & strFileStatus & "') AND A.PropId=B.PropId AND B.DevId=C.DevId AND A.TitleCoUserId='" & Session("UserId") & "' AND C.DevId=" & cmbDevelopment.SelectedValue
            End If

            If SortExpression.Length > 0 Then
                If InStr(SortExpression, "A.", CompareMethod.Text) Then
                    strQuery &= " Order By " & SortExpression
                Else
                    strQuery &= " Order By " & "A." & SortExpression
                End If

                If SortAscending Then
                    strQuery &= " ASC"
                Else
                    strQuery &= " DESC"
                End If
            End If

            Try
                objDBClass.OpenDB()
                DataGrid1.DataSource = objDBClass.retDataSet(strQuery)
                Dim MyDataSet As DataSet
                DataGrid1.DataBind()
                objDBClass.CloseDB()

            Catch Exc As Exception
                Label4.Text = "Error: " & Exc.Message
            Finally
                objDBClass = Nothing
            End Try

        End Sub

        Public Sub optClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optOpen.CheckedChanged
            BindGrid(strStatus)
        End Sub

        Sub xxx(ByVal Sender As Object, ByVal E As DataGridSortCommandEventArgs)
            'DataGrid1.CurrentPageIndex = 0 'To sort from top
            'Dim strSortExp As String = SortExp.Text

            'If strSortExp = "asc" Then
            'strSortExp = "desc"
            'Else
            '    strSortExp = "asc"
            'End If

            'BindGrid(strStatus, E.SortExpression.ToString(), strSortExp) 'Rebind our Datagrid
        End Sub

        Sub Paginate(ByVal Source As Object, ByVal E As DataGridPageChangedEventArgs)
            DataGrid1.CurrentPageIndex = E.NewPageIndex
            BindGrid(strStatus)
        End Sub
        Public Sub cmbDevelopment_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDevelopment.SelectedIndexChanged
            BindGrid(strStatus)
        End Sub
        Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated


            If e.Item.ItemType = ListItemType.Pager Then
                Dim myarr As New ArrayList

                Dim mybtn As New Label
                mybtn.Text = "Pages "

                Dim oControl As Control
                Dim x As Integer
                x = 0
                For Each oControl In e.Item.Cells(0).Controls
                    If x > 0 Then Exit For
                    If oControl.GetType.ToString = "System.Web.UI.WebControls.DataGridLinkButton" Then
                        'myarr(x) = CType(oControl, LinkButton).Text
                        CType(oControl, LinkButton).Text = "Page " & CType(oControl, LinkButton).Text
                        x = x + 1
                    ElseIf TypeOf oControl Is Label Then
                        'myarr(x) = CType(oControl, LinkButton).Text
                        CType(oControl, Label).Text = "Page " & CType(oControl, Label).Text
                        x = x + 1
                    End If
                Next

                'e.Item.Cells(0).Controls.Add(mybtn)
                'x = 0

                'For Each oControl In e.Item.Cells(0).Controls
                '    'If x > 0 Then Exit For
                '    If oControl.GetType.ToString = "System.Web.UI.WebControls.DataGridLinkButton" Then
                '        'CType(oControl, LinkButton).Text = CType(oControl, LinkButton).Text
                '        CType(oControl, LinkButton).Text = myarr(x)
                '        x = x + 1
                '    ElseIf TypeOf oControl Is Label Then
                '        CType(oControl, Label).Text = myarr(x)
                '        'CType(oControl, Label).Text = CType(oControl, Label).Text
                '        x = x + 1
                '    End If
                'Next

            End If
        End Sub
        Public Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
            If e.SortExpression = Me.SortExpression Then
                SortAscending = Not SortAscending
            Else
                SortAscending = True
            End If
            Me.SortExpression = e.SortExpression
            BindGrid(strStatus)
        End Sub
        Private Property SortExpression() As String
            Get
                Dim o As Object = ViewState("SortExpression")
                If o Is Nothing Then
                    Return String.Empty
                Else
                    Return o.ToString
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("SortExpression") = Value
            End Set
        End Property
        Private Property SortAscending() As Boolean
            Get
                Dim o As Object = viewstate("SortAscending")
                If o Is Nothing Then
                    Return True
                Else
                    Return Convert.ToBoolean(o)
                End If
            End Get
            Set(ByVal Value As Boolean)
                viewstate("SortAscending") = Value
            End Set
        End Property
    End Class
End Namespace