﻿Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class Admin_ProductDisplay
    Inherits System.Web.UI.Page

#Region "Events"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Page.IsPostBack = False Then
                Fill_ComboWithSelect("SELECT     Supplier_Kid, Supplier_Name  FROM Supplier_Master WHERE     (Supplier_IsDeleted = '0') ORDER BY Supplier_Name", ddlSupplier, "Select Supplier")
            End If

        Catch ex As Exception

        End Try
    End Sub

    Protected Sub ddlStatus_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlStatus.SelectedIndexChanged
        Try
            If ddlSupplier.SelectedValue.ToString <> "Select" Then
                gvProducts.DataSource = Nothing
                gvProducts.DataBind()
                If ddlStatus.SelectedIndex > 0 Then
                    Session("SupplierID") = ddlSupplier.SelectedValue.ToString
                    If ddlStatus.SelectedIndex = 1 Then
                        FillGridPartiallyFilled(ddlSupplier.SelectedValue.ToString)
                    ElseIf ddlStatus.SelectedIndex = 2 Then
                        FillGridFilledTotalProduct(ddlSupplier.SelectedValue.ToString)
                    End If
                Else
                    lblMsg.Text = "Please Select Status..."
                End If
            Else
                lblMsg.Text = "Please Select Supplier First..."
            End If
            
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub gvProducts_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvProducts.SelectedIndexChanged
        Try
            Dim strProductID As String
            strProductID = gvProducts.SelectedDataKey.Value
            strProductID = strProductID.Replace("\", "\\")
            Dim script As String
            script = "window.open(""{0}"", ""{1}"", ""{2}"");"
            script = String.Format(script, "../suppliers/addnewproduct.aspx?Product=" & RemoveLiterals(strProductID), "_blank", "toolbar=0,location=0,scrollbars=1,status=0,menubar=0,resizable=0,width=' + parseInt(screen.width) + ',height=' + parseInt(screen.height) + ';'")
            ScriptManager.RegisterStartupScript(Page, GetType(Page), "Redirect", script, True)
        Catch ex As Exception

        End Try
    End Sub

#End Region

#Region "Methods"

    Protected Sub FillGridPartiallyFilled(ByVal suppId As String)
        Try
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            Dim ds As New DataSet
            Dim da As SqlDataAdapter
            da = New SqlDataAdapter("Product_NonApproved_Proc", con)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.Add(New SqlParameter("@SupplierId", SqlDbType.NVarChar))
            da.SelectCommand.Parameters("@SupplierId").Value = RemoveLiterals(suppId)
            da.SelectCommand.Parameters.Add(New SqlParameter("@Mode", SqlDbType.NVarChar))
            da.SelectCommand.Parameters("@Mode").Value = "Partially"
            da.Fill(ds)
            spCount.InnerHtml = "Total " + "<b>" + ds.Tables(0).Rows.Count.ToString + "</b>" + " Records Found."
            gvProducts.DataSource = ds
            gvProducts.DataBind()

        Catch ex As Exception
            Response.Write(ex.Message.ToString)
        End Try
    End Sub

    Protected Sub FillGridFilledTotalProduct(ByVal suppId As String)
        Try
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
            Dim ds As New DataSet
            Dim da As SqlDataAdapter
            da = New SqlDataAdapter("Product_NonApproved_Proc", con)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.Add(New SqlParameter("@SupplierId", SqlDbType.NVarChar))
            da.SelectCommand.Parameters("@SupplierId").Value = RemoveLiterals(suppId)
            da.SelectCommand.Parameters.Add(New SqlParameter("@Mode", SqlDbType.NVarChar))
            da.SelectCommand.Parameters("@Mode").Value = "Filled"
            da.Fill(ds)

            spCount.InnerHtml = "Total " + "<b>" + ds.Tables(0).Rows.Count.ToString + "</b>" + " Records Found."
            gvProducts.DataSource = ds
            gvProducts.DataBind()

        Catch ex As Exception
            Response.Write(ex.Message.ToString)
        End Try
    End Sub

#End Region

End Class
