' 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 STS_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

        If Trace_Appel_Fonctions = True Then
            Using sw As New StreamWriter(Application.StartupPath + "\traces\appels_fonctions.txt", True)
                sw.WriteLine(Me.Name + " / " + System.Reflection.MethodInfo.GetCurrentMethod.ToString + " (called by " + (New System.Diagnostics.StackTrace).GetFrame(1).GetMethod.Name + ")")
            End Using
        End If

        COMPTOIR_MAIN.Button_Facturation.Visible = False
        DataGridView1.Rows.Clear()
        Dim current_rows As Integer = 0


        If current_formule_STS.num_prestation_de_rattachement <> "" Then
            Dim temp As Integer = Val(current_formule_STS.num_prestation_de_rattachement.ToString)
            If Not COMPTOIR_MAIN.Vente.Item(1, temp - 1).Value Is Nothing Then
                Label_Head.Text = "CHOIX DE LE FORMULE A APPLIQUER A LA PRESTATION " + current_formule_STS.num_prestation_de_rattachement.ToString + ": " + COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteCodeActe.Index, temp - 1).Value.ToString
            Else
                Label_Head.Text = "CHOIX DE LE FORMULE A APPLIQUER A LA PRESTATION " + current_formule_STS.num_prestation_de_rattachement.ToString + ": " + COMPTOIR_MAIN.Vente.Item(COMPTOIR_MAIN.VenteCodeActe.Index, temp - 1).Value.ToString
            End If

        Else
            Label_Head.Text = "CHOIX DE LE FORMULE A APPLIQUER A LA FACTURE "
        End If

        grp_courant = 0
        occurence_grp_courant = 0
        OK_Button.Visible = False

        While SGD_LireGroupeSuivant(NuméroSessionSTS, grp_courant, occurence_grp_courant, grp_suiv, occurence_grp_suiv) = 0
            If grp_suiv = 3760 Then
                ValeurChamp = "XXXX"
                TailleMax = 4
                SGD_LireChamp(NuméroSessionSTS, grp_suiv, occurence_grp_suiv, 1, ValeurChamp, TailleMax)
                If Val(Mid(ValeurChamp, 1, TailleMax)) = Val(current_formule_STS.num_prestation_de_rattachement) Or Mid(ValeurChamp, 1, TailleMax) = "" 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)
                    DataGridView1(0, current_rows).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)
                    DataGridView1(1, current_rows).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)
                    DataGridView1(2, current_rows).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)
                    DataGridView1(3, current_rows).Value = Mid(ValeurChamp, 1, TailleMax)

                    current_rows = current_rows + 1
                End If
            End If
            grp_courant = grp_suiv
            occurence_grp_courant = occurence_grp_suiv
        End While


        Dim num_prest_rattach As String = current_formule_STS.num_prestation_de_rattachement
        current_formule_STS.Clear()
        current_formule_STS.num_prestation_de_rattachement = num_prest_rattach
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click

        If Trace_Appel_Fonctions = True Then
            Using sw As New StreamWriter(Application.StartupPath + "\traces\appels_fonctions.txt", True)
                sw.WriteLine(Me.Name + " / " + System.Reflection.MethodInfo.GetCurrentMethod.ToString + " (called by " + (New System.Diagnostics.StackTrace).GetFrame(1).GetMethod.Name + ")")
            End Using
        End If

        If TextBox1.Visible = True And TextBox1.Text = "" Then
            Beep()
            Exit Sub
        ElseIf TextBox2.Visible = True And TextBox2.Text = "" Then
            Beep()
            Exit Sub
        ElseIf TextBox3.Visible = True And TextBox3.Text = "" Then
            Beep()
            Exit Sub
        ElseIf TextBox4.Visible = True And TextBox4.Text = "" Then
            Beep()
            Exit Sub
        ElseIf TextBox5.Visible = True And TextBox5.Text = "" Then
            Beep()
            Exit Sub
        ElseIf TextBox6.Visible = True And TextBox6.Text = "" Then
            Beep()
            Exit Sub
        ElseIf TextBox7.Visible = True And TextBox7.Text = "" Then
            Beep()
            Exit Sub
        ElseIf TextBox8.Visible = True And TextBox8.Text = "" Then
            Beep()
            Exit Sub
        End If

        For n = 1 To current_formule_STS.nombre_de_paramètres
            Select Case current_formule_STS.paramètres(n).numéro
                Case 1
                    current_formule_STS.paramètres(n).valeur_affectée = TextBox1.Text
                Case 2
                    current_formule_STS.paramètres(n).valeur_affectée = TextBox2.Text
                Case 3
                    current_formule_STS.paramètres(n).valeur_affectée = TextBox3.Text
                Case 4
                    current_formule_STS.paramètres(n).valeur_affectée = TextBox4.Text
                Case 5
                    current_formule_STS.paramètres(n).valeur_affectée = TextBox5.Text
                Case 6
                    current_formule_STS.paramètres(n).valeur_affectée = TextBox6.Text
                Case 7
                    current_formule_STS.paramètres(n).valeur_affectée = TextBox7.Text
                Case 8
                    current_formule_STS.paramètres(n).valeur_affectée = TextBox8.Text
            End Select
        Next
        current_formule_STS.selected = True
        If CheckBox_toutes_prestations.Checked = True Then
            current_formule_STS.pour_toutes_prestations = True
        End If
        COMPTOIR_MAIN.Button_Facturation.Visible = True
        Me.Close()
    End Sub

    Private Sub Annuler_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Annuler_Button.Click

        If Trace_Appel_Fonctions = True Then
            Using sw As New StreamWriter(Application.StartupPath + "\traces\appels_fonctions.txt", True)
                sw.WriteLine(Me.Name + " / " + System.Reflection.MethodInfo.GetCurrentMethod.ToString + " (called by " + (New System.Diagnostics.StackTrace).GetFrame(1).GetMethod.Name + ")")
            End Using
        End If

        current_formule_STS.selected = False
        COMPTOIR_MAIN.Button_Facturation.Visible = True
        Me.Close()
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.CellClick

        If Trace_Appel_Fonctions = True Then
            Using sw As New StreamWriter(Application.StartupPath + "\traces\appels_fonctions.txt", True)
                sw.WriteLine(Me.Name + " / " + System.Reflection.MethodInfo.GetCurrentMethod.ToString + " (called by " + (New System.Diagnostics.StackTrace).GetFrame(1).GetMethod.Name + ")")
            End Using
        End If

        TextBox1.Visible = False
        TextBox1.Clear()
        TextBox2.Visible = False
        TextBox2.Clear()
        TextBox3.Visible = False
        TextBox3.Clear()
        TextBox4.Visible = False
        TextBox4.Clear()
        TextBox5.Visible = False
        TextBox5.Clear()
        TextBox6.Visible = False
        TextBox6.Clear()
        TextBox7.Visible = False
        TextBox7.Clear()
        TextBox8.Visible = False
        TextBox8.Clear()
        Label1.Visible = False
        Label2.Visible = False
        Label3.Visible = False
        Label4.Visible = False
        Label5.Visible = False
        Label6.Visible = False
        Label7.Visible = False
        Label8.Visible = False


        current_formule_STS.Numéro = DataGridView1(0, DataGridView1.CurrentRow.Index).Value
        current_formule_STS.Libellé = DataGridView1(1, DataGridView1.CurrentRow.Index).Value
        If current_formule_STS.Numéro <> "" Then
            current_formule_STS.selected = True
        Else
            Exit Sub
        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 ' For n = 1 To current_formule_STS.nombre_de_paramètres

                End If 'If current_formule_STS.nombre_de_paramètres > 0 Then


                Exit For 'For i = 1 To occurence_grp_courant

            End If 'If Mid(ValeurChamp, 1, TailleMax) = current_formule_STS.Numéro Then
        Next 'For i = 1 To occurence_grp_courant
        For n = 1 To current_formule_STS.nombre_de_paramètres
            Select Case current_formule_STS.paramètres(n).numéro
                Case 1
                    TextBox1.Text = current_formule_STS.paramètres(n).valeur_par_defaut
                    TextBox1.Visible = True
                    Label1.Visible = True
                Case 2
                    TextBox2.Text = current_formule_STS.paramètres(n).valeur_par_defaut
                    TextBox2.Visible = True
                    Label2.Visible = True
                Case 3
                    TextBox3.Text = current_formule_STS.paramètres(n).valeur_par_defaut
                    TextBox3.Visible = True
                    Label3.Visible = True
                Case 4
                    TextBox4.Text = current_formule_STS.paramètres(n).valeur_par_defaut
                    TextBox4.Visible = True
                    Label4.Visible = True
                Case 5
                    TextBox5.Text = current_formule_STS.paramètres(n).valeur_par_defaut
                    TextBox5.Visible = True
                    Label5.Visible = True
                Case 6
                    TextBox6.Text = current_formule_STS.paramètres(n).valeur_par_defaut
                    TextBox6.Visible = True
                    Label6.Visible = True
                Case 7
                    TextBox7.Text = current_formule_STS.paramètres(n).valeur_par_defaut
                    TextBox7.Visible = True
                    Label7.Visible = True
                Case 8
                    TextBox8.Text = current_formule_STS.paramètres(n).valeur_par_defaut
                    TextBox8.Visible = True
                    Label8.Visible = True
            End Select
        Next
        OK_Button.Visible = True
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged, TextBox6.TextChanged, TextBox7.TextChanged, TextBox8.TextChanged
        If Trace_Appel_Fonctions = True Then
            Using sw As New StreamWriter(Application.StartupPath + "\traces\appels_fonctions.txt", True)
                sw.WriteLine(Me.Name + " / " + System.Reflection.MethodInfo.GetCurrentMethod.ToString + " (called by " + (New System.Diagnostics.StackTrace).GetFrame(1).GetMethod.Name + ")")
            End Using
        End If
        If sender.text = "" Then
            Exit Sub
        End If
        If Not IsNumeric(sender.text) Then
            sender.text = ""
        End If

    End Sub

    Private Sub Jeux_D_Essai_AutoClick()

        If Trace_Appel_Fonctions = True Then
            Using sw As New StreamWriter(Application.StartupPath + "\traces\appels_fonctions.txt", True)
                sw.WriteLine(Me.Name + " / " + System.Reflection.MethodInfo.GetCurrentMethod.ToString + " (called by " + (New System.Diagnostics.StackTrace).GetFrame(1).GetMethod.Name + ")")
            End Using
        End If

        Dim formule_sts As String = JeuX_D_ESSAI_LignesTableAdapter1.GetDataBy_Serie_Num(Jeux_d_essai2.Serie_ComboBox.Text, Jeux_d_essai2.Num_ComboBox.Text)(Val(current_formule_STS.num_prestation_de_rattachement) - 1).STS_Formule

        Dim paramètres_formule_sts As String = ""
        If InStr(formule_sts, "/") <> 0 Then
            paramètres_formule_sts = Mid(formule_sts, InStr(formule_sts, "/") + 1)
            formule_sts = Mid(formule_sts, 1, InStr(formule_sts, "/") - 1)
        End If


        For r = 0 To DataGridView1.RowCount - 1
            If Val(DataGridView1.Item(0, r).Value) = Val(formule_sts) Then
                DataGridView1.Rows.Item(r).Selected = True
                DataGridView1.CurrentCell = DataGridView1.Item(0, r)
                DataGridView1_CellClick(Nothing, Nothing)
            End If
        Next

        While paramètres_formule_sts <> ""
            Dim param = Mid(paramètres_formule_sts, 1, InStr(paramètres_formule_sts, ":") - 1)
            paramètres_formule_sts = Mid(paramètres_formule_sts, InStr(paramètres_formule_sts, ":") + 1)
            Dim valeur_param = Mid(paramètres_formule_sts, 1, InStr(paramètres_formule_sts, "/") - 1)
            paramètres_formule_sts = Mid(paramètres_formule_sts, InStr(paramètres_formule_sts, "/") + 1)
            Select Case param
                Case "1"
                    TextBox1.Text = valeur_param
                Case "2"
                    TextBox2.Text = valeur_param
                Case "3"
                    TextBox3.Text = valeur_param
                Case "4"
                    TextBox4.Text = valeur_param
                Case "5"
                    TextBox5.Text = valeur_param
                Case "6"
                    TextBox6.Text = valeur_param
                Case "7"
                    TextBox7.Text = valeur_param
                Case "8"
                    TextBox8.Text = valeur_param
            End Select

        End While

        OK_Button_Click(Nothing, Nothing)
    End Sub


    Private Sub STS_Choix_Formule_STS_Shown(sender As Object, e As EventArgs) Handles Me.Shown

        If Trace_Appel_Fonctions = True Then
            Using sw As New StreamWriter(Application.StartupPath + "\traces\appels_fonctions.txt", True)
                sw.WriteLine(Me.Name + " / " + System.Reflection.MethodInfo.GetCurrentMethod.ToString + " (called by " + (New System.Diagnostics.StackTrace).GetFrame(1).GetMethod.Name + ")")
            End Using
        End If

        If Jeux_d_essai2.Visible = True Then
            Jeux_D_Essai_AutoClick()
        End If

    End Sub


End Class