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

    'fermeture du formulaire
    Private Sub Accident_Travail_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'fermeture manuelle (croix ou alt-f4)
        If e.CloseReason = CloseReason.UserClosing Then
            current_AT.selected = False
        End If
    End Sub



    Private Sub Accident_Travail_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If CurrentBenéficiaire.G101C10 = "01" Then 'Table 11.3 Régimes acceptant l’envoi d’une FSE AT à l’organisme maladie inscrit en carte
            TextBox1.Text = CurrentBenéficiaire.G101C10
            TextBox2.Text = CurrentBenéficiaire.G101C11
            TextBox3.Text = CurrentBenéficiaire.G101C12
        End If
        current_AT.clear()
        For i = 0 To 2
            ValeurChamp = "123456789"
            TailleMax = 9
            If SGD_LireChamp(NumeroSessionVitale, 103, 1, (3 * i) + 1, ValeurChamp, TailleMax) = 0 Then
                DataGridView1.Rows.Add()
                DataGridView1.Item(0, i).Value = Mid(ValeurChamp, 1, TailleMax)
                ValeurChamp = "123456789"
                TailleMax = 9
                SGD_LireChamp(NumeroSessionVitale, 103, 1, (3 * i) + 3, ValeurChamp, TailleMax)
                ValeurChamp = Mid(ValeurChamp, 1, TailleMax)
                If ValeurChamp <> "" Then
                    While ValeurChamp.Length < 9
                        ValeurChamp = "0" + ValeurChamp
                    End While
                End If
                DataGridView1.Item(1, i).Value = ValeurChamp
            End If
        Next
        COMPTOIR_MAIN.PictureBox_Facturation.Visible = False
        COMPTOIR_MAIN.Vente.Visible = False
        COMPTOIR_MAIN.Détail_Bénéficiaire_Panel.Visible = False
    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        TextBox1.Text = Mid(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value, 1, 2)
        TextBox2.Text = Mid(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value, 3, 3)
        TextBox3.Text = Mid(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value, 6, 4)
        TextBox4.Text = Mid(DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value, 1, DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value.ToString.Length - 1)
        TextBox5.Text = Mid(DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value, DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value.ToString.Length, 1)
    End Sub


    Private Sub Valider_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Valider_PictureBox.Click
        current_AT.clear()
        current_AT.DateAT = Format(DateTimePicker1.Value, "yyyyMMdd") + "0000"
        current_AT.NumAT = TextBox4.Text
        current_AT.CléAT = TextBox5.Text
        current_AT.CodeRégime = TextBox1.Text
        current_AT.CaisseGest = TextBox2.Text
        current_AT.CentreGest = TextBox3.Text
        current_AT.selected = True
        Dim ModeDegradé As Boolean = True
        If current_AT.CodeRégime <> CurrentBenéficiaire.G101C10 Or current_AT.CaisseGest <> CurrentBenéficiaire.G101C11 Or current_AT.CentreGest <> CurrentBenéficiaire.G101C12 Then
            For i = 0 To DataGridView1.RowCount - 1
                If current_AT.CodeRégime.ToString + current_AT.CaisseGest.ToString + current_AT.CentreGest.ToString = DataGridView1.Item(OrgGest.Index, i).Value.ToString Then
                    ModeDegradé = False
                    Exit For
                End If
            Next
        Else
            ModeDegradé = False
        End If
        '----------------------------------------------------------------------------------
        ''Table 11.2 Régimes autorisant l’envoi d’une FSE AT
        'Select Case TextBox1.Text
        '    Case "02"
        '        Select Case TextBox2.Text
        '            Case "571", "575", "579", "671", "675", "679", "681", "685", "689"
        '                If MsgBox("Régimes n'acceptant pas l’envoi d’une FSE AT." + Chr(10) + _
        '                  "Voulez vous transmettre une feuille papier au risque AT?", 36) = MsgBoxResult.Yes Then
        '                    CurrentFactureVitale.TP_AMO = False
        '                    CurrentFactureVitale.TP_AMC = False
        '                    COMPTOIR_MAIN.FS_Papier = True
        '                    current_AT.selected = False
        '                Else
        '                    If MsgBox("Voulez vous forcer l'envoi de l'AT en FSE?" + Chr(10) + "(Régimes n'acceptant pas l’envoi d’une FSE AT)", 36) = MsgBoxResult.Yes Then
        '                        CurrentFactureVitale.G1440C1 = "W"
        '                    Else
        '                        Exit Sub
        '                    End If
        '                End If
        '        End Select

        '    Case "08", "10", "12", "14", "15", "16", "91", "92", "93", "94", "95", "96", "99"
        '        If MsgBox("Régimes n'acceptant pas l’envoi d’une FSE AT." + Chr(10) + _
        '                 "Voulez vous transmettre une feuille papier au risque AT?", 36) = MsgBoxResult.Yes Then
        '            CurrentFactureVitale.TP_AMO = False
        '            CurrentFactureVitale.TP_AMC = False
        '            COMPTOIR_MAIN.FS_Papier = True
        '            current_AT.selected = False
        '        Else
        '            If MsgBox("Voulez vous forcer l'envoi de l'AT en FSE?" + Chr(10) + "(Régimes n'acceptant pas l’envoi d’une FSE AT)", 36) = MsgBoxResult.Yes Then
        '                CurrentFactureVitale.G1440C1 = "W"
        '            Else
        '                Exit Sub
        '            End If
        '        End If

        'End Select
        '----------------------------------------------------------------------------------
        'Table 11.3 Régimes acceptant l’envoi d’une FSE AT à l’organisme maladie inscrit en carte
        If TextBox1.Text <> "01" Then

            Dim envoi_accepté = False

            If TextBox1.Text <> CurrentBenéficiaire.G101C10 Or TextBox2.Text <> CurrentBenéficiaire.G101C11 Or TextBox3.Text <> CurrentBenéficiaire.G101C12 Then
                envoi_accepté = True

            ElseIf DataGridView1.RowCount > 0 Then
                For i = 0 To DataGridView1.RowCount - 1
                    If current_AT.CodeRégime.ToString + current_AT.CaisseGest.ToString + current_AT.CentreGest.ToString = DataGridView1.Item(OrgGest.Index, i).Value.ToString Then
                        envoi_accepté = True
                        Exit For
                    End If
                Next
            End If

            If envoi_accepté = False Then
                If MsgBox("Régimes n'acceptant pas l’envoi d’une FSE AT à l’organisme maladie inscrit en carte." + Chr(10) + _
                          "Voulez vous transmettre une feuille papier au risque AT?", 36) = MsgBoxResult.Yes Then
                    CurrentFactureVitale.TP_AMO = False
                    CurrentFactureVitale.TP_AMC = False
                    COMPTOIR_MAIN.FS_Papier = True
                    current_AT.selected = False
                Else
                    If MsgBox("Voulez vous forcer l'envoi de l'AT en FSE?" + Chr(10) + "(Régimes n'acceptant pas l’envoi d’une FSE AT à l’organisme maladie inscrit en carte)", 36) = MsgBoxResult.Yes Then
                        CurrentFactureVitale.G1440C1 = "W"
                    Else
                        current_AT.selected = False
                        Exit Sub
                    End If
                End If

            End If

        End If


        '----------------------------------------------------------------------------------
        If ModeDegradé = True Then
            MsgBox("Facturation AT en mode dégradé" + Chr(10) + "Organisme Gestionnaire n'est pas encarte vitale", MsgBoxStyle.Information)
            COMPTOIR_MAIN.ModeSecur = "5"
        End If
        '----------------------------------------------------------------------------------
        'pas de part AMC dans les AT
        CurrentFactureVitale.TP_AMC = False
        '----------------------------------------------------------------------------------
        'cas particulier régime 06 ENIM
        If current_AT.CodeRégime = "06" Then
            If MsgBox("Il faut établire une Feuille de Soins Papier pour le Régime 06 ENIM si l'armateur prend en charge l'accident." + Chr(10) + _
                      "L'armateur prend il en charge l'accident?", 36) = MsgBoxResult.Yes Then
                CurrentFactureVitale.TP_AMO = False
                CurrentFactureVitale.TP_AMC = False
                COMPTOIR_MAIN.FS_Papier = True
                current_AT.selected = False
            End If
        End If
        '----------------------------------------------------------------------------------
        Me.Dispose()
    End Sub

    'controle clé AT règle27
    Private Sub TextBox5_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox5.Validated
        If TextBox5.Text = "" Then
            Exit Sub
        End If
        If TextBox4.Text.Length < 8 Then
            MsgBox("erreur format num AT", MsgBoxStyle.Critical)
            TextBox4.Clear()
            TextBox5.Clear()
            Exit Sub
        End If
        Dim mystring As String
        mystring = StrReverse(Mid(TextBox4.Text, 1, 8))
        Dim myint As Integer = 0
        For i = 1 To 8
            If i Mod 2 = 0 Then
                myint = myint + Val(Mid(mystring, i, 1))
            Else
                myint = myint + 2 * Val(Mid(mystring, i, 1))
            End If
        Next
        myint = myint Mod 10
        If Val(TextBox5.Text) <> myint Then
            MsgBox("erreur clé", MsgBoxStyle.Critical)
            TextBox5.Text = ""
        End If

    End Sub



    Private Sub TextBox1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated
        If TextBox1.Text = "01" Then
            TextBox4.Enabled = True
            TextBox5.Enabled = True
        Else
            TextBox4.Enabled = False
            TextBox4.Text = ""
            TextBox5.Enabled = False
            TextBox5.Text = ""
        End If
    End Sub

    Private Sub Annuller_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Annuller_PictureBox.Click
        current_AT.selected = False
        COMPTOIR_MAIN.PictureBox_Facturation.Visible = True
        Me.Dispose()
    End Sub


  
End Class