Imports System.Windows.Controls
Imports GrapeCity.ActiveReports.ReportsCore.Tools
Imports Microsoft.Office.Interop.Word
Imports VERAG_PROG_ALLGEMEIN

Public Class frmSprachen

    Dim SPRACHENBind As cEasyBinding
    Public table
    Public BIND_DB
    Public programId As Integer
    Private EditRow As Integer = -1
    Sub New(table, BIND_DB, programId)
        Me.table = table
        Me.BIND_DB = BIND_DB
        Me.programId = programId
        InitializeComponent()
    End Sub

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

        cbxSprachen.fillWithSQL("SELECT distinct([trs_sprache])  FROM [tblTranslate] WHERE trs_progId='" & programId & "' ORDER By [trs_sprache]", False, "FMZOLL", False)

        cbxSprachen.changeItem("EN")

        If cbxSprachen.SelectedIndex = -1 Then
            cbxSprachen.Items.Add("EN")
            cbxSprachen.Items.Add("TR")
            cbxSprachen.changeItem("EN")
        End If


        txtProgramm.fillWithSQL("SELECT [prog_codename] from [tblProgramme] WHERE [prog_id] ='" & programId & "'", "ADMIN")
        btnOK.Visible = True


    End Sub

    Private Sub cbxSprachen_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxSprachen.SelectedIndexChanged

        Dim sqlLanguage = " AND [trs_sprache] = '" & cbxSprachen.SelectedItem.ToString & "'"

        loadDGV(sqlLanguage)
    End Sub

    Private Sub loadDGV(Optional language As String = "", Optional sqlwhere As String = "")

        Try
            SPRACHENBind = New cEasyBinding(BIND_DB)

            Select Case language
                Case <> ""
                    If sqlwhere = "" Then

                        SPRACHENBind.initBinding("SELECT *  FROM " & table & " WHERE [trs_progId] = '" & programId & "'" & language, table)

                    Else

                        SPRACHENBind.initBinding("SELECT *  FROM " & table & " WHERE [trs_progId] = '" & programId & "'" & language & sqlwhere, table)
                    End If


                Case Else

                    If sqlwhere = "" Then

                        SPRACHENBind.initBinding("SELECT *  FROM " & table & "WHERE [trs_progId] = '" & programId & "'", table)
                    Else

                        SPRACHENBind.initBinding("SELECT *  FROM " & table & " WHERE [trs_progId] = '" & programId & "'" & sqlwhere, table)

                    End If

            End Select

            DataGridView.DataSource = SPRACHENBind.bindingdataTable

            With DataGridView

                If .Columns.Count > 0 Then
                    .Columns("trs_id").Visible = False
                    .Columns("trs_progId").Visible = False
                    .Columns("trs_progName").Visible = False
                    .Columns("trs_object").HeaderText = "Object"
                    .Columns("trs_object").Width = 152
                    .Columns("trs_control").HeaderText = "Control"
                    .Columns("trs_control").Width = 155
                    .Columns("trs_subControl").HeaderText = "Subcontrol"
                    .Columns("trs_subControl").Width = 100
                    .Columns("trs_sprache").HeaderText = "Spr"
                    .Columns("trs_sprache").Width = 40
                    .Columns("trs_text").HeaderText = "Übersetzung"
                    .Columns("trs_text").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                    .Columns("trs_textDE").HeaderText = "DE-Text"
                    .Columns("trs_textDE").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                    .Columns("trs_active").HeaderText = "aktiv"
                    .Columns("trs_active").Width = 40
                    .Columns("trs_firmaID").HeaderText = "Firma"
                    .Columns("trs_firmaID").Width = 40
                    .Columns("trs_option").HeaderText = "Option"
                    .Columns("trs_option").Width = 40
                End If

            End With



        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub TextBox1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles txtObject.PreviewKeyDown, txtDE.PreviewKeyDown, txtSubcontrol.PreviewKeyDown, txtControl.PreviewKeyDown, txttranslate.PreviewKeyDown, txtFirmaID.PreviewKeyDown, txtOption.PreviewKeyDown

        Dim sqladdon As String = ""

        If e.KeyCode = Keys.Enter Then
            If txtControl.Text <> "" Then
                sqladdon &= " AND [trs_control] like '%" & txtControl.Text & "%'"
            End If

            If txtObject.Text <> "" Then
                sqladdon &= " AND [trs_object] like '%" & txtObject.Text & "%'"
            End If

            If txtSubcontrol.Text <> "" Then
                sqladdon &= " AND [trs_subControl] like '%" & txtSubcontrol.Text & "%'"
            End If

            If txtDE.Text <> "" Then
                sqladdon &= " AND [trs_textDE] like '%" & txtDE.Text & "%'"
            End If

            If txttranslate.Text <> "" Then
                sqladdon &= " AND [trs_text] like '%" & txttranslate.Text & "%'"
            End If

            If txtOption.Text <> "" Then
                sqladdon &= " AND [trs_Option] like '%" & txtOption.Text & "%'"
            End If

            If txtFirmaID.Text <> "" Then
                sqladdon &= " AND [trs_firmaID] = '" & txtFirmaID.Text & "'"
            End If

            Dim sqlLanguage = " AND [trs_sprache] = '" & cbxSprachen.SelectedItem.ToString & "'"

            loadDGV(sqlLanguage, sqladdon)

        End If
    End Sub


    Private Sub DataGridView_DefaultValuesNeeded(sender As Object, e As DataGridViewRowEventArgs) Handles DataGridView.DefaultValuesNeeded
        Try

            e.Row.Cells("trs_progId").Value = programId
            e.Row.Cells("trs_progName").Value = txtProgramm.Text
            e.Row.Cells("trs_sprache").Value = cbxSprachen._value
            e.Row.Cells("trs_active").Value = True

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click

        If DataGridView.Columns.Count = 0 Or DataGridView.SelectedRows.Count = 0 Then 'alles gelöscht oder markierte Zeile gelöscht
            SPRACHENBind.updateBinding()
            Exit Sub
        End If

        If DataGridView.CurrentRow.Cells("trs_object").ToString <> "" AndAlso DataGridView.CurrentRow.Cells("trs_control").ToString <> "" AndAlso DataGridView.CurrentRow.Cells("trs_text").ToString <> "" AndAlso DataGridView.CurrentRow.Cells("trs_textDE").ToString <> "" AndAlso EditRow >= 0 Then
            SPRACHENBind.updateBinding()
        End If

    End Sub


    Private Sub DataGridView_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView.EditingControlShowing
        EditRow = DataGridView.CurrentRow.Index
    End Sub

    Private Sub DataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView.SelectionChanged
        If EditRow >= 0 Then
            Dim new_row As Integer = EditRow
            EditRow = -1
            DataGridView.CurrentCell = DataGridView.Rows(new_row).Cells(
                DataGridView.CurrentCell.ColumnIndex)
        End If
    End Sub

End Class