Imports System.Data.SqlClient
Imports System.Data
Imports System.IO
Imports System.String
Partial Class UserControl_ProductSearchDetails
    Inherits System.Web.UI.UserControl

    Dim str, str1 As String
    Dim slc1 = "Select Date"
    Dim slc3 = "Select Year"
    Dim slc4 = "Select Country"
    Dim strcon As String = ConfigurationSettings.AppSettings.Item("ConnectionString")
    Dim con As New SqlConnection(strcon)
    Dim cmd As New SqlCommand()
    Dim dr As SqlDataReader
    Shared Category(10) As String
    Shared i As Integer



    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
        
        Dim parentid As String
        If Not Me.Page.IsPostBack Then
            g_FinYear = Session("g_FinYear").ToString()
            g_Company = Session("g_Company").ToString()
            '   g_User = Session("g_User").ToString()
            parentid = Request.QueryString("subcatid")
            If parentid = Nothing Or parentid = "Root" Then
                parentid = "Root Category"
            Else
                parentid = Request.QueryString("subcatid")
            End If
            i = 0
            LoadCategory(parentid)
            cbosubCategory1.Visible = False
            cbosubCategory2.Visible = False
            cboCategory.Focus()
            
        End If
        cboCategory.Visible = True
    End Sub



    Private Sub cboCategory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCategory.SelectedIndexChanged
        If cboCategory.SelectedItem.Value = "0" Then
            Label2.Visible = True
            Label2.Text = "Please Select the Category"
            cbosubCategory1.Visible = False
            cbosubCategory2.Visible = False
            Label15.Visible = False
        Else
            Session("lastsubcategory") = ""
            Session("lastsubcategory") = cboCategory.SelectedItem.Value
            getparents(cboCategory.SelectedItem.Value)
            LoadSubCategory1(cboCategory.SelectedItem.Value)

        End If

    End Sub
    Private Sub cbosubCategory1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbosubCategory1.SelectedIndexChanged
        Session("lastsubcategory") = cbosubCategory1.SelectedItem.Value
        getparents(cbosubCategory1.SelectedItem.Value)
        LoadSubCategory2(cbosubCategory1.SelectedItem.Value)
        cbosubCategory1.Visible = True
    End Sub
    Private Sub cbosubCategory2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbosubCategory2.SelectedIndexChanged

        Session("lastsubcategory") = cbosubCategory2.SelectedItem.Value
        getparents(cbosubCategory2.SelectedItem.Value)
        LoadSubCategory3(cbosubCategory2.SelectedItem.Value)
    End Sub
    Private Sub LoadSubCategory3(ByVal intId As String)
        'Dim strsql As String
        'strsql = "SELECT Category_Kid as id, Category_Name as Name FROM Category_Master WHERE Category_ParentId = '" & intId & "' ORDER BY Category_Name"
        ''If con.State = ConnectionState.Open Then
        '    con.Close()
        'End If
        'con.Open()

        'Dim cmd As New SqlCommand(strsql, con)
        'Dim mReader As SqlDataReader
        'mReader = cmd.ExecuteReader
        'cboCategory.Items.Clear()
        'cboCategory.Items.Add(New ListItem("Select Subcategory", "-4"))
        'If mReader.Read Then
        '    cboCategory.Items.Add(New ListItem(mReader("Name"), mReader("id")))
        '    While mReader.Read
        '        cboCategory.Items.Add(New ListItem(mReader("Name"), mReader("id")))
        '    End While
        '    Label14.Visible = True
        '    Label15.Visible = False
        '    Label16.Visible = False
        '    cbosubCategory1.Visible = False
        '    cbosubCategory2.Visible = False
        '    cboCategory.ForeColor = Drawing.Color.Red
        '    cbosubCategory1.ForeColor = Drawing.Color.Black
        '    cbosubCategory2.ForeColor = Drawing.Color.Black
        '    Label14.Text = "Select the next level"
        'Else
        '    Label14.Visible = False
        '    Label15.Visible = False
        '    Label16.Visible = False
        '    Label2.Visible = True
        '    Label2.Text = "Add the product"
        '    Session("CategoryId") = intId
        '    If Label2.Visible = True Then
        '        Label1.Visible = False
        '    End If
        'End If
        'mReader.Close()

        ''Dim strsql As String
        ''strsql = "SELECT Category_Kid as id, Category_Name as Name FROM Category_Master WHERE Category_ParentId = '" & intId & "' ORDER BY Category_Name"
        ''cboCategory.Items.Clear()
        ''cboCategory.Items.Add(New ListItem("Select Subcategory", "-1"))
        ''Fill_Combo(strsql, cboCategory)

        Dim dt As DataTable = ReturnDataTable("SELECT Category_Kid as id, Category_Name as Name FROM Category_Master WHERE Category_ParentId = '" & intId & "' ORDER BY Category_Name")

        ''If cboCategory.Items.Count > 1 Then

        If dt.Rows.Count > 0 Then

            Dim strsql As String
            strsql = "SELECT Category_Kid as id, Category_Name as Name FROM Category_Master WHERE Category_ParentId = '" & intId & "' ORDER BY Category_Name"
            cboCategory.Items.Clear()
            cboCategory.Items.Add(New ListItem("Select Subcategory", "-1"))
            Fill_Combo(strsql, cboCategory)


            Label14.Visible = True
            Label15.Visible = False
            Label16.Visible = False
            cbosubCategory1.Visible = False
            cbosubCategory2.Visible = False
            cboCategory.ForeColor = Drawing.Color.Red
            cbosubCategory1.ForeColor = Drawing.Color.Black
            cbosubCategory2.ForeColor = Drawing.Color.Black
            Label14.Text = "Select the next level"
        Else
            Label14.Visible = False
            Label15.Visible = False
            Label16.Visible = False
            Label2.Visible = True
            Label2.Text = "Add the product"
            Session("CategoryId") = intId
            If Label2.Visible = True Then
                Label1.Visible = False
            End If
        End If

    End Sub
    Private Sub LoadCategory(ByVal parentid As String)
        'Dim strsql As String
        'strsql = "SELECT Category_Kid as id, Category_Name as Name FROM Category_Master WHERE Category_ParentId = '" & parentid & "' ORDER BY Category_Name"
        'If con.State = ConnectionState.Open Then
        '    con.Close()
        'End If
        'con.Open()
        'Dim cmd As New SqlCommand(strsql, con)
        'Dim mReader As SqlDataReader
        'mReader = cmd.ExecuteReader
        'cboCategory.Items.Clear()
        'cboCategory.Items.Add(New ListItem("Select Category", "-1"))
        'If mReader.Read Then
        '    cboCategory.Items.Add(New ListItem(mReader("Name"), mReader("id")))
        '    While mReader.Read
        '        cboCategory.Items.Add(New ListItem(mReader("Name"), mReader("id")))
        '    End While
        '    Label14.Visible = True
        '    Label15.Visible = False
        '    Label16.Visible = False

        '    cboCategory.ForeColor = Drawing.Color.Red
        '    cbosubCategory1.ForeColor = Drawing.Color.Black
        '    cbosubCategory2.ForeColor = Drawing.Color.Black

        '    Label14.Text = "Select the category"
        'Else
        '    Label14.Visible = False
        '    Label15.Visible = False
        '    Label16.Visible = False
        '    Label2.Visible = True
        '    Label2.Text = "Add the product"
        '    Session("CategoryId") = parentid
        '    If Label2.Visible = True Then
        '        Label1.Visible = False
        '    End If
        'End If
        'mReader.Close()
        Dim strsql As String
        strsql = "SELECT Category_Kid as id, Category_Name as Name FROM Category_Master WHERE Category_ParentId = '" & parentid & "' ORDER BY Category_Name"
        cboCategory.Items.Clear()
        cboCategory.Items.Add(New ListItem("Select Category", "-1"))
        Fill_Combo(strsql, cboCategory)
        If cboCategory.Items.Count > 1 Then
            Label14.Visible = True
            Label15.Visible = False
            Label16.Visible = False

            cboCategory.ForeColor = Drawing.Color.Red
            cbosubCategory1.ForeColor = Drawing.Color.Black
            cbosubCategory2.ForeColor = Drawing.Color.Black

            Label14.Text = "Select the category"
        Else
            Label14.Visible = False
            Label15.Visible = False
            Label16.Visible = False
            Label2.Visible = True
            Label2.Text = "Add the product"
            Session("CategoryId") = parentid
            If Label2.Visible = True Then
                Label1.Visible = False
            End If
        End If


    End Sub
    Private Sub LoadSubCategory1(ByVal intId As String)
        'Dim strsql As String
        'strsql = "SELECT Category_Kid as id, Category_Name as Name FROM Category_Master WHERE Category_ParentId = '" & intId & "' ORDER BY Category_Name"
        'If con.State = ConnectionState.Open Then
        '    con.Close()
        'End If
        'con.Open()

        'Dim cmd As New SqlCommand(strsql, con)
        'Dim mReader As SqlDataReader
        'mReader = cmd.ExecuteReader
        'cbosubCategory1.Items.Clear()
        'cbosubCategory1.Items.Add(New ListItem("Select Subcategory", "-2"))
        'If mReader.Read Then
        '    cbosubCategory1.Items.Add(New ListItem(mReader("Name"), mReader("id")))
        '    While mReader.Read
        '        cbosubCategory1.Items.Add(New ListItem(mReader("Name"), mReader("id")))
        '    End While
        '    Label14.Visible = False
        '    Label15.Visible = True
        '    Label16.Visible = False
        '    cboCategory.ForeColor = Drawing.Color.Black
        '    cbosubCategory1.ForeColor = Drawing.Color.Red
        '    cbosubCategory2.ForeColor = Drawing.Color.Black
        '    cbosubCategory1.Visible = True
        '    cbosubCategory2.Visible = False
        '    Label15.Text = "Select the next level"
        'Else
        '    cbosubCategory1.Visible = False
        '    cbosubCategory2.Visible = False
        '    Label14.Visible = False
        '    Label15.Visible = False
        '    Label16.Visible = False
        '    Label2.Visible = True
        '    Label2.Text = "Add the product"
        '    Session("CategoryId") = intId
        '    If Label2.Visible = True Then
        '        Label1.Visible = False
        '    End If
        'End If
        'mReader.Close()
        Dim strsql As String
        strsql = "SELECT Category_Kid as id, Category_Name as Name FROM Category_Master WHERE Category_ParentId = '" & intId & "' ORDER BY Category_Name"
      
        cbosubCategory1.Items.Clear()
        cbosubCategory1.Items.Add(New ListItem("Select Subcategory", "-1"))
        Fill_Combo(strsql, cbosubCategory1)
        If cbosubCategory1.Items.Count > 1 Then
            Label14.Visible = False
            Label15.Visible = True
            Label16.Visible = False
            cboCategory.ForeColor = Drawing.Color.Black
            cbosubCategory1.ForeColor = Drawing.Color.Red
            cbosubCategory2.ForeColor = Drawing.Color.Black
            cbosubCategory1.Visible = True
            cbosubCategory2.Visible = False
            Label15.Text = "Select the next level"
        Else
            cbosubCategory1.Visible = False
            cbosubCategory2.Visible = False
            Label14.Visible = False
            Label15.Visible = False
            Label16.Visible = False
            Label2.Visible = True
            Label2.Text = "Add the product"
            Session("CategoryId") = intId
            If Label2.Visible = True Then
                Label1.Visible = False
            End If
        End If

    End Sub
    Private Sub LoadSubCategory2(ByVal intId As String)
        'Dim strsql As String
        'strsql = "SELECT Category_Kid as id, Category_Name as Name FROM Category_Master WHERE Category_ParentId = '" & intId & "' ORDER BY Category_Name"
        'If con.State = ConnectionState.Open Then
        '    con.Close()
        'End If
        'con.Open()

        'Dim cmd As New SqlCommand(strsql, con)
        'Dim mReader As SqlDataReader
        'mReader = cmd.ExecuteReader
        'cbosubCategory2.Items.Clear()
        'cbosubCategory2.Items.Add(New ListItem("Select Subcategory", "-3"))
        'If mReader.Read Then
        '    cbosubCategory2.Items.Add(New ListItem(mReader("Name"), mReader("id")))
        '    While mReader.Read
        '        cbosubCategory2.Items.Add(New ListItem(mReader("Name"), mReader("id")))
        '    End While
        '    Label14.Visible = False
        '    Label15.Visible = False
        '    Label16.Visible = True
        '    cboCategory.ForeColor = Drawing.Color.Black
        '    cbosubCategory1.ForeColor = Drawing.Color.Black
        '    cbosubCategory2.ForeColor = Drawing.Color.Red
        '    Label16.Text = "Select the next level"
        '    cbosubCategory2.Visible = True
        'Else
        '    cbosubCategory2.Visible = False
        '    Label14.Visible = False
        '    Label15.Visible = False
        '    Label16.Visible = False
        '    Label2.Visible = True
        '    Label2.Text = "Add the product"
        '    Session("CategoryId") = intId
        '    If Label2.Visible = True Then
        '        Label1.Visible = False
        '    End If
        'End If
        'mReader.Close()
        Dim strsql As String
        strsql = "SELECT Category_Kid as id, Category_Name as Name FROM Category_Master WHERE Category_ParentId = '" & intId & "' ORDER BY Category_Name"
        cbosubCategory2.Items.Clear()
        cbosubCategory2.Items.Add(New ListItem("Select Subcategory", "-1"))
        Fill_Combo(strsql, cbosubCategory2)
        If cbosubCategory2.Items.Count > 1 Then

            Label14.Visible = False
            Label15.Visible = False
            Label16.Visible = True
            cboCategory.ForeColor = Drawing.Color.Black
            cbosubCategory1.ForeColor = Drawing.Color.Black
            cbosubCategory2.ForeColor = Drawing.Color.Red
            Label16.Text = "Select the next level"
            cbosubCategory2.Visible = True
        Else
            cbosubCategory2.Visible = False
            Label14.Visible = False
            Label15.Visible = False
            Label16.Visible = False
            Label2.Visible = True
            Label2.Text = "Add the product"
            Session("CategoryId") = intId
            If Label2.Visible = True Then
                Label1.Visible = False
            End If
        End If



    End Sub
    Private Function getparents(ByVal i As String) As String
        Dim strsql As String
        strsql = "SELECT Category_ParentId, Category_Name FROM Category_Master WHERE Category_Kid = '" & i & "'"
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        'con.Open()

        'Dim cmd As New SqlCommand(strsql, con)
        'Dim dr As SqlDataReader
        'dr = cmd.ExecuteReader
        Dim da As New SqlDataAdapter(strsql, con)
        Dim ds As New DataSet
        da.Fill(ds)
        Dim j As String

        Dim flag As Boolean = False
        If ds.Tables(0).Rows.Count > 0 Then
            j = ds.Tables(0).Rows(0).Item(0).ToString
            flag = True
        Else
            Return 0
            Exit Function
        End If

        If flag = True Then
            str1 = ds.Tables(0).Rows(0).Item(1).ToString & " > " & str1
            str = "<a href=./Product_Information.aspx?subcatid=" & ds.Tables(0).Rows(0).Item(0).ToString & ">" & ds.Tables(0).Rows(0).Item(1).ToString & "</a>" & " > " & str
            getparents(j)
        End If
        'dr.Close()
        con.Close()
        Label13.Text = str
        Session("categorylevel") = Nothing
        Session("categorylevel") = str1
        Session("Category") = str1
    End Function


    Public Sub FillCategoryName(ByVal Catid As String)
        Dim dt As DataTable
        dt = ReturnDataTable("Select * From Category_Master Where Category_Kid ='" & Catid & "' and Category_Isdeleted = '0' and Category_CompanyId ='" & g_Company & "'")
        Dim ParentId As String
        ParentId = dt.Rows(0).Item(3).ToString()
        If ParentId = "Root Category" Then
            Dim j As Integer
            Category(i) = dt.Rows(0).Item(2).ToString() & "  >  "

            For j = 0 To i

                i = i - 1
            Next
            Exit Sub
        Else
            Category(i) = dt.Rows(0).Item(2).ToString() & "  >  "
            i = i + 1

            FillCategoryName(dt.Rows(0).Item(3).ToString())
        End If
     
    End Sub

    Public Function NewPrimaryKey1(ByVal aspxpage As System.Web.UI.UserControl) As String
        NewPrimaryKey1 = ""
        Try
            Dim mycon As New SqlConnection()
            mycon.ConnectionString = ConfigurationManager.AppSettings("ConnectionString")     'Read the ConnectionString from web.config File
            mycon.Open()
            Dim myCommand As New SqlCommand("New_Kid", mycon)
            myCommand.CommandType = CommandType.StoredProcedure
            Dim retValParam As New SqlParameter("@sKey", SqlDbType.Char, 7)
            retValParam.Direction = ParameterDirection.Output
            retValParam.IsNullable = True
            retValParam.Size = 7
            myCommand.Parameters.Add(retValParam)
            Dim reader As SqlDataReader = myCommand.ExecuteReader()
            NewPrimaryKey1 = retValParam.Value.ToString

            myCommand.Dispose()
            If Not reader.IsClosed Then reader.Close()
            If mycon.State = ConnectionState.Open Then mycon.Close()

        Catch Ex As Exception
            NewPrimaryKey1 = ""
        End Try
    End Function
End Class
