Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Decimal
Partial Class Admin_Login_Record
    Inherits System.Web.UI.Page

    Dim sel_val As String
    Dim ret_user As String
    Dim ds As DataSet
    Dim dt As DataTable
    Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Session("AdminID") Is Nothing Then
                Response.Redirect("AdminLogin.aspx?sesn=expire")
                Exit Sub
            End If

            If Not IsPostBack Then
                If Session("AdminID") = Nothing Then
                    Response.Redirect("AdminLogin.aspx")

                End If
                lblName.Text = ""
                Call Fill_Combo("SELECT  UserType_Kid, UserType_Name FROM UserType where UserType_CompanyId='" & Session("g_Company") & "' and  UserType_IsDeleted='0' and UserType_Name NOT IN('Alliance') order by UserType_Name", ddlusertype)
            End If

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkey")   'Display error Message
        End Try
    End Sub


    Protected Sub ddlusertype_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlusertype.SelectedIndexChanged
        lblName.Text = ""
        If ddlusertype.SelectedItem.Text = "Supplier" Then
            Call Fill_Combo("SELECT DISTINCT Supplier_Master.Supplier_AdminUserId, Supplier_Master.Supplier_Name FROM  SupplierLogin RIGHT OUTER JOIN  Supplier_Master ON SupplierLogin.AdminUser_Kid = Supplier_Master.Supplier_AdminUserId WHERE     SupplierLogin.AdminUser_IsDeleted = '0' order by Supplier_Name", ddlUserName)
        End If
        If ddlusertype.SelectedItem.Text = "Buyer" Then
            Call Fill_Combo("SELECT DISTINCT BuyerRegister_Kid, BuyerRegister_EmailId FROM BuyerRegister WHERE     (BuyerRegister_IsDeleted = '0') ORDER BY BuyerRegister_EmailId", ddlUserName)  'Fill dropdownlist 
        End If
        If ddlusertype.SelectedItem.Text = "Admin" Then
            '' display those Admin whose Status='Active'
            Call Fill_Combo("select AdminUser_Kid,AdminUser_Name from AdminUser where AdminUser_CompanyId='" & Session("g_Company").ToString & "' and AdminUser_IsDeleted='0' and AdminUser_UserTypeId ='" & ddlusertype.SelectedValue & "'AND AdminUser_ActiveFlag='Y' ORDER BY AdminUser_Name", ddlUserName)  'Fill dropdownlist
        End If
        ddlUserName.SelectedIndex = 0
        GridView1.Visible = False
      
    End Sub

    Protected Sub ddlUserName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlUserName.SelectedIndexChanged
        Try
            GridView1.Visible = True
            fillgrid(0)

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkey")            'Display Error Message
        End Try
    End Sub

   
    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
        GridView1.Visible = True
        GridView1.PageIndex = e.NewPageIndex

        fillgrid(e.NewPageIndex)
    End Sub

    Protected Sub fillgrid(ByVal k As Integer)
        Dim usertype As String = ""
        Try

            sel_val = ddlUserName.SelectedValue.ToString()

            If ddlusertype.SelectedItem.Text = "Admin" Then
                ret_user = ReturnValue("select AdminUser_Name from AdminUser where AdminUser_Kid='" & sel_val & "' and AdminUser_IsDeleted='0' ")
                usertype = "Admin"

            End If
            If ddlusertype.SelectedItem.Text = "Supplier" Then
                ret_user = ReturnValue("select AdminUser_Name from SupplierLogin where AdminUser_Kid='" & sel_val & "' and Supplier_IsDeleted='0' ")
                usertype = "Supplier"
            End If

            If ddlusertype.SelectedItem.Text = "Buyer" Then
                ret_user = ReturnValue("select BuyerRegister_EmailId from BuyerRegister where BuyerRegister_Kid='" & sel_val & "' and BuyerRegister_IsDeleted='0'")
                usertype = "Buyer"
            End If
            ds = UserLogReport(ret_user, usertype)
            GridView1.DataSource = ds
            GridView1.DataBind()
            GridView1.Columns(1).Visible = False
            '' dt = ReturnDataTable("SELECT AdminUserLog_UserName, (CONVERT(NVarChar(20), AdminUserLog_LoginDateTime, 106)),AdminUserLog_LoginDateTime, AdminUserLog_LogoutDateTime, AdminUserLog_ClientIP, AdminUserLog_Status,(CONVERT(nvarchar(10), ((DATEPART(hour, AdminUserLog_LogoutDateTime) * 60 + DATEPART(minute, AdminUserLog_LogoutDateTime)) - (DATEPART(hour, AdminUserLog_LoginDateTime) * 60 + DATEPART(minute, AdminUserLog_LoginDateTime))) / 60) + ':' + CONVERT(nvarchar(10),DATEDIFF(minute, AdminUserLog_LoginDateTime, AdminUserLog_LogoutDateTime) % 60) + ':' + CONVERT(nvarchar(10), DATEDIFF(second,AdminUserLog_LoginDateTime, AdminUserLog_LogoutDateTime) % 60)) FROM AdminUserLog WHERE     (AdminUserLog_UserName = '" & ret_user & "')ORDER BY AdminUserLog_LoginDateTime DESC")
           
            If ds.Tables(0).Rows.Count = 0 Then
                lblName.Text = ""
                Exit Sub
            End If
            lblName.Text = "User Name:  " & ds.Tables(0).Rows(0).Item("AdminUserLog_UserName")

            Dim i As Integer
            Dim j As Integer
            For i = 0 To ds.Tables(0).Rows.Count - 1

                j = (k * GridView1.PageSize) + (i + 1) - 1
                GridView1.Rows(i).Cells(0).Text = j + 1                 'for counter
                'GridView1.Rows(i).Cells(4).Text = ds.Tables(0).Rows(j).Item(5).ToString    'for TimeDuration
                If ds.Tables(0).Rows(j).Item("AdminUserLog_Status").ToString = "0" Then
                    GridView1.Rows(i).Cells(6).Text = "Logged In"
                Else
                    GridView1.Rows(i).Cells(6).Text = "Logged Out"
                End If
            Next

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "strkey")            'Display Error Message
        End Try
    End Sub

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then
                'e.Row.Cells(1).Text = Format$(Convert.ToDateTime(e.Row.Cells(1).Text.ToString), "dd-MMM-yyyy")
                If e.Row.Cells(2).Text.ToString = "" Then
                    e.Row.Cells(2).Text = ""
                Else
                    e.Row.Cells(2).Text = Format$(Convert.ToDateTime(e.Row.Cells(2).Text.ToString), "dd-MMM-yy hh:mm:ss tt")
                End If
                If e.Row.Cells(3).Text.ToString = "" Then
                    e.Row.Cells(3).Text = ""
                Else
                    e.Row.Cells(3).Text = Format$(Convert.ToDateTime(e.Row.Cells(3).Text.ToString), "dd-MMM-yy hh:mm:ss tt")
                End If
                Dim dateday As Long
                Dim datehr As Long
                Dim datemin As Long
                Dim datesec As Long
                dateday = DateDiff(DateInterval.Day, Convert.ToDateTime(e.Row.Cells(2).Text.ToString), Convert.ToDateTime(e.Row.Cells(3).Text.ToString))
                datehr = DateDiff(DateInterval.Hour, Convert.ToDateTime(e.Row.Cells(2).Text.ToString), Convert.ToDateTime(e.Row.Cells(3).Text.ToString))
                datemin = DateDiff(DateInterval.Minute, Convert.ToDateTime(e.Row.Cells(2).Text.ToString), Convert.ToDateTime(e.Row.Cells(3).Text.ToString))
                datesec = DateDiff(DateInterval.Second, Convert.ToDateTime(e.Row.Cells(2).Text.ToString), Convert.ToDateTime(e.Row.Cells(3).Text.ToString))
                e.Row.Cells(4).Text = datehr.ToString("00") & " : " & (datemin Mod 60).ToString("00") & " : " & ((datesec Mod 60) Mod 60).ToString("00")
               

            End If
            
        Catch ex As Exception

        End Try
      
    End Sub
End Class
