' 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 PDA_Détail

    Public num_PDA As Integer

    Private Sub PDA_Détail_Load(sender As Object, e As EventArgs) Handles Me.Load
        affichage()
    End Sub

    Private Sub affichage()
        Dim pda = BDD.PDATableAdapter.GetDataBy_Num(num_PDA).Item(0)
        Num_TextBox.Text = num_PDA.ToString
        Date_DateTimePicker.Value = pda._Date
        opérateur_TextBox.Text = pda.Opérateur
        Début_DateTimePicker.Value = pda.Date_Debut
        Fin_DateTimePicker.Value = pda.Date_Fin
        Lignes_DataGridView.Rows.Clear()
        Dim lignes = BDD.PDA_LignesTableAdapter.GetDataBy_Num_PDA(num_PDA)
        For r = 0 To lignes.Rows.Count - 1
            Lignes_DataGridView.Rows.Add()
        Next
        For r = 0 To lignes.Rows.Count - 1
            Lignes_DataGridView.Item(Ligne.Index, r).Value = lignes(r).Ligne
            Lignes_DataGridView.Item(Patient.Index, r).Value = BDD.VitaleTableAdapter.GetDataBy_Num(lignes(r).Vitale_Num).Item(0).G104C1 + " " + _
                BDD.VitaleTableAdapter.GetDataBy_Num(lignes(r).Vitale_Num).Item(0).G104C3
            Lignes_DataGridView.Item(Produit.Index, r).Value = lignes(r).Prod_Désignation
            Lignes_DataGridView.Item(Date_column.Index, r).Value = lignes(r)._Date
            Lignes_DataGridView.Item(modifier_Column.Index, r).Value = "modifier"
            Lignes_DataGridView.Item(Supprimer_Column.Index, r).Value = "supprimer"
        Next
    End Sub

    Private Sub Lignes_DataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles Lignes_DataGridView.CellContentClick
        Select Case e.ColumnIndex
            Case modifier_Column.Index
                Dim ligne_ = BDD.PDA_LignesTableAdapter.GetDataBy_NumPda_ligne(num_PDA, Lignes_DataGridView.Item(Ligne.Index, e.RowIndex).Value).Item(0)
                Num_PDATextBox.Text = ligne_.Num_PDA
                LigneTextBox.Text = ligne_.Ligne
                Vitale_NumTextBox.Text = ligne_.Vitale_Num
                Prod_CIPTextBox.Text = ligne_.Prod_CIP
                Prod_CABTextBox.Text = ligne_.Prod_CAB
                Prod_DésignationTextBox.Text = ligne_.Prod_Désignation
                Prod_EAN13TextBox.Text = ligne_.Prod_EAN13
                DateDateTimePicker.Value = ligne_._Date
                Poso_textbox.Text = ligne_.Poso.ToString
                Heure_TextBox.Text = ligne_.heure
                Info_complTextBox.Text = ligne_.Info_compl
                Num_LotTextBox.Text = ligne_.Num_Lot
                PeremptionDateTimePicker.Value = ligne_.Peremption
                Prescr_numTextBox.Text = ligne_.Prescr_num
            Case Supprimer_Column.Index
                MyMsg_Box.Presentation("Officine ?", "La suppression de la ligne est définitive, poursuivre ?", True, "Oui", True, "Non", False, "")
                If MyMsg_Box.ShowDialog = Windows.Forms.DialogResult.Yes Then
                    BDD.PDA_LignesTableAdapter.Delete_Ligne(num_PDA, Lignes_DataGridView.Item(Ligne.Index, e.RowIndex).Value)
                    affichage()
                End If
        End Select
    End Sub

    Private Sub OK_Button_Click(sender As Object, e As EventArgs) Handles OK_Button.Click
        If Num_PDATextBox.Text = "" Or LigneTextBox.Text = "" Then
            Exit Sub
        End If
        BDD.PDA_LignesTableAdapter.Delete_Ligne(CDec(Num_PDATextBox.Text), CDec(LigneTextBox.Text))
        BDD.PDA_LignesTableAdapter.Insert(CDec(Num_PDATextBox.Text), CDec(LigneTextBox.Text), Vitale_NumTextBox.Text, Prod_CIPTextBox.Text, Prod_CABTextBox.Text, _
                                          Prod_EAN13TextBox.Text, Prod_DésignationTextBox.Text, DateDateTimePicker.Value, Info_complTextBox.Text, Num_LotTextBox.Text, PeremptionDateTimePicker.Value, _
                                          Prescr_numTextBox.Text, CInt(Poso_textbox.Text), Heure_TextBox.Text)
        Num_PDATextBox.Text = ""
        LigneTextBox.Text = ""
        Vitale_NumTextBox.Text = ""
        Prod_CIPTextBox.Text = ""
        Prod_CABTextBox.Text = ""
        Prod_DésignationTextBox.Text = ""
        Prod_EAN13TextBox.Text = ""
        Poso_textbox.Text = ""
        Info_complTextBox.Text = ""
        Num_LotTextBox.Text = ""
        Prescr_numTextBox.Text = ""
        affichage()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim parameters As String = "&PDA_Num=" & num_PDA.ToString
        Web_browser.Visible = True
        Web_browser.BringToFront()
        Web_browser.WebBrowser1.Navigate("http://" & Replace(Config.LocalNetwork_ServerName, "\", "") & "/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2fPDA%2fEtiquettes&rs:Command=Render" & parameters)

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim parameters As String = "&PDA_Num=" & num_PDA.ToString
        Web_browser.Visible = True
        Web_browser.BringToFront()
        Web_browser.WebBrowser1.Navigate("http://" & Replace(Config.LocalNetwork_ServerName, "\", "") & "/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2fPDA%2fbon_livraison&rs:Command=Render" & parameters)
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim parameters As String = "&PDA_Num=" & num_PDA.ToString
        Web_browser.Visible = True
        Web_browser.BringToFront()
        Web_browser.WebBrowser1.Navigate("http://" & Replace(Config.LocalNetwork_ServerName, "\", "") & "/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2fposo%2fPLAN_POSO_PDA&rs:Command=Render" & parameters)

    End Sub
End Class