' 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/>.
Imports System.IO
Public Class DP_Traces_Patient

    Dim string_to_print As String
    Dim next_row__to_print As Integer
    Dim num_page As Integer

    Public Sub affiche_traces_patient()
        DataGridView1.Rows.Clear()

        Dim file = Dir(Config.LocalNetwork_ServerName & "\DP\traces\trace_fonc_" & DP.Identifiant_patient_function)
        If file <> "" Then
            Dim r = 0
            Dim reader = New StreamReader(Config.LocalNetwork_ServerName & "\DP\traces\trace_fonc_" & DP.Identifiant_patient_function)
            Dim Line = reader.ReadLine
            While Not Line Is Nothing
                DataGridView1.Rows.Add()
                DataGridView1.Item(0, r).Value = Mid(Line, 1, InStr(Line, "|") - 1)
                Line = Mid(Line, InStr(Line, "|") + 1)
                DataGridView1.Item(1, r).Value = Mid(Line, 1, InStr(Line, "|") - 1)
                Line = Mid(Line, InStr(Line, "|") + 1)
                DataGridView1.Item(2, r).Value = Mid(Line, 1, InStr(Line, "|") - 1)
                Line = Mid(Line, InStr(Line, "|") + 1)
                DataGridView1.Item(3, r).Value = Mid(Line, 1, InStr(Line, "|") - 1)
                Line = Mid(Line, InStr(Line, "|") + 1)
                DataGridView1.Item(4, r).Value = Line
                r = r + 1
                Line = reader.ReadLine
            End While
            reader.Close()

        Else
            MyMsg_Box.Presentation("Officine", "PAS DE TRACES DP POUR CE PATIENT.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        PrintDialog1.Document = PrintDocument1


        next_row__to_print = 0
        num_page = 1

        If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            string_to_print = "INTERVENTIONS REALISEES SUR LE DOSSIER PHARMACEUTIQUE" & Chr(10) & Chr(10) & _
                "Ce document décrit les opérations qui ont été réalisées sur le DP du patient depuis l'établissement où il a été édité." & Chr(10) & Chr(10) _
                & Now.ToLongDateString & "  " & Now.ToLongTimeString & Chr(10) _
                & CurrentBenéficiaire.G104C1 & " " & CurrentBenéficiaire.G104C3 & " " & CurrentBenéficiaire.G104C12 & Chr(10) _
                & CurrentBenéficiaire.G104C4 & " " & CurrentBenéficiaire.G104C5 & " " & CurrentBenéficiaire.G104C6 & " " & CurrentBenéficiaire.G104C7 & " " & CurrentBenéficiaire.G104C8 & Chr(10) & Chr(10) & _
                Config.Titulaire_raison_sociale & Chr(10) & Chr(10) & Chr(10)



            DP.Write_Trace_Fonc("Impression des traces des opérations DP", "", "", DP.Identifiant_patient_function)
            PrintDocument1.Print()
        Else
            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 drawFont As New Font("Century Gothic", 10)
        Dim drawBrush As New SolidBrush(Color.Black)
        If num_page = 1 Then
            e.Graphics.DrawString(string_to_print, drawFont, drawBrush, 10, 10)
        End If


        Dim nbr_tot_l As Integer
        nbr_tot_l = 0
        For r = next_row__to_print To DataGridView1.Rows.Count - 1
            If nbr_tot_l < 48 Then
                If DataGridView1.Item(Editer.Index, r).Value = True Then
                    nbr_tot_l = nbr_tot_l + 1
                    e.Graphics.DrawRectangle(Pens.Black, New Rectangle(25, 150 + (nbr_tot_l * 20), 155, 20))
                    e.Graphics.DrawString(DataGridView1.Item(0, r).Value, drawFont, Brushes.Black, New Point(27, 150 + (nbr_tot_l * 20) + 3))
                    e.Graphics.DrawRectangle(Pens.Black, New Rectangle(25 + 155, 150 + (nbr_tot_l * 20), 300, 20))
                    e.Graphics.DrawString(DataGridView1.Item(1, r).Value, drawFont, Brushes.Black, New Point(27 + 155, 150 + (nbr_tot_l * 20) + 3))
                    e.Graphics.DrawRectangle(Pens.Black, New Rectangle(25 + 155 + 300, 150 + (nbr_tot_l * 20), 300, 20))
                    e.Graphics.DrawString(DataGridView1.Item(2, r).Value, drawFont, Brushes.Black, New Point(27 + 155 + 300, 150 + (nbr_tot_l * 20) + 3))
                End If
                next_row__to_print = r
            Else
                Exit For
            End If

        Next
        If nbr_tot_l = 48 Then
            num_page = num_page + 1
            next_row__to_print = next_row__to_print + 1
            e.HasMorePages = True
        End If


    End Sub

    Private Sub DP_Traces_Patient_Load(sender As Object, e As EventArgs) Handles Me.Load
        affiche_traces_patient()
    End Sub
End Class