Imports EDSL.Entities
Imports EDSL.DAL
Partial Public Class CaseStudyEdit3Control
    Inherits System.Web.UI.UserControl
    Dim lstOfSelectedSmallCategories As New List(Of Integer)
    Dim lstOfSelectedMediumCategories As New List(Of Integer)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        hypPage1.NavigateUrl = "~/CaseStudyAdmin/edit1.aspx?id=" & Request.QueryString("id")
        hypPage2.NavigateUrl = "~/CaseStudyAdmin/edit2.aspx?id=" & Request.QueryString("id")


        If IsPostBack = False Then
            If Request.QueryString("id") <> "" Then

                Dim objCategories As New dbMainCategory
                Dim categoriesSmall As New List(Of entmainCategory)
                Dim categoriesMedium As New List(Of entmainCategory)
                categoriesSmall = objCategories.GetRelatedCaseStudies(Convert.ToInt32(Request.QueryString("id")), 2)
                categoriesMedium = objCategories.GetRelatedCaseStudies(Convert.ToInt32(Request.QueryString("id")), 3)
                For Each cat In categoriesSmall
                    lstOfSelectedSmallCategories.Add(cat.CategoryID)
                Next
                For Each cat In categoriesMedium
                    lstOfSelectedMediumCategories.Add(cat.CategoryID)
                Next
                Call GetDetails(Request.QueryString("id"))

            End If
        End If
        btnBack.NavigateUrl = "~/ProductAdmin/edit2.aspx?id=" & txtID.Value
    End Sub
    Private Sub GetDetails(ByVal intCaseStudyID As Integer)
        Dim objMain As New dbMainCaseStudy
        Dim objMainEntity As entMainCaseStudy = objMain.GetItem(intCaseStudyID)
        txtID.Value = objMainEntity.CaseStudyID
        txtCaseStudyHeadline.Text = objMainEntity.CaseStudyHeadline

        Dim objCategories As New dbMainCategory
        Dim categoriesParent As New List(Of entmainCategory)
        categoriesParent = objCategories.GetRelatedCaseStudies(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
            End Select
        Next

        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 rptCategorySmall.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.GetRelatedCaseStudy(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


        End If
    End Sub
    Private Sub CategoryRepeaterMedium_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptCategoryMedium.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.GetRelatedCaseStudy(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
        End If
    End Sub
    Protected Sub btnDelete_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
        Dim objMain As New dbMainCaseStudy
        Response.Redirect("Delete.aspx?id=" & txtID.Value & "&idtext=" & txtCaseStudyHeadline.Text & "&r=e")
    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 caseStudyID As Integer = txtID.Value

        objCategory.DeleteAllRelatedCaseStudies(caseStudyID)
        objProduct.DeleteAllRelatedCaseStudies(caseStudyID)

        'Get all the data from the check boxes
        'Get solvitt first
        If chkCategorySolvitt.Checked Then
            solvitt = 1
            objCategory.InsertAllRelatedCaseStudies(caseStudyID, 1, 0)
        End If

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

        Response.Redirect("List.aspx")


    End Sub
    Private Sub InsertNewCategoriesAndProduct(ByVal mainRepeaterCollection As RepeaterItemCollection, ByVal repeaterProductsName As String, ByVal categoryCheckBoxName As String, ByVal productCheckboxName As String, ByVal caseStudyID 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.InsertAllRelatedCaseStudies(caseStudyID, 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.InsertAllRelatedCaseStudies(caseStudyID, categoryID, productID)
                        End If
                    Next
                End If
            End If
        Next

    End Sub
End Class