' 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_impression
    Public preview As Boolean

    Private Sub Etiquettes_produits_impression_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim quantité_restante = 0
        For Each etiquette In etiquettes
            quantité_restante = quantité_restante + etiquette.quantité
        Next
        Dim quantité_sur_planche = 0

        For Each etiquette In etiquettes
            If quantité_sur_planche + etiquette.quantité > 48 Then
                print_planche()
                quantité_sur_planche = 0
            End If
            quantité_sur_planche = quantité_sur_planche + etiquette.quantité
            For qtt = 1 To etiquette.quantité


                Dim Etiquette_control As New Etiquette_control
                If Not Etiquettes_produits.PictureBox1.Image Is Nothing Then
                    Etiquette_control.Panel1.BackgroundImage = Etiquettes_produits.PictureBox1.Image
                    Etiquette_control.Panel1.BackgroundImageLayout = ImageLayout.Stretch
                End If

                Etiquette_control.Prod_Label.Text = etiquette.produit
                Etiquette_control.PPTTC_Label.Text = etiquette.PPTTC & "€"
                Etiquette_control.Info.Text = etiquette.Info
                FlowLayoutPanel1.Controls.Add(Etiquette_control)
            Next

        Next
        If preview = False Then
            print_planche()
            Me.Close()
        End If
       
    End Sub
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim b As New Bitmap(Panel1.Width, Panel1.Height)
        Panel1.DrawToBitmap(b, Panel1.ClientRectangle)
        Dim destRect As New Rectangle(0, 0, 760, 937)
        e.Graphics.DrawImage(b, destRect)
    End Sub

    Private Sub print_planche()
        Application.DoEvents()
        PrintDialog1.Document = PrintDocument1
        If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            PrintDocument1.Print()
        End If
        FlowLayoutPanel1.Controls.Clear()
        Application.DoEvents()
    End Sub
 
End Class