Public Class frmDokumentart_NameConvention
    Private DT_ENTITIES As DataTable
    Private FORM_ID As Integer
    Private FORM_VIEW_ID As Integer
    Private TBPMO_WD_FORMVIEW_DOKTYPES_GUID As Integer
    Private loaded As Boolean = False

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnAddIndexM.Click
        If Not Me.cmbIndex_AUTO.Text = "" Then
            Dim text As String
            If Me.cmbIndex_AUTO.Text.StartsWith("NC_") Then
                text = "[%a" & Me.cmbIndex_AUTO.Text.ToString.ToUpper & "#" & Me.cmbIndex_AUTO.SelectedValue & "]"
            Else
                text = "[%a" & Me.cmbIndex_AUTO.Text.ToString.ToUpper & "]"
            End If
            'AREANLAGE-[%mARENR]-[%aKurzname]-I[%Version]

            Dim altePosition As Integer = Me.txtnamenKonv.SelectionStart()
            Me.txtnamenKonv.Text = Me.txtnamenKonv.Text.Insert(altePosition, Text)
            Me.txtnamenKonv.SelectionStart = altePosition + Text.Length
        End If
    End Sub

    Private Sub btnAddIndexA_Click(sender As Object, e As EventArgs) Handles btnAddIndexA.Click
        If Not Me.cmbIndex_Controls.Text = "" Then
            'AREANLAGE-[%mARENR]-[%aKurzname]-I[%Version]
            Dim text As String = "[%a" & Me.cmbIndex_Controls.Text.ToString.ToUpper & "#" & cmbIndex_Controls.SelectedValue & "]"
            Dim altePosition As Integer = Me.txtnamenKonv.SelectionStart()
            Me.txtnamenKonv.Text = Me.txtnamenKonv.Text.Insert(altePosition, text)
            Me.txtnamenKonv.SelectionStart = altePosition + text.Length
        End If
    End Sub

    Private Sub frmNamenkonvention_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing

    End Sub

    Private Sub frmNamenkonvention_Load(sender As Object, e As EventArgs) Handles Me.Load
        'TODO: Diese Codezeile lädt Daten in die Tabelle "DD_DMSDataSet.TBPMO_KONFIGURATION". Sie können sie bei Bedarf verschieben oder entfernen.

        Try
            'Me.TBINDEX_MANTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBDD_INDEX_AUTOMTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBPMO_KONFIGURATIONTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBPMO_KONFIGURATIONTableAdapter.Fill(Me.DD_DMSDataSet.TBPMO_KONFIGURATION)

            Dim autoIIndex = "SELECT GUID, CASE WHEN INDEXNAME = '(ONLY for Nameconvention)' THEN 'NC_' + COMMENT ELSE INDEXNAME END as INDEXNAME FROM TBDD_INDEX_AUTOM WHERE DOCTYPE_ID = " & frmDokumentart_Konfig.akt_DokartID
            Dim DTAUTOINDEX As DataTable = ClassDatabase.Return_Datatable(autoIIndex, "LOAD AUTOINDEXE")

            'Me.TBDD_INDEX_AUTOMTableAdapter.Fill(Me.DD_DMSDataSet.TBDD_INDEX_AUTOM, frmDokumentart_Konfig.akt_DokartID)
            DT_ENTITIES = ClassDatabase.Return_Datatable("select T.GUID, T.FORMVIEW_ID, T1.FORM_TITLE FROM TBPMO_WD_FORMVIEW_DOKTYPES T, TBPMO_FORM_VIEW T1 WHERE T.FORMVIEW_ID = T1.GUID AND T1.SCREEN_ID = 1 AND T.DOCTYPE_ID = " & frmDokumentart_Konfig.akt_DokartID & " ORDER BY T1.FORM_TITLE", "Load NameConventionData")
            cmbEntity.DataSource = DT_ENTITIES
            cmbEntity.DisplayMember = DT_ENTITIES.Columns(2).ColumnName
            cmbEntity.ValueMember = DT_ENTITIES.Columns(0).ColumnName
            cmbEntity.SelectedIndex = 0


            cmbIndex_AUTO.DataSource = DTAUTOINDEX
            cmbIndex_AUTO.DisplayMember = DTAUTOINDEX.Columns(1).ColumnName
            cmbIndex_AUTO.ValueMember = DTAUTOINDEX.Columns(0).ColumnName
            cmbIndex_AUTO.SelectedIndex = 0
            'txtnamenKonv.Text = frmDokumentart_Konfig.akt_Namenkonvention
        Catch ex As Exception
            MsgBox("Fehler beim Laden der Grunddaten zur Dokumentart: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click

    End Sub
    Private Sub btndelete_Click(sender As Object, e As EventArgs) Handles btndelete.Click
        txtnamenKonv.Text = ""
    End Sub

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        Dim sql = "UPDATE TBPMO_WD_FORMVIEW_DOKTYPES SET NAME_CONVENTION = '" & txtnamenKonv.Text & "', CHANGED_WHO = '" & Environment.UserName & "' WHERE GUID = " & TBPMO_WD_FORMVIEW_DOKTYPES_GUID
        If ClassDatabase.Execute_non_Query(sql, True) = True Then
            Me.Close()
        Else
            MsgBox("Unexpected Error in Save NameConvention - Check logfile", MsgBoxStyle.Exclamation)
        End If
    End Sub

    Private Sub btnclose_Click(sender As Object, e As EventArgs) Handles btnclose.Click
        Me.Close()
    End Sub

    Private Sub btnTrennzeichen_Click(sender As Object, e As EventArgs) Handles btnTrennzeichen.Click
        Dim text As String = FILE_DELIMITERTextBox.Text
        Dim altePosition As Integer = Me.txtnamenKonv.SelectionStart()
        Me.txtnamenKonv.Text = Me.txtnamenKonv.Text.Insert(altePosition, text)
        Me.txtnamenKonv.SelectionStart = altePosition + text.Length
    End Sub
    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        If Not Me.cmbDate.Text = "" Then
            'AREANLAGE-[%mARENR]-[%aKurzname]-I[%Version]
            Dim text As String = "[%v" & Me.cmbDate.Text.ToString.ToUpper & "]"
            Dim altePosition As Integer = Me.txtnamenKonv.SelectionStart()
            Me.txtnamenKonv.Text = Me.txtnamenKonv.Text.Insert(altePosition, text)
            Me.txtnamenKonv.SelectionStart = altePosition + text.Length
        End If
    End Sub

    Private Sub cmbEntity_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbEntity.SelectedIndexChanged
        Load_EntityData()
    End Sub
    Sub Load_EntityData()
        Try
            If cmbEntity.SelectedIndex <> -1 Then
                TBPMO_WD_FORMVIEW_DOKTYPES_GUID = Me.cmbEntity.SelectedValue
                FORM_VIEW_ID = ClassDatabase.Execute_Scalar("SELECT FORMVIEW_ID FROM TBPMO_WD_FORMVIEW_DOKTYPES WHERE GUID = " & TBPMO_WD_FORMVIEW_DOKTYPES_GUID, True)
                Dim NameConv = ClassDatabase.Execute_Scalar("SELECT NAME_CONVENTION FROM TBPMO_WD_FORMVIEW_DOKTYPES WHERE GUID = " & TBPMO_WD_FORMVIEW_DOKTYPES_GUID, True)
                If Not IsNothing(NameConv) Then
                    Me.txtnamenKonv.Text = NameConv
                    Load_Controls()
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in cmbEntity.SelectedIndexChanged: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Sub Load_Controls()
        If loaded = False Then Exit Sub
        Try
            Dim DT1 As DataTable = ClassDatabase.Return_Datatable("SELECT [CONTROL_ID] , [CONTROL_COL_NAME]  AS DISPLAY FROM [VWPMO_CONTROL_SCREEN] WHERE CTRLTYPE_ID in (2,3,4) and FORM_ID = (SELECT FORM_ID FROM TBPMO_FORM_VIEW WHERE GUID = " & FORM_VIEW_ID & ")", "LoadControlsForEntity - NameConvention")
            If DT1.Rows.Count >= 1 Then
                cmbIndex_Controls.DataSource = DT1
                cmbIndex_Controls.DisplayMember = DT1.Columns(1).ColumnName
                cmbIndex_Controls.ValueMember = DT1.Columns(0).ColumnName
                cmbIndex_Controls.Enabled = True
                btnAddIndexA.Enabled = True
            Else
                cmbIndex_Controls.Enabled = False
                btnAddIndexA.Enabled = False
            End If

        Catch ex As Exception
            MsgBox("Error in Load Controls for EntitySelect: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub frmDokumentart_NameConvention_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        loaded = True
        If DT_ENTITIES.Rows.Count = 1 Then
            Load_Controls()
            Load_EntityData()
        End If
    End Sub
End Class