
Imports IntraSell_DLL
Imports IntraSell_Net_Base

Public Class ArtikelKategorieControl

    Dim _ArtKatNr As Integer
    Dim loading As Boolean
    Dim SQL As String = "SELECT artkatnr, name from `grArtikel-Kategorien` order by name asc"

    ''' <summary>
    ''' wenn mit dem '+' der user einen neue Kategorie eingefügt hat 
    ''' </summary>
    ''' <param name="ArtKatNr"></param>
    ''' <remarks></remarks>
    Public Event ArtKatNrAdded(ArtKatNr As Integer) '
    ''' <summary>
    ''' wenn der User eine neue Kategorie gewählt hat 
    ''' </summary>
    ''' <param name="ArtKatNr"></param>
    ''' <remarks></remarks>
    Public Event ArtKatNrChanged2(ArtKatNr As Integer) '

    Dim _zustandSpeichern = False
    Public Property ZustandSpeichern As Boolean
        Get
            Return _zustandSpeichern
        End Get
        Set(value As Boolean)
            _zustandSpeichern = value
            ZustandLaden()
        End Set
    End Property

    Public Property ArtKatNr() As Integer
        Get
            Return _ArtKatNr
        End Get
        Set(value As Integer)
            _ArtKatNr = value
            loading = True
            ArtKatNrComboBox.SelectedValue = _ArtKatNr
            If ArtKatNrComboBox.Items.Count = 0 Then
                ReloadData()
            End If
            loading = False
        End Set
    End Property

    Public Property ShowAddNew() As Boolean
        Get
            Return AddNewButton.Visible
        End Get
        Set(value As Boolean)
            AddNewButton.Visible = value
        End Set
    End Property

    Private Sub ArtikelComboBox_DropDown(sender As Object, e As EventArgs) Handles ArtKatNrComboBox.DropDown
        ReloadData()
    End Sub

    Private Sub ReloadData()
        Try
            loading = True
            FillComboBox(ArtKatNrComboBox, SQL, "Name", "ArtKatNr")
            ArtKatNrComboBox.Text = ""
            ArtKatNrComboBox.SelectedValue = _ArtKatNr
            ZustandLaden()
        Catch ex As Exception
        Finally
            loading = False
        End Try
    End Sub

    Private Sub ArtKatNrComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ArtKatNrComboBox.SelectedIndexChanged
        Try
            If loading Then Exit Sub
            ArtKatNr = ArtKatNrComboBox.SelectedValue
            If Not loading Then
                If ZustandSpeichern Then
                    SetVarValue("FILTER_ARTKATNR", ArtKatNr)
                End If
                RaiseEvent ArtKatNrChanged2(ArtKatNr)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub AddNewButton_Click(sender As Object, e As EventArgs) Handles AddNewButton.Click
        Try
            ArtikelKategorien.MdiParent = Main
            Try
                ArtikelKategorien.ShowDialog()
            Catch ex As Exception
                ArtikelKategorien.Show()
                ArtikelKategorien.TopMost = True
            End Try

        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ArtKatNrComboBox_TextChanged(sender As Object, e As EventArgs) Handles ArtKatNrComboBox.TextUpdate

        Try
            If ArtKatNrComboBox.Text = "" Then
                ArtKatNr = -1
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try

    End Sub

    Private Sub ArtKatNrComboBox_Validated(sender As Object, e As EventArgs) Handles ArtKatNrComboBox.Validated
        Try
            Dim KateName As String = ArtKatNrComboBox.Text

            If KateName = "" Then
                ArtKatNr = -1
                Return
            End If

            Dim exists = -1
            Try
                Dim cmd As New MySql.Data.MySqlClient.MySqlCommand("SELECT artkatnr from `grArtikel-Kategorien` where name like @KatName")
                cmd.Parameters.AddWithValue("KatName", KateName)
                exists = FirstRow(cmd)
            Catch
                'Ignore
            End Try

            If IsNumeric(exists) AndAlso exists > 0 Then
                'RaiseEvent ArtKatNrChanged(exists)
            Else
                If MsgBox("Wollen Sie eine neue Kategorie '" & KateName & "' anlegen?", vbYesNo) = vbYes Then
                    Dim neueArtKatNr = GetArtKatNr(KateName)
                    ReloadData()
                    ArtKatNr = neueArtKatNr
                    RaiseEvent ArtKatNrAdded(neueArtKatNr)
                End If
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try

    End Sub

    Private Sub ZustandLaden()
        Try
            If ZustandSpeichern Then
                If ExistsVarValue("FILTER_ARTKATNR") Then
                    If ArtKatNr = 0 Then
                        ArtKatNr = VarValue("FILTER_ARTKATNR")

                        RaiseEvent ArtKatNrChanged2(ArtKatNr)

                    End If
                End If
            End If
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

End Class
