Imports System.Data
Imports System.Data.SqlClient


Partial Class Buyer_ShopCart
    Inherits System.Web.UI.Page
    Public Shared amount As Decimal = 0.0
    Shared id As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            lblselect.Visible = False
            Page.LoadControl("~/UserControl/buyeruserinfo.ascx") ' Load Buyeruserinfo control to display how many item available in shopcart 

            CType(CType(Me.Master.FindControl("Buyeruserinfo1"), UserControl).FindControl("Image2"), Image).Visible = True
            CType(CType(Me.Master.FindControl("Buyeruserinfo1"), UserControl).FindControl("LinkButton2"), LinkButton).Font.Bold = True
            CType(CType(Me.Master.FindControl("Buyeruserinfo1"), UserControl).FindControl("LinkButton2"), LinkButton).Font.Size = 10

            amount = 0.0
            If Page.IsPostBack = False Then
                trbutton.Visible = False
                trgrid.Visible = False
                tramount.Visible = False
                viewcart()                      ' Function to fill Shop Cart Gridview
            End If

        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub

    Protected Sub viewcart() ' Function to fill Shop Cart Gridview
        Try
            lblselect.Visible = False
            GvshopCart.Columns(1).Visible = True      ' Hide The Shopcart ID
            'GvshopCart.Columns(2).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 dbo.Product_Master.Product_Name,Product_Master.Product_Image,Product_Code,Product_ModelNo,dbo.Product_Master.Product_AtcoPrice,Product_SpecialPrice,dbo.ShopCart.ShopCart_Kid, dbo.ShopCart.ShopCart_Quantity, " & _
                          "dbo.Product_Master.Product_AtcoPrice * dbo.ShopCart.ShopCart_Quantity AS SubTotal, dbo.ShopCart.ShopCart_BuyerUserId,ShopCart_NetAmount, " & _
                          "dbo.Product_Master.Product_Kid " & _
                          "FROM dbo.Product_Master INNER JOIN " & _
                           "dbo.ShopCart ON dbo.Product_Master.Product_Kid = dbo.ShopCart.ShopCart_ProductId " & _
                           "and dbo.ShopCart.ShopCart_BuyerUserId='" & Session("BuyID") & "'"
            dtTemp = ReturnDataTable(strQuery)
            For Each row In dtTemp.Rows                         ' Loop to Count Net Amount
                amount = amount + row("ShopCart_NetAmount")
            Next
            GvshopCart.DataSource = dtTemp
            GvshopCart.DataBind()
            GvshopCart.Columns(1).Visible = False
            trgrid.Visible = True
            If dtTemp.Rows.Count > 0 Then

                lblother.Visible = True
                lblother.Text = "Excise duty,vat,cst and other Charges are applied extra."
                trbutton.Visible = True
                trgrid.Visible = True
                tramount.Visible = True
            End If

        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
        ' GvshopCart.Columns(2).Visible = False

    End Sub


    ' function to check the any single checkbox is selected or not.
    Function singlecheckstatus() As Boolean

        Dim gvRow As GridViewRow

        For Each gvRow In GvshopCart.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

    'To Select All The Checbox 
    'Protected Sub chkSelectAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    '    lblselect.Visible = False
    '    Dim chkHeader As New CheckBox
    '    chkHeader = CType(sender, CheckBox)
    '    Dim GvRow As GridViewRow
    '    If chkHeader.Checked = True Then
    '        For Each GvRow In GvShopCart.Rows
    '            CType(GvRow.FindControl("ChkItem"), CheckBox).Checked = True
    '        Next
    '    Else
    '        For Each GvRow In GvShopCart.Rows
    '            CType(GvRow.FindControl("ChkItem"), CheckBox).Checked = False
    '        Next
    '    End If
    'End Sub
    'To check whether Select All option is checked or not

    Function checkstatus() As Boolean

        Dim gvRow As GridViewRow

        For Each gvRow In GvshopCart.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

    ' Pagging for grid view
    Protected Sub GvshopCart_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GvshopCart.PageIndexChanging
        Try

            GvshopCart.PageIndex = e.NewPageIndex
            viewcart()

        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub

    ' To delete the item from shop Cart. 
 
    Protected Sub imgDelete_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Try

            'dim imgdelete as  ImageButton=ctype(sender,"imgDelete")
            Dim imgdelete As ImageButton = CType(sender, ImageButton)
            id = imgdelete.CommandArgument.ToString
            Dim qry As String = "delete from ShopCart where ShopCart_Kid='" & RemoveLiterals(id) & "' and ShopCart_BuyerUserId='" & Session("BuyID") & "'"
            ExecuteQuery(qry)
            ' viewcart()
            Response.Redirect("ShopCart.aspx")

        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub
    'Protected Sub imgUpdate_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
    '    'dim imgdelete as  ImageButton=ctype(sender,"imgDelete")
    '    Dim imgdelete As ImageButton = CType(sender, ImageButton)
    '    Dim prodid As String = imgdelete.CommandArgument.ToString

    '    Dim gvRow As GridViewRow
    '    For Each gvRow In GvshopCart.Rows
    '        If prodid = gvRow.Cells(1).Text Then
    '            Dim txtQty As TextBox = CType(gvRow.FindControl("txtQuantity"), TextBox)
    '            Dim qty As Integer = CInt(txtQty.Text)
    '            Dim qry As String = "update ShopCart set ShopCart_Quantity=" & txtQty.Text & " where ShopCart_Kid='" & gvRow.Cells(1).Text & "'"
    '            ExecuteQuery(qry)
    '            Exit Sub
    '        End If
    '    Next
    '    Response.Redirect("ShopCart.aspx")
    'End Sub

  
    Protected Sub cmdContinueShop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdContinueShop.Click
        Response.Redirect("~/Buyer/newBuyerSearch.aspx")
    End Sub

    Protected Sub cmdClearCart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdClearCart.Click
        Try

            lblselect.Visible = False
            Dim ans As Boolean = checkstatus() ' check the status where all items are selected or not.
            If ans = True Then

                Dim gvRow As GridViewRow
                For Each gvRow In GvshopCart.Rows
                    Dim chkItem As CheckBox = CType(gvRow.FindControl("ChkItem"), CheckBox)
                    If chkItem.Checked = True Then
                        Dim qry As String = "delete from ShopCart where ShopCart_Kid='" & RemoveLiterals(gvRow.Cells(1).Text) & "' and ShopCart_BuyerUserId='" & Session("BuyID") & "'"
                        ExecuteQuery(qry)
                    End If
                Next
                viewcart() ' function to bind grid view for the shop cart.
                Response.Redirect("ShopCart.aspx")
            Else
                lblselect.Visible = True
                lblselect.Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/javascripts.gif") & "'/>&nbsp;Select All Product !!"
            End If
        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub

    Protected Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
        Try

            lblselect.Visible = False
            Dim ans As Boolean = singlecheckstatus()
            If ans = True Then

                'To check which items are selected for update
                Dim gvRow As GridViewRow
                For Each gvRow In GvshopCart.Rows
                    Dim chkItem As CheckBox = CType(gvRow.FindControl("ChkItem"), CheckBox) 'Find Checkbox in Grid View
                    If chkItem.Checked = True Then
                        Dim txtQty As TextBox = CType(gvRow.FindControl("txtQuantity"), TextBox)   ' find item quantity in text box of grid view
                        Dim qty As Integer = CInt(txtQty.Text)
                        Dim lblAmount As Label = CType(gvRow.FindControl("lblMarketPrice"), Label) ' find label for item market price. 
                        Dim amount As Integer = CInt(lblAmount.Text) * CInt(qty)
                        Dim qry As String = "update ShopCart set ShopCart_Quantity=" & txtQty.Text & " where ShopCart_Kid='" & RemoveLiterals(gvRow.Cells(1).Text) & "' and ShopCart_BuyerUserId='" & Session("BuyID") & "'"
                        ExecuteQuery(qry)

                    End If
                Next
                viewcart() ' Function to fill Shop Cart Gridview
            Else
                lblselect.Visible = True
                lblselect.Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/javascripts.gif") & "'/>&nbsp;Select At Least One Product !!"   ' Display Information Message
            End If

        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub

    Protected Sub cmdCheckOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCheckOut.Click
        Response.Redirect("../Buyer/BillDetails.aspx")
    End Sub

    Protected Sub GvshopCart_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GvshopCart.RowDataBound
        Try

            If e.Row.RowType = DataControlRowType.DataRow Then
                CType(e.Row.FindControl("ChkItem"), CheckBox).Attributes.Add("onclick", "return CheckOnOff(this);")
                CType(e.Row.FindControl("txtQuantity"), TextBox).Attributes.Add("onkeypress", "return CheckNumericValue(event)")
            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

    
End Class
