Imports System.Data
Imports System.Data.SqlClient

Partial Class Buyer_BuyerOrderTracking
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            Page.LoadControl("~/UserControl/buyeruserinfo.ascx")


            CType(CType(Me.Master.FindControl("Buyeruserinfo1"), UserControl).FindControl("Image4"), Image).Visible = True
            CType(CType(Me.Master.FindControl("Buyeruserinfo1"), UserControl).FindControl("LinkButton7"), LinkButton).Font.Bold = True
            CType(CType(Me.Master.FindControl("Buyeruserinfo1"), UserControl).FindControl("LinkButton7"), LinkButton).Font.Size = 10

            If Page.IsPostBack = False Then
                'Dim query As String = "select BuyerRegister_EmailId from BuyerRegister where BuyerRegister_KId='" & Session("BuyId") & "'"
                'Dim cmd As New SqlCommand
                '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

                lblMsg.Text = "Order details for " & ReturnValue("select BuyerRegister_Name from BuyerRegister where BuyerRegister_KId='" & Session("BuyId") & "'")
                loadorder()
                'End If
                'loadorder()
            End If

        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub

    Private Sub loadorder()
        Try

            'sql = "SELECT dbo.BillingHeader.BillingHeader_BillNo, dbo.BillDetails.BillDetails_BuyerID, dbo.BillDetails.BillDetails_ProdId, " & _
            '      "dbo.BillingHeader.BillingHeader_BillDate, dbo.Product_Master.Product_Name, dbo.BillDetails.BillDetails_Status " & _
            '      "FROM dbo.BillingHeader INNER JOIN " & _
            '       "dbo.BillDetails ON dbo.BillingHeader.BillingHeader_Kid = dbo.BillDetails.BillDetails_BillNo INNER JOIN " & _
            '      "dbo.Product_Master ON dbo.BillDetails.BillDetails_ProdId = dbo.Product_Master.Product_Kid " & _
            '      "where dbo.BillingHeader.BillingHeader_BuyerID='" & Session("BuyId") & "'"

            sql = "SELECT     BillingHeader_Kid, BillingHeader_BillNo, convert(nvarchar(25),BillingHeader_BillDate,100) as BillingHeader_BillDate  FROM BillingHeader WHERE     (BillingHeader_BuyerID ='" & Session("BuyId") & "') order by BillingHeader_BillDate desc"
            GvOrder.DataSource = getTable(sql, "OrderTracking")
            GvOrder.DataBind()
            GvOrder.AllowPaging = True
            If GvOrder.Rows.Count = 0 Then
                Label1.Visible = True
                lblMsg.Visible = False
                Label1.Text = "No order history for you"
                GvOrder.Visible = False
            Else
                Label1.Visible = False
            End If
        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        Finally
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
        End Try
    End Sub

    Protected Sub GvOrder_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GvOrder.PageIndexChanged

    End Sub

    Protected Sub GvOrder_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GvOrder.PageIndexChanging
        '       Dim i As Integer = GvOrder.PageCount
        '        If GvOrder.PageIndex < i Then


        '      Else
        '           gvo()
        Try

        
            GvOrder.PageIndex = e.NewPageIndex
            'sql = "SELECT dbo.BillingHeader.BillingHeader_BillNo, dbo.BillDetails.BillDetails_BuyerID, dbo.BillDetails.BillDetails_ProdId, " & _
            '                "dbo.BillingHeader.BillingHeader_BillDate, dbo.Product_Master.Product_Name, dbo.BillDetails.BillDetails_Status " & _
            '                "FROM dbo.BillingHeader INNER JOIN " & _
            '                 "dbo.BillDetails ON dbo.BillingHeader.BillingHeader_Kid = dbo.BillDetails.BillDetails_BillNo INNER JOIN " & _
            '                "dbo.Product_Master ON dbo.BillDetails.BillDetails_ProdId = dbo.Product_Master.Product_Kid " & _
            '                "where dbo.BillingHeader.BillingHeader_BuyerID='" & Session("BuyId") & "'"

            sql = "SELECT     BillingHeader_Kid, BillingHeader_BillNo, convert(nvarchar(25),BillingHeader_BillDate,100) as BillingHeader_BillDate     FROM BillingHeader WHERE     (BillingHeader_BuyerID ='" & Session("BuyId") & "') order by BillingHeader_BillDate"
            GvOrder.DataSource = getTable(sql, "OrderTracking")
            GvOrder.DataBind()


        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
            ' CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try

    End Sub

    'Public Sub dispatchdetails(ByVal sender As Object, ByVal e As System.EventArgs)
    '    Dim i As New LinkButton()
    '    i = CType(sender, LinkButton)
    '    Dim bno As String = i.CommandArgument
    '    loaddispatch(bno)
    'End Sub

    'Private Sub loaddispatch(ByVal billno As String)
    '    Try
    '        sql = "select BillNo,CourierDate,CourierName,CourierNo from DispatchDetails where BillNo='" & billno & "'"
    '        cmd = New SqlCommand(sql, con)
    '        If con.State = ConnectionState.Open Then
    '            con.Close()
    '        End If
    '        con.Open()
    '        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    '        dgdispatch.DataSource = dr
    '        dgdispatch.DataBind()
    '        dr.Close()
    '    Catch ex As Exception
    '        Response.Write(ex.Message.ToString())
    '    Finally
    '        If con.State = ConnectionState.Open Then
    '            con.Close()
    '        End If
    '    End Try
    'End Sub


   
    'Protected Sub GvOrder_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GvOrder.SelectedIndexChanged
    '    Try

    '        Dim billid As String = GvOrder.SelectedValue
    '        fillorderdetail(billid)
    '        If gvOrderDetail.Rows.Count > 0 Then
    '            lblorder.Visible = True
    '            lblorder.Text = "Order details for the Purchase Order No:" & GvOrder.SelectedRow.Cells(1).Text & "<br/><br/>" & "Purchase Order Date: " & GvOrder.SelectedRow.Cells(2).Text & ""
    '        Else
    '            lblorder.Visible = False
    '        End If
    '    Catch ex As Exception
    '        CreateMessageAlert(Me, ex.Message, "strkeyval")
    '    End Try
    'End Sub
    Protected Sub fillorderdetail(ByVal billid As String)
        Try

            Dim con As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))
            Dim strqry As String = "SELECT DISTINCT     BillDetails.BillDetails_ProdQty, BillDetails.BillDetails_ProdAmt, BillDetails.BillDetails_BuyerID, BillDetails.BillDetails_ProdId,Product_Master.Product_Name, BillDetails.BillDetails_BillNo, AddressBook.AddressBook_Add1, BillDetails.BillDetails_Status FROM   BillDetails INNER JOIN      Product_Master ON BillDetails.BillDetails_ProdId = Product_Master.Product_Kid INNER JOIN    BillingAddress ON BillDetails.BillDetails_BillNo = BillingAddress.BillingAddress_BillNo INNER JOIN     AddressBook ON BillDetails.BillDetails_ShipAddID = AddressBook.AddressBook_Kid WHERE     (BillDetails.BillDetails_BillNo ='" & billid & "') AND (AddressBook.AddressBook_BuyerID = '" & Session("BuyId") & "') order by Product_Master.Product_Name"
            Dim sda As New SqlDataAdapter(strqry, con)
            Dim ds As New DataSet()
            sda.Fill(ds)
            gvOrderDetail.DataSource = ds

            gvOrderDetail.DataBind()
        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
        End Try
    End Sub

    Protected Sub lnkProductcode_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try

            Dim lnk As LinkButton
            lnk = CType(sender, LinkButton)

            Dim billid As String = lnk.CommandArgument()
            fillorderdetail(billid)

            Dim mypurchaseno As String
            mypurchaseno = ReturnValue("select BillingHeader_BillNo from BillingHeader where BillingHeader_Kid='" & billid & "'")
            Dim mybilldate As String
            mybilldate = ReturnValue("select convert(nvarchar(25),BillingHeader_BillDate,100) as BillingHeader_BillDate from BillingHeader where BillingHeader_Kid='" & billid & "'")

            If gvOrderDetail.Rows.Count > 0 Then
                lblorder.Visible = True
                lblorder.Text = "Order details for the Purchase Order No:  " & mypurchaseno.ToString & "<br/><br/>" & "Purchase Order Date:    " & mybilldate.ToString
            Else
                lblorder.Visible = False
            End If
        Catch ex As Exception
            showmessage(ex.Message.ToString, lblerror)
            ' CreateMessageAlert(Me, ex.Message, "strkeyval")
        End Try
    End Sub
End Class
