Imports System
Imports System.Data
Imports System.Data.SqlClient

Partial Class Admin_SupplierUpdateHistory
    Inherits System.Web.UI.Page
    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 Supplier_Master.Supplier_Kid, Supplier_Master.Supplier_Name FROM Supplier_Master INNER JOIN SupplierUpdate_History ON Supplier_Master.Supplier_Kid = SupplierUpdate_History.SupplierUpdate_Id WHERE (Supplier_Master.Supplier_IsDeleted = '0') AND (Supplier_Master.Supplier_Approved = 'Y') ORDER BY Supplier_Master.Supplier_Name", ddlSupplierName)
            gvDetails.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

        If ddlSupplierName.SelectedItem.Text = "Select" Then
            'lblError.Visible = False
            lnkSName.Text = ""
            lblAddress.Text = ""
            lblContactPerson.Text = ""
            lblPhoneNo.Text = ""
            lblCity.Text = ""
            gvDetails.Visible = False
            Exit Sub
        End If
        fillSupplierInfo(ddlSupplierName.SelectedItem.Value)
        fillgridSupplierinfo()
        fillgriddeletedproduct()
    End Sub
    Private Sub fillSupplierInfo(ByVal supid As String)
        Try

            Dim dt As New DataTable
            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
                lblCity.Text = dt.Rows(0).Item("City").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

            End If

        Catch ex As Exception

        End Try
    End Sub
    Public Sub fillgridSupplierinfo()
        Try


            Dim dt As DataTable
            dt = ReturnDataTable("SELECT SupplierUpdate_History.SupplierUpdate_FieldName, case when SupplierUpdate_History.SupplierUpdate_FieldName = 'City' then (Select City_Name From City_Master WHERE City_KId = SupplierUpdate_History.SupplierUpdate_OldValue) else SupplierUpdate_History.SupplierUpdate_OldValue end as SupplierUpdate_OldValue,case when SupplierUpdate_History.SupplierUpdate_FieldName = 'City' then (Select City_Name From City_Master WHERE City_KId = SupplierUpdate_History.SupplierUpdate_NewValue) else SupplierUpdate_History.SupplierUpdate_NewValue end as SupplierUpdate_NewValue, SupplierUpdate_History.SupplierUpdate_UpdateDate, AdminUser.AdminUser_Name,SupplierUpdate_History.SupplierUpdate_UserIP FROM SupplierUpdate_History INNER JOIN AdminUser ON SupplierUpdate_History.SupplierUpdate_UpdatedBy = AdminUser.AdminUser_Kid WHERE (SupplierUpdate_History.SupplierUpdate_Id = '" & RemoveLiterals(ddlSupplierName.SelectedItem.Value) & "') order by SupplierUpdate_FieldName")

            If dt.Rows.Count > 0 Then
                gvDetails.DataSource = dt
                gvDetails.DataBind()

            Else
                gvDetails.DataSource = Nothing
                gvDetails.DataBind()
            End If
            gvDetails.Visible = True

        Catch ex As Exception

        End Try
    End Sub
    Public Sub fillgriddeletedproduct()
        Try
            Dim dt As DataTable
            dt = ReturnDataTable("SELECT Product_Master.Product_Name, Product_Master.Product_ModelNo, ProductUpdate_History.ProductUpdate_UpdateDate, ProductUpdate_History.ProductUpdate_RegDate FROM  Product_Master INNER JOIN ProductUpdate_History ON Product_Master.Product_Kid = ProductUpdate_History.ProductUpdate_Id WHERE (ProductUpdate_History.ProductUpdate_FieldName = N'Product Deleted') AND (ProductUpdate_History.ProductUpdate_SupplierId = '" & ddlSupplierName.SelectedItem.Value & "')")
            lblmsg.Text = "Supplier Deleted Products:" & dt.Rows.Count.ToString
            If dt.Rows.Count > 0 Then
                gvproduct.DataSource = dt
                gvproduct.DataBind()
                lblmsg.Visible = True
            Else
                gvproduct.DataSource = Nothing
                gvproduct.DataBind()
                lblmsg.Visible = False
            End If
            gvproduct.Visible = True

        Catch ex As Exception

        End Try
    End Sub
    Protected Sub gvDetails_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
        gvDetails.PageIndex = e.NewPageIndex
        If ddlSupplierName.SelectedIndex > 0 Then
            ' ''gvDetails.Visible = True
            fillgridSupplierinfo()
        Else
            ' ''gvDetails.Visible = False
        End If
    End Sub

    Protected Sub gvDetails_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDetails.RowDataBound
        Dim oldvalue As String
        Dim newvalue As String
        Dim dk As String
        Dim strFieldName As String
        Dim strOldPwd As String
        Dim strNewPwd As String
        Try

            If e.Row.RowType = DataControlRowType.DataRow Then

                strFieldName = e.Row.Cells(0).Text

                If strFieldName = "Password" Then
                    If e.Row.Cells(1).Text = "N.A." Then
                        strOldPwd = ""
                    Else
                        strOldPwd = Decrypt(e.Row.Cells(1).Text)
                    End If
                    If e.Row.Cells(2).Text = "N.A." Then
                        strNewPwd = ""
                    Else
                        strNewPwd = Decrypt(e.Row.Cells(2).Text)
                    End If
                   
                    e.Row.Cells(1).Text = strOldPwd
                    e.Row.Cells(2).Text = strNewPwd
                End If

                oldvalue = e.Row.Cells(1).Text
                newvalue = e.Row.Cells(2).Text
                Dim value1 As String = ReturnValue("select Unit_Name from Unit_Master where Unit_Kid='" & RemoveLiterals(oldvalue) & "' and unit_IsDeleted='0' and unit_CompanyId = '" & Session("g_Company").ToString() & "'")
                Dim value2 As String = ReturnValue("select Unit_Name from Unit_Master where Unit_Kid='" & RemoveLiterals(newvalue) & "' and unit_IsDeleted='0' and unit_CompanyId = '" & Session("g_Company").ToString() & "'")

                If value1 <> "" And value2 <> "" Then
                    e.Row.Cells(1).Text = value1
                    e.Row.Cells(2).Text = value2
                End If

                dk = e.Row.Cells(3).Text
                e.Row.Cells(3).Text = Format$(Convert.ToDateTime(dk), "dd-MMM-yyyy hh:mm:ss tt")

            End If
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub gvproduct_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvproduct.PageIndexChanging
        gvproduct.PageIndex = e.NewPageIndex
        If ddlSupplierName.SelectedIndex > 0 Then
            ' ''gvDetails.Visible = True
            fillgriddeletedproduct()
        Else
            ' ''gvDetails.Visible = False
        End If
    End Sub
End Class
