﻿Imports System
Imports System.Data


Partial Class SearchProductIndex
    Inherits System.Web.UI.Page
    Shared dtMain As DataTable
    Shared dtChild As DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Try

                'If (Request.QueryString("BuyerProductIndexPage")) = Nothing Then
                '    Session("BuyerProductIndexPage") = 1
                'Else
                '    Session("BuyerProductIndexPage") = Request.QueryString("BuyerProductIndexPage").ToString
                'End If

                fillgrid()

                HiddenField1.Value = ReturnValue("Select Product_Name from ProductIndex where ProductName='" & Request.QueryString("ProductIndex").ToString.Trim & "'").ToString.Trim
                Me.Header.Title = "Product Index - " & ReturnValue("Select Product_Name from ProductIndex where ProductName='" & Request.QueryString("ProductIndex").ToString.Trim & "'").ToString.Trim & " - Atcomaart Industrial Supply."
            Catch ex As Exception

            End Try
        End If

    End Sub

    Public Sub fillgrid()
        Try
            Dim dt As DataTable
            dt = ReturnDataTable("SELECT     CategoryDistinct.Category_ParentName, CategoryDistinct.Category_Kid, CategoryDistinct.Category_Name, CategoryDistinct.Category_ParentId, CategoryDistinct.ParentName, CategoryDistinct.CategoryName, SUM(V1.Count) AS Count, V1.CategoryIndex_Name, V1.Name FROM   V1 INNER JOIN  CategoryDistinct ON V1.Category_ParentId = CategoryDistinct.Category_Kid GROUP BY CategoryDistinct.Category_ParentName, CategoryDistinct.Category_Kid, CategoryDistinct.Category_Name, CategoryDistinct.Category_ParentId,              CategoryDistinct.ParentName, CategoryDistinct.CategoryName, V1.CategoryIndex_Name, V1.Name HAVING      (V1.CategoryIndex_Name = '" & ReturnValue("Select Product_Name from ProductIndex where ProductName='" & Request.QueryString("ProductIndex").ToString.Trim & "'").ToString.Trim & "')")

            gvProductIndex.Columns(0).Visible = True
            gvProductIndex.DataSource = dt
            gvProductIndex.DataBind()
            gvProductIndex.Columns(0).Visible = False

            Dim count As Integer
            count = 0
            If dt.Rows.Count > 0 Then
                Dim i As Integer
                For i = 0 To dt.Rows.Count - 1
                    count += CType(dt.Rows(i).Item("Count").ToString, Integer)
                Next
                lblHeader.Text = "You Searched >&nbsp; <a href='" & Request.RawUrl.ToString & "' style='color:#082b53;' > " & ReturnValue("Select Product_Name from ProductIndex where ProductName='" & Request.QueryString("ProductIndex").ToString.Trim & "'").ToString.Trim & "&nbsp;<b>(" & count.ToString & ")</b></a>"
                tdProductIndex.Visible = True
            Else

                lblHeader.Text = "<b>Search Result Not Found </b>"
                tdSearchNotFound.InnerHtml = "<img alt='' src='" & Page.ResolveUrl("~/images/Search_not_Found.GIF") & "' />"
                tdProductIndex.Visible = False
            End If
            'gvProductIndex.HeaderRow.Cells(1).Text = "Search For :  " & ReturnValue("Select Product_Name from ProductIndex where ProductName='" & Request.QueryString("ProductIndex").ToString.Trim & "'").ToString.Trim
            dt.Dispose()
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub gvProductIndex_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvProductIndex.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim dt As DataTable
            'dt = ReturnDataTable("SELECT DISTINCT Category_Index_Master.Category_Kid, Category_Master.Category_Name FROM         Category_Index_Master INNER JOIN                       Category_Master ON Category_Index_Master.Category_Kid = Category_Master.Category_Kid WHERE     (Category_Master.Category_ParentId = N'" & RemoveLiterals(e.Row.Cells(0).Text.ToString).ToString & "') AND (Category_Index_Master.CategoryIndex_IsDeleted = N'0') AND (Category_Master.Category_IsDeleted = N'0') and (Category_Index_Master.CategoryIndex_Name = N'" & ReturnValue("Select Product_Name from ProductIndex where ProductName='" & Request.QueryString("ProductIndex").ToString.Trim & "'").ToString.Trim & "')")
            'dt = ReturnDataTable("SELECT DISTINCT CategoryDistinct.Category_ParentName, CategoryDistinct.Category_Kid, CategoryDistinct.Category_Name, CategoryDistinct.Category_ParentId,              CategoryDistinct.ParentName, CategoryDistinct.CategoryName FROM         Category_Index_Master LEFT OUTER JOIN                       CategoryDistinct ON Category_Index_Master.Category_Kid = CategoryDistinct.Category_Kid WHERE     (Category_Index_Master.CategoryIndex_IsDeleted = N'0') AND (Category_Index_Master.CategoryIndex_Name = N'" & ReturnValue("Select Product_Name from ProductIndex where ProductName='" & Request.QueryString("ProductIndex").ToString.Trim & "'").ToString.Trim & "') AND                        (CategoryDistinct.Category_ParentId = N'" & RemoveLiterals(e.Row.Cells(0).Text.ToString).ToString & "')")
            'dt = ReturnDataTable("SELECT DISTINCT                       CategoryDistinct.Category_ParentName, CategoryDistinct.Category_Kid, CategoryDistinct.Category_Name, CategoryDistinct.Category_ParentId,                       CategoryDistinct.ParentName, CategoryDistinct.CategoryName, ISNULL(ProductCategoryCount.Expr1, N'0') AS Count FROM         ProductCategoryCount RIGHT OUTER JOIN                       CategoryDistinct ON ProductCategoryCount.Product_CategoryId = CategoryDistinct.Category_Kid RIGHT OUTER JOIN                       Category_Index_Master ON CategoryDistinct.Category_Kid = Category_Index_Master.Category_Kid WHERE     (Category_Index_Master.CategoryIndex_IsDeleted = N'0') AND (Category_Index_Master.CategoryIndex_Name = N'" & ReturnValue("Select Product_Name from ProductIndex where ProductName='" & Request.QueryString("ProductIndex").ToString.Trim & "'").ToString.Trim & "') AND                       (CategoryDistinct.Category_ParentId = N'" & RemoveLiterals(e.Row.Cells(0).Text.ToString).ToString & "')")
            'dt = ReturnDataTable("SELECT DISTINCT                       CategoryDistinct.Category_ParentName, CategoryDistinct.Category_Kid, CategoryDistinct.Category_Name, CategoryDistinct.Category_ParentId, CategoryDistinct.ParentName, CategoryDistinct.CategoryName, COUNT(Product_Information.Product_CategoryId) AS Count,Category_Index_Master.CategoryIndex_Name, Category_Index_Master.CategoryIndex_IsDeleted FROM         Product_Information RIGHT OUTER JOIN                       Category_Index_Master ON Product_Information.Product_CategoryId = Category_Index_Master.Category_Kid LEFT OUTER JOIN                       CategoryDistinct ON Category_Index_Master.Category_Kid = CategoryDistinct.Category_Kid GROUP BY CategoryDistinct.Category_ParentName, CategoryDistinct.CategoryName, CategoryDistinct.ParentName, CategoryDistinct.Category_Name,                        CategoryDistinct.Category_Kid, Product_Information.Product_CategoryId, CategoryDistinct.Category_ParentId,              Category_Index_Master.CategoryIndex_Name, Category_Index_Master.CategoryIndex_IsDeleted HAVING      (Category_Index_Master.CategoryIndex_IsDeleted = N'0') AND (Category_Index_Master.CategoryIndex_Name = N'" & ReturnValue("Select Product_Name from ProductIndex where ProductName='" & Request.QueryString("ProductIndex").ToString.Trim & "'").ToString.Trim & "') AND                        (CategoryDistinct.Category_ParentId = N'" & RemoveLiterals(e.Row.Cells(0).Text.ToString).ToString & "')")
            dt = ReturnDataTable("SELECT     Category_ParentName, Category_Kid, Category_Name, ParentName, CategoryName, Count, CategoryIndex_Name, CategoryIndex_IsDeleted,                        Category_ParentId, CategoryIndex_Name AS Expr1, CategoryIndex_IsDeleted AS Expr2, Category_ParentId AS Expr3             FROM V1 WHERE     (CategoryIndex_Name = '" & ReturnValue("Select Product_Name from ProductIndex where ProductName='" & Request.QueryString("ProductIndex").ToString.Trim & "'").ToString.Trim & "') AND (CategoryIndex_IsDeleted = N'0') AND (Category_ParentId = N'" & RemoveLiterals(e.Row.Cells(0).Text.ToString).ToString & "')ORDER BY Category_Name")
            CType(e.Row.Cells(1).FindControl("dlchildCategory"), DataList).DataSource = dt
            CType(e.Row.Cells(1).FindControl("dlchildCategory"), DataList).DataBind()
            'If e.Row.RowState = DataControlRowState.Normal Then
            '    e.Row.BackColor = Drawing.Color.White
            '    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#B7CBE3'")
            '    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='white'")
            'End If
            'If e.Row.RowState = DataControlRowState.Alternate Then
            '    Dim c As Drawing.Color
            '    c = Drawing.Color.FromArgb(230, 239, 246)
            '    e.Row.BackColor = c
            '    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#B7CBE3'")
            '    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E6EFF6'")
            'End If
            'CType(e.Row.Cells(1).FindControl("hlParentCategory"), HyperLink).NavigateUrl = ResolveUrl("~/category/" & EncodeURL(RemoveLiterals(e.Row.Cells(0).Text)) & "/" & e.Row.Cells(3).Text & "/" & e.Row.Cells(2).Text & ".aspx")
           
        End If
        'If e.Row.RowType = DataControlRowType.Footer Then
        '    Dim nextimg, backimg As String
        '    nextimg = "Images/aroow_inside.jpg"
        '    backimg = "Images/raroow_inside.jpg"
        '    CType(e.Row.Cells(0).FindControl("hrlNext"), HyperLink).Text = "Next <img src='" & Page.ResolveUrl(nextimg) & "' alt='Next' style='height: 10px; width: 21px;border:none;' /> "
        '    CType(e.Row.Cells(0).FindControl("hrlBack"), HyperLink).Text = "<img src='" & Page.ResolveUrl(backimg) & "' alt='Back' style='height: 10px; width: 21px;border:none;' /> Back"
        'End If

    End Sub

    Protected Sub dlChildCategory_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            'Dim dl As New DataList
            'dl = sender
            'Dim str As String
            'str = dl.DataKeys(e.Item.ItemIndex).ToString
            'Dim count As Integer
            'count = ReturnValue("SELECT     COUNT(*) AS Expr1             FROM Product_Information WHERE  (Product_CategoryId = N'" & RemoveLiterals(dl.DataKeys(e.Item.ItemIndex).ToString) & "')")
            'CType(e.Item.FindControl("hlchildcategory"), HyperLink).Text += "&nbsp;&nbsp;(" & count & ")"
            'str = ""
        End If
    End Sub
    'Protected Sub dlChildCategory_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    '    Dim dl As DataList
    '    dl = sender
    '    Dim i As Integer
    '    'CType(dl.Items(0).FindControl("tdHide"), HtmlTableCell).InnerText = "asdad"


    '    Dim count As Integer = dl.Items.Count
    '    Dim countstart As Integer

    '    If (count Mod 2) = 0 Then
    '        countstart = count / 2

    '        For i = countstart To count - 1
    '            'CType(dl.Items(i).FindControl("tdHide"), HtmlTableCell).InnerText = i.ToString
    '            CType(dl.Items(i).FindControl("tdHide"), HtmlTableCell).Visible = False
    '        Next


    '    ElseIf count <> 1 Then

    '        Dim strCount As String
    '        strCount = count / 2
    '        countstart = Convert.ToInt32(strCount.Remove(strCount.LastIndexOf("."))) 'Convert.ToInt32((count / 2))
    '        countstart = countstart + 1


    '        For i = countstart To count - 1
    '            'CType(dl.Items(i).FindControl("tdHide"), HtmlTableCell).InnerText = i.ToString
    '            CType(dl.Items(i).FindControl("tdHide"), HtmlTableCell).Visible = False
    '        Next

    '    End If

    'End Sub

    Protected Sub gvProductIndex_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvProductIndex.PageIndexChanging
        gvProductIndex.PageIndex = e.NewPageIndex
        fillgrid()
    End Sub
End Class
