' Copyright (C) 2025  P4pillon.org
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU Affero General Public License as
' published by the Free Software Foundation, either version 3 of the
' License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU Affero General Public License for more details.
'
' You should have received a copy of the GNU Affero General Public License
' along with this program.  If not, see <https://www.gnu.org/licenses/>.
Public Class Auto_Réassort_Liste

    Private Sub Button1_Click(sender As Object, e As EventArgs)
        afficher()
    End Sub

    Private Sub afficher()
        DataGridView.Rows.Clear()
        Dim data = BDD.ProduitsTableAdapter.GetData_autoRéassort_Liste
        For r = 0 To data.Rows.Count - 1
            If (RadioButton1.Checked = True Or (data(r).Réassort_auto_seuil_min > data(r).Stock And data(r).Réassort_auto_seuil_max > data(r).Stock)) Then
                If (data(r).EAN13 = Tri_Produit_TextBox.Text Or data(r).CIP = Tri_Produit_TextBox.Text Or data(r).CAB = Tri_Produit_TextBox.Text Or _
                    InStr(data(r).Designation, Tri_Produit_TextBox.Text) <> 0 Or InStr(data(r).Designation, UCase(Tri_Produit_TextBox.Text)) <> 0) Then
                    If data(r).Répartiteur_principal = Fournisseur_MyCombobox.Text Or Fournisseur_MyCombobox.Text = "" Then
                        DataGridView.Rows.Add()
                    End If
                End If
            End If
        Next
        Dim current_index = 0
        For r = 0 To data.Rows.Count - 1
            If (RadioButton1.Checked = True Or (data(r).Réassort_auto_seuil_min > data(r).Stock And data(r).Réassort_auto_seuil_max > data(r).Stock)) Then
                If (data(r).EAN13 = Tri_Produit_TextBox.Text Or data(r).CIP = Tri_Produit_TextBox.Text Or data(r).CAB = Tri_Produit_TextBox.Text Or _
                    InStr(data(r).Designation, Tri_Produit_TextBox.Text) <> 0 Or InStr(data(r).Designation, UCase(Tri_Produit_TextBox.Text)) <> 0) Then
                    If data(r).Répartiteur_principal = Fournisseur_MyCombobox.Text Or Fournisseur_MyCombobox.Text = "" Then
                        DataGridView.Item(Désignation.Index, current_index).Value = data(r).Designation
                        DataGridView.Item(EAN13.Index, current_index).Value = data(r).EAN13
                        DataGridView.Item(CIP.Index, current_index).Value = data(r).CIP
                        DataGridView.Item(CAB.Index, current_index).Value = data(r).CAB
                        DataGridView.Item(Stock.Index, current_index).Value = data(r).Stock
                    DataGridView.Item(Mini.Index, current_index).Value = data(r).Réassort_auto_seuil_min
                    DataGridView.Item(Maxi.Index, current_index).Value = data(r).Réassort_auto_seuil_max
                    DataGridView.Item(Qtté.Index, current_index).Value = 0
                    If data(r).Réassort_auto_seuil_min > data(r).Stock And data(r).Réassort_auto_seuil_max > data(r).Stock Then
                        DataGridView.Item(Qtté.Index, current_index).Value = data(r).Réassort_auto_seuil_max - data(r).Stock
                    End If
                    DataGridView.Item(Fournisseur.Index, current_index).Value = data(r).Répartiteur_principal
                    DataGridView.Item(Commande_Auto.Index, current_index).Value = data(r).Réassort_auto

                    DataGridView.Item(Commande.Index, current_index).ReadOnly = True
                    If DataGridView.Item(Qtté.Index, current_index).Value > 0 Then
                        If DataGridView.Item(Commande_Auto.Index, current_index).Value = True And DataGridView.Item(Fournisseur.Index, current_index).Value <> "" Then
                            DataGridView.Item(Commande.Index, current_index).Value = True
                            DataGridView.Item(Commande.Index, current_index).ReadOnly = False
                            DataGridView.Item(Commande.Index, current_index).Style.BackColor = Color.Green
                        Else
                            DataGridView.Item(Commande.Index, current_index).Style.BackColor = Color.Orange
                        End If
                    End If
                    current_index = current_index + 1
                End If
                End If
            End If
        Next
    End Sub

    Private Sub DataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView.CellContentClick
        If e.RowIndex < 0 Then Exit Sub
        Select Case e.ColumnIndex
            Case Désignation.Index
                Dim produits_form As New Produits
                ' Produits.Visible = True
                If DataGridView.Item(EAN13.Index, e.RowIndex).Value <> "" Then
                    produits_form.Recherche_TextBox.Text = DataGridView.Item(EAN13.Index, e.RowIndex).Value
                ElseIf DataGridView.Item(CAB.Index, e.RowIndex).Value <> "" Then
                    produits_form.Recherche_TextBox.Text = DataGridView.Item(CAB.Index, e.RowIndex).Value
                ElseIf DataGridView.Item(CIP.Index, e.RowIndex).Value <> "" Then
                    produits_form.Recherche_TextBox.Text = DataGridView.Item(CIP.Index, e.RowIndex).Value
                Else
                    produits_form.Recherche_TextBox.Text = DataGridView.Item(Désignation.Index, e.RowIndex).Value
                End If
                produits_form.Recherche_TextBox_Validated(Nothing, Nothing)
                produits_form.ControlBox = False
                produits_form.ShowDialog()
                afficher()
                'Produits.BringToFront()
                'Produits.Select()
        End Select
    End Sub

    Private Sub OK_Button_Click(sender As Object, e As EventArgs) Handles OK_Button.Click
        Me.Close()
    End Sub

    Private Sub Tri_Produit_TextBox_TextChanged(sender As Object, e As EventArgs) Handles Tri_Produit_TextBox.TextChanged
        afficher()
    End Sub

    Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
        afficher()
    End Sub

    Private Sub Auto_Réassort_Liste_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Dim fournisseurs = BDD.FournisseurTableAdapter.GetData_OrderByNom
        Fournisseur_MyCombobox.Items.Clear()
        For r = 0 To fournisseurs.Rows.Count - 1
            If fournisseurs.Item(r).URL <> "" Then
                Fournisseur_MyCombobox.Items.Add(fournisseurs.Item(r).Nom)
            End If
        Next
        afficher()
    End Sub


    Private Sub Fournisseur_MyCombobox_TextChanged(sender As Object, e As EventArgs) Handles Fournisseur_MyCombobox.TextChanged
        afficher()
    End Sub

    Private Sub SuiviTraitement_Commandes_Button_Click(sender As Object, e As EventArgs) Handles SuiviTraitement_Commandes_Button.Click
        For r = 0 To DataGridView.Rows.Count - 1
            If DataGridView.Item(Commande.Index, r).Value = True Then

            End If
        Next
    End Sub

    Private Sub DataGridView_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView.CellValidated
        Select Case e.ColumnIndex
            Case Qtté.Index
                Try
                    CInt(DataGridView.Item(Qtté.Index, e.RowIndex).Value).ToString()
                Catch ex As Exception
                    DataGridView.Item(Qtté.Index, e.RowIndex).Value = 0
                End Try
        End Select
    End Sub

    'Private Sub DataGridView_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView.CellValueChanged
    '    Select Case e.ColumnIndex
    '        Case Qtté.Index
    '            If DataGridView.Item(Qtté.Index, e.RowIndex).Value Is Nothing Then Exit Sub
    '            Try
    '                CInt(DataGridView.Item(Qtté.Index, e.RowIndex).Value).ToString()
    '            Catch ex As Exception
    '                DataGridView.Item(Qtté.Index, e.RowIndex).Value = 0
    '            End Try
    '    End Select
    'End Sub
End Class