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

    Public NumLot As String
    Public Date_lot As String


    Private Sub SCOR_Traitement_Papier_Load(sender As Object, e As EventArgs) Handles Me.Load
        Me.Text = "PIECES CONSTITUTIVES DU LOT " & NumLot

        Dim fact_table = Teletransmission_MAIN.FacturesTableAdapter1.GetDataBy_Ref_Lot(NumLot, Date_lot, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
        For r = 0 To fact_table.Rows.Count - 1
            Dim file = Dir(Config.LocalNetwork_ServerName & "\scor\doc\" & fact_table.Item(r).G1110C1 & "_*_*.pdf")
            While file <> ""
                CheckedListBox1.Items.Add(file)
                file = Dir()

            End While
        Next
    End Sub

    Private Sub CheckedListBox1_DoubleClick(sender As Object, e As EventArgs) Handles CheckedListBox1.DoubleClick
        Process.Start(Config.LocalNetwork_ServerName & "\scor\doc\" + CheckedListBox1.Items.Item(CheckedListBox1.SelectedIndex).ToString)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         Bandeau_info_bas_show("Impression des pièces...")
        Dim nbr = 0
        Dim result As String = ""
        For Each itemChecked In CheckedListBox1.CheckedItems
            Try

            
            nbr = nbr + 1
                Bandeau_info_bas_change_text("Impression des pièces... (" & nbr.ToString & "/" & CheckedListBox1.CheckedItems.Count.ToString & ")")
            Application.DoEvents()
            Dim objStartInfo As New ProcessStartInfo
            Dim objProcess = New System.Diagnostics.Process

            ' set start info properties 
            With objStartInfo
                .CreateNoWindow = True
                .FileName = "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe"
                .Arguments = "/t  " & Config.LocalNetwork_ServerName & "\scor\doc\" & itemChecked.ToString 'C:\test.pdf" ' Microsoft XPS Document Writer"
                .UseShellExecute = False
                '.WindowStyle = ProcessWindowStyle.Hidden 
                .ErrorDialog = True
                .RedirectStandardError = True
            End With
            objProcess = Process.Start(objStartInfo)
         

            'wait for process to print 
            System.Threading.Thread.Sleep(5000)

            'kill processes called AcroRd32 
            Dim myprocesses As Process() = System.Diagnostics.Process.GetProcessesByName("AcroRd32")
            For Each myproces As Process In myprocesses
                myproces.Kill()
                Next
            Catch ex As Exception
                result = result & itemChecked.ToString & vbCrLf
            End Try
        Next
        Bandeau_info_bas_hide()'Bandeau_info_bas_show_thread)
        If result <> "" Then
            MyMsg_Box.Presentation("Pièces non imprimées:", result, False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        End If

    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        For i = 0 To CheckedListBox1.Items.Count - 1
            CheckedListBox1.SetItemChecked(i, CheckBox1.Checked)
        Next
    End Sub
End Class