Public Class frmWiedervorlage
    Dim Formloaded As Boolean = False
    Private Sub TBPMO_FOLLOW_UP_EMAILBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TBPMO_FOLLOW_UP_EMAILBindingNavigatorSaveItem.Click
      Save_FollowUP_EMail
    End Sub
    Sub Save_FollowUP_EMail()
        Try
            Me.TBPMO_FOLLOW_UP_EMAILBindingSource.EndEdit()
            If DD_DMSDataSet.TBPMO_FOLLOW_UP_EMAIL.GetChanges Is Nothing = False Then
                Me.CHANGED_WHOTextBox.Text = Environment.UserName
                Me.TBPMO_FOLLOW_UP_EMAILBindingSource.EndEdit()
                TBPMO_FOLLOW_UP_EMAILTableAdapter.Update(Me.DD_DMSDataSet.TBPMO_FOLLOW_UP_EMAIL)
                lblSaveWVEmail.Visible = True
            Else
                lblSaveWVEmail.Visible = False
            End If
        Catch ex As Exception
            MsgBox("Fehler in Save_FollowUP_EMail:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Sub frmWiedervorlage_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: Diese Codezeile lädt Daten in die Tabelle "DD_DMSDataSet.TBPMO_FORM_CONSTRUCTOR". Sie können sie bei Bedarf verschieben oder entfernen.

        Try
            Dim DT1 As DataTable = ClassDatabase.Return_Datatable("SELECT [CONTROL_ID] ,[FORM_NAME] + ' | ' + [CONTROL_NAME] AS DISPLAY FROM [VWPMO_CONTROL_SCREEN] WHERE CTRLTYPE_ID = 4 ORDER BY FORM_ID")
            Dim DT2 As DataTable = ClassDatabase.Return_Datatable("SELECT [CONTROL_ID] ,[FORM_NAME] + ' | ' + [CONTROL_NAME] AS DISPLAY FROM [VWPMO_CONTROL_SCREEN] WHERE CTRLTYPE_ID = 10 ORDER BY FORM_ID")
            Dim DT3 As DataTable = ClassDatabase.Return_Datatable("select GUID,NAME FROM TBPMO_FOLLOW_UP_EMAIL ORDER BY NAME")
            Dim DTUser As DataTable = ClassDatabase.Return_Datatable("select guid, username from TBDD_USER t where T.EMAIL IS NOT NULL AND MODULE_RECORD_ORG = 1 order by USERNAME")

            PARENT_IDComboBox.DataSource = DT3
            PARENT_IDComboBox.DisplayMember = DT3.Columns(1).ColumnName
            PARENT_IDComboBox.ValueMember = DT3.Columns(0).ColumnName

            DEPENDENT_DATE_CTRL_IDComboBox.DataSource = DT1
            DEPENDENT_DATE_CTRL_IDComboBox.DisplayMember = DT1.Columns(1).ColumnName
            DEPENDENT_DATE_CTRL_IDComboBox.ValueMember = DT1.Columns(0).ColumnName
            DEPENDENT_DONE_CTRL_IDComboBox.DataSource = DT2
            DEPENDENT_DONE_CTRL_IDComboBox.DisplayMember = DT2.Columns(1).ColumnName
            DEPENDENT_DONE_CTRL_IDComboBox.ValueMember = DT2.Columns(0).ColumnName
            cmbUser.DataSource = DTUser
            cmbUser.DisplayMember = DTUser.Columns(1).ColumnName
            cmbUser.ValueMember = DTUser.Columns(0).ColumnName
            TBPMO_FOLLOW_UP_EMAILTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBPMO_FORM_CONSTRUCTORTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBPMO_FORM_CONSTRUCTORTableAdapter.Fill(Me.DD_DMSDataSet.TBPMO_FORM_CONSTRUCTOR, USER_LANGUAGE, CURRENT_SCREEN_ID, USER_GUID)

            Load_WV_Profiles()
        Catch ex As Exception
            MsgBox("Error in Load Form:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub
    Sub Load_WV_PRofiles()
        Try
            Me.TBPMO_FOLLOW_UP_EMAILTableAdapter.Fill(Me.DD_DMSDataSet.TBPMO_FOLLOW_UP_EMAIL)
        Catch ex As Exception
            MsgBox("Error in Load_WV_PRofiles:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Dim loaded As Boolean = False
    Private Sub XtraTabControl1_SelectedPageChanged(sender As Object, e As DevExpress.XtraTab.TabPageChangedEventArgs) Handles XtraTabControl1.SelectedPageChanged
        Select Case XtraTabControl1.SelectedTabPageIndex
            Case 0
            Case 1
                Try
                    loaded = False
                    Dim sql2 = "SELECT FOLLOW_UP_LOG_ERR_ONLY FROM TBPMO_KONFIGURATION WHERE GUID = 1"
                    chkbxLogErrorsonly.Checked = CBool(ClassDatabase.Execute_Scalar(sql2))
                    load_EmailProfiles()
                Catch ex As Exception
                    MsgBox("Fehler beim Laden der Grundeinstellungen:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
                End Try
                loaded = True
        End Select
    End Sub

    Private Sub chkbxLogErrorsonly_CheckedChanged(sender As Object, e As EventArgs) Handles chkbxLogErrorsonly.CheckedChanged
        Try
            If loaded = True Then
                Dim bool As Integer
                If chkbxLogErrorsonly.Checked Then
                    bool = 1
                Else
                    bool = 0
                End If
                Dim upd = "UPDATE TBPMO_KONFIGURATION SET FOLLOW_UP_LOG_ERR_ONLY = " & bool & " WHERE GUID = 1"
                ClassDatabase.Execute_non_Query(upd, True)
            End If
        Catch ex As Exception
            MsgBox("Fehler bei Update LogErrorsOnly:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btnEmailConfig_Click(sender As Object, e As EventArgs) Handles btnEmailConfig.Click
        frmDD_EMAIL_ACCOUNT.ShowDialog()
        load_EmailProfiles()
    End Sub
    Sub load_EmailProfiles()
        Try
            Dim sql1 = "SELECT FOLLOW_UP_EMAIL_PROFILE FROM TBPMO_KONFIGURATION WHERE GUID = 1"
            Dim email_id = ClassDatabase.Execute_Scalar(sql1)
            Dim sql = "SELECT GUID, NAME FROM TBDD_EMAIL_ACCOUNT WHERE ACTIVE = 1"
            Dim DT As DataTable = ClassDatabase.Return_Datatable(sql)
            If Not IsNothing(DT) Then
                cmbWWEmail.DataSource = DT
                cmbWWEmail.DisplayMember = DT.Columns(1).ColumnName
                cmbWWEmail.ValueMember = DT.Columns(0).ColumnName
                Dim emailp = ClassDatabase.Execute_Scalar("SELECT NAME FROM TBDD_EMAIL_ACCOUNT WHERE GUID = " & email_id)
                cmbWWEmail.SelectedIndex = cmbWWEmail.FindStringExact(emailp)
            End If
        Catch ex As Exception
            MsgBox("Fehler bei Load EmailProfiles:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub TBPMO_FOLLOW_UP_EMAILBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBPMO_FOLLOW_UP_EMAILBindingSource.AddingNew
        Try
            Me.FOLLOW_UP_SEQUENCEComboBox.SelectedIndex = 0
            DD_DMSDataSet.TBPMO_FOLLOW_UP_EMAIL.ADDED_WHOColumn.DefaultValue = Environment.UserName
            DD_DMSDataSet.TBPMO_FOLLOW_UP_EMAIL.FOLLOW_UP_DAYSColumn.DefaultValue = "0000000"
        Catch ex As Exception
            MsgBox("Fehler bei Add Record FollowUp:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Sub Load_User_2_Profile(ID As Integer)
        Try
            Dim sql = "SELECT T.GUID, T1.USERNAME FROM TBPMO_FOLLUPEMAIL_USER T, TBDD_USER T1 WHERE T.USER_ID = T1.GUID AND T.FOLLOW_UP_ID = " & GUIDTextBox.Text & " ORDER BY T1.USERNAME"
            Dim DT As DataTable = ClassDatabase.Return_Datatable(sql)
            ListBoxUser2Profile.DataSource = DT
            ListBoxUser2Profile.DisplayMember = DT.Columns(1).ColumnName
            ListBoxUser2Profile.ValueMember = DT.Columns(0).ColumnName
        Catch ex As Exception
            MsgBox("Fehler bei LaodUser2Profile:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub TBPMO_FOLLOW_UP_EMAILBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles TBPMO_FOLLOW_UP_EMAILBindingSource.CurrentChanged
        If GUIDTextBox.Text <> "" Then
            Load_User_2_Profile(GUIDTextBox.Text)
            SetCheckbox()
        End If
        lblSaveWVEmail.Visible = False
    End Sub

    Private Sub btnAddUser_WV_Click(sender As Object, e As EventArgs) Handles btnAddUser_WV.Click
        If cmbUser.SelectedIndex <> -1 Then
            Try
                Dim ins = "insert into TBPMO_FOLLUPEMAIL_USER (USER_ID,FOLLOW_UP_ID,ADDED_WHO) VALUES (" & cmbUser.SelectedValue & ", " & Me.GUIDTextBox.Text & ", '" & Environment.UserName & "')"
                If ClassDatabase.Execute_non_Query(ins, True) = True Then
                    Load_User_2_Profile(Me.GUIDTextBox.Text)
                End If
            Catch ex As Exception
                MsgBox("Fehler bei AddUser2Profile:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            End Try
        End If
    End Sub

    Private Sub btndelete_Click(sender As Object, e As EventArgs) Handles btndelete.Click
        If ListBoxUser2Profile.SelectedIndex <> -1 Then
            Try
                Dim del = "DELETE FROM TBPMO_FOLLUPEMAIL_USER WHERE GUID = " & ListBoxUser2Profile.SelectedValue
                If ClassDatabase.Execute_non_Query(del, True) = True Then
                    Load_User_2_Profile(Me.GUIDTextBox.Text)
                End If
            Catch ex As Exception
                MsgBox("Fehler bei DeleteUserRelation:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            End Try
        End If
    End Sub
    Private Sub frmWiedervorlage_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Load_User_2_Profile(Me.GUIDTextBox.Text)
        Formloaded = True
        SetCheckbox()
    End Sub

    Private Sub FOLLOW_UP_SEQUENCEComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles FOLLOW_UP_SEQUENCEComboBox.SelectedIndexChanged
        If Formloaded = True And FOLLOW_UP_SEQUENCEComboBox.SelectedIndex <> -1 Then
            Select Case FOLLOW_UP_SEQUENCEComboBox.Text
                Case "ONCE_WEEKLY"
                Case "ONCE_DAILY"
                Case "Hour"
                  
            End Select
        End If
    End Sub
    Dim CheckChange As Boolean = False
    Private Sub FOLLOW_UP_DAYSTextBox_TextChanged(sender As Object, e As EventArgs)
        'If Formloaded = True And CheckChange = False And follup_Days <> "" Then
        '    If follup_Days.Length = 7 Then
        '        SetCheckbox()
        '    End If

        'End If
    End Sub
    Sub SetCheckbox()
        Try
            Dim sql As String = "select FOLLOW_UP_DAYS from TBPMO_FOLLOW_UP_EMAIL where guid = " & GUIDTextBox.Text
            Dim follup_Days As String = ClassDatabase.Execute_Scalar(sql)
            Formloaded = False
            CheckBox1.Checked = False
            chkDienstag.Checked = False
            chkMittwoch.Checked = False
            chkDonnerstag.Checked = False
            chkFreitag.Checked = False
            chkSamstag.Checked = False
            chkSonntag.Checked = False


            If follup_Days.Substring(0, 1) = 1 Then
                CheckBox1.Checked = True
            End If
            If follup_Days.Substring(1, 1) = 1 Then
                chkDienstag.Checked = True
            End If
            If follup_Days.Substring(2, 1) = 1 Then
                chkMittwoch.Checked = True
            End If
            If follup_Days.Substring(3, 1) = 1 Then
                chkDonnerstag.Checked = True
            End If
            If follup_Days.Substring(4, 1) = 1 Then
                chkFreitag.Checked = True
            End If
            If follup_Days.Substring(5, 1) = 1 Then
                chkSamstag.Checked = True
            End If
            If follup_Days.Substring(6, 1) = 1 Then
                chkSonntag.Checked = True
            End If
        Catch ex As Exception
            MsgBox("Fehler bei SetCheckbox:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
        Formloaded = True
    End Sub
    Sub CheckBox_CheckChanged()
        If Formloaded = False Then
            Exit Sub
        End If
        Dim sql As String = "select FOLLOW_UP_DAYS from TBPMO_FOLLOW_UP_EMAIL where guid = " & GUIDTextBox.Text
        Dim follup_Days = ClassDatabase.Execute_Scalar(sql)
        If IsNothing(follup_Days) Then
            follup_Days = "0000000"
        End If
        Dim follup_Old = follup_Days
        CheckChange = True
        If CheckBox1.Checked Then
            follup_Days = "1" & follup_Days.Substring(1)
        Else
            follup_Days = "0" & follup_Days.Substring(1)
        End If
        If chkDienstag.Checked Then
            Select Case FOLLOW_UP_SEQUENCEComboBox.Text
                Case "ONCE_WEEKLY"
                    If follup_Days.Contains("1") Then
                        MsgBox("Achtung: Sie können nur einen Tag auswählen!", MsgBoxStyle.Exclamation)
                        chkDienstag.Checked = False
                        Exit Sub
                    End If
            End Select
            follup_Days = follup_Days.Substring(0, 1) & "1" & follup_Days.Substring(2)
        Else
            follup_Days = follup_Days.Substring(0, 1) & "0" & follup_Days.Substring(2)
        End If
        If chkMittwoch.Checked Then
            Select Case FOLLOW_UP_SEQUENCEComboBox.Text
                Case "ONCE_WEEKLY"
                    If follup_Days.Contains("1") Then
                        MsgBox("Achtung: Sie können nur einen Tag auswählen!", MsgBoxStyle.Exclamation)
                        chkMittwoch.Checked = False
                        Exit Sub
                    End If
            End Select
            follup_Days = follup_Days.Substring(0, 2) & "1" & follup_Days.Substring(3)
        Else
            follup_Days = follup_Days.Substring(0, 2) & "0" & follup_Days.Substring(3)
        End If
        If chkDonnerstag.Checked Then
            Select Case FOLLOW_UP_SEQUENCEComboBox.Text
                Case "ONCE_WEEKLY"
                    If follup_Days.Contains("1") Then
                        MsgBox("Achtung: Sie können nur einen Tag auswählen!", MsgBoxStyle.Exclamation)
                        chkDonnerstag.Checked = False
                        Exit Sub
                    End If
            End Select
            follup_Days = follup_Days.Substring(0, 3) & "1" & follup_Days.Substring(4)
        Else
            follup_Days = follup_Days.Substring(0, 3) & "0" & follup_Days.Substring(4)
        End If
        If chkFreitag.Checked Then
            Select Case FOLLOW_UP_SEQUENCEComboBox.Text
                Case "ONCE_WEEKLY"
                    If follup_Days.Contains("1") Then
                        MsgBox("Achtung: Sie können nur einen Tag auswählen!", MsgBoxStyle.Exclamation)
                        chkFreitag.Checked = False
                        Exit Sub
                    End If
            End Select
            follup_Days = follup_Days.Substring(0, 4) & "1" & follup_Days.Substring(5)
        Else
            follup_Days = follup_Days.Substring(0, 4) & "0" & follup_Days.Substring(5)
        End If
        If chkSamstag.Checked Then
            Select Case FOLLOW_UP_SEQUENCEComboBox.Text
                Case "ONCE_WEEKLY"
                    If follup_Days.Contains("1") Then
                        MsgBox("Achtung: Sie können nur einen Tag auswählen!", MsgBoxStyle.Exclamation)
                        chkSamstag.Checked = False
                        Exit Sub
                    End If
            End Select
            follup_Days = follup_Days.Substring(0, 5) & "1" & follup_Days.Substring(6)
        Else
            follup_Days = follup_Days.Substring(0, 5) & "0" & follup_Days.Substring(6)
        End If
        If chkSonntag.Checked Then
            Select Case FOLLOW_UP_SEQUENCEComboBox.Text
                Case "ONCE_WEEKLY"
                    If follup_Days.Contains("1") Then
                        MsgBox("Achtung: Sie können nur einen Tag auswählen!", MsgBoxStyle.Exclamation)
                        chkSonntag.Checked = False
                        Exit Sub
                    End If
            End Select
            follup_Days = follup_Days.Substring(0, 6) & "1"
        Else
            follup_Days = follup_Days.Substring(0, 6) & "0"
        End If


        If follup_Old <> follup_Days Then
            FOLLOW_UP_DAYSTextBox.Text = follup_Days
        End If
        CheckChange = False
    End Sub
    Private Sub chkDienstag_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, chkDienstag.CheckedChanged, chkMittwoch.CheckedChanged, chkDonnerstag.CheckedChanged, chkFreitag.CheckedChanged, _
        chkSamstag.CheckedChanged, chkSonntag.CheckedChanged
        CheckBox_CheckChanged()
    End Sub
End Class