' 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.Xml

Public Class Disponibilité_Produit

    Private current_Code_produit As String

    Private Sub Disponibilité_Produit_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Icon = Icon.FromHandle(officine.My.Resources.Commandes2.GetHicon)
        
    End Sub

    Public Sub affichage_retour(ByVal fichierxml As String, ByVal Fournisseur As String, ByVal Code_produit As String)
        current_Code_produit = Code_produit
        Dim xmldoc As New XmlDocument
        xmldoc.Load(fichierxml)
        Produit_RichTextBox.Text = ""
        Prix_RichTextBox1.Text = ""
        Info_RichTextBox1.Text = ""
        fournisseur_TextBox1.Text = Fournisseur
        Try
            Dim root As XmlNode = xmldoc.DocumentElement
            Dim nsmgr As New XmlNamespaceManager(xmldoc.NameTable)
            nsmgr.AddNamespace("bk", "urn:x-csrp:fr.csrp.protocole:enveloppe")
            Dim nsmgr2 As New XmlNamespaceManager(xmldoc.NameTable)
            nsmgr2.AddNamespace("bk", "urn:x-csrp:fr.csrp.protocole:message")
            Dim CORPSnode As XmlNode = root.SelectSingleNode("descendant::bk:CORPS", nsmgr)
            Dim MESSAGE_REPARTITEURnode As XmlNode = CORPSnode.SelectSingleNode("descendant::bk:MESSAGE_REPARTITEUR", nsmgr2)
            Dim MESSAGE_REPARTITEUR_CORPSnode As XmlNode = MESSAGE_REPARTITEURnode.SelectSingleNode("descendant::bk:CORPS", nsmgr2)
            Dim REP_INFO_PRODUITnode As XmlNode = MESSAGE_REPARTITEUR_CORPSnode.SelectSingleNode("descendant::bk:REP_INFO_PRODUIT", nsmgr2)
            Dim LIGNEnode As XmlNode = REP_INFO_PRODUITnode.SelectSingleNode("descendant::bk:LIGNE", nsmgr2)
            If Not LIGNEnode.Attributes("Designation") Is Nothing Then
                Produit_RichTextBox.Text = LIGNEnode.Attributes("Designation").Value
            End If

            Dim LIGNE_ChildNodes = LIGNEnode.ChildNodes
            For Each LIGNE_ChildNode As XmlNode In LIGNE_ChildNodes
                Select Case LIGNE_ChildNode.Name
                    Case "PRIX"
                        Prix_RichTextBox1.Text = Prix_RichTextBox1.Text & LIGNE_ChildNode.Attributes("Nature").Value & " : " & LIGNE_ChildNode.Attributes("Valeur").Value & vbCrLf
                    Case "NON_DISPO"
                        Dispo_TextBox1.Text = "NON DISPONIBLE"
                        Dispo_TextBox1.BackColor = Color.Red
                        If Not LIGNE_ChildNode.Attributes("Additif") Is Nothing Then
                            Info_RichTextBox1.Text = Info_RichTextBox1.Text & LIGNE_ChildNode.Attributes("Additif").Value & vbCrLf
                            Try
                                Dim INFOS_DISPO_NDnode = LIGNE_ChildNode.SelectSingleNode("descendant::bk:INFOS_DISPO_ND ", nsmgr2)
                                For Each attrib As XmlAttribute In INFOS_DISPO_NDnode.Attributes
                                    Info_RichTextBox1.Text = Info_RichTextBox1.Text & attrib.Name & " : " & attrib.Value & vbCrLf
                                Next
                            Catch ex As Exception
                            End Try

                        End If
                    Case "DISPO"
                        Dispo_TextBox1.Text = "DISPONIBLE"
                        Dispo_TextBox1.BackColor = Color.Green
                End Select


            Next


        Catch ex As Exception
            MyMsg_Box.Presentation("Officine !", "Impossible d'interpreter la réponse du fournisseur!", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Me.Close()
        End Try
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Commandes_PharmaML.ModeLight = True
        Commandes_PharmaML.Recherche_TextBox.Text = current_Code_produit
        Commandes_PharmaML.Visible = True
        Commandes_PharmaML.TopMost = True
        Commandes_PharmaML.Focus()
        Commandes_PharmaML.Recherche_TextBox.Focus()
        Commandes_PharmaML.SelectNextControl(Commandes_PharmaML.Recherche_TextBox, True, True, True, True)
        If Commandes_PharmaML.Recherche_DataGridView.Rows.Count > 0 Then
            Commandes_PharmaML.Recherche_DataGridView.Rows(0).Selected = True
            Commandes_PharmaML.Recherche_DataGridView_CellClick(Commandes_PharmaML.Recherche_DataGridView, New DataGridViewCellEventArgs(0, 0))
            Commandes_PharmaML.Fournisseurs_ComboBox.Text = fournisseur_TextBox1.Text
        End If
        Me.Close()
    End Sub
End Class