Imports System.Data
Imports System.Data.SqlClient

Partial Class WishList
    Inherits System.Web.UI.Page
    Public Shared amount As Decimal = 0.0
    Shared flag As String
    Shared ID As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try



            CType(CType(Me.Master.FindControl("Buyeruserinfo1"), UserControl).FindControl("Image1"), Image).Visible = True
            CType(CType(Me.Master.FindControl("Buyeruserinfo1"), UserControl).FindControl("lnkWhislist"), LinkButton).Font.Bold = True
            CType(CType(Me.Master.FindControl("Buyeruserinfo1"), UserControl).FindControl("lnkWhislist"), LinkButton).Font.Size = 10
          
            lblselect.Visible = False
            If Session("BuyId") Is Nothing Then
                Response.Redirect("buyer-home.aspx?ses=expire")
                Exit Sub
            End If
            If Page.IsPostBack = False Then
                'trGrid.Visible = False
                ' trButtons.Visible = False
                viewwishlist() ' function to bind the grid view 
            End If

        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub

    Protected Sub viewwishlist() ' function to bind the grid view 
        Try

            lblselect.Visible = False
            GvAddWishList.Columns(1).Visible = True
            'GvAddWishList.Columns(2).Visible = True
            GvAddWishList.Columns(7).Visible = True

            Dim row As DataRow
            Dim dtTemp As New DataTable
            Dim con As New SqlConnection                        'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            con.Open()
            Dim strQuery As String = "SELECT Product_Master.Product_Image,dbo.Product_Master.Product_Name,Product_Code,Product_ModelNo,dbo.Product_Master.Product_MarketPrice,Product_SpecialPrice,dbo.AddWishList.AddWishList_Kid," & _
                          "dbo.Product_Master.Product_MarketPrice ,dbo.AddWishList.AddWishList_ProductId, dbo.AddWishList.AddWishList_BuyerUserId, " & _
                          "dbo.Product_Master.Product_Kid " & _
                          "FROM dbo.Product_Master INNER JOIN " & _
                           "dbo.AddWishList ON dbo.Product_Master.Product_Kid = dbo.AddWishList.AddWishList_ProductId " & _
                           "and dbo.AddWishList.AddWishList_BuyerUserId='" & Session("BuyID") & "'"
            dtTemp = ReturnDataTable(strQuery)
            'For Each row In dtTemp.Rows
            '    amount = amount + row("AddWishList_NetAmount")
            'Next
            GvAddWishList.DataSource = dtTemp           ' applied table to a grid view.
            GvAddWishList.DataBind()
            If dtTemp.Rows.Count > 0 Then
                'trGrid.Visible = True
                ' trButtons.Visible = True
                cmdClear.Visible = True
                btncontinue.Visible = True
            Else
                'trButtons.Visible = False
                cmdClear.Visible = False
                btncontinue.Visible = True

            End If

            lbltitle.Text = "WISH LIST (" & dtTemp.Rows.Count & ")"
            GvAddWishList.Columns(1).Visible = False
            'GvAddWishList.Columns(2).Visible = False
            GvAddWishList.Columns(7).Visible = False

            'Dim count As String = returnvalue("select count(*) from AddWishList where AddWishList_BuyerUserId='" & Session("BuyId") & "'")


        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub
    'BtnContinue
    'Public Sub BtnContinue_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    '    Response.Redirect("~/Home.aspx")
    'End Sub


    ' select all checkbox
    Protected Sub chkSelectAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim chkHeader As New CheckBox
        chkHeader = CType(sender, CheckBox)
        Dim GvRow As GridViewRow
        If chkHeader.Checked = True Then
            For Each GvRow In GvAddWishList.Rows
                CType(GvRow.FindControl("ChkItem"), CheckBox).Checked = True
            Next
        Else
            For Each GvRow In GvAddWishList.Rows
                CType(GvRow.FindControl("ChkItem"), CheckBox).Checked = False
            Next
        End If
    End Sub


    'function to add the record on shop cart table
    Protected Function ShopCartProcedure(ByVal prodid As String) As Boolean
        Try
            Dim strr As String = "select Product_MarketPrice from product_information where Product_Approved='Y' and product_Kid='" & prodid & "'"
            Dim con As New SqlConnection
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
            cmd = New SqlCommand(strr, con)
            'con.Open()
            'dr = cmd.ExecuteReader
            Dim da1 As New SqlDataAdapter(cmd)
            Dim ds1 As New DataSet
            If Not da1.Fill(ds1) = Nothing Then


                Dim shopid As String = NewPrimaryKey(Me.Page)
                shopid = RemoveLiterals(shopid)
                Dim shopCode As String = Generate_Code("select count(*) from ShopCart where ShopCart_Code='", "SC")
                Dim con1 As New SqlConnection                        'Define connection variable for connecting to SQL-Server
                con1.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
                Dim cmd As New SqlCommand
                con1.Open()
                Dim sqlcol As Data.SqlClient.SqlParameterCollection = cmd.Parameters
                sqlcol.Add("@ShopCart_Kid", SqlDbType.NVarChar, 10).Value = shopid.Trim
                sqlcol.Add("@ShopCart_Code", SqlDbType.NVarChar, 25).Value = shopCode.Trim
                sqlcol.Add("@ShopCart_ProductId", SqlDbType.NVarChar, 10).Value = RemoveLiterals(prodid)
                sqlcol.Add("@ShopCart_BuyerUserId", SqlDbType.NVarChar, 25).Value = Session("BuyId")
                sqlcol.Add("@ShopCart_Quantity", SqlDbType.Int).Value = 1
                sqlcol.Add("@ShopCart_SpecialPrice", SqlDbType.Decimal, 18).Value = ds1.Tables(0).Rows(0).Item(0)
                sqlcol.Add("@ShopCart_FinancialYearId", SqlDbType.NVarChar, 10).Value = "2007"
                sqlcol.Add("@ShopCart_CompanyId", SqlDbType.NVarChar, 10).Value = Session("companyId")
                sqlcol.Add("@ShopCart_UserId", SqlDbType.NVarChar, 10).Value = Session("UserId")
                sqlcol.Add("@FormName", SqlDbType.NVarChar, 50).Value = Me.Page.ToString
                sqlcol.Add("@Mode", SqlDbType.VarChar, 10).Value = "Insert"
                ExecQuery("ShopCart_Proc", sqlcol).StartsWith("True")
                sqlcol = Nothing
                cmd = Nothing
                Return True
            End If
        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Function

    ' code for pagging of grid view
    Protected Sub GvAddWishList_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GvAddWishList.PageIndexChanging
        Try

            GvAddWishList.PageIndex = e.NewPageIndex
            viewwishlist()

        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub
    ' function for retrive value of any query.
    Function returnvalue(ByVal str As String) As String
        '   Dim query As String
        Dim cmd As New SqlCommand
        Dim sqlcon As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
        cmd.Connection = sqlcon
        cmd.CommandText = str
        Dim dr As SqlDataReader
        sqlcon.Open()
        dr = cmd.ExecuteReader
        If dr.Read Then
            Return dr(0)
        End If
    End Function
    'To check whether Select All option is checked or not
    Function checkstatus() As Boolean

        Dim gvRow As GridViewRow

        For Each gvRow In GvAddWishList.Rows
            Dim chkItem As CheckBox = CType(gvRow.FindControl("ChkItem"), CheckBox)

            If chkItem.Checked = True Then
                
            Else
                Return False
                Exit Function
            End If
        Next
        Return True


    End Function
    ' function to select any single checkbox is selected or not.
    Function singlecheckstatus() As Boolean

        Dim gvRow As GridViewRow

        For Each gvRow In GvAddWishList.Rows
            Dim chkItem As CheckBox = CType(gvRow.FindControl("ChkItem"), CheckBox)

            If chkItem.Checked = True Then
                Return True
                Exit Function
            End If
        Next
        Return False


    End Function

    ' code to delete the product from the wish list
    Protected Sub imgDelete_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        'dim imgdelete as  ImageButton=ctype(sender,"imgDelete")
        Try

            Dim imgdelete As ImageButton = CType(sender, ImageButton)
            ID = imgdelete.CommandArgument.ToString
            Dim qry As String = "delete from AddWishList where AddWishList_Kid='" & ID & "' and AddWishList_BuyerUserId='" & Session("BuyID") & "'"
            ExecuteQuery(qry)
            viewwishlist()

            Response.Redirect("WishList.aspx")

        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub

   
    Protected Sub cmdAddToCart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdAddToCart.Click
        Try

            lblselect.Visible = False
            Dim ans As Boolean = singlecheckstatus() ' function to check the any item selected or not.
            If ans = True Then
                Dim gvRow As GridViewRow

                For Each gvRow In GvAddWishList.Rows
                    Dim chkItem As CheckBox = CType(gvRow.FindControl("ChkItem"), CheckBox)

                    If chkItem.Checked = True Then
                        Dim prodid As String = gvRow.Cells(7).Text
                        ShopCartProcedure(prodid)
                    End If
                Next

                'GvAddWishList.Columns(5).Visible = False
                Response.Redirect("ShopCart.aspx")
            Else

                lblselect.Visible = True
                lblselect.Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/javascripts.gif") & "'/>&nbsp;Select At Least One Product !!"   ' information message
                Exit Sub

            End If
        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub

    Protected Sub cmdClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdClear.Click
        'Page.LoadControl("~/UserControl/buyeruserinfo.ascx").Dispose()
        Try
            lblselect.Visible = False
            Dim ans As Boolean = checkstatus()
            If ans = True Then
                Dim gvRow As GridViewRow
                For Each gvRow In GvAddWishList.Rows
                    Dim chkItem As CheckBox = CType(gvRow.FindControl("ChkItem"), CheckBox)
                    If chkItem.Checked = True Then
                        Dim qry As String = "delete from AddWishList where AddWishList_Kid='" & RemoveLiterals(gvRow.Cells(1).Text) & "' and AddWishList_BuyerUserId='" & Session("BuyID") & "'"
                        ExecuteQuery(qry)
                    End If
                Next
                viewwishlist()                      ' function to bind the grid view 
                Response.Redirect("wishList.aspx")  ' to refresh the quantity in buyeruserhome.ascx control.
            Else
                lblselect.Visible = True
                lblselect.Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/javascripts.gif") & "'/>&nbsp;Select All Product !!"
            End If
            'Page.EnableViewState = True
        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try

    End Sub

    Protected Sub GvAddWishList_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GvAddWishList.RowDataBound
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then
                CType(e.Row.Cells(0).FindControl("imgDelete"), ImageButton).Attributes.Add("onclick", "return confirm('Are you sure want to delete.?');")

                CType(e.Row.Cells(0).FindControl("ImageButton1"), Image).Attributes.Add("onmouseover", "ImageAspectRatio(this,'" & Image1.ClientID.ToString & "',400,300);")
                CType(e.Row.Cells(0).FindControl("ImageButton1"), Image).Attributes.Add("onmouseout", "enlargimgout(this,'" & Image1.ClientID.ToString & "');")
                Dim imgpath As String

                CType(e.Row.Cells(0).FindControl("ImageButton1"), Image).ImageUrl = ResolveUrl(CType(e.Row.Cells(0).FindControl("ImageButton1"), Image).ImageUrl)
                imgpath = Server.MapPath(CType(e.Row.Cells(0).FindControl("ImageButton1"), Image).ImageUrl)
                KeepAspectRatioNew(CType(e.Row.Cells(0).FindControl("ImageButton1"), Image), imgpath, 75, 75)
                'CType(e.Row.Cells(0).FindControl("ImageButton1"), Image).Attributes.Add("onmouseover", "enlargimg(this,'" & Image1.ClientID.ToString & "');")
                'CType(e.Row.Cells(0).FindControl("ImageButton1"), Image).Attributes.Add("onmouseout", "enlargimgout(this,'" & Image1.ClientID.ToString & "');")
                CType(e.Row.FindControl("ChkItem"), CheckBox).Attributes.Add("onclick", "return CheckOnOff(this);")

                Dim ID As String
                Dim lnk As LinkButton
                lnk = CType(e.Row.Cells(0).FindControl("lnkinquiry"), LinkButton)
                ID = lnk.CommandArgument
                CType(e.Row.Cells(0).FindControl("lblMarketPrice"), Label).Text = Producttotalprice(lnk.CommandArgument.ToString)
                CType(e.Row.Cells(0).FindControl("lblMarketPrice"), Label).ForeColor = Drawing.Color.Black
                CType(e.Row.Cells(0).FindControl("lnkinquiry"), LinkButton).Attributes.Add("onclick", "return openform('" & EncodeURL(ID) & "');")
                If CType(e.Row.Cells(0).FindControl("lblMarketPrice"), Label).Text = "" Or CType(e.Row.Cells(0).FindControl("lblMarketPrice"), Label).Text = "0.00" Or CType(e.Row.Cells(0).FindControl("lblMarketPrice"), Label).Text = "0" Then
                    CType(e.Row.Cells(0).FindControl("lblMarketPrice"), Label).Text = "N. A."
                    CType(e.Row.Cells(0).FindControl("lblMarketPrice"), Label).ForeColor = Drawing.Color.Red
                End If
            ElseIf e.Row.RowType = DataControlRowType.Header Then
                CType(e.Row.FindControl("chkSelectAll"), CheckBox).Attributes.Add("onclick", "return FindCheckBox(this);")
            End If



        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub

   
    Protected Sub btncontinue_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btncontinue.Click
        Response.Redirect("newBuyerSearch.aspx")
    End Sub

End Class


