Imports Microsoft.Office.Interop
Imports DevExpress.XtraEditors.Repository
Imports System.IO

Public Class frmBestellung
    Dim FormLoaded As Boolean = False
    Dim vMarkt, vSeriennummer, vGeraet As String
    Dim vGeraete_ID As Integer
    Sub Save_GridLayout_Geraete()
        Dim XMLPath = ClassHelper.GetSelectionGridName("GridViewGeraeteBestellung")
        GridControlGeraete.MainView.SaveLayoutToXml(XMLPath)
    End Sub
    Sub Load_GridLayout_Geraete()
        Try
            Dim XMLPath = ClassHelper.GetSelectionGridName("GridViewGeraeteBestellung")
            If File.Exists(XMLPath) Then
                GridControlGeraete.MainView.RestoreLayoutFromXml(XMLPath)
                GridViewGeraete.ClearColumnsFilter()
                'grvwSelection.ClearGrouping()
                GridViewGeraete.ClearSelection()
                GridViewGeraete.OptionsView.ShowPreview = False
            End If
        Catch ex As Exception

        End Try

    End Sub
    Private Sub FMBestellung_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            Save_GridLayout_Geraete()
            FormLoaded = False
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Speichern der Formulardaten:")
        End Try
    End Sub

    Private Sub FMBestellung_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            TBSUPPLY_BESTELLUNGTableAdapter.Connection.ConnectionString = My.Settings.ConfigConnectionString
            TBWH_MARKTTableAdapter.Connection.ConnectionString = My.Settings.ConfigConnectionString
            TBSUPPLY_BESTELLUNGViewTableAdapter.Connection.ConnectionString = My.Settings.ConfigConnectionString
            TBWH_SUPPLIES_MARKTTableAdapter.Connection.ConnectionString = My.Settings.ConfigConnectionString
            Me.TBWH_MARKTTableAdapter.Fill(Me.MyDataset.TBWH_MARKT)
            '  _Datagrid.LoadC1_DG(DGFilterMarkt, Application.UserAppDataPath.ToString & "\config\Datagrid\", "DGFilterMarkt.xml")
            Load_GridLayout_Geraete()

            rbSerienNr.Checked = True
            Me.lblStatus.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Laden der Formulardaten:")
        End Try
    End Sub
    Sub Load_Datagrid(ByVal _Datagrid As DataGridView, ByVal _settings_String As String)
        Dim settings() As String
        If My.Settings(_settings_String) <> String.Empty Then
            settings = My.Settings(_settings_String).Split("$"c)
            For idx As Integer = 0 To _Datagrid.Columns.Count - 1
                Dim sv() As String = settings(idx).Split("/"c)
                _Datagrid.Columns(idx).DisplayIndex = CInt(sv(0))
                _Datagrid.Columns(idx).Width = CInt(sv(1))
            Next
        End If
    End Sub
    Sub Save_Datagrid(ByVal _Datagrid As DataGridView, ByVal _settings_String As String)
        Dim settings As String = String.Empty
        For idx As Integer = 0 To _Datagrid.Columns.Count - 1
            settings += _Datagrid.Columns(idx).DisplayIndex.ToString()
            settings += "/"
            settings += _Datagrid.Columns(idx).Width.ToString
            settings += "$"
        Next
        My.Settings(_settings_String) = settings
    End Sub
    Sub Refresh_Supplies()
        Try
            If vGeraete_ID <> 0 Then
                Me.TBWH_SUPPLIES_MARKTTableAdapter.FillByGeraet(MyDataset.TBWH_SUPPLIES_MARKT, vGeraete_ID)
                If MyDataset.TBWH_SUPPLIES_MARKT.Rows.Count = 0 Then
                    btninsertOrder.Enabled = False
                Else
                    btninsertOrder.Enabled = True
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler bei Abrufen der Supplies:")
        End Try
    End Sub
    Public Sub Clear_Grid_View()
        GridControlGeraete.DataSource = Nothing
        If GridViewGeraete.Columns.Count > 0 Then
            GridViewGeraete.Columns.Clear()
        End If

    End Sub
    Sub Refresh_Devices()
        'If rbSerienNr.Checked Then
        If FormLoaded = True Then
            Try
                Me.Cursor = Cursors.WaitCursor
                Dim sql = "select GERAET_ID as [Device-ID], IP_ADRESSE as IP,VERTRAGSNR AS Vertrag, BEZEICHNUNG AS Model, SERIENNR as Serial, MARKT as Markt, MODEL_NR as Model, Mandant, MANDANT_KZ AS [Mandant-KZ] from VWGERAET where AKTIV_REFLISTE = 1"
                Dim DT As DataTable = ClassDatabase.Return_Datatable(sql, True)
                If Not IsNothing(DT) Then

                    'Grid leeren
                    Clear_Grid_View()
                    'Databinding Neu
                    VWGERAETBindingSource.DataSource = DT
                    GridControlGeraete.DataSource = VWGERAETBindingSource


                    '' Den Editor Initialisieren und Optionen setzen
                    'Dim CheckBoxEditorForDisplay = New RepositoryItemCheckEdit()
                    'CheckBoxEditorForDisplay.ValueChecked = 1
                    'CheckBoxEditorForDisplay.ValueUnchecked = 0
                    '' Alle Checkbox Spalten durchgehen und CheckBoxEditor zuweisen
                    'For Each col As String In listcheck
                    '    grvwGrid.GridControl.RepositoryItems.Add(CheckBoxEditorForDisplay)

                    '    If Not IsNothing(grvwGrid.Columns(col)) Then
                    '        grvwGrid.Columns(col).ColumnEdit = CheckBoxEditorForDisplay
                    '    End If
                    'Next
                End If
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler bei Refresh_geraetebyMarkt:")
            End Try
            Me.Cursor = Cursors.Default
        End If
        'End If
    End Sub

    Private Sub FMBestellung_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        FormLoaded = True
        Refresh_Devices()
        Me.BringToFront()
    End Sub
    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        Select Case TabControl1.SelectedIndex
            Case 1
                vMarkt = Me.MARKTTextBox.Text
                Refresh_data()
        End Select
    End Sub


    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        vMarkt = Me.MARKTTextBox.Text
        Refresh_data()
    End Sub
    Private Sub Refresh_data()
        Try
            Me.TBSUPPLY_BESTELLUNGViewTableAdapter.FillByManuellMArkt(Me.MyDataset.VWSUPPLY_BESTELLUNG_FRONTEND, vMarkt)
        Catch ex As System.Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler bei Refresh Data:")
        End Try
    End Sub
    Private Sub TBSUPPLY_BESTELLUNGDataGridView_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        If Me.MARKTTextBox.Text = String.Empty Then
            MsgBox("Bitte wählen Sie einen Markt aus!", MsgBoxStyle.Exclamation, "Achtung:")
            Exit Sub
        End If
        If Me.TBWH_MARKTBindingSource.Count >= 1 Then
            Try
                vMarkt = Me.MARKTTextBox.Text
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Speichern des Marktes:")
            End Try
        End If
    End Sub
    Private Sub rbVASPAM_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbVASPAM.CheckedChanged
        If rbVASPAM.Checked = True Then
            lblOrderNr.Visible = True
            txtVASNR.Visible = True
            Me.dtpBestelldatum.Visible = True
            lblBestelldatum.Visible = True
        Else
            lblOrderNr.Visible = False
            txtVASNR.Visible = False
            Me.dtpBestelldatum.Visible = False
            lblBestelldatum.Visible = False
        End If

    End Sub
    Private Sub ToolStripButton6_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton6.Click
        Refresh_Devices()
    End Sub
    Private Sub cmbSupplies_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbSupplies.SelectedIndexChanged
        If cmbSupplies.SelectedIndex <> -1 Then
            Me.txtMenge.Focus()
        End If
    End Sub

    Private Sub txtMenge_GotFocus(sender As Object, e As EventArgs) Handles txtMenge.GotFocus
        txtMenge.BackColor = Color.Lime
    End Sub

    Private Sub txtMenge_LostFocus(sender As Object, e As EventArgs) Handles txtMenge.LostFocus
        txtMenge.BackColor = Color.White
    End Sub

    Private Sub btninsertOrder_Click(sender As Object, e As EventArgs) Handles btninsertOrder.Click
        Try
            Me.lblStatus.Visible = False
            If txtMenge.Text <> "" And cmbSupplies.SelectedIndex <> -1 Then
                If rbVASPAM.Checked Then
                    If txtVASNR.Text <> "" Then
                        TBSUPPLY_BESTELLUNGTableAdapter.cmdInsertManuell_MBestellnr(cmbSupplies.Text, txtMenge.Text, vMarkt, Environment.UserName, vGeraet, vSeriennummer, txtVASNR.Text, vGeraete_ID, cmbSupplies.SelectedValue, dtpBestelldatum.Text, "MANUALLY_VAS#")
                        Me.lblStatus.Visible = True
                        Me.txtMenge.Text = ""
                        Me.txtVASNR.Text = ""
                    End If
                Else
                    TBSUPPLY_BESTELLUNGTableAdapter.CmdInsert_Manuell_Serial(cmbSupplies.Text, txtMenge.Text, vMarkt, Environment.UserName, vGeraet, vSeriennummer, vGeraete_ID, cmbSupplies.SelectedValue)
                    Me.lblStatus.Visible = True
                    Me.txtMenge.Text = ""
                    Me.txtVASNR.Text = ""
                End If
                Me.lblStatus.Text = "Bestellung: " & cmbSupplies.Text & vbNewLine & "Menge: " & txtMenge.Text & " wurde eingepflegt - " & Now
            End If
        Catch ex As Exception
            MsgBox("Fehler bei manueller Bestellung: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Me.lblStatus.Visible = False
        End Try
    End Sub

    Private Sub txtVASNR_GotFocus(sender As Object, e As EventArgs) Handles txtVASNR.GotFocus
        txtVASNR.BackColor = Color.Lime
    End Sub

    Private Sub txtVASNR_LostFocus(sender As Object, e As EventArgs) Handles txtVASNR.LostFocus
        txtVASNR.BackColor = Color.White
    End Sub

    Private Sub rbSerienNr_CheckedChanged(sender As Object, e As EventArgs) Handles rbSerienNr.CheckedChanged

    End Sub
    Private Sub GridViewGeraete_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles GridViewGeraete.FocusedRowChanged
        Try
            If FormLoaded = True Then
                If rbSerienNr.Checked Or rbVASPAM.Checked Then
                    If VWGERAETBindingSource.Count >= 1 Then
                        Dim Geraet_id = GridViewGeraete.GetFocusedRowCellValue(GridViewGeraete.Columns("Device-ID"))
                        If Not IsNothing(Geraet_id) Then
                            vGeraete_ID = Geraet_id
                            vSeriennummer = GridViewGeraete.GetFocusedRowCellValue(GridViewGeraete.Columns("Serial"))
                            vGeraet = GridViewGeraete.GetFocusedRowCellValue(GridViewGeraete.Columns("Model"))
                            vMarkt = GridViewGeraete.GetFocusedRowCellValue(GridViewGeraete.Columns("Markt"))
                            Refresh_Supplies()
                        Else
                            vGeraete_ID = 0
                        End If
                    Else
                        vGeraete_ID = 0
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler bei TBGERAETBindingSource.PositionChanged:")
        End Try
    End Sub
End Class