Imports EDSL.Entities
Imports EDSL.DAL
Partial Public Class userPageEditControl
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtPageContent.BaseHref = ConfigurationManager.AppSettings("BaseHref") & "PageEdit"
        txtPageContent.BasePath = ConfigurationManager.AppSettings("BasePath")
        txtBulletList.BaseHref = ConfigurationManager.AppSettings("BaseHref") & "PageEdit"
        txtBulletList.BasePath = ConfigurationManager.AppSettings("BasePath")
        If IsPostBack = False Then
            Call BuildDropDowns()
            If Request.QueryString("id") = 0 OrElse Request.QueryString("id") = "" Then
                imgMainImage.ImageUrl = ConfigurationManager.AppSettings("BaseHref") & "images/noimage.png"
                txtPageMetaName.Text = "Description"
            Else

                Call GetDetails(Request.QueryString("id"))
            End If
        End If
    End Sub
    Private Sub GetDetails(ByVal intLoginID As Integer)
        ' Populate the form fields
        Dim objMain As New dbMainUserPage
        Dim objMainEntity As entUserPage = objMain.GetItem(intLoginID)
        With objMainEntity
            txtID.Value = .PageID
            txtPageTitle.Text = .PageTitle
            txtPageContent.Value = .PageContent
            txtBulletList.Value = .BulletList
            txtPageMetaName.Text = .PageMetaName
            txtPageMetaContent.Text = .PageMetaContent
            txtMetaTitle.Text = .MetaTitle
            ddlCategories.SelectedValue = .CategoryID
            If objMainEntity.MainImage = "" Then
                imgMainImage.ImageUrl = ConfigurationManager.AppSettings("BaseHref") & "images/noimage.png"
            Else
                Dim strUploadPath As String = ConfigurationManager.AppSettings("UploadFileURL") & "images/"
                imgMainImage.ImageUrl = strUploadPath & .MainImage
                txtMainImage.Value = .MainImage
            End If
            If .Active = 1 Then
                chkActive.Checked = True
            Else
                chkActive.Checked = False
            End If
            txtPageURL.Text = ConfigurationManager.AppSettings("SiteURL") & "Page.aspx?id=" & .PageID

        End With
    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 dbMainUserPage
        Dim intResultFromSave As Integer
        Dim intID As Integer
        If txtID.Value = "" Then
            intID = 0
        Else
            intID = CInt(txtID.Value)
        End If

        Dim strFileName As String
        Dim booTryToSave As Boolean = False
        If chkRemoveImage.Checked = True Then
            strFileName = ""
            booTryToSave = True
        Else
            strFileName = clsMisc.UploadFile(filMainImage, objGlobals.UploadFileTypes.Image)

            If strFileName = "" Then
                strFileName = txtMainImage.Value
                booTryToSave = True
            ElseIf Left(strFileName, 17) = "EdenlightError:::" Then
                divErrorPanel.InnerHtml = Right(strFileName, Len(strFileName) - 17)
                booTryToSave = False
            Else
                booTryToSave = True
            End If
        End If

        If booTryToSave = True Then
            intResultFromSave = objMainAdmin.SaveItem(intID, txtPageTitle.Text, txtPageContent.Value.ToString(), txtPageMetaName.Text, txtPageMetaContent.Text, txtMetaTitle.Text, strFileName, IIf(chkActive.Checked = True, 1, 0), Session("passover"), Convert.ToInt32(ddlCategories.SelectedValue.ToString()), txtBulletList.Value.ToString())
            Select Case intResultFromSave
                Case 0
                    Response.Redirect("userPagelist.aspx")
                Case 1
                    divErrorPanel.InnerHtml = "Error saving page details"
                Case Else
                    Response.Redirect("userPagelist.aspx")
            End Select
        End If
    End Sub
    Protected Sub btnBack_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBack.Click
        Response.Redirect("userPagelist.aspx")
    End Sub
    Private Sub BuildDropDowns()
        'Get dropdown values for template styles
        'Dim objSector As New dbMainDropDownFunctions
        'Dim sectorList As List(Of entMainDropDown) = objSector.GetDropDownValues(2)
        'ddlCategories.Items.Add(New ListItem("Please Select..", ""))
        'For Each sector As entMainDropDown In sectorList
        '    ddlCategories.Items.Add(New ListItem(sector.TextDisplay, sector.DropDownID))
        'Next

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

        For Each category As entmainCategory In categoryList
            ddlCategories.Items.Add(New ListItem(category.CategoryAdminTitle, category.CategoryID))
        Next


    End Sub
End Class
