Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class UserControl_buyer_right_banner
    Inherits System.Web.UI.UserControl
    Dim query As String
    

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ScriptManager1.SetFocus(txtName)
        imgbtnlogin.ImageUrl = Page.ResolveUrl("~/Images/submit_btn.gif")
        If Request.QueryString("ses") = "expire" Then
            lblError.Visible = True
            lblError.Text = "Your Session has been expired!"
            Exit Sub
        End If
    End Sub

    Protected Sub imgbtnlogin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtnlogin.Click
        Dim flag As Boolean = False
        Dim adminid As String = ""
        Dim cmd As New SqlCommand
        lblError.Visible = False
        'query = "select * from AdminUser where AdminUser_Name=@uname and AdminUser_Password=@pwd"
        'query = "SELECT BuyerRegister_EmailId, BuyerRegister_AdminUserId FROM dbo.BuyerRegister WHERE (BuyerRegister_EmailId = N'pankaj@atco.com') AND (BuyerRegister_AdminUserId = N'p')"
        query = "select * from BuyerRegister where (BuyerRegister_EmailId=@uname) and (BuyerRegister_pwd=@pwd)"

        Dim uname As String
        uname = txtName.Text
        If uname.Contains("'") Or uname.Contains("--") Or uname.Contains(";") Or uname.Contains(" ") Or uname.Contains("*") Or uname.Contains("where") Or uname.Contains("=") Then
            lblError.Visible = True
            lblError.Text = "Invalid UserName or Password."
            Exit Sub
        End If

        Dim ds_log As DataSet
        ds_log = AdminUserLog(uname, "Buyer")

        If ds_log.Tables(0).Rows.Count <> 0 Then
            If (ds_log.Tables(0).Rows(0).Item(5).ToString = 0) Then
                lblError.Visible = True
                lblError.Text = "You are already logged in"
                Exit Sub
            End If
        End If
        

        cmd.Parameters.AddWithValue("@uname", Trim(txtName.Text.ToString()))
        cmd.Parameters.AddWithValue("@pwd", Encrypt(txtpwd.Text))
        Dim sqlcon As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
        cmd.Connection = sqlcon
        cmd.CommandText = query
        Dim dr As SqlDataReader
        sqlcon.Open()
        dr = cmd.ExecuteReader

        If dr.Read Then
            Dim key As String = RemoveLiterals(NewPrimaryKey(Me.Page))
            InsertAdminUserLog(key, txtName.Text, Request.UserHostAddress, 0, "Buyer", "AdminLogin", "Insert")
            Session("LogKid") = key
            Session("LogName") = txtName.Text
            '  Do While dr.Read
            'DBUserId is nothing but AdminId
            'Session("DBUserID") = dr(0)
            'Session("ComanyId") = dr(8)
            'Session("UserId") = dr(9)
            'Session("BuyId") = RetriveBuyerId(Session("DBUserId"))
            Session("UserId") = dr(0)
            Session("BuyId") = dr(0)
            Session("Byrusername") = dr(2).ToString
            flag = True
            Session("g_Company") = "00006Z4"
            Session("g_FinYear") = "000010N"
            ' Loop
        End If


        sqlcon.Close()
        System.Threading.Thread.Sleep(1000)
        If flag = True Then
            'Dim con1 As New SqlConnection
            'con1.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")

            'con1.Open()
            'Dim str As String = "delete  FROM ShopCart where ShopCart_BuyerUserId ='" & Session("BuyId") & "'"
            'Dim da As New SqlDataAdapter
            'cmd = New SqlCommand(str, con1)
            'cmd.ExecuteNonQuery()
            'con1.Close()
            'cmd.Dispose()
            'da.Dispose()
            'con1.Dispose()
            'If Session("Checkstatus") = "ShopCart" Then

            '    Dim shoptabel As New DataTable
            '    shoptabel = Session("ShopTabel")
            '    Dim shopi As Integer
            '    Dim shoplen As Integer = shoptabel.Rows.Count
            '    For shopi = 0 To shoplen - 1
            '        ShopCartProcedure(shoptabel.Rows(shopi).Item(0).ToString)
            '    Next
            '    Session("Checkstatus") = Nothing
            '    Response.Redirect("ShopCart.aspx")
            'Else
            If Session("Checkstatus") = "WishLsit" Then

                Dim Wishtabel As New DataTable
                Wishtabel = Session("WishTabel")
                Dim wishi As Integer
                Dim len As Integer = Wishtabel.Rows.Count
                For wishi = 0 To len - 1

                    WishListProcedure(Wishtabel.Rows(wishi).Item(0).ToString)
                Next
                'sqlcon.Close()
                Session("Checkstatus") = Nothing
                Response.Redirect("WishList.aspx")
            Else
                '    Response.Redirect("newBuyerSearch.aspx")
                Response.Redirect("~/Buyer/newBuyerSearch.aspx")
            End If

        Else
            lblError.Visible = True
            lblError.Text = "Invalid User name or Password."
        End If
    End Sub
    'Protected Function ShopCartProcedure(ByVal prodid As String)
    '    Try
    '        Dim strr As String = "select Product_AtcoPrice from product_Information where Product_Approved='Y' and Product_Kid='" & prodid & "'"

    '        'Dim strr As String = "select * from TempShopCart"
    '        Dim con As New SqlConnection
    '        con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")

    '        con.Open()
    '        'dr = cmd.ExecuteReader
    '        Dim da1 As New SqlDataAdapter(strr, con)
    '        Dim ds1 As New DataSet
    '        ' Dim i As Integer = 0
    '        da1.Fill(ds1)

    '        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.ToString.Trim
    '        sqlcol.Add("@ShopCart_Code", SqlDbType.NVarChar, 25).Value = shopCode.Trim
    '        sqlcol.Add("@ShopCart_ProductId", SqlDbType.NVarChar, 10).Value = prodid.Trim
    '        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).ToString
    '        sqlcol.Add("@ShopCart_FinancialYearId", SqlDbType.NVarChar, 10).Value = Session("g_FinYear")
    '        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

    '    Catch ex As Exception
    '        ex.Message.ToString()
    '    End Try
    'End Function

    Protected Function WishListProcedure(ByVal prodid As String)
        Try
            Dim wishlistid As String = NewPrimaryKey(Me.Page)
            wishlistid = RemoveLiterals(wishlistid)
            Dim wishlistCode As String = Generate_Code("select count(*) from AddWishList where AddWishList_Code='", "WL")
            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
            Dim cmd As New SqlCommand
            con.Open()
            Dim str As String = "select AddWishList_Code from AddWishList where AddWishList_ProductId='" & prodid & "'"
            Dim da As New SqlDataAdapter(str, con)
            Dim ds As New DataSet
            da.Fill(ds)
            If ds.Tables(0).Rows.Count > 0 Then
                Return False
            Else
                Dim sqlcol As Data.SqlClient.SqlParameterCollection = cmd.Parameters
                sqlcol.Add("@AddWishList_Kid", SqlDbType.NVarChar, 10).Value = wishlistid.Trim
                sqlcol.Add("@AddWishList_Code", SqlDbType.NVarChar, 25).Value = wishlistCode.Trim
                sqlcol.Add("@AddWishList_ProductId", SqlDbType.NVarChar, 10).Value = prodid.Trim
                sqlcol.Add("@AddWishList_BuyerUserId", SqlDbType.NVarChar, 25).Value = Session("BuyId")
                sqlcol.Add("@AddWishList_FinancialYearId", SqlDbType.NVarChar, 10).Value = Session("g_FinYear")
                sqlcol.Add("@AddWishList_CompanyId", SqlDbType.NVarChar, 10).Value = Session("g_Company")
                sqlcol.Add("@AddWishList_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("AddWishList_Proc", sqlcol).StartsWith("True")
                sqlcol = Nothing
                cmd = Nothing
                ' Dim gvRow As GridViewRow
                Return True
            End If
        Catch ex As Exception
            ex.Message.ToString()
        End Try
    End Function
End Class
