Imports System.Text
Imports System.IO

Public Class frmLanguage_Translator

    Private Sub btnclearFilter_Click(sender As Object, e As EventArgs) Handles btnclearFilter.Click
        cmbLanguage.SelectedIndex = -1
        cmbObjectgroup.SelectedIndex = -1
    End Sub
    Public Sub Clear_Grid_View()
        GridControlObjects.DataSource = Nothing
        TBPMO_LANGUAGE_OBJECTBindingSource.DataSource = Nothing
    End Sub
    Sub Load_GridData()
        Try
            Dim group As String
            Select Case cmbObjectgroup.Text
                Case "Constructor titles"
                    group = "CONSTR_MENUE"
                Case "Entity titles"
                    group = "FORMVIEW_TITLE"
                Case "Doctype titles"
                    group = "DOCTYPE_TITLE"
            End Select
            Dim sql = "select * from TBPMO_LANGUAGE_OBJECT where LANGUAGE_TYPE = '@LANGUAGE' AND PMO_OBJECT_NAME like '%@OBJECT%'"
            If cmbLanguage.SelectedIndex = -1 And cmbObjectgroup.SelectedIndex = -1 Then
                sql = "select * from TBPMO_LANGUAGE_OBJECT"
            ElseIf cmbLanguage.SelectedIndex <> -1 And cmbObjectgroup.SelectedIndex <> -1 Then
                sql = sql.Replace("@LANGUAGE", cmbLanguage.Text)
                sql = sql.Replace("@OBJECT", group)
            ElseIf cmbLanguage.SelectedIndex <> -1 And cmbObjectgroup.SelectedIndex = -1 Then
                sql = sql.Replace("@LANGUAGE", cmbLanguage.Text)
                sql = sql.Replace("AND PMO_OBJECT_NAME like '%@OBJECT%'", "")
            ElseIf cmbLanguage.SelectedIndex = -1 And cmbObjectgroup.SelectedIndex <> -1 Then
                sql = sql.Replace("LANGUAGE_TYPE = '@LANGUAGE' AND", "")
                sql = sql.Replace("@OBJECT", group)
            End If

            Dim DT_OBJ As DataTable = ClassDatabase.Return_Datatable(sql, "Load language objects")
            Clear_Grid_View()
            If Not IsNothing(DT_OBJ) Then
                TBPMO_LANGUAGE_OBJECTBindingSource1.DataSource = DT_OBJ
                GridControlObjects.DataSource = TBPMO_LANGUAGE_OBJECTBindingSource1
                TBPMO_LANGUAGE_OBJECTBindingNavigator.BindingSource = TBPMO_LANGUAGE_OBJECTBindingSource1
                ' --- Editoren laden für Combobox ---
                'Create_Grid_Editor(POS_ENTITY)
                'HideColumns_Pos(PARENT_RECORDID)
                For Each col As DevExpress.XtraGrid.Columns.GridColumn In grvwGridObjects.Columns
                    Dim column As DevExpress.XtraGrid.Columns.GridColumn = col

                Next

            End If

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

    End Sub

    Private Sub tsbtnRefresh_Grid_Click(sender As Object, e As EventArgs) Handles tsbtnRefresh_Grid.Click
        Load_GridData()
    End Sub

    Private Sub cmbLanguage_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbLanguage.SelectedIndexChanged

    End Sub

    Private Sub frmLanguage_Translator_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Save_POSGrid_Layout()
    End Sub

    Private Sub frmLanguage_Translator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Load_POSGrid_Layout()

    End Sub
    Sub Save_POSGrid_Layout()
        Try
            Dim Filename As String = String.Format("{0}-UserLayout.xml", grvwGridObjects.Name)
            Dim XMLPath = System.IO.Path.Combine(Application.UserAppDataPath(), Filename)
            grvwGridObjects.SaveLayoutToXml(XMLPath)
        Catch ex As Exception
            MsgBox("Error in Save_POSGrid_Layout:" & vbNewLine & ex.Message)
        End Try
    End Sub
    Sub Load_POSGrid_Layout()
        Try
            Dim Filename As String = String.Format("{0}-UserLayout.xml", grvwGridObjects.Name)
            Dim XMLPath = System.IO.Path.Combine(Application.UserAppDataPath(), Filename)
            If File.Exists(XMLPath) Then
                grvwGridObjects.RestoreLayoutFromXml(XMLPath)
            End If
        Catch ex As Exception
            MsgBox("Error in Load_POSGrid_Layout:" & vbNewLine & ex.Message)
        End Try
    End Sub

    Private Sub tsbtnExcel_Click(sender As Object, e As EventArgs) Handles tsbtnExcel.Click
        Dim saveFileDialog1 As New SaveFileDialog
        saveFileDialog1.Filter = "Excel File|*.xlsx"
        saveFileDialog1.Title = "Export to Excel:"
        saveFileDialog1.ShowDialog()
        If saveFileDialog1.FileName <> "" Then
            Cursor = Cursors.WaitCursor
            GridControlObjects.MainView.ExportToXlsx(saveFileDialog1.FileName)
            Dim result As MsgBoxResult
            result = MessageBox.Show("Export file was created! Open the file right now?", "Exportresult:", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If result = MsgBoxResult.Yes Then
                Process.Start(saveFileDialog1.FileName)
            End If
        End If
        Cursor = Cursors.Default
    End Sub
    Public Function Get_Focused_Row_Cell_Value_pos(columnName As String)
        Return grvwGridObjects.GetFocusedRowCellValue(grvwGridObjects.Columns(columnName))
    End Function
    Private Sub grvwGridObjects_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles grvwGridObjects.CellValueChanged
        Try
            Dim column = e.Column.FieldName
            If e.Column.FieldName = "CAPTION" Or e.Column.FieldName = "HINT" Then
                Dim Grid_RecordID = Get_Focused_Row_Cell_Value_pos("GUID")
                If Grid_RecordID Is Nothing Then
                    ts_statelbl.Text = "GUID could not be read"
                    ts_statelbl.Visible = True
                    ts_statelbl.BackColor = Color.Red
                    Exit Sub
                End If
                Dim upd = String.Format("UPDATE TBPMO_LANGUAGE_OBJECT SET {3} = '{0}', CHANGED_WHO = '{1}' WHERE GUID = {2}", e.Value, Environment.UserName, Grid_RecordID, e.Column.FieldName)
                If ClassDatabase.Execute_non_Query(upd) = True Then
                    ts_statelbl.Text = "Row/ID (" & Grid_RecordID.ToString & ") Updated - " & Now
                    ts_statelbl.BackColor = Color.Yellow
                    ts_statelbl.Visible = True
                Else
                    ts_statelbl.Text = "Error in updating value - Check Logfile"
                    ts_statelbl.Visible = True
                    ts_statelbl.BackColor = Color.Red
                End If
            Else
                ts_statelbl.Visible = False
            End If
        Catch ex As Exception
            MsgBox("Error in grvwGridObjects_CellValueChanged:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
End Class