Imports System.Windows.Forms
Imports System.Data.SqlClient
Public Class frm_login
    Dim conn As New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("SO_Conn"))
    Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs)
        ' Create a new instance of the child form.
        Dim ChildForm As New System.Windows.Forms.Form
        ' Make it a child of this MDI form before showing it.
        ChildForm.MdiParent = Me
        m_ChildFormNumber += 1
        ChildForm.Text = "Window " & m_ChildFormNumber
        ChildForm.Show()
    End Sub
    Private Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs)
        Dim OpenFileDialog As New OpenFileDialog
        OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        OpenFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
            Dim FileName As String = OpenFileDialog.FileName
            ' TODO: Add code here to open the file.
        End If
    End Sub
    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim SaveFileDialog As New SaveFileDialog
        SaveFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        SaveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        If (SaveFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
            Dim FileName As String = SaveFileDialog.FileName
            ' TODO: Add code here to save the current contents of the form to a file.
        End If
    End Sub
    Private Sub ExitToolsStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.Close()
    End Sub
    Private Sub CutToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' Use My.Computer.Clipboard to insert the selected text or images into the clipboard
    End Sub
    Private Sub CopyToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' Use My.Computer.Clipboard to insert the selected text or images into the clipboard
    End Sub
    Private Sub PasteToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        'Use My.Computer.Clipboard.GetText() or My.Computer.Clipboard.GetData to retrieve information from the clipboard.
    End Sub
    Private Sub CascadeToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.LayoutMdi(MdiLayout.Cascade)
    End Sub
    Private Sub TileVerticleToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.LayoutMdi(MdiLayout.TileVertical)
    End Sub
    Private Sub TileHorizontalToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.LayoutMdi(MdiLayout.TileHorizontal)
    End Sub
    Private Sub ArrangeIconsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.LayoutMdi(MdiLayout.ArrangeIcons)
    End Sub
    Private Sub CloseAllToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' Close all child forms of the parent.
        For Each ChildForm As Form In Me.MdiChildren
            ChildForm.Close()
        Next
    End Sub
    Private m_ChildFormNumber As Integer

    Private Sub UsersToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UsersToolStripMenuItem.Click
        Dim childForm As Integer = 41
        Dim childForms(41) As frm_user
        childForms(childForm) = Service_Order_R2R.frm_user
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub

    Private Sub DataEntryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataEntryToolStripMenuItem.Click
        Dim childForm As Integer = 31
        Dim childForms(31) As frm_dataentry
        childForms(childForm) = Service_Order_R2R.frm_dataentry
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        childForms(childForm).WindowState = FormWindowState.Maximized
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub

    Private Sub VerificationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VerificationToolStripMenuItem.Click
        Dim childForm As Integer = 32
        Dim childForms(32) As frm_QAnVerify
       
        childForms(childForm) = Service_Order_R2R.frm_QAnVerify
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()

        End If
    End Sub

    Private Sub DataAssignToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataAssignToolStripMenuItem.Click
        Dim childForm As Integer = 33
        Dim childForms(33) As frm_assign
        childForms(childForm) = Service_Order_R2R.frm_assign
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub

    Private Sub DataReceiveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataReceiveToolStripMenuItem.Click
        Dim childForm As Integer = 34
        Dim childForms(34) As frm_receive
        childForms(childForm) = Service_Order_R2R.frm_receive
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub
    Private Sub SetupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetupToolStripMenuItem.Click
        Try
            Dim count As Integer = 0
            If Not Me.MdiChildren Is Nothing Then
                Dim childform As System.Windows.Forms.Form
                For Each childform In Me.MdiChildren
                    'count += 1
                    childform.Close()
                Next
                ' MsgBox(count)
            End If
            Dim cmdtext1 As String = "SO_FrmloginAdd"
            Dim cmd_login As New SqlCommand(cmdtext1, conn)
            cmd_login.CommandType = CommandType.StoredProcedure
            cmd_login.Parameters.AddWithValue("@userid", commonUser.Common.uID)
            'Dim cmd_login As New SqlCommand("Insert into tbl_log_archive(user_id,login_logout_flag) values(" & commonUser.Common.uID & ",'log out')", conn)
            cmd_login.ExecuteNonQuery()
        Catch ex As Exception
        End Try
        login_visible()
    End Sub
    Private Sub frm_login_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        If commonUser.Common.uID = "" Then Exit Sub
        Try
            Dim count As Integer = 0
            If Not Me.MdiChildren Is Nothing Then
                Dim childform As System.Windows.Forms.Form
                For Each childform In Me.MdiChildren
                    'count += 1
                    childform.Close()
                Next
                ' MsgBox(count)
            End If
            Dim cmdtext1 As String = "SO_FrmloginAdd"
            'cmd = New SqlCommand("select * from tbl_users  where id = " & commonUser.Common.uID, conn)
            Dim cmd_login As New SqlCommand(cmdtext1, conn)
            cmd_login.CommandType = CommandType.StoredProcedure
            cmd_login.Parameters.AddWithValue("@userid", commonUser.Common.uID)
            'Dim cmd_login As New SqlCommand("Insert into tbl_log_archive(user_id,login_logout_flag) values(" & commonUser.Common.uID & ",'log out')", conn)
            cmd_login.ExecuteNonQuery()
        Catch ex As Exception
        End Try
        login_visible()
    End Sub
    Private Sub login_invisible()
        Me.ToolStrip.Visible = True
        Me.MenuStrip.Visible = True
        Me.StatusStrip.Visible = True
        'Panel2.Visible = False
        'Panel1.Visible = False
        btn_login.Visible = False
        lbl_title.Visible = False
        PictureBox1.Visible = False
        lbl_userid.Visible = False
        Label1.Visible = False
        txt_userid.Visible = False
        txt_pswd.Visible = False
    End Sub

    Private Sub login_visible()
        commonUser.Common.uID = ""
        commonUser.Common.uName = ""
        Me.ToolStrip.Visible = False
        Me.MenuStrip.Visible = False
        Me.StatusStrip.Visible = False
        Panel2.Visible = True
        Panel1.Visible = True
        btn_login.Visible = True
        lbl_title.Visible = True
        PictureBox1.Visible = True
        lbl_userid.Visible = True
        Label1.Visible = True
        txt_userid.Visible = True
        txt_pswd.Visible = True
    End Sub

    Private Sub btn_login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_login.Click
        Try
            If conn.State = ConnectionState.Closed Then
                'conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings("SO_Conn").ToString()
                conn.Open()
            End If
            Dim cmdtext As String = "SO_FrmloginSelectUser"
            Dim cmd As New SqlCommand
            cmd = Nothing
            cmd = New SqlCommand(cmdtext, conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@userid", txt_userid.Text)
            cmd.Parameters.AddWithValue("@pwd", txt_pswd.Text)
            Dim ds As New DataSet
            ' Dim da As New SqlDataAdapter("select id, userid,password,usertype, user_name,status from tbl_users where userid ='" & txt_userid.Text & "' and password ='" & txt_pswd.Text & "'", conn)
            Dim da As New SqlDataAdapter(cmd)
            da.Fill(ds)
            If txt_userid.Text = "" And txt_pswd.Text = "" Then
                MsgBox("Application Authentication Failed. Please Check Login Details and Try Again", MsgBoxStyle.Information, "Login Error")
                txt_userid.Focus()
            ElseIf ds.Tables(0).Rows.Count > 0 Then
                If ds.Tables(0).Rows(0).Item("status") = "I" Then
                    MsgBox("User is currently inactive. Contact System Administrator.", MsgBoxStyle.Information, "Login Error")
                    txt_userid.Clear()
                    txt_pswd.Clear()
                    txt_userid.Focus()
                    Exit Sub
                End If
                commonUser.Common.uID = ds.Tables(0).Rows(0).Item("id")
                commonUser.Common.uName = ds.Tables(0).Rows(0).Item("user_name")
                Me.Text = "Service Order R2R : Welcome " & commonUser.Common.uName & "  !"
                txt_userid.Clear()
                txt_pswd.Clear()
                Try
                    Dim cmdtext1 As String = "SO_FrmloginAdd"
                    'cmd = New SqlCommand("select * from tbl_users  where id = " & commonUser.Common.uID, conn)
                    Dim cmd_login As New SqlCommand(cmdtext1, conn)
                    cmd_login.CommandType = CommandType.StoredProcedure
                    cmd_login.Parameters.AddWithValue("@userid", commonUser.Common.uID)
                    'Dim cmd_login As New SqlCommand("Insert into tbl_log_archive(user_id,login_logout_flag) values(" & commonUser.Common.uID & ",'log in')", conn)
                    cmd_login.ExecuteNonQuery()
                Catch ex As Exception
                End Try
                login_invisible()
                If ds.Tables(0).Rows(0).Item("usertype") = "Admin" Then
                    Me.UserToolStripMenuItem.Enabled = True
                    VerificationToolStripMenuItem.Enabled = False
                    DataAssignToolStripMenuItem.Enabled = False
                    DataReceiveToolStripMenuItem.Enabled = False
                    DataEntryToolStripMenuItem.Enabled = False
                    DataReEntryToolStripMenuItem.Enabled = False
                    CurrentStatusToolStripMenuItem.Enabled = True
                    SubmitDataToolStripMenuItem.Enabled = True
                    PerformanceReportToolStripMenuItem.Enabled = True
                    DataReassignToolStripMenuItem.Enabled = False
                    DataMismatchToolStripMenuItem.Enabled = False
                    WrongDataFrequencyToolStripMenuItem.Enabled = False

                ElseIf ds.Tables(0).Rows(0).Item("usertype") = "Supervisor" Then
                    Me.UserToolStripMenuItem.Enabled = True
                    VerificationToolStripMenuItem.Enabled = True
                    DataAssignToolStripMenuItem.Enabled = True
                    DataReceiveToolStripMenuItem.Enabled = True
                    DataEntryToolStripMenuItem.Enabled = False
                    CurrentStatusToolStripMenuItem.Enabled = True
                    SubmitDataToolStripMenuItem.Enabled = False
                    PerformanceReportToolStripMenuItem.Enabled = True
                    DataReassignToolStripMenuItem.Enabled = True
                    DataMismatchToolStripMenuItem.Enabled = True
                    DataReEntryToolStripMenuItem.Enabled = False
                    WrongDataFrequencyToolStripMenuItem.Enabled = True
                ElseIf ds.Tables(0).Rows(0).Item("usertype") = "DEP" Then
                    Me.UserToolStripMenuItem.Enabled = False
                    VerificationToolStripMenuItem.Enabled = False
                    DataAssignToolStripMenuItem.Enabled = False
                    DataReceiveToolStripMenuItem.Enabled = False
                    DataEntryToolStripMenuItem.Enabled = True
                    CurrentStatusToolStripMenuItem.Enabled = False
                    SubmitDataToolStripMenuItem.Enabled = False
                    PerformanceReportToolStripMenuItem.Enabled = False
                    DataReassignToolStripMenuItem.Enabled = False
                    DataMismatchToolStripMenuItem.Enabled = False
                    DataReEntryToolStripMenuItem.Enabled = True
                    WrongDataFrequencyToolStripMenuItem.Enabled = False
                End If
            Else
                MsgBox("Access Denied. Please enter Valid Login Details...", MsgBoxStyle.Information, "Login Error")
                txt_pswd.Clear()
                txt_userid.Focus()
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Login Error")
            txt_userid.Clear()
            txt_pswd.Clear()
            txt_userid.Focus()
        End Try
    End Sub
    Private Sub ChangePasswordToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangePasswordToolStripMenuItem.Click
        Dim childForm As Integer = 12
        Dim childForms(13) As frm_forgetpw
        childForms(childForm) = Service_Order_R2R.frm_forgetpw
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub
    Private Sub ReportToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReportToolStripMenuItem1.Click
        Dim childForm As Integer = 12
        Dim childForms(12) As frm_home
        childForms(childForm) = Service_Order_R2R.frm_home
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub
    Private Sub SetupToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Private Sub SetUpToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetUpToolStripMenuItem2.Click
        Dim childForm As Integer = 14
        Dim childForms(14) As frm_setups
        childForms(childForm) = Service_Order_R2R.frm_setups
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub
    Private Sub CurrentStatusToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CurrentStatusToolStripMenuItem.Click
        Dim childForm As Integer = 31
        Dim childForms(31) As frm_curstat
        childForms(childForm) = Service_Order_R2R.frm_curstat
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub
    Private Sub DataReportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataReportToolStripMenuItem.Click
        Dim childForm As Integer = 32
        Dim childForms(32) As frm_datareport
        childForms(childForm) = Service_Order_R2R.frm_datareport
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Dim answer As DialogResult
        answer = MessageBox.Show("Are you sure you want to exit Service Order r2r System", "Exit", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
        If answer = Windows.Forms.DialogResult.Cancel Then
            Return
        End If
        Me.Close()
    End Sub

    Private Sub SubmitDataToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubmitDataToolStripMenuItem.Click
        Dim childForm As Integer = 43


      

        Dim childForms(43) As frm_submit
        childForms(childForm) = Service_Order_R2R.frm_submit


        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub

    Private Sub HelpTopicsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpTopicsToolStripMenuItem.Click

    End Sub

    Private Sub AddressLookupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddressLookupToolStripMenuItem.Click

        Dim childForm As Integer = 65
        Dim childForms(65) As frm_addlook
        childForms(childForm) = Service_Order_R2R.frm_addlook
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub

    Private Sub PerformanceReportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PerformanceReportToolStripMenuItem.Click
        Dim childForm As Integer = 37
        Dim childForms(37) As frm_performance
        childForms(childForm) = Service_Order_R2R.frm_performance
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub

    Private Sub LoadXMLToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadXMLToolStripMenuItem.Click

        Dim childForm As Integer = 61
        Dim childForms(61) As frm_loadxml
        childForms(childForm) = Service_Order_R2R.frm_loadxml
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub

 
    
    Private Sub CreateDataToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateDataToolStripMenuItem.Click
        Dim childForm As Integer = 46


        Dim childForms(46) As frm_export_csv
        childForms(childForm) = Service_Order_R2R.frm_export_csv


        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub

    Private Sub DataReEntryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataReEntryToolStripMenuItem.Click
        Dim childForm As Integer = 35
        Dim childForms(35) As frm_redataentry
        childForms(childForm) = Service_Order_R2R.frm_redataentry
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        childForms(childForm).WindowState = FormWindowState.Maximized
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub

    Private Sub DataReassignToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataReassignToolStripMenuItem.Click
        Dim childForm As Integer = 36
        Dim childForms(36) As frm_reassign
        childForms(childForm) = Service_Order_R2R.frm_reassign
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        'childForms(childForm).WindowState = FormWindowState.Maximized
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub

    Private Sub DataMismatchToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataMismatchToolStripMenuItem.Click
        Dim childForm As Integer = 37
        Dim childForms(37) As frm_mismatchData
        childForms(childForm) = Service_Order_R2R.frm_mismatchData
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        'childForms(childForm).WindowState = FormWindowState.Maximized
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub

    Private Sub WrongDataFrequencyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WrongDataFrequencyToolStripMenuItem.Click
        Dim childForm As Integer = 38
        Dim childForms(38) As frm_wrongdatafrequency
        childForms(childForm) = Service_Order_R2R.frm_wrongdatafrequency
        Panel1.Visible = False
        Panel2.Visible = False
        childForms(childForm).MdiParent = Me
        'childForms(childForm).WindowState = FormWindowState.Maximized
        If childForms(childForm).Visible Then
            childForms(childForm).Focus()
        Else
            childForms(childForm).Show()
        End If
    End Sub

    Private Sub txt_pswd_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt_pswd.KeyDown
        If e.KeyCode.Equals(Keys.Return) Then
            btn_login.PerformClick()
        End If
    End Sub
End Class
