Public Class frmDoctype_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
    Dim DTTBPMO_WD_FORMVIEW_DOKTYPES As DataTable

    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 value = Me.cmbIndex_Controls.Text.ToString.ToUpper

            Dim text As String = "[%a" & value & "#" & cmbIndex_Controls.SelectedValue & "]"
            text = text.Replace("##", "#")
            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
        If Save_Convention() = False Then
            e.Cancel = True
        End If
    End Sub

    Private Sub frmNamenkonvention_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            lblSave.Visible = False
            'Me.TBINDEX_MANTableAdapter.Connection.ConnectionString = MYDB_ECM.CurrentConnectionString
            Me.TBPMO_WD_NAMECONVENTION_FORMATTableAdapter.Connection.ConnectionString = MYDB_ECM.CurrentConnectionString
            Me.TBDD_INDEX_AUTOMTableAdapter.Connection.ConnectionString = MYDB_ECM.CurrentConnectionString

            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 = " & frmWM_DoctypeConfig.akt_DokartID
            Dim DTAUTOINDEX As DataTable = MYDB_ECM.GetDatatable(autoIIndex)

            'Me.TBDD_INDEX_AUTOMTableAdapter.Fill(Me.DD_DMSDataSet.TBDD_INDEX_AUTOM, frmDokumentart_Konfig.akt_DokartID)
            DT_ENTITIES = MYDB_ECM.GetDatatable("select T.GUID, T.FORMVIEW_ID, [dbo].[FNPMO_GETOBJECTCAPTION]('" & USER_LANGUAGE & "','FORMVIEW_TITLE' + CONVERT(VARCHAR(5), T.FORMVIEW_ID), 1) AS '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 = " & frmWM_DoctypeConfig.akt_DokartID & " ORDER BY T1.FORM_TITLE")
            cmbEntity.DataSource = DT_ENTITIES
            cmbEntity.DisplayMember = DT_ENTITIES.Columns(2).ColumnName
            cmbEntity.ValueMember = DT_ENTITIES.Columns(0).ColumnName
            cmbEntity.SelectedIndex = 0

            If DTAUTOINDEX.Rows.Count > 0 Then
                cmbIndex_AUTO.DataSource = DTAUTOINDEX
                cmbIndex_AUTO.DisplayMember = DTAUTOINDEX.Columns(1).ColumnName
                cmbIndex_AUTO.ValueMember = DTAUTOINDEX.Columns(0).ColumnName
                cmbIndex_AUTO.SelectedIndex = 0
                cmbIndex_AUTO.Enabled = True
                btnAddIndexA.Enabled = True
            Else
                cmbIndex_AUTO.Enabled = False
                btnAddIndexA.Enabled = False
            End If
        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
        Save_Convention()
    End Sub
    Private Function Save_Convention()
        Dim sf
        If chkManFolder.Checked Then
            sf = 1
        Else
            sf = 0
        End If
        Dim sql = ""
        If DTTBPMO_WD_FORMVIEW_DOKTYPES.Rows.Count = 1 Then
            sql = "UPDATE TBPMO_WD_FORMVIEW_DOKTYPES SET NAME_CONVENTION = '" & txtnamenKonv.Text & "', MANUAL_SUBFOLDER = " & sf & ",  CHANGED_WHO = '" & USER_USERNAME & "' WHERE GUID = " & TBPMO_WD_FORMVIEW_DOKTYPES_GUID
        Else
            sql = $"INSERT INTO TBPMO_WD_FORMVIEW_DOKTYPES (NAME_CONVENTION,MANUAL_SUBFOLDER,ADDED_WHO) VALUES ('{txtnamenKonv.Text}',{sf},'{USER_USERNAME}')"
        End If
        If MYDB_ECM.ExecuteNonQuery(Sql) = True Then
            DTTBPMO_WD_FORMVIEW_DOKTYPES = MYDB_ECM.GetDatatable("SELECT * FROM TBPMO_WD_FORMVIEW_DOKTYPES WHERE GUID = " & TBPMO_WD_FORMVIEW_DOKTYPES_GUID)
            lblSave.Visible = True
            lblSave.Text = "Data saved - " & Now.ToString
            Return True
        Else
            MsgBox("Unexpected Error in Save NameConvention - Check logfile", MsgBoxStyle.Exclamation)
            lblSave.Visible = False
            Return False
        End If
    End Function
    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)
        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()
        Load_Format_Rules()
    End Sub
    Sub Load_EntityData()
        Try
            If cmbEntity.SelectedIndex <> -1 Then
                TBPMO_WD_FORMVIEW_DOKTYPES_GUID = Me.cmbEntity.SelectedValue
                DTTBPMO_WD_FORMVIEW_DOKTYPES = MYDB_ECM.GetDatatable("SELECT * FROM TBPMO_WD_FORMVIEW_DOKTYPES WHERE GUID = " & TBPMO_WD_FORMVIEW_DOKTYPES_GUID)
                If DTTBPMO_WD_FORMVIEW_DOKTYPES.Rows.Count = 1 Then
                    FORM_VIEW_ID = DTTBPMO_WD_FORMVIEW_DOKTYPES.Rows(0).Item("FORMVIEW_ID")
                    Dim NameConv = DTTBPMO_WD_FORMVIEW_DOKTYPES.Rows(0).Item("NAME_CONVENTION")
                    chkManFolder.Checked = CBool(DTTBPMO_WD_FORMVIEW_DOKTYPES.Rows(0).Item("MANUAL_SUBFOLDER"))
                    If Not IsDBNull(NameConv) Then
                        Me.txtnamenKonv.Text = NameConv
                        Load_Controls()
                    Else
                        Me.txtnamenKonv.Text = ""
                    End If
                Else
                    Me.txtnamenKonv.Text = ""
                End If
               
            End If
        Catch ex As Exception
            MsgBox("Error in cmbEntity.SelectedIndexChanged: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Sub Load_Format_Rules()
        Try
            Me.TBPMO_WD_NAMECONVENTION_FORMATTableAdapter.Fill(Me.DD_DMSDataSet.TBPMO_WD_NAMECONVENTION_FORMAT, TBPMO_WD_FORMVIEW_DOKTYPES_GUID)
        Catch ex As Exception
            MsgBox("Error in load formatrules: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Sub Load_Controls()
        If loaded = False Then Exit Sub
        Try
            Dim sql = String.Format("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 = {0}) UNION ALL " & _
                    "SELECT [CONTROL_ID] , [CTRLSCR_CAPTION]  AS DISPLAY FROM [VWPMO_CONTROL_SCREEN] " & _
                    "WHERE CTRLTYPE_ID = 1 AND [CONTROL_SQLCOMMAND_1] <> '' and FORM_ID = (SELECT FORM_ID FROM TBPMO_FORM_VIEW WHERE GUID = {0}) " & _
                    "ORDER BY DISPLAY", FORM_VIEW_ID)
            Dim DT1 As DataTable = MYDB_ECM.GetDatatable(sql)
            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()
            Load_Format_Rules()
        End If
    End Sub

    Private Sub TBPMO_WD_NAMECONVENTION_FORMATBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBPMO_WD_NAMECONVENTION_FORMATBindingSource.AddingNew
        Try
            DD_DMSDataSet.TBPMO_WD_NAMECONVENTION_FORMAT.ADDED_WHOColumn.DefaultValue = USER_USERNAME
            DD_DMSDataSet.TBPMO_WD_NAMECONVENTION_FORMAT.IDColumn.DefaultValue = TBPMO_WD_FORMVIEW_DOKTYPES_GUID
        Catch ex As Exception
            MsgBox("Error in Adding Format Rules: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub ToolStripButtonSave_Click(sender As Object, e As EventArgs) Handles ToolStripButtonSave.Click
        Try
            Me.TBPMO_WD_NAMECONVENTION_FORMATBindingSource.EndEdit()
            If DD_DMSDataSet.TBPMO_WD_NAMECONVENTION_FORMAT.GetChanges Is Nothing = False Then
                Me.TBPMO_WD_NAMECONVENTION_FORMATTableAdapter.Update(Me.DD_DMSDataSet.TBPMO_WD_NAMECONVENTION_FORMAT)
                Load_Format_Rules()
            End If
        Catch ex As Exception
            MsgBox("Error in Save Format Rules: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btnAddDivers_Click(sender As Object, e As EventArgs) Handles btnAddDivers.Click
        If Not Me.cmbdivers.Text = "" Then
            'AREANLAGE-[%mARENR]-[%aKurzname]-I[%Version]
            Dim text As String
            Select Case cmbdivers.Text
                Case "Original Filename"
                    text = "[%vOFilename]"
                Case Else
                    text = "[%v" & Me.cmbdivers.Text.ToString.ToUpper & "]"
            End Select
            If Not txtnamenKonv.Text.Contains(text) Then
                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 If
    End Sub

    Private Sub txtnamenKonv_TextChanged(sender As Object, e As EventArgs) Handles txtnamenKonv.TextChanged
        lblSave.Visible = False
    End Sub
End Class