' 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 Etiquettes_produits

    Public Structure etiquette
        Dim produit As String
        Dim PPTTC As String
        Dim Info As String
        Dim quantité As Integer
    End Structure

   
    Public Sub affiche_etiquettes()

        If Not etiquettes Is Nothing Then
            For Each etiquette In etiquettes
                DataGridView1.Rows.Add()
                DataGridView1.Item(Produit.Index, DataGridView1.Rows.Count - 2).Value = etiquette.produit
                DataGridView1.Item(PPTTC.Index, DataGridView1.Rows.Count - 2).Value = etiquette.PPTTC
                DataGridView1.Item(Info.Index, DataGridView1.Rows.Count - 2).Value = etiquette.Info
                DataGridView1.Item(Quantité.Index, DataGridView1.Rows.Count - 2).Value = etiquette.quantité
            Next
        End If
       
    End Sub

 






    Private Sub DataGridView1_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValidated
        For r = 0 To DataGridView1.Rows.Count - 1
            Try
                Dim x = CInt(DataGridView1.Item(Quantité.Index, r).Value)
                If x > 48 Then
                    MyMsg_Box.Presentation("Officine!", "48 maximum", False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()
                    DataGridView1.Item(Quantité.Index, r).Value = 48
                End If
            Catch ex As Exception
                MyMsg_Box.Presentation("Officine!", "Verifier Quantités", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                Exit Sub
            End Try
        Next
        etiquettes = New List(Of etiquette)
        For r = 0 To DataGridView1.Rows.Count - 1
            If Not DataGridView1.Item(Quantité.Index, r) Is Nothing Then
                If Not DataGridView1.Item(Quantité.Index, r).Value Is Nothing Then
                    If IsNumeric(DataGridView1.Item(Quantité.Index, r).Value) Then
                        If Val(DataGridView1.Item(Quantité.Index, r).Value) > 0 Then
                            Dim etiq As New etiquette
                            etiq.produit = DataGridView1.Item(Produit.Index, r).Value
                            etiq.PPTTC = DataGridView1.Item(PPTTC.Index, r).Value
                            etiq.Info = DataGridView1.Item(Info.Index, r).Value
                            etiq.quantité = CInt(DataGridView1.Item(Quantité.Index, r).Value)
                            etiquettes.Add(etiq)
                        End If
                    End If
                End If
            End If
        Next

        '    affiche_etiquettes()
    End Sub

    Private Sub Etiquettes_produits_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        affiche_etiquettes()
    End Sub

  

    Private Sub LinkLabel2_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
        Etiquettes_produits_impression.Close()
        Etiquettes_produits_impression.preview = False
        Etiquettes_produits_impression.Visible = True
        Me.Close()
    End Sub

    Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        OpenFileDialog1.InitialDirectory = Application.StartupPath + "\images\etiquettes\"
        OpenFileDialog1.ShowDialog()
        If OpenFileDialog1.FileName <> "" Then
            Try
                PictureBox1.ImageLocation = OpenFileDialog1.FileName
                PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
            Catch ex As Exception
                PictureBox1.Image = Nothing
            End Try
        Else
            PictureBox1.Image = Nothing
        End If
    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

    Private Sub LinkLabel3_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel3.LinkClicked
        Etiquettes_produits_impression.Close()
        Etiquettes_produits_impression.preview = True
        Etiquettes_produits_impression.Visible = True
    End Sub
End Class