Imports System
Imports System.Data
Imports System.Data.SqlClient

Partial Class Admin_new_admin_user
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("AdminID") Is Nothing Then
            Response.Redirect("AdminLogin.aspx?sesn=expire")
            Exit Sub
        End If
        If Not IsPostBack Then

            If Session("AdminID") = Nothing Then
                Response.Redirect("AdminLogin.aspx")
            End If
            fillgrid()
            ClearFields()
            rdbstatus.Items(0).Selected = False
            rdbstatus.Items(1).Selected = False
            clearselected()
            gvAdmin.SelectedIndex = -1
            BtnDelete.Attributes.Add("onclick", "return confirm('Are you Sure! Want to Delete ? ');")
            Call CancelClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
            txtName.Enabled = False
            txtPassword.Enabled = False
            rdbstatus.Enabled = False
            LoadCategory()
            listcategory.Enabled = False

        End If
    End Sub
    Protected Sub ClearFields()
        txtName.Text = ""
        txtPassword.Text = ""
    End Sub
    Protected Sub clearselected()
        Dim i, j As Integer
        i = listcategory.Items.Count
        For j = 0 To i - 1
            listcategory.Items(j).Selected = False
        Next
    End Sub
    Public Sub LoadCategory()
        Dim dt As DataTable
        dt = ReturnDataTable("Select Category_KId, Category_Name from Category_Master where Category_ParentId ='Root Category' and Category_IsDeleted ='0' order by Category_Name")
        listcategory.DataSource = dt
        listcategory.DataTextField = "Category_Name"
        listcategory.DataValueField = "Category_KId"
        listcategory.DataBind()
    End Sub
    Protected Sub BtnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ClearFields()   'Clears the TextFields
        listcategory.Enabled = True
        txtName.Enabled = True
        txtPassword.Enabled = True
        rdbstatus.Enabled = True
        gvAdmin.SelectedIndex = -1
        Call AddClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
        fillgrid()

    End Sub

    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try

       
            txtkid.Text = NewPrimaryKey(Me)
            UpdateData("Insert")   'Inserts the Record
            ClearFields()  'Clear the TextFields
            CreateMessageAlert(Me, "Record Saved Successfully...", "StrKeyVal")             'Display the Message

            fillgrid()
            Call SaveClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
            lblMsg.Text = ""
            txtName.Enabled = False
            txtPassword.Enabled = False
            rdbstatus.Enabled = False
            rdbstatus.Items(0).Selected = False
            rdbstatus.Items(1).Selected = False
            clearselected()
            listcategory.Enabled = False
            gvAdmin.SelectedIndex = -1
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub BtnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try

            UpdateData("Update")  'Updates the record
            ClearFields()   'Clear the TextFields
            fillgrid()
            lblMsg.Text = ""
            txtName.Enabled = False
            txtPassword.Enabled = False
            rdbstatus.Enabled = False
            Call EditClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
            rdbstatus.Items(0).Selected = False
            rdbstatus.Items(1).Selected = False
            clearselected()
            listcategory.Enabled = False
            gvAdmin.SelectedIndex = -1
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub BtnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try

     
            UpdateData("Delete")  'Deletes the record                                               'Execute the Query
            CreateMessageAlert(Me, "Record Deleted Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()  'Clear the TextFields
            fillgrid()
            txtName.Enabled = False
            txtPassword.Enabled = False
            rdbstatus.Enabled = False
            Call DeleteClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
            rdbstatus.Items(0).Selected = False
            rdbstatus.Items(1).Selected = False
            clearselected()
            listcategory.Enabled = False
            gvAdmin.SelectedIndex = -1
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub BtnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ClearFields()                                       'Clears the TextFields
        Call fillgrid()
        gvAdmin.SelectedIndex = -1
        Call CancelClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
        rdbstatus.Items(0).Selected = False
        rdbstatus.Items(1).Selected = False
        lblMsg.Text = ""
        txtName.Enabled = False
        txtPassword.Enabled = False
        rdbstatus.Enabled = False
        clearselected()
        listcategory.Enabled = False
    End Sub

    Protected Sub fillgrid()
        Try
            Dim dt As DataTable
            gvAdmin.Columns(5).Visible = True
            dt = ReturnDataTable("SELECT AdminUser.AdminUser_Kid,  AdminUser.AdminUser_Name,AdminUser.AdminUser_Password,AdminUser.AdminUser_ActiveFlag FROM AdminUser INNER JOIN UserType ON AdminUser.AdminUser_UserTypeId = UserType.UserType_Kid WHERE     AdminUser.AdminUser_IsDeleted = '0' AND UserType.UserType_Name = 'Admin' ORDER BY AdminUser.AdminUser_Name")
            gvAdmin.DataSource = dt
            gvAdmin.DataBind()
            gvAdmin.Columns(5).Visible = False
        Catch ex As Exception
        End Try
    End Sub

    Private Sub UpdateData(ByVal strMode As String)
        Dim selcatid As String = ""
        Dim i, j As Integer
        i = listcategory.Items.Count
        For j = 0 To i - 1
            If listcategory.Items(j).Selected = True Then
                selcatid += listcategory.Items(j).Value & " - "
            End If
        Next
        If rdbstatus.SelectedValue = 1 Then
            If (txtPassword.Text.Length < 6) Then
                CreateMessageAlert(Me, "Please Enter Password atleast 6 Character.!", "StrKeyVal")
                Exit Sub
            End If
            Dim cipher As String
            cipher = Encrypt(txtPassword.Text)
            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
            sqlcoll.Clear()
            sqlcoll.Add("@AdminUser_Kid", Data.SqlDbType.NVarChar, 20).Value = txtkid.Text.Trim
            sqlcoll.Add("@AdminUser_Code", Data.SqlDbType.NVarChar, 20).Value = Generate_Code("select count(*) from AdminUser_Master where AdminUser_Code='", "AU")
            sqlcoll.Add("@AdminUser_Name", Data.SqlDbType.NVarChar, 200).Value = txtName.Text.Trim
            sqlcoll.Add("@AdminUser_UserTypeId", Data.SqlDbType.NVarChar, 20).Value = "000007?"  'For Admin Only
            sqlcoll.Add("@AdminUser_Password", Data.SqlDbType.NVarChar, 100).Value = cipher.ToString
            If rdbstatus.SelectedValue = "1" Then
                sqlcoll.Add("@AdminUser_ActiveFlag", Data.SqlDbType.NVarChar, 20).Value = "Y"
            ElseIf rdbstatus.SelectedValue = "2" Then
                sqlcoll.Add("@AdminUser_ActiveFlag", Data.SqlDbType.NVarChar, 20).Value = "N"
            End If
            sqlcoll.Add("@AdminUser_Assigned", Data.SqlDbType.NVarChar, 20).Value = "Y"
            sqlcoll.Add("@AdminUser_CategoryId", Data.SqlDbType.NVarChar, selcatid.Length).Value = selcatid

            sqlcoll.Add("@AdminUser_FinancialYearId", Data.SqlDbType.NVarChar, 20).Value = Session("g_FinYear").ToString
            sqlcoll.Add("@AdminUser_CompanyID", Data.SqlDbType.NVarChar, 20).Value = Session("g_Company").ToString
            sqlcoll.Add("@AdminUser_UserID", Data.SqlDbType.NVarChar, 20).Value = Session("g_User").ToString
            sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 20).Value = Me.Title.ToString
            sqlcoll.Add("@Mode", Data.SqlDbType.Char, 15).Value = strMode.Trim
            ExecQuery("AdminUser_Proc", sqlcoll)
            CreateMessageAlert(Me, "Record Modified Successfully...", "StrKeyVal")             'Display the Message
        Else
            ExecuteQuery("update adminuser set AdminUser_ActiveFlag='N' where AdminUser_Kid='" & txtkid.Text.Trim & "' ")
        End If
       
      
    End Sub

    Protected Sub gvAdmin_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvAdmin.PageIndexChanging
        gvAdmin.PageIndex = e.NewPageIndex
        fillgrid()
        gvAdmin.SelectedIndex = -1
        ClearFields()
        txtName.Enabled = False
        txtPassword.Enabled = False
        rdbstatus.Enabled = False
        rdbstatus.Items(0).Selected = False
        rdbstatus.Items(1).Selected = False
        clearselected()
        Call CancelClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
    End Sub

    Protected Sub gvAdmin_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvAdmin.SelectedIndexChanged
        txtkid.Text = gvAdmin.SelectedDataKey.Value
        txtName.Enabled = False
        txtPassword.Enabled = True
        txtPassword.TextMode = TextBoxMode.SingleLine
        txtPassword.Text = CType(gvAdmin.SelectedRow.FindControl("Label1"), Label).Text.ToString
        rdbstatus.Enabled = True
        listcategory.Enabled = True
        txtName.Text = gvAdmin.SelectedRow.Cells(1).Text  'gvAdmin.Rows(gvAdmin.SelectedIndex).Cells(1).Text
        Dim catins As String = ""
        catins = ReturnValue("select AdminUser_CategoryId from AdminUser where AdminUser_Kid='" & gvAdmin.SelectedRow.Cells(5).Text.Trim & "'")
        Dim dk() As String = Split(catins, " - ")
        Dim i As Integer
        Dim mycategory As String = ""
        If catins <> Nothing Then
            Dim j As Integer
            For j = 0 To dk.Length - 1
                For i = 0 To listcategory.Items.Count - 1
                    If listcategory.Items(i).Value = (dk(j).ToString) Then
                        listcategory.Items(i).Selected = True
                    End If
                Next
            Next
        End If
        If gvAdmin.SelectedRow.Cells(4).Text = "Active" Then
            rdbstatus.SelectedValue = 1
        Else
            rdbstatus.SelectedValue = 2
        End If
       
        Call GridClickVisible(Me, BtnAdd, BtnSave, BtnEdit, BtnDelete, BtnCancel)
    End Sub

    Protected Sub gvAdmin_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvAdmin.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then

            If e.Row.Cells(4).Text = "Y" Then
                e.Row.Cells(4).Text = "Active"
            End If

            If e.Row.Cells(4).Text = "N" Then
                e.Row.Cells(4).Text = "De Active"
                e.Row.Cells(4).ForeColor = Drawing.Color.Red
            End If

            Dim catins As String = ""
            catins = ReturnValue("select AdminUser_CategoryId from AdminUser where AdminUser_Kid='" & e.Row.Cells(5).Text.Trim & "'")
            Dim dk = Split(catins, " - ")
            Dim i As Integer
            Dim mycategory As String = ""
            If catins <> Nothing Then
                For i = 0 To dk.Length - 1
                    mycategory += ReturnValue("select Category_Name from Category_Master where Category_Kid='" & dk(i).ToString & "' and Category_IsDeleted='0'") & "<br/>"
                Next
                CType(e.Row.Cells(2).FindControl("linkcategory"), Label).Attributes.Add("onmouseover", "fixedtooltip('" & mycategory & "',this,event,'200px');")
            Else
                CType(e.Row.Cells(2).FindControl("linkcategory"), Label).Attributes.Add("onmouseover", "fixedtooltip('No Category Assign.',this,event,'200px');")
            End If
            CType(e.Row.Cells(2).FindControl("linkcategory"), Label).Attributes.Add("onmouseout", "delayhidetip();")
        End If
    End Sub

   
    Protected Sub rdbstatus_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdbstatus.SelectedIndexChanged
        If rdbstatus.SelectedValue = 1 Then
            BtnEdit.CausesValidation = True
        Else
            BtnEdit.CausesValidation = False
            txtPassword.CausesValidation = False
        End If
    End Sub


End Class
