Imports EDSL.Entities
Imports EDSL.DAL
Partial Public Class CategoryEditControl
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        hypPage2.NavigateUrl = "~/CategoryAdmin/edit2.aspx?id=" & Request.QueryString("id")
        txtCaseStudySummary.BaseHref = ConfigurationManager.AppSettings("BaseHref") & "PageEdit"
        txtCaseStudySummary.BasePath = ConfigurationManager.AppSettings("BasePath")
        txtBulletList.BaseHref = ConfigurationManager.AppSettings("BaseHref") & "PageEdit"
        txtBulletList.BasePath = ConfigurationManager.AppSettings("BasePath")
        If IsPostBack = False Then
            Call Populateddl()
            If Request.QueryString("id") = 0 OrElse Request.QueryString("id") = "" Then
                txtHiddenStyleID.Value = 0
                btnDelete.Visible = False
                If Request.QueryString("id") = "" Then
                    txtParentCategory.Value = 0

                Else
                    txtParentCategory.Value = Request.QueryString("cid")
                    ddlCategories.SelectedValue = Request.QueryString("cid")
                End If
            Else
                btnDelete.Visible = True
                Call GetDetails(Request.QueryString("id"))
            End If
        End If

    End Sub
    Private Sub Populateddl()
        'Get dropdown values for Categories
        Dim objCategory As New dbMainCategory
        Dim categoryList As List(Of entmainCategory) = objCategory.GetAll(" AND ( Active = 1  )", " ORDER BY CategoryName")

        For Each category As entmainCategory In categoryList
            ddlCategories.Items.Add(New ListItem(category.CategoryName, category.CategoryID))
        Next
    End Sub
    Private Sub GetDetails(ByVal intCategoryID As Integer)
        Dim objMain As New dbMainCategory
        Dim objMainEntity As entmainCategory = objMain.GetItem(intCategoryID)
        With objMainEntity
            txtID.Value = .CategoryID
            txtName.Text = .CategoryName
            txtParentCategory.Value = .ParentCategoryID
            txtCaseStudySummary.Value = .CategorySummary
            txtMetaDescription.Text = .MetaDescription
            txtMetaTitleText.Text = .MetaTitleBar
            txtBulletList.Value = .BulletList
            If .CategoryStyleID > 0 Then
                txtHiddenStyleID.Value = .CategoryStyleID
            Else
                txtHiddenStyleID.Value = 0
            End If

            If .ParentCategoryID = 0 Then
                ddlCategories.SelectedValue = .CategoryID
            Else
                ddlCategories.SelectedValue = .ParentCategoryID
            End If


            If .ThumbImage = "" Then
                imgThumbnail.ImageUrl = ConfigurationManager.AppSettings("BaseHref") & "Images/noimage.png"
            Else
                Dim strUploadPath As String = ConfigurationManager.AppSettings("UploadFileURL") & "images/"
                imgThumbnail.ImageUrl = strUploadPath & .ThumbImage
                txtThumbnail.Value = .ThumbImage
            End If


            If .Active = 1 Then
                chkActive.Checked = True
            Else
                chkActive.Checked = False
            End If

        End With
    End Sub
    Protected Sub btnDelete_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
        Dim objMain As New dbMainCategory
        Response.Redirect("Delete.aspx?id=" & txtID.Value & "&idtext=" & txtName.Text & "&id=" & txtParentCategory.Value & "&r=e")
    End Sub

    Protected Sub btnBack_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBack.Click
        Dim objMain As New dbMainCategory
        Response.Redirect("List.aspx?cid=" & txtParentCategory.Value)
    End Sub

    Protected Sub btnSave_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        Dim clsMisc As New miscFunctions
        Dim objGlobals As New TheGlobals
        Dim objMainAdmin As New dbMainCategory
        Dim intResultFromSave As Integer
        Dim intID As Integer

        If txtID.Value = "" Then
            intID = 0
        Else
            intID = CInt(txtID.Value)
        End If

        Dim strFileNameThumb As String
        Dim booTryToSaveThumbnail As Boolean = False
        If chkRemoveThumbnailImage.Checked = True Then
            strFileNameThumb = ""
            booTryToSaveThumbnail = True
        Else
            strFileNameThumb = clsMisc.UploadFile(filThumbnail, objGlobals.UploadFileTypes.Image)

            If strFileNameThumb = "" Then
                strFileNameThumb = txtThumbnail.Value
                booTryToSaveThumbnail = True
            ElseIf Left(strFileNameThumb, 17) = "EdenlightError:::" Then
                divErrorPanel.InnerHtml += "Thumb logo : " & Right(strFileNameThumb, Len(strFileNameThumb) - 17) & "<br />"
                booTryToSaveThumbnail = False
            Else
                booTryToSaveThumbnail = True
            End If
        End If

        If booTryToSaveThumbnail = True Then
            intResultFromSave = objMainAdmin.SaveItem(intID, txtName.Text, txtParentCategory.Value, txtCaseStudySummary.Value.ToString, strFileNameThumb, IIf(chkActive.Checked = True, 1, 0), Session("passover"), txtMetaTitleText.Text.ToString(), txtMetaDescription.Text.ToString(), txtHiddenStyleID.Value, txtBulletList.Value.ToString())
            Select Case intResultFromSave
                Case 0
                    Response.Redirect("List.aspx?cid=" & txtParentCategory.Value)
                Case 1
                    divErrorPanel.InnerHtml = "Error saving category item"
                Case Else
                    Response.Redirect("List.aspx?cid=" & txtParentCategory.Value)
            End Select
        End If
    End Sub
End Class