' 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
Imports System.IO
Imports System.Text
Imports System.Xml

Public Class MENU_PRINCIPAL

    Private Sub MENU_PRINCIPAL_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If UCase(e.KeyChar) = "C" Then
            COMPTOIR_MAIN.Visible = True
        End If
    End Sub
    Private Sub resize_form()
        Dim div_height, div_width As Single
        div_height = 1000 / Screen.PrimaryScreen.Bounds.Height
        div_width = 1600 / Screen.PrimaryScreen.Bounds.Width
        If div_height > 1 And div_width > 1 Then
            Me.Height = Int(Me.Height / div_height)
            Me.Width = Int(Me.Width / div_width)
            For Each obj In Controls
                Select Case obj.GetType.FullName.ToString

                    Case "System.Windows.Forms.PictureBox"
                        Dim pict As System.Windows.Forms.PictureBox
                        pict = obj
                        pict.Width = Int(pict.Width / div_width)
                        pict.Height = Int(pict.Height / div_height)
                        pict.Location = New Point(Int(pict.Location.X / div_width), Int(pict.Location.Y / div_height))

                    Case "System.Windows.Forms.PictureBox"
                        Dim lab As System.Windows.Forms.Label
                        lab = obj
                        lab.Width = Int(lab.Width / div_width)
                        lab.Height = Int(lab.Height / div_height)
                        lab.Location = New Point(Int(lab.Location.X / div_width), Int(lab.Location.Y / div_height))

                    Case "System.Windows.Forms.DataGridView"
                        Dim dat As System.Windows.Forms.DataGridView
                        dat = obj
                        dat.Width = Int(dat.Width / div_width)
                        dat.Height = Int(dat.Height / div_height)
                        dat.Location = New Point(Int(dat.Location.X / div_width), Int(dat.Location.Y / div_height))

                    Case "System.Windows.Forms.GroupBox"
                        Dim grp As System.Windows.Forms.GroupBox
                        grp = obj
                        grp.Width = Int(grp.Width / div_width)
                        grp.Height = Int(grp.Height / div_height)
                        grp.Location = New Point(Int(grp.Location.X / div_width), Int(grp.Location.Y / div_height))

                    Case "System.Windows.Forms.Panel"
                        Dim pan As System.Windows.Forms.Panel
                        pan = obj
                        pan.Width = Int(pan.Width / div_width)
                        pan.Height = Int(pan.Height / div_height)
                        pan.Location = New Point(Int(pan.Location.X / div_width), Int(pan.Location.Y / div_height))

                    Case "System.Windows.Forms.Button"
                        Dim but As System.Windows.Forms.Button
                        but = obj
                        but.Width = Int(but.Width / div_width)
                        but.Height = Int(but.Height / div_height)
                        but.Location = New Point(Int(but.Location.X / div_width), Int(but.Location.Y / div_height))

                    Case "System.Windows.Forms.DateTimePicker"
                        Dim dtp As System.Windows.Forms.DateTimePicker
                        dtp = obj
                        dtp.Width = Int(dtp.Width / div_width)
                        dtp.Height = Int(dtp.Height / div_height)
                        dtp.Location = New Point(Int(dtp.Location.X / div_width), Int(dtp.Location.Y / div_height))

                End Select


            Next
        End If
    End Sub

    Private Sub MENU_PRINCIPAL_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        resize_form()
        Me.Enabled = False
        Config.Load_Config_File()

        '____________________________________________________________________________________________________
        'Chemin application
        Chemin_application = Mid(Application.ExecutablePath, 1, InStrRev(Application.ExecutablePath, "/"))
        '____________________________________________________________________________________________________
        Dim CNDAxml As New XmlDocument
        CNDAxml.Load("CNDA.xml")

        Dim ConfigXml As New XmlDocument
        Dim ConfigXMLelementliste As XmlNodeList
        ConfigXml.Load("config.xml")



        ConfigXMLelementliste = CNDAxml.GetElementsByTagName("Visu_API")
        If ConfigXMLelementliste.Item(0).InnerText = "oui" Then
            Visu_API = True
            CNDAxml = Nothing
        Else
            Visu_API = False
            CNDAxml = Nothing
        End If
        '--------------------------------------------
        'initialisation
        ReDim Tab_Valeur_STS(0)

        '____________________________________________________________________________________________________
        'initialisation des tableaux de currentuser
        ReDim Preserve CurrentBenéficiaire.G105C1(0)
        ReDim Preserve CurrentBenéficiaire.G105C2(0)
        ReDim Preserve CurrentBenéficiaire.G106C1(0)
        ReDim Preserve CurrentBenéficiaire.G106C2(0)
        ReDim Preserve CurrentBenéficiaire.G106C3(0)
        ReDim Preserve CurrentBenéficiaire.G106C4(0)
        ReDim Preserve CurrentBenéficiaire.G108C1(0)
        ReDim Preserve CurrentBenéficiaire.G108C2(0)
        '____________________________________________________________________________________________________
        'Eclatement des flux par un OCT
        ConfigXMLelementliste = ConfigXml.GetElementsByTagName("UtilisationOCT")
        If UCase(ConfigXMLelementliste.Item(0).InnerText) = "OUI" Then
            Config.OCT_UtilisationOCT = True
        Else
            Config.OCT_UtilisationOCT = False
        End If
        ConfigXMLelementliste = ConfigXml.GetElementsByTagName("FSEenrichies")
        If UCase(ConfigXMLelementliste.Item(0).InnerText) = "OUI" Then
            Config.OCT_FSEenrichies = True
        Else
            Config.OCT_FSEenrichies = False
        End If

        '____________________________________________________________________________________________________
        'Galss.ini
        ConfigXMLelementliste = ConfigXml.GetElementsByTagName("NomRessourcePS")
        NomRessourcePS = ConfigXMLelementliste.Item(0).InnerText
        ConfigXMLelementliste = ConfigXml.GetElementsByTagName("NomRessourceLecteur")
        NomRessourceLecteur = ConfigXMLelementliste.Item(0).InnerText

        '____________________________________________________________________________________________________
        'Fichier Code Spécialités
        FicCodesSpecialités = Application.StartupPath + "\Tables\CodeSpecialites.msk"
        'Remplit tableau Code Spécialités
        Dim line As String
        line = ""
        Dim i = 0
        Try
            Using sr As New StreamReader(FicCodesSpecialités, Encoding.Default)
                line = sr.ReadLine
                While line <> ""
                    ReDim Preserve tabCodeSpécialité(i)
                    tabCodeSpécialité(i).valeur = Mid(line, 1, 2)
                    tabCodeSpécialité(i).signification = Mid(line, 4, line.Length - 3)
                    line = sr.ReadLine
                    i = i + 1
                End While
            End Using
        Catch ex As Exception
            MsgBox("ERREUR DE CHARGEMENT DE LA TABLE DES CODES SPECIALITES" + Chr(10) + ex.ToString)
        End Try
        '____________________________________________________________________________________________________
        'RESERVATION DE SESSION CPS
        Dim erreur As Integer = SGD_ReserverSession(0, NumeroSessionCPS)
        If erreur <> 0 Then
            MsgBox("ERREUR POUR LA RESERVATION DE SESSION CPS: " + erreur.ToString)
            'Me.Close()
        End If
        'RESERVATION DE SESSION VITALE
        erreur = SGD_ReserverSession(0, NumeroSessionVitale)
        If erreur <> 0 Then
            MsgBox("ERREUR POUR LA RESERVATION DE SESSION VITALE: " + erreur.ToString)
            'Me.Close()
        End If
        '____________________________________________________________________________________________________
        'SSV_InitLIB
        erreur = SSV_InitLIB()
        AfficheErreur(erreur, "ERREUR POUR SSV_InitLib")
        If erreur = 63136 Then
            Config.LOI_Activer = False
        End If
        'If SSV_InitLIB() <> 0 Then MsgBox("ERREUR POUR SSV_InitLib")
        '____________________________________________________________________________________________________
        'SIS_LireCartePS
        AfficheErreur(SIS_LireCartePS(NomRessourcePS, NomRessourceLecteur, "", NumeroSessionCPS), "Erreur LireCarte PS")
        Occurence_Grp2 = 1
        While SGD_LireGroupeSuivant(NumeroSessionCPS, 2, Occurence_Grp2, 2, 1) = 0
            Occurence_Grp2 = Occurence_Grp2 + 1
        End While
        If Occurence_Grp2 > 1 Then
            Occurence_Grp2 = InputBox("Choix situation PS 1 à " + Occurence_Grp2.ToString, "", "1")
        End If
        If Config.Activer_Session_Remplacement = True Then
            Dim Num_id As String = "123456789012345678901234567890"
            TailleMax = 30
            SGD_LireChamp(NumeroSessionCPS, 1, 1, 3, Num_id, TailleMax)
            Num_id = Mid(Num_id, 1, TailleMax)
            Dim Cle_Num_id As String = "1"
            TailleMax = 1
            SGD_LireChamp(NumeroSessionCPS, 1, 1, 4, Cle_Num_id, TailleMax)
            Cle_Num_id = Mid(Cle_Num_id, 1, TailleMax)
            If Num_id <> Config.Remplacant_Num_Id_Nat Or Cle_Num_id <> Config.Remplacant_Cle_Num_Id_Nat Then
                MsgBox("La carte PS n'est pas la carte déclarée pour le remplacement.", MsgBoxStyle.Critical)
                SGD_LibererSession(NumeroSessionCPS)
            End If
        End If
        '____________________________________________________________________________________________________
        'Affichache numéro version logiciel
        VersionLabel.Text = "Gestion de Pharmacie V" + Application.ProductVersion
        If Config.Logiciel_ModeFonctionement = "Test" Then
            VersionLabel.Text = VersionLabel.Text + "  Mode TEST"
        ElseIf Config.Logiciel_ModeFonctionement = "Démonstration" Then
            VersionLabel.Text = VersionLabel.Text + "  Mode DEMO"
        End If
        '____________________________________________________________________________________________________
        ConfirmerDateDuJourButton.BackColor = Color.Red
        ConfigXml = Nothing

        'LOI
        If Config.LOI_Demande_Auto = True Then
            If LOI.LOI_DemandeTableAdapter.Select_Count_By_Date(Format(Now, "dd/MM/yyyy")) = 0 Then
                LOI.Getmail_LOI()
                LOI.Alerte_Non_Reception()
                LOI.Demande_Auto_Loi()
            End If
        End If

        Me.Enabled = True
    End Sub

    'PRESCRIPTEURS_Click
    Private Sub PRESCRIPTEURS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PRESCRIPTEURS.Click
        Edition_Prescripteurs.Visible = True
    End Sub

    Private Sub TECHNIQUE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TECHNIQUE.Click
        Technique_MAIN.Visible = True
    End Sub

    Private Sub TELETRANSMISSION_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TELETRANSMISSION.Click
        Teletransmission_MAIN.Visible = True

    End Sub

    Private Sub ConfirmerDateDuJourButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConfirmerDateDuJourButton.Click
        ConfirmerDateDuJourButton.Visible = False
        COMPTOIR_Button.Enabled = True
        PRESCRIPTEURS.Enabled = True
        TELETRANSMISSION.Enabled = True
        TECHNIQUE.Enabled = True
        button_CONFIGURATION.Enabled = True
        Me.KeyPreview = True
        ButtonAssurés.Enabled = True
        GestionAMC.Enabled = True
        Factures_SV_Button.Enabled = True
        Button_Données_LOI.Enabled = True
        Button_Données_CodeActe.Enabled = True
        Button_Sécuriser_Facture.Enabled = True
        PRODUITS_Button.Enabled = True
    End Sub

    Private Sub GestionAMC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GestionAMC.Click

    End Sub

    Private Sub button_CONFIGURATION_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_CONFIGURATION.Click
        Configuration.Visible = True
    End Sub


    Private Sub ButtonReTransmission_Lot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ReTransmission_Lot.Visible = True
    End Sub

    Private Sub ButtonAssurés_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAssurés.Click
        Assurés.Visible = True
    End Sub

    Private Sub Button_Sécuriser_Facture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Sécuriser_Facture.Click
        Sécuriser_Facture.ShowDialog()
    End Sub

    Private Sub Button_Données_LOI_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Données_LOI.Click
        LOI.Visible = True
    End Sub

    Private Sub Button_Données_CodeActe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Données_CodeActe.Click
        Codes_Acte.Visible = True
    End Sub

    Private Sub Factures_SV_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Factures_SV_Button.Click
        Facture.Visible = True
    End Sub


 


    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Me.Close()
    End Sub


    Private Sub Comptoir_PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        COMPTOIR_MAIN.Visible = True
    End Sub

    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        Process.Start("http://www.pharmacie-houdemont.com/")
    End Sub

    Private Sub COMPTOIR_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles COMPTOIR_Button.Click
        COMPTOIR_MAIN.Visible = True
    End Sub

    Private Sub PRODUITS_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PRODUITS_Button.Click
        PRODUITS_MAIN.Visible = True
    End Sub

    Public Sub COMPTOIR_MAIN_ResetForm()
        If MsgBox("RÉINITIALISER FORMULAIRE", 36) = MsgBoxResult.Yes Then
            COMPTOIR_MAIN.ResetForm()
            COMPTOIR_MAIN.Enabled = True
            COMPTOIR_MAIN.Dispose()
            COMPTOIR_MAIN.Visible = True
        End If
    End Sub
End Class
