Imports EDSL.Entities
Imports EDSL.DAL
Partial Public Class Edit21
    Inherits System.Web.UI.UserControl
    Dim lstOfSelectedSmallProduct As New List(Of Integer)
    Dim lstOfSelectedMediumProduct As New List(Of Integer)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        hypPage1.NavigateUrl = "~/NewsAdmin/edit.aspx?id=" & Request.QueryString("id")
        If IsPostBack = False Then
            If Request.QueryString("id") <> "" Then

                Dim objProducts As New dbMainProduct
                Dim productsSmall As New List(Of entMainProduct)
                Dim productsMedium As New List(Of entMainProduct)
                productsSmall = objProducts.GetRelatedNews(Convert.ToInt32(Request.QueryString("id")), 2)
                productsMedium = objProducts.GetRelatedNews(Convert.ToInt32(Request.QueryString("id")), 3)
                For Each cat In productsSmall
                    lstOfSelectedSmallProduct.Add(cat.ProductID)
                Next
                For Each cat In productsMedium
                    lstOfSelectedMediumProduct.Add(cat.ProductID)
                Next
                Call GetDetails(Request.QueryString("id"))

            End If
        End If
        btnBack.NavigateUrl = "~/ProductAdmin/edit1.aspx?id=" & txtID.Value
    End Sub
    Private Sub GetDetails(ByVal intNewsId As Integer)
        Dim objMain As New dbMainNews
        Dim objMainEntity As entMainNews = objMain.GetItem(intNewsId)
        txtID.Value = objMainEntity.NewsID
        txtNewsArticle.Text = objMainEntity.NewsHeadline
        Dim objCategories As New dbMainCategory
        Dim categoriesParent As New List(Of entmainCategory)
        categoriesParent = objCategories.GetRelatedNews(Convert.ToInt32(Request.QueryString("id")), 0)
        For Each cat In categoriesParent
            Select Case cat.CategoryID
                Case 1
                    chkCategorySolvitt.Checked = True
                Case 2
                    chkCategorySmall.Checked = True
                Case 3
                    chkCategoryMedium.Checked = True
                Case 0
                    chkBoxGeneral.Checked = True
            End Select
        Next
        Dim objProducts As New dbMainProduct
        Dim productsSmall As New List(Of entMainProduct)
        Dim productsMedium As New List(Of entMainProduct)
        productsSmall = objProducts.GetAllByCategoryID(2)
        productsMedium = objProducts.GetAllByCategoryID(3)
        rptCategoryMediumProducts.DataSource = productsMedium
        rptCategorySmallProducts.DataSource = productsSmall
        rptCategoryMediumProducts.DataBind()
        rptCategorySmallProducts.DataBind()


        'Dim objMainCat As New dbMainCategory
        'Dim objMainEntityCategory As New List(Of entmainCategory)
        ''Small Category
        'objMainEntityCategory = objMainCat.GetAll(" AND ParentCategoryID = 2 AND ( CategoryID <> 4 AND CategoryID <> 5)", " ORDER BY CategoryName ASC")
        'rptCategorySmall.DataSource = objMainEntityCategory
        'rptCategorySmall.DataBind()
        ''Medium Category
        'Dim objMainEntityMediumCategory As New List(Of entmainCategory)
        'objMainEntityMediumCategory = objMainCat.GetAll(" AND ParentCategoryID = 3 AND ( CategoryID <> 4 AND CategoryID <> 5)", " ORDER BY CategoryName ASC")
        'rptCategoryMedium.DataSource = objMainEntityMediumCategory
        'rptCategoryMedium.DataBind()


    End Sub

    Private Sub CategoryRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptCategorySmallProducts.ItemDataBound


        Dim item As RepeaterItem = e.Item
        If (item.ItemType = ListItemType.Item) OrElse (item.ItemType = ListItemType.AlternatingItem) Then
            'Dim hidfield As HiddenField = DirectCast(item.FindControl("hid"), HiddenField)
            'Dim chkBox As CheckBox = DirectCast(item.FindControl("chkCategoryItemSmall"), CheckBox)

            'Dim categoryArray As Array
            'Dim categoryID As New Integer

            'categoryArray = Split(hidfield.Value, ",")
            'categoryID = Convert.ToInt32(categoryArray(0))

            'Dim objProduct As New dbMainProduct
            'Dim productAssigned As New List(Of entMainProduct)
            'Dim lstOfAssignProductID As New List(Of Integer)
            'productAssigned = objProduct.GetRelatedNews(Convert.ToInt32(txtID.Value), categoryID)
            'For Each product In productAssigned
            '    lstOfAssignProductID.Add(product.ProductID)
            'Next



            ''Populate the checkboxes with existing data
            'If lstOfSelectedSmallCategories.Contains(categoryID) Then
            '    chkBox.Checked = True
            'End If

            'Dim rptCategorySmallProducts As Repeater = DirectCast(item.FindControl("rptCategorySmallProducts"), Repeater)
            'Dim objMainProd As New dbMainProduct
            'Dim objMainEntityProduct As New List(Of entMainProduct)
            'objMainEntityProduct = objMainProd.GetAllByCategoryID(categoryID)

            'rptCategorySmallProducts.DataSource = objMainEntityProduct
            'rptCategorySmallProducts.DataBind()

            'Dim repeaterCollection As RepeaterItemCollection = rptCategorySmallProducts.Items

            'Dim productCheckBox As CheckBox
            'Dim productHidID As HiddenField
            'For Each ItemProduct As RepeaterItem In repeaterCollection
            '    productCheckBox = CType(ItemProduct.FindControl("chkProductItemSmall"), CheckBox)
            '    productHidID = CType(ItemProduct.FindControl("hidProduct"), HiddenField)
            '    If lstOfAssignProductID.Contains(productHidID.Value) Then
            '        productCheckBox.Checked = True
            '    End If

            'Next
            Dim productCheckBox As CheckBox = DirectCast(item.FindControl("chkProductItemSmall"), CheckBox)
            Dim productHidID As HiddenField = DirectCast(item.FindControl("hidProduct"), HiddenField)
            If lstOfSelectedSmallProduct.Contains(productHidID.Value) Then
                productCheckBox.Checked = True
            End If

        End If
    End Sub
    Private Sub CategoryRepeaterMedium_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptCategoryMediumProducts.ItemDataBound

        Dim item As RepeaterItem = e.Item
        If (item.ItemType = ListItemType.Item) OrElse (item.ItemType = ListItemType.AlternatingItem) Then
            'Dim hidfield As HiddenField = DirectCast(item.FindControl("hid"), HiddenField)
            'Dim chkBox As CheckBox = DirectCast(item.FindControl("chkCategoryItemMedium"), CheckBox)
            'Dim categoryID As New Integer
            'Dim categoryArray As Array
            'categoryArray = Split(hidfield.Value, ",")
            'categoryID = Convert.ToInt32(categoryArray(0))

            'Dim objProduct As New dbMainProduct
            'Dim productAssigned As New List(Of entMainProduct)
            'Dim lstOfAssignProductID As New List(Of Integer)
            'productAssigned = objProduct.GetRelatedNews(Convert.ToInt32(txtID.Value), categoryID)
            'For Each product In productAssigned
            '    lstOfAssignProductID.Add(product.ProductID)
            'Next



            ''Populate the checkboxes with existing data
            'If lstOfSelectedMediumCategories.Contains(categoryID) Then
            '    chkBox.Checked = True
            'End If


            'Dim rptCategoryMediumProducts As Repeater = DirectCast(item.FindControl("rptCategoryMediumProducts"), Repeater)
            'Dim objMainProd As New dbMainProduct
            'Dim objMainEntityProduct As New List(Of entMainProduct)
            'objMainEntityProduct = objMainProd.GetAllByCategoryID(categoryID)

            'rptCategoryMediumProducts.DataSource = objMainEntityProduct
            'rptCategoryMediumProducts.DataBind()

            'Dim repeaterCollection As RepeaterItemCollection = rptCategoryMediumProducts.Items

            'Dim productCheckBox As CheckBox
            'Dim productHidID As HiddenField
            'For Each ItemProduct As RepeaterItem In repeaterCollection
            '    productCheckBox = CType(ItemProduct.FindControl("chkProductItemMedium"), CheckBox)
            '    productHidID = CType(ItemProduct.FindControl("hidProduct"), HiddenField)
            '    If lstOfAssignProductID.Contains(productHidID.Value) Then
            '        productCheckBox.Checked = True
            '    End If

            'Next

            Dim productCheckBox As CheckBox = DirectCast(item.FindControl("chkProductItemMedium"), CheckBox)
            Dim productHidID As HiddenField = DirectCast(item.FindControl("hidProduct"), HiddenField)
            If lstOfSelectedMediumProduct.Contains(productHidID.Value) Then
                productCheckBox.Checked = True
            End If
        End If
    End Sub
    
    Protected Sub btnSave_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExit.Click
        Dim clsMisc As New miscFunctions
        Dim objGlobals As New TheGlobals
        Dim objMainAdmin As New dbMainProduct
        Dim solvitt As New Integer
        Dim medium As New Integer
        Dim small As New Integer

        'Delete all preferences first
        Dim objCategory As New dbMainCategory
        Dim objProduct As New dbMainProduct
        Dim newsID As Integer = txtID.Value
        Dim categoryID As New Integer
        objCategory.DeleteAllRelatedNews(newsID)
        objProduct.DeleteAllRelatedNews(newsID)

        'Get all the data from the check boxes
        'Get general
        If chkBoxGeneral.Checked Then
            objCategory.InsertAllRelatedNews(newsID, 0, 0)

        End If

        'Get solvitt first
        If chkCategorySolvitt.Checked Then
            categoryID = 1
            solvitt = 1
            objCategory.InsertAllRelatedNews(newsID, 1, 0)
        End If

        'Get medium selection
        If chkCategoryMedium.Checked Then
            categoryID = 3
            medium = 1
            objCategory.InsertAllRelatedNews(newsID, 3, 0)
        End If
        'Get the data from the medium checkboxes 
        Dim rc As RepeaterItemCollection = rptCategoryMediumProducts.Items
        InsertNewCategoriesAndProduct(rc, categoryID, "chkProductItemMedium", newsID)
        'Get small selection
        If chkCategorySmall.Checked Then
            categoryID = 2
            small = 1
            objCategory.InsertAllRelatedNews(newsID, 2, 0)
        End If
        'Get the data from the small checkboxes 
        Dim rcSmall As RepeaterItemCollection = rptCategorySmallProducts.Items
        InsertNewCategoriesAndProduct(rcSmall, categoryID, "chkProductItemSmall", newsID)

        Response.Redirect("List.aspx")
    End Sub
    Private Sub InsertNewCategoriesAndProduct(ByVal mainRepeaterCollection As RepeaterItemCollection, ByVal categoryType As Integer, ByVal productCheckboxName As String, ByVal newsID As Integer)

        'Dim repeaterProductName As String = repeaterProductsName
        'Dim catCheckboxName As String = categoryCheckBoxName
        'Dim prodCheckboxName As String = productCheckboxName
        'Dim objproduct As New dbMainProduct
        'Dim objCategory As New dbMainCategory

        'Dim categoryArray As Array
        'Dim categoryID As New Integer
        'Dim parentID As New Integer
        'Dim MyCheckBox As CheckBox
        '' Go through each item the category repeater
        'For Each Item As RepeaterItem In mainRepeaterCollection
        '    MyCheckBox = CType(Item.FindControl(catCheckboxName), CheckBox)
        '    If MyCheckBox.Checked = True Then
        '        categoryArray = Split(CType(Item.FindControl("hid"), HiddenField).Value, ",")
        '        categoryID = Convert.ToInt32(categoryArray(0))
        '        parentID = Convert.ToInt32(categoryArray(1))
        '        If categoryID > 0 Then
        '            'Insert the new categories
        '            objCategory.InsertAllRelatedNews(newsID, categoryID, parentID)

        '            'Do something with the products
        '            Dim rpt As Repeater
        '            Dim rcProduct As RepeaterItemCollection
        '            rpt = CType(Item.FindControl(repeaterProductName), Repeater)
        '            rcProduct = rpt.Items
        '            Dim productCheckBox As CheckBox
        '            For Each ItemProduct As RepeaterItem In rcProduct
        '                Dim productID As New Integer
        '                productCheckBox = CType(ItemProduct.FindControl(prodCheckboxName), CheckBox)
        '                If productCheckBox.Checked = True Then
        '                    productID = CType(ItemProduct.FindControl("hidProduct"), HiddenField).Value
        '                    ' Insert the new products
        '                    objproduct.InsertAllRelatedNews(newsID, categoryID, productID)
        '                End If
        '            Next
        '        End If
        '    End If
        'Next

        Dim objproduct As New dbMainProduct
        Dim objCategory As New dbMainCategory

        Dim MyCheckBox As CheckBox


        ' Go through each item the category repeater
        For Each Item As RepeaterItem In mainRepeaterCollection
            MyCheckBox = CType(Item.FindControl(productCheckboxName), CheckBox)
            If MyCheckBox.Checked = True Then
                'Do something with the products
                Dim productID As New Integer
                productID = CType(Item.FindControl("hidProduct"), HiddenField).Value
                ' Insert the new products
                objproduct.InsertAllRelatedNews(newsID, categoryType, productID)
            End If
        Next

    End Sub


End Class