' 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 Choix_Formule_STS
    Dim grp_courant As Integer
    Dim grp_suiv As Integer
    Dim occurence_grp_courant As Integer
    Dim occurence_grp_suiv As Integer
    Private Sub Choix_Formule_STS_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        grp_courant = 0
        occurence_grp_courant = 0

        While SGD_LireGroupeSuivant(NuméroSessionSTS, grp_courant, occurence_grp_courant, grp_suiv, occurence_grp_suiv) = 0
            If grp_suiv = 3760 Then
                DataGridView1.Rows.Add()

                ValeurChamp = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                TailleMax = "1000"
                SGD_LireChamp(NuméroSessionSTS, grp_suiv, occurence_grp_suiv, 2, ValeurChamp, TailleMax)
                DataGridView1(0, occurence_grp_suiv - 1).Value = Mid(ValeurChamp, 1, TailleMax)
                ValeurChamp = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                TailleMax = "1000"
                SGD_LireChamp(NuméroSessionSTS, grp_suiv, occurence_grp_suiv, 3, ValeurChamp, TailleMax)
                DataGridView1(1, occurence_grp_suiv - 1).Value = Mid(ValeurChamp, 1, TailleMax)
                ValeurChamp = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                TailleMax = "1000"
                SGD_LireChamp(NuméroSessionSTS, grp_suiv, occurence_grp_suiv, 4, ValeurChamp, TailleMax)
                DataGridView1(2, occurence_grp_suiv - 1).Value = Mid(ValeurChamp, 1, TailleMax)
                ValeurChamp = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                TailleMax = "1000"
                SGD_LireChamp(NuméroSessionSTS, grp_suiv, occurence_grp_suiv, 5, ValeurChamp, TailleMax)
                DataGridView1(3, occurence_grp_suiv - 1).Value = Mid(ValeurChamp, 1, TailleMax)
            End If
            grp_courant = grp_suiv
            occurence_grp_courant = occurence_grp_suiv
        End While

    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        TextBox1.Enabled = False
        TextBox1.Clear()
        TextBox2.Enabled = False
        TextBox2.Clear()
        TextBox3.Enabled = False
        TextBox3.Clear()
        TextBox4.Enabled = False
        TextBox4.Clear()
        TextBox5.Enabled = False
        TextBox5.Clear()
        TextBox6.Enabled = False
        TextBox6.Clear()
        TextBox7.Enabled = False
        TextBox7.Clear()
        TextBox8.Enabled = False
        TextBox8.Clear()

        current_formule_STS.Numéro = DataGridView1(0, DataGridView1.CurrentRow.Index).Value
        If current_formule_STS.Numéro <> "" Then
            current_formule_STS.selected = True
        End If

        For i = 1 To occurence_grp_courant
            ValeurChamp = "XXX"
            TailleMax = 3
            SGD_LireChamp(NuméroSessionSTS, 3760, i, 2, ValeurChamp, TailleMax)
            If Mid(ValeurChamp, 1, TailleMax) = current_formule_STS.Numéro Then

                ValeurChamp = "  "
                TailleMax = 2
                SGD_LireChamp(NuméroSessionSTS, 3760, i, 5, ValeurChamp, TailleMax)
                current_formule_STS.nombre_de_paramètres = Val(Mid(ValeurChamp, 1, TailleMax))

                If current_formule_STS.nombre_de_paramètres > 0 Then

                    ReDim current_formule_STS.paramètres(current_formule_STS.nombre_de_paramètres)

                    For n = 1 To current_formule_STS.nombre_de_paramètres

                        ValeurChamp = "  "
                        TailleMax = 2
                        SGD_LireChamp(NuméroSessionSTS, 3760, i, (4 * n) + 2, ValeurChamp, TailleMax)
                        current_formule_STS.paramètres(n).numéro = Mid(ValeurChamp, 1, TailleMax)

                        ValeurChamp = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                        TailleMax = 1000
                        SGD_LireChamp(NuméroSessionSTS, 3760, i, (4 * n) + 3, ValeurChamp, TailleMax)
                        current_formule_STS.paramètres(n).Libellé = Mid(ValeurChamp, 1, TailleMax)

                        ValeurChamp = "X"
                        TailleMax = 1
                        SGD_LireChamp(NuméroSessionSTS, 3760, i, (4 * n) + 4, ValeurChamp, TailleMax)
                        current_formule_STS.paramètres(n).Type = Mid(ValeurChamp, 1, TailleMax)

                        ValeurChamp = "XXXXXXXX"
                        TailleMax = 8
                        SGD_LireChamp(NuméroSessionSTS, 3760, i, (4 * n) + 5, ValeurChamp, TailleMax)
                        current_formule_STS.paramètres(n).valeur_par_defaut = Mid(ValeurChamp, 1, TailleMax)

                    Next

                End If

                Exit For
            End If
        Next
   
    End Sub
End Class