﻿Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class Admin_category_relation
    Inherits System.Web.UI.Page
    Shared Category(10) As String
    Shared count As Integer
    Shared cat As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsNothing(Session("dtCategory")) Then
            Session("dtCategory") = ReturnDataTable("Select Category_Kid, Category_Name, Category_ParentId from Category_Master where Category_Isdeleted='0' and Category_ParentId<>'' order by Category_Name")
        End If
        If Not IsPostBack Then
            fillMainCategory()
            BtnSave.Attributes.Add("onclick", "return CheckCategoryRelation();")
            fillCategoryRelationGrid("")
        End If
        lblMsg.Visible = False
        'fillCategoryRelationGrid("")

    End Sub

    Public Sub fillCategoryRelationGrid(ByVal categoryID As String)

        Dim dt As DataTable
        If categoryID = "" Then
            dt = ReturnDataTable("SELECT     CategoryDistinct.Category_Name, CategoryRelation_Master.CategoryRelation_CategoryID, CategoryRelation_Master.CategoryRelation_Flag FROM         CategoryRelation_Master INNER JOIN                       CategoryDistinct ON CategoryRelation_Master.CategoryRelation_CategoryID = CategoryDistinct.Category_Kid ORDER BY CategoryRelation_Master.CategoryRelation_Flag")
        Else
            dt = ReturnDataTable("SELECT     CategoryDistinct.Category_Name, CategoryRelation_Master.CategoryRelation_CategoryID, CategoryRelation_Master.CategoryRelation_Flag FROM         CategoryRelation_Master INNER JOIN                       CategoryDistinct ON CategoryRelation_Master.CategoryRelation_CategoryID = CategoryDistinct.Category_Kid WHERE     (CategoryRelation_Master.CategoryRelation_Flag IN                           (SELECT     CategoryRelation_Flag                             FROM          CategoryRelation_Master AS CategoryRelation_Master_1                             WHERE      (CategoryRelation_CategoryID = '" & RemoveLiterals(categoryID.ToString) & "')))")
        End If
        gvCategoryRelation.Columns(1).Visible = True
        gvCategoryRelation.Columns(2).Visible = True
        gvCategoryRelation.DataSource = dt
        gvCategoryRelation.DataBind()
        gvCategoryRelation.Columns(1).Visible = False
        gvCategoryRelation.Columns(2).Visible = False

    End Sub

    Public Sub fillMainCategory()
        lblSourceLastCat.Text = ""
        lblDestinationLastCat.Text = ""
        hfDestinationCategoryID.Value = ""
        hfSourceCategoryID.Value = ""
        Dim dt As DataTable
        CType(Session("dtCategory"), DataTable).DefaultView.RowFilter = "Category_ParentId='Root Category'"
        dt = CType(Session("dtCategory"), DataTable).DefaultView.ToTable
        CType(Session("dtCategory"), DataTable).RejectChanges()
        ddlSource.DataSource = dt
        ddlSource.DataValueField = "Category_Kid"
        ddlSource.DataTextField = "Category_Name"
        ddlSource.DataBind()
        ddlSource.Items.Insert(0, New ListItem("Select Main Category", "0"))
        ddlSubCat1.Visible = False
        ddlSubCat2.Visible = False
        ddlSource.BackColor = Drawing.Color.LightYellow
        ddlDestination.DataSource = dt
        ddlDestination.DataValueField = "Category_Kid"
        ddlDestination.DataTextField = "Category_Name"
        ddlDestination.DataBind()
        ddlDestination.Items.Insert(0, New ListItem("Select Main Category", "0"))
        ddlSubCat11.Visible = False
        ddlSubCat22.Visible = False
        ddlDestination.BackColor = Drawing.Color.LightYellow
        'ReturnDataTable("Select Category_KId, Category_Name from Category_Master where Category_ParentId='Root Category'")
    End Sub

    Protected Sub ddlSource_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSource.SelectedIndexChanged
        fillCategoryRelationGrid("")

        lblSourceLastCat.Text = ""
        hfSourceCategoryID.Value = ""
        If ddlSource.SelectedIndex > 0 Then
            ddlSubCat1.Visible = True
            Dim dt As DataTable
            CType(Session("dtCategory"), DataTable).DefaultView.RowFilter = "Category_ParentId='" & RemoveLiterals(ddlSource.SelectedItem.Value.ToString) & "'"
            dt = CType(Session("dtCategory"), DataTable).DefaultView.ToTable
            CType(Session("dtCategory"), DataTable).RejectChanges()
            ddlSubCat1.DataSource = dt
            ddlSubCat1.DataValueField = "Category_Kid"
            ddlSubCat1.DataTextField = "Category_Name"
            ddlSubCat1.DataBind()
            ddlSubCat1.Items.Insert(0, New ListItem("Select Sub Category", 0))
            ddlSubCat2.Visible = False

            'Dim dr() As DataRow = CType(Session("dtCategory"), DataTable).Select("Category_KId = '" & ddlSource.SelectedItem.Value.ToString & "'")(0).Item("Category_Name").ToString
            ddlSource.BackColor = Drawing.Color.White
            ddlSubCat1.BackColor = Drawing.Color.LightYellow
            lblSourceCategoryHierarchy.Text = fillcategory(RemoveLiterals(ddlSource.SelectedItem.Value.ToString))
            'lblSourceCategoryHierarchy.Text = "Product Category >  <b>" + CType(Session("dtCategory"), DataTable).Select("Category_KId = '" & ddlSource.SelectedItem.Value.ToString & "'")(0).Item("Category_Name").ToString + "</b>"

        Else
            ddlSubCat1.Visible = False
            ddlSubCat2.Visible = False
        End If


        'Dim ddlSource2 As New DropDownList
        'Dim dt As DataTable
        'CType(Session("dtCategory"), DataTable).DefaultView.RowFilter = "Category_ParentId='" & RemoveLiterals(ddlSource.SelectedItem.Value.ToString) & "'"
        'dt = CType(Session("dtCategory"), DataTable).DefaultView.ToTable
        'CType(Session("dtCategory"), DataTable).RejectChanges()
        'ddlSource2.DataSource = dt
        'ddlSource2.DataValueField = "Category_Kid"
        'ddlSource2.DataTextField = "Category_Name"
        'ddlSource2.DataBind()
        ''ddlSource2.AutoPostBack = True
        'AddHandler ddlSource2.SelectedIndexChanged, AddressOf ddlSource2_SelectedIndexChanged

        'tdSourceDDL.Controls.Add(ddlSource2)

    End Sub

    Public Function fillcategory(ByVal strCatID As String) As String

        Dim str As String
        Dim strValue As String = "Product Category  > "
        Dim strV(10) As String
        Dim count As Integer = 0
        str = strCatID
        While str <> "Root Category"
            strV(count) = CType(Session("dtCategory"), DataTable).Select("Category_KID='" & str & "'")(0).Item("Category_Name").ToString
            count = count + 1
            'strValue += "<b>" & str & "<b>  >  "
            str = CType(Session("dtCategory"), DataTable).Select("Category_KID='" & str & "'")(0).Item("Category_ParentID")
        End While

        Dim i As Integer
        Array.Reverse(strV)
        For i = 0 To strV.Length - 1
            If IsNothing(strV(i)) Then
            Else
                strValue += "<b>  " & strV(i).ToString & "  </b>  >  "
            End If

        Next
        Return strValue


    End Function

    Protected Sub ddlSubCat1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSubCat1.SelectedIndexChanged
        fillCategoryRelationGrid("")

        hfSourceCategoryID.Value = ""
        If ddlSubCat1.SelectedIndex > 0 Then
            Dim dt As DataTable
            CType(Session("dtCategory"), DataTable).DefaultView.RowFilter = "Category_ParentId='" & RemoveLiterals(ddlSubCat1.SelectedItem.Value.ToString) & "'"
            dt = CType(Session("dtCategory"), DataTable).DefaultView.ToTable
            CType(Session("dtCategory"), DataTable).RejectChanges()

            'lblSourceCategoryHierarchy.Text += "<b> >  " + CType(Session("dtCategory"), DataTable).Select("Category_KId = '" & ddlSubCat1.SelectedItem.Value.ToString & "'")(0).Item("Category_Name").ToString + "</b>"
            lblSourceCategoryHierarchy.Text = fillcategory(RemoveLiterals(ddlSubCat1.SelectedItem.Value.ToString))

            If dt.Rows.Count > 0 Then
                ddlSubCat2.Visible = True
                ddlSubCat2.DataSource = dt
                ddlSubCat2.DataValueField = "Category_Kid"
                ddlSubCat2.DataTextField = "Category_Name"
                ddlSubCat2.DataBind()
                ddlSubCat2.Items.Insert(0, New ListItem("Select Sub Category", 0))
                lblSourceLastCat.Text = ""
                ddlSubCat1.BackColor = Drawing.Color.White
                ddlSubCat2.BackColor = Drawing.Color.LightYellow
            Else
                hfSourceCategoryID.Value = RemoveLiterals(ddlSubCat1.SelectedItem.Value.ToString)
                'ddlSource.BackColor = Drawing.Color.White
                ddlSubCat1.BackColor = Drawing.Color.Yellow
                lblSourceLastCat.Text = "Last Category Selected."
                fillCategoryRelationGrid(RemoveLiterals(ddlSubCat1.SelectedItem.Value.ToString))

            End If


        Else
            ddlSubCat2.Visible = False
        End If
    End Sub

    Protected Sub ddlSubCat2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSubCat2.SelectedIndexChanged
        hfSourceCategoryID.Value = ""
        fillCategoryRelationGrid("")

        If ddlSubCat2.SelectedIndex > 0 Then
            Dim dt As DataTable
            CType(Session("dtCategory"), DataTable).DefaultView.RowFilter = "Category_ParentId='" & RemoveLiterals(ddlSubCat2.SelectedItem.Value.ToString) & "'"
            dt = CType(Session("dtCategory"), DataTable).DefaultView.ToTable
            CType(Session("dtCategory"), DataTable).RejectChanges()
            'lblSourceCategoryHierarchy.Text += "<b> >  " + CType(Session("dtCategory"), DataTable).Select("Category_KId = '" & ddlSubCat2.SelectedItem.Value.ToString & "'")(0).Item("Category_Name").ToString + "</b>"
            lblSourceCategoryHierarchy.Text = fillcategory(RemoveLiterals(ddlSubCat2.SelectedItem.Value.ToString))
            If dt.Rows.Count > 0 Then
                'ddlSubCat1.Visible = True
                ddlSubCat1.DataSource = dt
                ddlSubCat1.DataValueField = "Category_Kid"
                ddlSubCat1.DataTextField = "Category_Name"
                ddlSubCat1.DataBind()
                ddlSubCat1.Items.Insert(0, New ListItem("Select Sub Category", 0))
                lblSourceLastCat.Text = ""
                ddlSubCat2.Visible = False
                'ddlSubCat2.BackColor = Drawing.Color.White
                ddlSubCat1.BackColor = Drawing.Color.LightYellow
            Else
                hfSourceCategoryID.Value = RemoveLiterals(ddlSubCat2.SelectedItem.Value.ToString)
                ddlSubCat2.BackColor = Drawing.Color.Yellow
                lblSourceLastCat.Text = "Last Category Selected."
                fillCategoryRelationGrid(RemoveLiterals(ddlSubCat2.SelectedItem.Value.ToString))
            End If


        Else
            'ddlSubCat2.Visible = False
        End If
    End Sub

    Protected Sub ddlDestination_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDestination.SelectedIndexChanged
        lblDestinationLastCat.Text = ""
        hfDestinationCategoryID.Value = ""
        If ddlDestination.SelectedIndex > 0 Then
            ddlSubCat11.Visible = True
            Dim dt As DataTable
            CType(Session("dtCategory"), DataTable).DefaultView.RowFilter = "Category_ParentId='" & RemoveLiterals(ddlDestination.SelectedItem.Value.ToString) & "'"
            dt = CType(Session("dtCategory"), DataTable).DefaultView.ToTable
            CType(Session("dtCategory"), DataTable).RejectChanges()
            ddlSubCat11.DataSource = dt
            ddlSubCat11.DataValueField = "Category_Kid"
            ddlSubCat11.DataTextField = "Category_Name"
            ddlSubCat11.DataBind()
            ddlSubCat11.Items.Insert(0, New ListItem("Select Sub Category", 0))
            ddlSubCat22.Visible = False

            'Dim dr() As DataRow = CType(Session("dtCategory"), DataTable).Select("Category_KId = '" & ddlSource.SelectedItem.Value.ToString & "'")(0).Item("Category_Name").ToString
            ddlDestination.BackColor = Drawing.Color.White
            ddlSubCat11.BackColor = Drawing.Color.LightYellow
            lblDestinationCategoryHierarchy.Text = fillcategory(RemoveLiterals(ddlDestination.SelectedItem.Value.ToString))
            'lblSourceCategoryHierarchy.Text = "Product Category >  <b>" + CType(Session("dtCategory"), DataTable).Select("Category_KId = '" & ddlSource.SelectedItem.Value.ToString & "'")(0).Item("Category_Name").ToString + "</b>"

        Else
            ddlSubCat11.Visible = False
            ddlSubCat22.Visible = False
        End If
    End Sub

    Protected Sub ddlSubCat11_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSubCat11.SelectedIndexChanged
        hfDestinationCategoryID.Value = ""
        If ddlSubCat11.SelectedIndex > 0 Then
            Dim dt As DataTable
            CType(Session("dtCategory"), DataTable).DefaultView.RowFilter = "Category_ParentId='" & RemoveLiterals(ddlSubCat11.SelectedItem.Value.ToString) & "'"
            dt = CType(Session("dtCategory"), DataTable).DefaultView.ToTable
            CType(Session("dtCategory"), DataTable).RejectChanges()

            'lblSourceCategoryHierarchy.Text += "<b> >  " + CType(Session("dtCategory"), DataTable).Select("Category_KId = '" & ddlSubCat1.SelectedItem.Value.ToString & "'")(0).Item("Category_Name").ToString + "</b>"
            lblDestinationCategoryHierarchy.Text = fillcategory(RemoveLiterals(ddlSubCat11.SelectedItem.Value.ToString))

            If dt.Rows.Count > 0 Then
                ddlSubCat22.Visible = True
                ddlSubCat22.DataSource = dt
                ddlSubCat22.DataValueField = "Category_Kid"
                ddlSubCat22.DataTextField = "Category_Name"
                ddlSubCat22.DataBind()
                ddlSubCat22.Items.Insert(0, New ListItem("Select Sub Category", 0))
                lblDestinationLastCat.Text = ""
                ddlSubCat11.BackColor = Drawing.Color.White
                ddlSubCat22.BackColor = Drawing.Color.LightYellow
                hfDestinationCategoryID.Value = ""
            Else
                hfDestinationCategoryID.Value = RemoveLiterals(ddlSubCat11.SelectedItem.Value.ToString)
                'ddlSource.BackColor = Drawing.Color.White
                ddlSubCat11.BackColor = Drawing.Color.Yellow
                lblDestinationLastCat.Text = "Last Category Selected."
            End If


        Else
            ddlSubCat22.Visible = False
        End If
    End Sub

    Protected Sub ddlSubCat22_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSubCat22.SelectedIndexChanged
        hfDestinationCategoryID.Value = ""
        If ddlSubCat22.SelectedIndex > 0 Then
            Dim dt As DataTable
            CType(Session("dtCategory"), DataTable).DefaultView.RowFilter = "Category_ParentId='" & RemoveLiterals(ddlSubCat22.SelectedItem.Value.ToString) & "'"
            dt = CType(Session("dtCategory"), DataTable).DefaultView.ToTable
            CType(Session("dtCategory"), DataTable).RejectChanges()
            'lblSourceCategoryHierarchy.Text += "<b> >  " + CType(Session("dtCategory"), DataTable).Select("Category_KId = '" & ddlSubCat2.SelectedItem.Value.ToString & "'")(0).Item("Category_Name").ToString + "</b>"
            lblDestinationCategoryHierarchy.Text = fillcategory(RemoveLiterals(ddlSubCat22.SelectedItem.Value.ToString))
            If dt.Rows.Count > 0 Then
                'ddlSubCat1.Visible = True
                ddlSubCat11.DataSource = dt
                ddlSubCat11.DataValueField = "Category_Kid"
                ddlSubCat11.DataTextField = "Category_Name"
                ddlSubCat11.DataBind()
                ddlSubCat11.Items.Insert(0, New ListItem("Select Sub Category", 0))
                lblDestinationLastCat.Text = ""
                ddlSubCat22.Visible = False
                'ddlSubCat2.BackColor = Drawing.Color.White
                ddlSubCat11.BackColor = Drawing.Color.LightYellow
            Else
                hfDestinationCategoryID.Value = RemoveLiterals(ddlSubCat22.SelectedItem.Value.ToString)
                ddlSubCat22.BackColor = Drawing.Color.Yellow
                lblDestinationLastCat.Text = "Last Category Selected."
            End If


        Else
            'ddlSubCat2.Visible = False
        End If
    End Sub

    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        'Dim str1, str2 As String
        'str1 = hfSourceCategoryID.Value.ToString
        'str2 = hfDestinationCategoryID.Value.ToString
        'Dim intFlag As Integer
        'intFlag=ReturnValue("Select")

        Dim intFlag As Integer
        intFlag = 1
        Dim strFlag As String
        Dim strSourceFlag As String = ReturnValue("Select CategoryRelation_Flag from CategoryRelation_Master where CategoryRelation_CategoryID ='" & RemoveLiterals(hfSourceCategoryID.Value.ToString) & "'").ToString
        Dim strDestinationFlag As String = ReturnValue("Select CategoryRelation_Flag from CategoryRelation_Master where CategoryRelation_CategoryID ='" & RemoveLiterals(hfDestinationCategoryID.Value.ToString) & "'").ToString

        If strSourceFlag = "" And strDestinationFlag = "" Then
            ' New entry of with incremented flag two row one for source and second for destination
            strFlag = ReturnValue("Select Max(CategoryRelation_Flag) from CategoryRelation_Master")
            If strFlag <> "" Then
                intFlag = Convert.ToInt32(strFlag) + 1
            End If
            ExecuteQuery("insert into CategoryRelation_Master values('" & NewPrimaryKey(Me) & "'," & intFlag & ",'" & RemoveLiterals(hfSourceCategoryID.Value.ToString) & "')")
            ExecuteQuery("insert into CategoryRelation_Master values('" & NewPrimaryKey(Me) & "'," & intFlag & ",'" & RemoveLiterals(hfDestinationCategoryID.Value.ToString) & "')")
        ElseIf strSourceFlag <> "" And strDestinationFlag <> "" Then
            ' Entry is already present
            'CreateMessageAlert(Me, "This relation is already present.!", "strkeyval")
            showmessage("This Relation is Already Present.!", lblMsg)
            Exit Sub
        ElseIf strSourceFlag <> "" Then
            intFlag = Convert.ToInt32(strSourceFlag)
            ExecuteQuery("insert into CategoryRelation_Master values('" & NewPrimaryKey(Me) & "'," & intFlag & ",'" & RemoveLiterals(hfDestinationCategoryID.Value.ToString) & "')")
        ElseIf strDestinationFlag <> "" Then
            intFlag = Convert.ToInt32(strDestinationFlag)
            ExecuteQuery("insert into CategoryRelation_Master values('" & NewPrimaryKey(Me) & "'," & intFlag & ",'" & RemoveLiterals(hfSourceCategoryID.Value.ToString) & "')")
        End If

        'CreateMessageAlert(Me, "Record Saved Sucessfully.!", "strkeyval")
        showmessage("Record Saved Sucessfully.!", lblMsg)
        fillCategoryRelationGrid("")
        'If strFlag = "" Then
        '    strFlag = ReturnValue("Select CategoryRelation_Flag from CategoryRelation_Master where CategoryRelation_CategoryID ='" & RemoveLiterals(hfDestinationCategoryID.Value.ToString) & "'").ToString
        'End If

        'If strFlag = "" Then
        '    strFlag = ReturnValue("Select Max(CategoryRelation_Flag) from CategoryRelation_Master")
        '    If strFlag = "" Then
        '        ExecuteQuery("insert into Category")
        '    Else

        '    End If
        'Else

        'End If



    End Sub
    Public Function FillCategoryNameGrid(ByVal Catid As String) As String
        ' Fill category herarchy in label
        Dim str As String
        Dim strValue As String = ""
        Dim strV(10) As String
        Dim count As Integer = 0
        str = Catid
        While str <> "Root Category"
            strV(count) = CType(Session("dtCategory"), DataTable).Select("Category_KID='" & str & "'")(0).Item("Category_Name").ToString
            count = count + 1
            'strValue += "<b>" & str & "<b>  >  "
            str = CType(Session("dtCategory"), DataTable).Select("Category_KID='" & str & "'")(0).Item("Category_ParentID")
        End While

        Dim i As Integer
        Array.Reverse(strV)
        For i = 0 To strV.Length - 1
            If IsNothing(strV(i)) Then
            Else
                strValue += "<b>  " & strV(i).ToString & "  </b>  >  "
            End If

        Next
        Return strValue

        'Dim dt As DataTable
        'dt = ReturnDataTable("Select * From Category_Master Where Category_Kid ='" & RemoveLiterals(Catid) & "' and Category_Isdeleted = '0'")
        'Dim ParentId As String
        'ParentId = dt.Rows(0).Item(3).ToString()
        'If ParentId = "Root Category" Then
        '    Dim j As Integer
        '    Category(count) = dt.Rows(0).Item(2).ToString() & "  >  "
        '    For j = 0 To count
        '        cat += Category(count)
        '        count = count - 1
        '    Next

        '    Return cat
        '    Exit Function
        'Else
        '    Category(count) = dt.Rows(0).Item(2).ToString() & "  >  "
        '    count = count + 1
        '    FillCategoryNameGrid(dt.Rows(0).Item(3).ToString())
        'End If
        'dt.Clear()
        'Return cat
    End Function
    Protected Sub gvCategoryRelation_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCategoryRelation.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then

            CType(e.Row.Cells(3).FindControl("btnDelete"), Button).Attributes.Add("onclick", "return confirm('Are you sure want to delete category relation.!');")
            e.Row.Cells(0).Text = FillCategoryNameGrid(RemoveLiterals(e.Row.Cells(1).Text.ToString.Trim))
            If e.Row.RowIndex > 0 Then

                If gvCategoryRelation.Rows(e.Row.RowIndex - 1).BackColor.Name = "0" Or gvCategoryRelation.Rows(e.Row.RowIndex - 1).BackColor.Name = "White" Then
                    e.Row.BackColor = Drawing.Color.FromArgb(234, 241, 247)
                Else
                    e.Row.BackColor = Drawing.Color.White  'Drawing.Color.FromArgb(0, 0, 0)
                End If

                If e.Row.Cells(2).Text = gvCategoryRelation.Rows(e.Row.RowIndex - 1).Cells(2).Text Then
                    CType(e.Row.Cells(3).FindControl("btnDelete"), Button).Visible = False
                    e.Row.BackColor = gvCategoryRelation.Rows(e.Row.RowIndex - 1).BackColor
                End If
            End If
        End If

    End Sub

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim btn As Button = sender
        Dim CategoryFlag As String = RemoveLiterals(btn.CommandArgument.ToString)
        ExecuteQuery("Delete from CategoryRelation_Master where CategoryRelation_Flag='" & RemoveLiterals(CategoryFlag.ToString) & "'")
        fillCategoryRelationGrid("")
        showmessage("Record Deleted Sucessfully.!", lblMsg)

    End Sub

End Class
