Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Views.Grid

Public Class frmAdmin_Doctype

    Public Property HasChanges As Boolean = False
    Public Property IsInsert As Boolean = False
    Public Property PrimaryKey As Integer
    Dim SelSystitle As String
    Dim SelLanguage As String
    Dim SelChangedTerm As String

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

        TBIDB_LANGUAGETableAdapter.Connection.ConnectionString = My.DatabaseIDB.CurrentSQLConnectionString
        Me.TBIDB_LANGUAGETableAdapter.Fill(Me.DSIDB_Stammdaten.TBIDB_LANGUAGE)
        For Each orow As DataRow In DSIDB_Stammdaten.TBIDB_LANGUAGE
            ComboBoxEditLanguage.Properties.Items.Add(orow.Item("LANG_CODE"))
        Next
        Load_Doctypes()
    End Sub
    Sub Load_Doctypes()
        Try
            VWIDB_DOCTYPE_LANGUAGETableAdapter.Connection.ConnectionString = My.DatabaseIDB.CurrentSQLConnectionString
            Me.VWIDB_DOCTYPE_LANGUAGETableAdapter.Fill(Me.DSIDB_Stammdaten.VWIDB_DOCTYPE_LANGUAGE)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        pnlAddDoctype.Visible = True
        BarButtonItemCancel.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
        BarButtonItem1.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        BarButtonItemDTsave.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
    End Sub

    Public Sub New(PrimaryKey As Integer)
        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        Me.PrimaryKey = PrimaryKey
        Me.IsInsert = IsInsert
    End Sub

    Private Sub BarButtonItemCancel_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemCancel.ItemClick
        pnlAddDoctype.Visible = True
        BarButtonItemCancel.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        BarButtonItem1.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
        BarButtonItemDTsave.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
    End Sub

    Private Sub BarButtonItemDTsave_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemDTsave.ItemClick
        If NewUpdateDoctype(TextEditDoctypeSys.EditValue, TextEditDoctypeTranslate.EditValue, ComboBoxEditLanguage.EditValue) = True Then
            ShowStatus("Sys Doctype successfully added/updated", Color.DodgerBlue)
        End If
    End Sub
    Private Function NewUpdateDoctype(DoctypeSys As String, Term As String, Language As String) As Boolean
        Try
            Dim oSQL = $"EXEC PRIDB_NEW_DOCTYPE '{DoctypeSys}','{Term}','{My.Application.User.UserName}','{Language}'"

            If My.DatabaseIDB.ExecuteNonQuery(oSQL) = True Then
                Load_Doctypes()
                Return True
            End If
        Catch ex As Exception

        End Try
    End Function
    Private Sub ShowStatus(Message As String, ocolor As Color)
        labelStatus.Caption = $"{Message} - {Now.ToString}"
        labelStatus.ItemAppearance.Normal.BackColor = ocolor
        labelStatus.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
    End Sub

    Private Sub GridView1_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles GridView1.CellValueChanged
        Dim oView As GridView = sender
        Dim cellValue As String = e.Value.ToString()
        SelChangedTerm = cellValue
        BarButtonItemSaveChange.Enabled = True
    End Sub

    Private Sub GridView1_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles GridView1.FocusedRowChanged
        Dim oView As GridView = sender
        SelSystitle = oView.GetFocusedRowCellValue(oView.Columns("SystemTitle")).ToString()
        SelLanguage = oView.GetFocusedRowCellValue(oView.Columns("Language")).ToString()
        BarButtonItemSaveChange.Enabled = False
    End Sub

    Private Sub BarButtonItemSaveChange_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSaveChange.ItemClick
        If NewUpdateDoctype(SelSystitle, SelChangedTerm, SelLanguage) = True Then
            ShowStatus("Doctype Translation successfully added/updated", Color.DodgerBlue)
        End If
    End Sub
End Class