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

    Private Tampon_valeur_Cellule As String

    Private Sub STS_Affiche_sortie_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        DataGridView1.Rows.Clear()
        Dim r = 0
        For i = 0 To Tab_Valeur_STS.Length - 1
            If Tab_Valeur_STS(i).groupe = 1610 And Tab_Valeur_STS(i).champ = 15 Then
                DataGridView1.Rows.Add()
                DataGridView1.Item(0, r).Value = Tab_Valeur_STS(i).occurence
                DataGridView1.Item(2, r).Value = Tab_Valeur_STS(i).valeur
                If Tab_Valeur_STS(i).modification = "N" Then
                    DataGridView1.Rows.Item(r).ReadOnly = True
                End If
                '--------------------------------------
                'affichage groupe 3710 en sortie STS
                For j = 0 To Tab_Valeur_STS.Length - 1
                    If Tab_Valeur_STS(j).groupe = 3710 And Tab_Valeur_STS(j).NumPrestation = r + 1 Then
                        DataGridView1.Item(1, r).Value = Tab_Valeur_STS(j).valeur
                    End If
                Next
                '--------------------------------------
                For j = 0 To tab_current_formule_STS.Length - 1
                    If Val(tab_current_formule_STS(j).num_prestation_de_rattachement) = Val(Tab_Valeur_STS(i).occurence) Then
                        DataGridView1.Item(1, r).Value = tab_current_formule_STS(j).Numéro.ToString + "  " + tab_current_formule_STS(j).Libellé.ToString
                    End If
                Next
                '--------------------------------------
                'affichage groupe 3715 en sortie STS
                For j = 0 To Tab_Valeur_STS.Length - 1
                    If Tab_Valeur_STS(j).groupe = 3715 And Tab_Valeur_STS(j).NumPrestation = r + 1 Then
                        DataGridView1.Item(1, r).Value = Tab_Valeur_STS(j).valeur
                    End If
                Next
                '--------------------------------------
                r = r + 1
            ElseIf Tab_Valeur_STS(i).groupe = 1910 And Tab_Valeur_STS(i).champ = 5 Then
                Total_Th_TextBox.Text = Tab_Valeur_STS(i).valeur
                If Tab_Valeur_STS(i).modification = "N" Then
                    Total_Th_TextBox.Enabled = False
                End If
            ElseIf Tab_Valeur_STS(i).groupe = 1910 And Tab_Valeur_STS(i).champ = 6 Then
                Total_TextBox.Text = Tab_Valeur_STS(i).valeur
                If Tab_Valeur_STS(i).modification = "N" Then
                    Total_TextBox.Enabled = False
                End If
            End If
        Next
    End Sub

    Private Sub Button_Ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Ok.Click
        COMPTOIR_MAIN.STS_Affiche_sortie_result = True
        Dim r = 0
        For i = 0 To Tab_Valeur_STS.Length - 1
            If Tab_Valeur_STS(i).groupe = 1610 And Tab_Valeur_STS(i).champ = 15 And Tab_Valeur_STS(i).occurence = r + 1 Then
                If Tab_Valeur_STS(i).valeur <> DataGridView1.Item(2, r).Value Then
                    Tab_Valeur_STS(i).valeur = DataGridView1.Item(2, r).Value
                    '------------------------
                    'indicateur de forçage (valeur de sortie des STS modifiée) voir TestFonc 6.27, 6.58
                    Dim groupe2010_STS_exists As Boolean = False
                    For j = 0 To Tab_Valeur_STS.Length - 1
                        If Tab_Valeur_STS(j).groupe = 2010 And Tab_Valeur_STS(j).champ = 2 And Tab_Valeur_STS(j).occurence = r + 1 Then
                            Tab_Valeur_STS(j).valeur = "1"
                            groupe2010_STS_exists = True
                        End If
                    Next
                    If groupe2010_STS_exists = False Then
                        ReDim Preserve Tab_Valeur_STS(Tab_Valeur_STS.Length)
                        Tab_Valeur_STS(Tab_Valeur_STS.Length - 1).groupe = 2010
                        Tab_Valeur_STS(Tab_Valeur_STS.Length - 1).champ = 2
                        Tab_Valeur_STS(Tab_Valeur_STS.Length - 1).occurence = r + 1
                        Tab_Valeur_STS(Tab_Valeur_STS.Length - 1).NumPrestation = Tab_Valeur_STS(i).NumPrestation
                        Tab_Valeur_STS(Tab_Valeur_STS.Length - 1).valeur = "1"
                    End If
                    '------------------------
                End If
                r = r + 1
            ElseIf Tab_Valeur_STS(i).groupe = 1910 And Tab_Valeur_STS(i).champ = 5 Then
                Tab_Valeur_STS(i).valeur = Total_Th_TextBox.Text
            ElseIf Tab_Valeur_STS(i).groupe = 1910 And Tab_Valeur_STS(i).champ = 6 Then
                Tab_Valeur_STS(i).valeur = Total_TextBox.Text
            End If
        Next

        If Val(Total_TextBox.Text) = 0 Then
            If MsgBox("Le montant Remboursable par la complémentaire est nul." + Chr(10) + "Voulez vous abandonner la facturation de la part complémentaire?", 36) = MsgBoxResult.Yes Then
                COMPTOIR_MAIN.CheckBox_Facturation_AMC.Checked = False
            Else
                COMPTOIR_MAIN.STS_Affiche_sortie_result = False
            End If
        End If
        Me.Dispose()
    End Sub

    Private Sub Button_Annuller_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Annuller.Click
        COMPTOIR_MAIN.STS_Affiche_sortie_result = False
        Me.Dispose()
    End Sub


    Private Sub Total_TextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Total_TextBox.Validated
        For i = 0 To Tab_Valeur_STS.Length - 1
            If Tab_Valeur_STS(i).groupe = 1910 And Tab_Valeur_STS(i).champ = 6 Then
                Tab_Valeur_STS(i).valeur = Total_TextBox.Text
            End If
        Next
    End Sub


    Private Sub Total_Th_TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Total_Th_TextBox.TextChanged
        For i = 0 To Tab_Valeur_STS.Length - 1
            If Tab_Valeur_STS(i).groupe = 1910 And Tab_Valeur_STS(i).champ = 5 Then
                Tab_Valeur_STS(i).valeur = Total_Th_TextBox.Text
            End If
        Next
    End Sub




    Private Sub DataGridView1_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValidated
        If DataGridView1.CurrentCell.ColumnIndex = Montant_Remboursable.Index Then
            If Val(DataGridView1.CurrentCell.Value) <> (Tampon_valeur_Cellule) Then
                If Val(DataGridView1.CurrentCell.Value) > COMPTOIR_MAIN.Vente(COMPTOIR_MAIN.VenteTotal.Index, DataGridView1.CurrentCell.RowIndex).Value * 100 _
                                                        * ((100 - COMPTOIR_MAIN.Vente(COMPTOIR_MAIN.VenteAMO.Index, DataGridView1.CurrentCell.RowIndex).Value) / 100) _
                And COMPTOIR_MAIN.RéférencedepriseenchargeToolStripMenuItem.Checked = False Then

                    For i = 0 To Tab_Valeur_STS.Length - 1
                        If Tab_Valeur_STS(i).groupe = 1610 And Tab_Valeur_STS(i).champ = 15 And Val(Tab_Valeur_STS(i).occurence) - 1 = DataGridView1.CurrentRow.Index Then
                            DataGridView1.CurrentCell.Value = Tab_Valeur_STS(i).valeur
                        End If
                    Next

                Else
                    Total_TextBox.Text = "0"
                    Total_Th_TextBox.Text = "0"
                    For r = 0 To DataGridView1.RowCount - 1
                        Total_TextBox.Text = Val(Total_TextBox.Text) + Val(DataGridView1.Item(Montant_Remboursable.Index, r).Value)
                        Total_Th_TextBox.Text = Val(Total_Th_TextBox.Text) + Val(DataGridView1.Item(Montant_Remboursable.Index, r).Value)
                    Next

                End If
            End If
        End If
    End Sub


  
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub
End Class