Imports EDSL.Entities
Imports EDSL.DAL
Partial Public Class ProductEdit3Control
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        hypPage1.NavigateUrl = "~/ProductAdmin/edit1.aspx?id=" & Request.QueryString("id")
        If IsPostBack = False Then
            If Request.QueryString("id") <> "" Then
                Call GetDetails(Request.QueryString("id"))
                Dim objChoices1 As New System.Web.UI.WebControls.ListItemCollection
                Dim objSelected1 As New System.Web.UI.WebControls.ListItemCollection
                Dim objMainSelector As New dbMainSelectorBox
                objChoices1 = objMainSelector.GetChoices("MainCategory", "CategoryID", "CategoryName", " AND ParentCategoryID = 0 ")
                objSelected1 = objMainSelector.GetSelected("MainProduct_Category", "MainProduct_CategoryID", "ProductID", "CategoryID", Request.QueryString("id"), objChoices1, "MainCategory", "CategoryID", "CategoryName")

                ctlCategories.TheChoices = objChoices1
                ctlCategories.TheSelected = objSelected1
            End If
        End If
        btnBack.NavigateUrl = "~/ProductAdmin/edit1.aspx?id=" & txtID.Value
    End Sub

    Private Sub GetDetails(ByVal intProductID As Integer)
        Dim objMain As New dbMainProduct
        Dim objMainEntity As entMainProduct = objMain.GetItem(intProductID)
        txtID.Value = objMainEntity.ProductID
        txtProductName.Text = objMainEntity.ProductName

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

        'If objMainEntity.MediumImage = "" Then
        '    imgMedium.ImageUrl = ConfigurationManager.AppSettings("SiteURL") & "images/noimage.png"
        'Else
        '    Dim strUploadPath As String = ConfigurationManager.AppSettings("UploadFileURL") & "/products/"
        '    imgMedium.ImageUrl = strUploadPath & objMainEntity.MediumImage
        '    txtMediumImage.Value = objMainEntity.MediumImage
        'End If

        'If objMainEntity.MainImage = "" Then
        '    imgMainImage.ImageUrl = ConfigurationManager.AppSettings("SiteURL") & "images/noimage.png"
        'Else
        '    Dim strUploadPath As String = ConfigurationManager.AppSettings("SiteURL") & "/products/"
        '    imgMainImage.ImageUrl = strUploadPath & objMainEntity.MainImage
        '    txtMainImage.Value = objMainEntity.MainImage
        'End If
    End Sub
    Protected Sub btnDelete_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
        Dim objMain As New dbMainProduct
        Response.Redirect("Delete.aspx?id=" & txtID.Value & "&idtext=" & txtProductName.Text & "&r=e")
    End Sub
    Protected Sub btnSave_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExit.Click, btnSaveNext.Click
        Dim clsMisc As New miscFunctions
        Dim objGlobals As New TheGlobals
        Dim objMainAdmin As New dbMainProduct
        Dim intResultFromSave As Integer

        divErrorPanel.InnerHtml = ""


        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


        'Dim strFileNameMedium As String
        'Dim booTryToSaveMedium As Boolean = False
        'If chkRemoveMainImage.Checked = True Then
        '    strFileNameMedium = ""
        '    booTryToSaveMedium = True
        'Else
        '    strFileNameMedium = clsMisc.UploadFile(filMediumImage, objGlobals.UploadFileTypes.Image)

        '    If strFileNameMedium = "" Then
        '        strFileNameMedium = txtMediumImage.Value
        '        booTryToSaveMedium = True
        '    ElseIf Left(strFileNameMedium, 17) = "EdenlightError:::" Then
        '        divErrorPanel.InnerHtml += "Medium logo : " & Right(strFileNameMedium, Len(strFileNameMedium) - 17) & "<br />"
        '        booTryToSaveMedium = False
        '    Else
        '        booTryToSaveMedium = True
        '    End If
        'End If


        'Dim strFileNameMain As String
        'Dim booTryToSaveMain As Boolean = False
        'If chkRemoveMainImage.Checked = True Then
        '    strFileNameMain = ""
        '    booTryToSaveMain = True
        'Else
        '    strFileNameMain = clsMisc.UploadFile(filMainImage, objGlobals.UploadFileTypes.Image)

        '    If strFileNameMain = "" Then
        '        strFileNameMain = txtMainImage.Value
        '        booTryToSaveMain = True
        '    ElseIf Left(strFileNameMain, 17) = "EdenlightError:::" Then
        '        divErrorPanel.InnerHtml += "Main logo : " & Right(strFileNameMain, Len(strFileNameMain) - 17) & "<br />"
        '        booTryToSaveMain = False
        '    Else
        '        booTryToSaveMain = True
        '    End If
        'End If

        If booTryToSaveThumbnail = True Then
            intResultFromSave = objMainAdmin.SaveItemPage3(txtID.Value, "", "", strFileNameThumb, Session("passover"))
            Select Case intResultFromSave
                Case 0
                    divErrorPanel.InnerHtml = "Error saving product"
                Case 1
                    If sender.id = "btnExit" Then
                        Response.Redirect("list.aspx")
                    Else
                        Response.Redirect("edit4.aspx?id=" & txtID.Value)
                    End If
            End Select
        End If
    End Sub
End Class