Imports DD_Rights
Public Class frmStart
    Private Sub frmStart_Load(sender As Object, e As EventArgs) Handles Me.Load
       
        Try
            DD_Rights.clsLogger.Init(Application.UserAppDataPath() & "\Log", Environment.UserName)
            If DD_Rights.clsDatabase.Init(My.Settings.MyConnectionString) = False Then
                MsgBox("Error in Initializing Database. Please check log.", MsgBoxStyle.Critical)
                Me.Close()
            End If
            ClassDatabase.ConnectionStringRM = My.Settings.MyConnectionString
            If ClassDatabase.Init = False Then
                MsgBox("Error in Initializing Database. Please check log.", MsgBoxStyle.Critical)
                Me.Close()
            End If

            chkLogerrorsonly.Checked = CBool(clsDatabase.Execute_Scalar("SELECT LOG_ERR_ONLY FROM TBPMO_SERVICE_RIGHT_CONFIG WHERE GUID = 1"))
            Dim sql = String.Format("SELECT * FROM TBDD_USER WHERE (LOWER(USERNAME) = LOWER('{0}'))", Environment.UserName)
            clsLogger.Add(">> Login at: " & Now.ToString, False)
            clsLogger.Add(">> Username: " & Environment.UserName, False)
            Dim USER_DT As DataTable = clsDatabase.Return_Datatable(sql)


            If USER_DT.Rows.Count = 0 Then
                clsLogger.Add(" - User '" & Environment.UserName & "' not listed in Useradminsitration!", False)
                'MsgBox("Achtung: Sie sind nicht in der Userverwaltung hinterlegt." & vbNewLine & "Bitte setzen Sie sich mit dem Systembetreuer in Verbindung!", MsgBoxStyle.Critical, "Achtung:")
                'Me.Close()
                Dim msg = String.Format("You are not listed in the Useradministration." & vbNewLine & "Please contact the admin.")
                MsgBox(msg, MsgBoxStyle.Exclamation)
                Me.Close()
            Else
                USER_GUID = USER_DT.Rows(0).Item("GUID")
                USERID_FK_INT_ECM = USER_DT.Rows(0).Item("USERID_FK_INT_ECM")
                USER_WAN = USER_DT.Rows(0).Item("WAN_ENVIRONMENT") 'clsDatabase.Execute_Scalar("SELECT WAN_ENVIRONMENT FROM TBDD_USER WHERE GUID = " & USER_GUID, False)
                USER_LANGUAGE = USER_DT.Rows(0).Item("LANGUAGE")
                USER_DATE_FORMAT = USER_DT.Rows(0).Item("DATE_FORMAT")
                USER_IS_ADMIN = USER_DT.Rows(0).Item("RECORD_ADMIN")
                'USER_LANGUAGE = clsDatabase.Execute_Scalar("SELECT LANGUAGE FROM TBDD_USER WHERE GUID = " & USER_GUID, False)
                Dim DT_CLIENT_USER As DataTable = clsDatabase.Return_Datatable("SELECT CLIENT_ID FROM TBDD_CLIENT_USER WHERE USER_ID = " & USER_GUID)
                'Dim i As Integer = 0
                'For Each row As DataRow In DT_CLIENT_USER.Rows
                '    If i = 0 Then
                '        USER_CLIENTS_COMMA_SEPERATED = row.Item(0).ToString
                '    Else
                '        USER_CLIENTS_COMMA_SEPERATED = USER_CLIENTS_COMMA_SEPERATED & "," & row.Item(0).ToString
                '    End If
                '    i += 1

                'Next

                Dim Right_RO As Boolean = USER_DT.Rows(0).Item("MODULE_RECORD_ORG")
                If Right_RO = False Then
                    clsLogger.Add(" - User: " & Environment.UserName & " nicht für Modul freigegben!", False)
                    'MsgBox("Achtung: Sie sind nicht für die Nutzung dieses Moduls freigeschaltet." & vbNewLine & "Bitte setzen Sie sich mit dem Systembetreuer in Verbindung!", MsgBoxStyle.Critical, "Achtung:")
                    Dim msg = String.Format("Sie sind nicht für die Nutzung dieses Moduls freigeschaltet." & vbNewLine & "Bitte setzen Sie sich mit dem Systembetreuer in Verbindung!")
                    If USER_LANGUAGE <> "de-DE" Then
                        msg = String.Format("You are not authorized for using this module." & vbNewLine & "Please contact the admin.")
                    End If
                    MsgBox(msg, MsgBoxStyle.Exclamation)
                    Me.Close()
                Else
                    sql = String.Format("SELECT * FROM TBPMO_ENTITY_SUPERVISOR WHERE USER_ID = {0}", USER_GUID)
                    Dim DT As DataTable = clsDatabase.Return_Datatable(sql)
                    If DT.Rows.Count > 0 Then
                        DT_SUPERVISOR_ENTITY = DT
                    End If

                    If USER_IS_ADMIN = False And DT.Rows.Count = 0 Then
                        Dim msg = String.Format("Sorry You are not an ADDI-Admin!")
                        MsgBox(msg, MsgBoxStyle.Exclamation)
                        Me.Close()
                    End If

                    Check_USER_REWORK()

                End If
            End If
        Catch ex As Exception
            clsLogger.Add("Unexpected Error in InitUserLogin: " & ex.Message, True)
            MsgBox("Unexpected Error in InitUserLogin: " & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Sub Check_USER_REWORK()
        'Überprüfen ob es User gibt die aktualisiert werden müssen
        DT_WORK_USER_RIGHTS = clsDatabase.Return_Datatable(SQL_USER_RIGHTS_CHANGED)
        If DT_WORK_USER_RIGHTS.Rows.Count > 0 And (Environment.UserName.ToLower = "windream" Or Environment.UserName.ToLower = "schreiberm") Then
            btnWorkUserRights.Text = "Userrelations were changed." & vbNewLine & DT_WORK_USER_RIGHTS.Rows.Count.ToString & " Users need to be refreshed!"
            btnWorkUserRights.Visible = True
        Else
            btnWorkUserRights.Visible = False
        End If
        DT_WORK_USER_CONTROL_SUPERVISOR = clsDatabase.Return_Datatable(SQL_USER_RIGHTS_SUPERVISOR_ADDED)
        If DT_WORK_USER_CONTROL_SUPERVISOR.Rows.Count > 0 And (Environment.UserName.ToLower = "windream" Or Environment.UserName.ToLower = "schreiberm") Then
            btnWorkUserRightsSV_ADD.Text = "New Supervisor for Record/Entity added." & vbNewLine & "Amount: " & DT_WORK_USER_CONTROL_SUPERVISOR.Rows.Count.ToString
            btnWorkUserRightsSV_ADD.Visible = True
        Else
            btnWorkUserRightsSV_ADD.Visible = False
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Me.Visible = False
        frmCheckRightsEntity.ShowDialog()
        'Me.Visible = True
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Process.Start(clsLogger.DateiSpeicherort)
    End Sub

    Private Sub btnWorkUserRights_Click(sender As Object, e As EventArgs) Handles btnWorkUserRights.Click
        frmReworkUsers.ShowDialog()
        Check_USER_REWORK()
    End Sub

    Private Sub btnWorkUserRightsSV_ADD_Click(sender As Object, e As EventArgs) Handles btnWorkUserRightsSV_ADD.Click
        frmSupervisorEntity.ShowDialog()
        Check_USER_REWORK()
    End Sub

    Private Sub XtraTabControl1_SelectedPageChanged(sender As Object, e As DevExpress.XtraTab.TabPageChangedEventArgs) Handles XtraTabControl1.SelectedPageChanged
        Select Case XtraTabControl1.SelectedTabPageIndex
            Case 1
                Load_jobs()
        End Select
    End Sub
    Sub Load_Jobs()
        Try
            Cursor = Cursors.WaitCursor
            Me.VWPMO_RIGHTS_2B_WORKEDTableAdapter.Connection.ConnectionString = My.Settings.MyConnectionString
            Me.VWPMO_RIGHTS_2B_WORKEDTableAdapter.Fill(Me.MyDataset.VWPMO_RIGHTS_2B_WORKED)
            If MyDataset.VWPMO_RIGHTS_2B_WORKED.Rows.Count > 0 Then
                XtraTabPage2.Text = "Rights 2b worked (" & MyDataset.VWPMO_RIGHTS_2B_WORKED.Rows.Count.ToString & " rights waiting)"
            End If

        Catch ex As Exception
            clsLogger.Add("Unexpected Error in Load_Jobs: " & ex.Message, True)
            MsgBox("Unexpected Error in Load_Jobs: " & ex.Message, MsgBoxStyle.Critical)
        End Try
        Cursor = Cursors.Default
    End Sub

    Private Sub btnrefreshJobs_Click(sender As Object, e As EventArgs) Handles btnrefreshJobs.Click
        Load_Jobs()
    End Sub

    Private Sub chkLogerrorsonly_CheckedChanged(sender As Object, e As EventArgs) Handles chkLogerrorsonly.CheckedChanged
        Dim _set = 0
        If chkLogerrorsonly.Checked Then
            _set = 1
        End If
        Dim upd = String.Format("UPDATE TBPMO_SERVICE_RIGHT_CONFIG SET LOG_ERR_ONLY = {0} where GUID = 1", _set)
        clsDatabase.Execute_non_Query(upd)
    End Sub

    Private Sub chklogerrors_CheckedChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub btnCheckRenewUserRights_Click(sender As Object, e As EventArgs) Handles btnCheckRenewUserRights.Click
        frmUsersReworkRights.ShowDialog()
    End Sub
End Class
