' 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

  



    Private Sub Accident_Travail_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        MyMsg_Box.Presentation("Officine ?", "L'assuré a-t-il présenté un feuillet AT?", True, "OUI", True, "NON", False, "")
        If MyMsg_Box.ShowDialog() = MsgBoxResult.No Then
            MyMsg_Box.Presentation("Officine ?", "Nous vous rappelons que la règlementation exige la présence du feuillet accident du travail pour réaliser une FSE en nature d'assurance AT" + Chr(10) + "Voulez vous poursuivre la facturation AT?", True, "OUI", True, "NON", False, "")
            If MyMsg_Box.ShowDialog() = MsgBoxResult.No Then
                current_AT.selected = False
                Me.Close()
            End If
        End If

        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 Val(ValeurChamp) <> 0 Then
                    While ValeurChamp.Length < 9
                        ValeurChamp = "0" + ValeurChamp
                    End While
                    DataGridView1.Item(1, i).Value = ValeurChamp
                Else
                    DataGridView1.Item(1, i).Value = ""
                End If
            End If
        Next
        COMPTOIR_MAIN.Button_Facturation.Visible = False
        COMPTOIR_MAIN.Vente.Visible = False
        COMPTOIR_MAIN.Détail_Bénéficiaire_Panel.Visible = False
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        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)
        If DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value <> "" Then
            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 If
    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.1 Régimes dont les assurés sont couverts pour le risque AT
        If TextBox1.Text = "03" Or TextBox1.Text = "90" Then
            MyMsg_Box.Presentation("Officine ?", "Les assurés de ce régime (" + TextBox1.Text + ") ne sont pas couverts pour le risque AT" + Chr(10) + "Vous pouvez choisir la nature d'assurance: Maladie." + Chr(10) + "Voulez vous forcer l'envoi en AT?", True, "OUI", True, "NON", False, "")
            If MyMsg_Box.ShowDialog() = MsgBoxResult.No Then
                current_AT.selected = False
                Exit Sub
            Else
                CurrentFactureVitale.G1440C1 = "W"
            End If

        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 "579", "679", "689"
                        MyMsg_Box.Presentation("Officine ?", "Caisse Gestionnaire de ce régime n'autorisant pas l'envoi d'une FSE AT" + Chr(10) + "Voulez vous forcer l'envoi ou editer une feuille de soin papier AT?", True, "OUI", True, "NON", False, "")
                        If MyMsg_Box.ShowDialog() = MsgBoxResult.No Then
                            current_AT.selected = False
                            Exit Sub
                        Else
                            CurrentFactureVitale.G1440C1 = "W"
                        End If
                End Select

            Case "08", "10", "12", "14", "15", "16", "91", "92", "93", "94", "95", "96", "99"
                MyMsg_Box.Presentation("Officine ?", "Régime n'autorisant pas l'envoi d'une FSE AT" + Chr(10) + "Voulez vous forcer l'envoi ou editer une feuille de soin papier AT?", True, "OUI", True, "NON", False, "")
                If MyMsg_Box.ShowDialog() = MsgBoxResult.No Then
                    current_AT.selected = False
                    Exit Sub
                Else
                    CurrentFactureVitale.G1440C1 = "W"
                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
                MyMsg_Box.Presentation("Officine ?", "Régime n'acceptant pas l’envoi d’une FSE AT à l’organisme maladie inscrit en carte." + Chr(10) + "Les informations de l'organisme gestionaire de l'AT sont elles issues d'une attestation papier?", True, "OUI", True, "NON", False, "")
                If MyMsg_Box.ShowDialog() = Windows.Forms.DialogResult.Yes Then
                    envoi_accepté = True
                End If
            End If

            If envoi_accepté = False Then
                MyMsg_Box.Presentation("Officine ?", "Régime n'acceptant pas l’envoi d’une FSE AT à l’organisme maladie inscrit en carte." + Chr(10) + "Voulez vous forcer l'envoi ou editer une feuille de soin papier AT?", True, "OUI", True, "NON", False, "")
                If MyMsg_Box.ShowDialog() = Windows.Forms.DialogResult.Yes Then
                    CurrentFactureVitale.G1440C1 = "W"
                Else
                    current_AT.selected = False
                    Exit Sub
                End If
            End If

        End If
        '----------------------------------------------------------------------------------    
        If ModeDegradé = True And COMPTOIR_MAIN.CheckBoxModeDegradé.Checked = False Then
            MyMsg_Box.Presentation("Officine !", "Facturation AT en mode dégradé" + Chr(10) + "Organisme Gestionnaire n'est pas encarte vitale", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        End If
        If ModeDegradé = True Then
            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
            MyMsg_Box.Presentation("Officine ?", "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?", True, "OUI", True, "NON", False, "")
            If MyMsg_Box.ShowDialog() = Windows.Forms.DialogResult.Yes Then
                CurrentFactureVitale.TP_AMO = False
                CurrentFactureVitale.TP_AMC = False
                COMPTOIR_MAIN.FS_Papier = True
                current_AT.selected = False
            End If
        End If
        '----------------------------------------------------------------------------------
        Me.Close()
    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
            MyMsg_Box.Presentation("Officine !", "erreur format num AT", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            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
            MyMsg_Box.Presentation("Officine !", "erreur clé", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            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 Button_quit_Click(sender As Object, e As EventArgs)
        current_AT.selected = False
        COMPTOIR_MAIN.Button_Facturation.Visible = True
    End Sub

    Private Sub Annuler_PictureBox_Click(sender As Object, e As EventArgs) Handles Annuler_PictureBox.Click
        current_AT.selected = False
        Me.Close()
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text <> "01" 
                TextBox4.Text = ""
                TextBox5.Text = ""
        End If
    End Sub

    Private Sub Valider_PictureBox_MouseHover(sender As Object, e As EventArgs) Handles Valider_PictureBox.MouseHover
        Valider_PictureBox.BackgroundImage = officine.My.Resources.validate2
    End Sub

    Private Sub Valider_PictureBox_MouseLeave(sender As Object, e As EventArgs) Handles Valider_PictureBox.MouseLeave
        Valider_PictureBox.BackgroundImage = officine.My.Resources.validate
    End Sub

    Private Sub Annuler_PictureBox_MouseHover(sender As Object, e As EventArgs) Handles Annuler_PictureBox.MouseHover
        Annuler_PictureBox.BackgroundImage = officine.My.Resources.QUITTER2
    End Sub
    Private Sub Annuler_PictureBox_MouseLeave(sender As Object, e As EventArgs) Handles Annuler_PictureBox.MouseLeave
        Annuler_PictureBox.BackgroundImage = officine.My.Resources.QUITTER
    End Sub

End Class