Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class supplierwiseproductinfo
    Inherits System.Web.UI.Page

#Region "Events"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("AdminID") Is Nothing Then
            Response.Redirect("AdminLogin.aspx?sesn=expire")
            Exit Sub
        End If

        If Not IsPostBack Then
            Call Fill_Combo("SELECT DISTINCT  dbo.Supplier_Master.Supplier_Kid, dbo.Supplier_Master.Supplier_Name FROM dbo.Supplier_Master WHERE dbo.Supplier_Master.Supplier_IsDeleted = '0' AND dbo.Supplier_Master.Supplier_Approved = 'Y' ORDER BY dbo.Supplier_Master.Supplier_Name", ddlSupplierName)
            imgproduct.Visible = False
            ddlproduct.Visible = False
            lbldescription.Visible = False
            lblproduct.Visible = False
        End If
    End Sub

    Protected Sub ddlSupplierName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSupplierName.SelectedIndexChanged

        fillSupplierInfo()
        fillproducts()
        DetailsView1.Visible = False
        imgproduct.Visible = False
    End Sub

    Protected Sub ddlproduct_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If ddlproduct.SelectedIndex > 0 Then
            fillgridproductinfo()
        Else
            DetailsView1.Visible = False
            imgproduct.Visible = False
            lbldesc.Visible = False
            lbldescription.Visible = False
        End If

    End Sub

    Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.DataBound
        Try


            imgproduct.ImageUrl = "../Upload Image/Product Image/UploadImages/" + DetailsView1.Rows.Item(3).Cells(1).Text.ToString
            lbldesc.Text = DetailsView1.Rows.Item(10).Cells(1).Text.ToString

            Dim i As Integer
            For i = 0 To DetailsView1.Rows.Count - 1
                If (DetailsView1.Rows.Item(i).Cells(1).Text.ToString) = "&nbsp;" Or (DetailsView1.Rows.Item(i).Cells(1).Text.ToString) = "0" Or (DetailsView1.Rows.Item(i).Cells(1).Text.ToString) = "0.0" Or (DetailsView1.Rows.Item(i).Cells(1).Text.ToString) = "0X0X0" Or (DetailsView1.Rows.Item(i).Cells(1).Text.ToString) = "" Or (DetailsView1.Rows.Item(i).Cells(1).Text.ToString) = "N" Then
                    DetailsView1.Rows.Item(i).Cells(1).Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/close_button1.jpg") & "'/>"
                Else
                    DetailsView1.Rows.Item(i).Cells(1).Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/ok_1.jpg") & "'/>"
                End If
            Next

            DetailsView1.Rows.Item(0).Visible = False       'product Kid
            DetailsView1.Rows.Item(1).Visible = False       'product name
            DetailsView1.Rows.Item(3).Visible = False       'product image
            DetailsView1.Rows.Item(10).Visible = False      'product description
            DetailsView1.Rows.Item(21).Visible = False      'Supplier ID
            DetailsView1.Rows.Item(22).Visible = False      'Brand ID
        Catch ex As Exception

        End Try
    End Sub

#End Region

#Region "Methods"

    Private Sub fillSupplierInfo()
        Try
            lbldesc.Text = ""
            lbldescription.Visible = False
            Dim dt As New DataTable
            Dim supid As String = ddlSupplierName.SelectedItem.Value
            dt = ReturnDataTable("SELECT dbo.Supplier_Master.Supplier_Kid, dbo.Supplier_Master.Supplier_Name, dbo.Supplier_Master.Supplier_Address,dbo.Supplier_Master.Supplier_TelCountryCode, dbo.Supplier_Master.Supplier_TelCityCode, dbo.Supplier_Master.Supplier_TelNumber,dbo.SupplierContactPerson_Master.SupplierContactPerson_Name, dbo.SupplierContactPerson_Master.SupplierContactPerson_Designation,dbo.SupplierContactPerson_Master.SupplierContactPerson_Title FROM dbo.Supplier_Master Left Outer JOIN dbo.SupplierContactPerson_Master ON dbo.Supplier_Master.Supplier_Kid = dbo.SupplierContactPerson_Master.SupplierContactPerson_SupplierId WHERE (dbo.Supplier_Master.Supplier_IsDeleted = '0') AND (dbo.Supplier_Master.Supplier_Kid = '" & RemoveLiterals(supid) & "')")

            ''dt = ReturnDataTable("SELECT dbo.Supplier_Master.Supplier_Kid, dbo.Supplier_Master.Supplier_Name, dbo.Supplier_Master.Supplier_Address,dbo.Supplier_Master.Supplier_TelCountryCode, dbo.Supplier_Master.Supplier_TelCityCode, dbo.Supplier_Master.Supplier_TelNumber,dbo.SupplierContactPerson_Master.SupplierContactPerson_Name, dbo.SupplierContactPerson_Master.SupplierContactPerson_Designation,dbo.SupplierContactPerson_Master.SupplierContactPerson_Title, City_Master.City_Name + ' - ' + Supplier_Master.Supplier_PinCode + '- (' + State_Master.State_Name + ') - ' + Country_Master.Country_Name AS City  FROM         Supplier_Master INNER JOIN  City_Master ON Supplier_Master.Supplier_CityId = City_Master.City_Kid INNER JOIN  State_Master ON City_Master.City_StateId = State_Master.State_Kid INNER JOIN Country_Master ON State_Master.State_CountryId = Country_Master.Country_Kid LEFT OUTER JOIN  SupplierContactPerson_Master ON Supplier_Master.Supplier_Kid = SupplierContactPerson_Master.SupplierContactPerson_SupplierId  WHERE (dbo.Supplier_Master.Supplier_IsDeleted = '0') AND (dbo.Supplier_Master.Supplier_Kid = '" & RemoveLiterals(supid) & "')")
            If dt.Rows.Count > 0 Then
                lnkSName.Text = IIf(dt.Rows(0).Item("Supplier_Name") <> "", dt.Rows(0).Item("Supplier_Name"), "N.A")
                lblAddress.Text = IIf(dt.Rows(0).Item("Supplier_Address") <> "", dt.Rows(0).Item("Supplier_Address"), "N.A.")
                lblContactPerson.Text = dt.Rows(0).Item("SupplierContactPerson_Title").ToString + dt.Rows(0).Item("SupplierContactPerson_Name").ToString + "," + dt.Rows(0).Item("SupplierContactPerson_Designation").ToString
                If lblContactPerson.Text = "," Then
                    lblContactPerson.Text = ""
                End If
                If dt.Rows(0).Item("Supplier_TelNumber").ToString = Nothing Then
                    lblPhoneNo.Text = ""
                Else
                    If dt.Rows(0).Item("Supplier_TelCountryCode").ToString = Nothing AndAlso dt.Rows(0).Item("Supplier_TelCityCode") = Nothing Then
                        lblPhoneNo.Text = dt.Rows(0).Item("Supplier_TelNumber")
                    ElseIf dt.Rows(0).Item("Supplier_TelCountryCode").ToString = Nothing Then
                        lblPhoneNo.Text = dt.Rows(0).Item("Supplier_TelCityCode") + "-" + dt.Rows(0).Item("Supplier_TelNumber")

                    Else
                        lblPhoneNo.Text = dt.Rows(0).Item("Supplier_TelCountryCode") + "-" + dt.Rows(0).Item("Supplier_TelCityCode") + "-" + dt.Rows(0).Item("Supplier_TelNumber")
                    End If
                End If
            Else
                lnkSName.Text = ""
                lblAddress.Text = ""
                lblContactPerson.Text = ""
                lblPhoneNo.Text = ""
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub fillproducts()
        Try


            Dim dt As New DataTable
            dt = ReturnDataTable("Select Product_Kid,Product_Name + '  -  ' + ISNULL(Product_Code,'N.A') AS Product_Name FROM Product_Master WHERE Product_Master.Product_SupplierId ='" & RemoveLiterals(ddlSupplierName.SelectedItem.Value) & "' and Product_Master.Product_IsDeleted='0' ORDER BY Product_Name,Product_Code,Product_RegDate DESC")
            If dt.Rows.Count = 0 Then
                lbldesc.Visible = False
                lbldescription.Visible = False
                ddlproduct.Visible = False
                lblproduct.Visible = False
                DetailsView1.Visible = False
                imgproduct.Visible = False
            Else
                lbldesc.Text = ""
                lbldesc.Visible = True
                lbldescription.Visible = True
                ddlproduct.Visible = True
                lblproduct.Visible = True
            End If

            ddlproduct.DataSource = dt
            ddlproduct.DataTextField = "Product_Name"
            ddlproduct.DataValueField = "Product_Kid"
            ddlproduct.DataBind()
            ddlproduct.Items.Insert(0, "Select Product")
        Catch ex As Exception

        End Try
    End Sub

    Public Sub fillgridproductinfo()
        Try


            Dim ds As DataSet
            ds = SupplierProductInformation(ddlSupplierName.SelectedItem.Value, ddlproduct.SelectedItem.Value)
            DetailsView1.Visible = True
            imgproduct.Visible = True
            lbldesc.Visible = True
            lbldescription.Visible = True

            DetailsView1.DataSource = ds
            DetailsView1.DataBind()
        Catch ex As Exception

        End Try
    End Sub

    Public Function SupplierProductInformation(ByVal suppid As String, ByVal prodid As String) As DataSet
        Dim cn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
        Dim objAdapter As SqlDataAdapter
        Dim ObjDS As DataSet

        cn.Open()
        Try
            objAdapter = New SqlDataAdapter("SupplierwiseProductInfo_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@Product_SupplierId", SqlDbType.NVarChar, 50))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@Product_KId", SqlDbType.NVarChar, 10))

            objAdapter.SelectCommand.Parameters("@Product_SupplierId").Value = suppid
            objAdapter.SelectCommand.Parameters("@Product_KId").Value = prodid
            ObjDS = New DataSet
            objAdapter.Fill(ObjDS)
            Return ObjDS

        Catch ex As Exception
            Throw ex
        Finally
            cn.Close()
            cn.Dispose()
        End Try
    End Function

#End Region

End Class
