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

    Public CodeSituation_from_Choix_remboursement_amo As String


    Private Sub Assurés_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        COMPTOIR_MAIN.DP_Affiche_Msg()
    End Sub 'pour cliquer automatiquement sur valider les données quand on vient du comptoir



    Private Sub Assurés_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For i = 1 To 7
            DataGridView_Couverture.Rows.Add()
        Next
        For r = 0 To DataGridView_Couverture.Rows.Count - 1
            DataGridView_Couverture.Item(Date_Début.Index, r).Value = ""
            DataGridView_Couverture.Item(Date_Fin.Index, r).Value = ""
            DataGridView_Couverture.Item(Code_Situation.Index, r).Value = ""
        Next
        Dim organismes = BDD.OrganismesTableAdapter.GetData_OrderByNom
        Organisme_payeur_MyCombobox.Items.Clear()
        Organisme_residence_MyCombobox.Items.Clear()
        For r = 0 To organismes.Rows.Count - 1
            If organismes.Item(r).Type_Encaissement = True Then
                Organisme_payeur_MyCombobox.Items.Add(organismes.Item(r).Nom)
            End If
            If organismes.Item(r).Residence = True Then
                Organisme_residence_MyCombobox.Items.Add(organismes.Item(r).Nom)
            End If
        Next
    End Sub


    'retourne une date string au format yyyyMMdd0000 à partir d'une date string au format dd/MM/aaaa
    Private Function Format_Date_Vitale(ByVal DateIn As String) As String
        If DateIn.Length = 10 And Mid(DateIn, 3, 1) = "/" And Mid(DateIn, 6, 1) = "/" Then
            Return Mid(DateIn, 7, 4) + Mid(DateIn, 4, 2) + Mid(DateIn, 1, 2) + "0000"
        Else
            Return "000000000000"
        End If
    End Function

    'sauvegarde
    Private Sub Enregistrer_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Enregistrer_Button.Click
        If Compte_Sesam_Vitale_CheckBox.Checked = True Then
            '___________________________________________________________________________________________________
            'Controle cohérence DATA
            If G101C8TextBox.Text.Length <> 13 Then
                MyMsg_Box.Presentation("Officine !", "Erreur N° Immatriculation", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                Exit Sub
            End If
            If G101C9TextBox.Text.Length <> 2 Then
                MyMsg_Box.Presentation("Officine !", "Erreur Clé du N° Immatriculation", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                Exit Sub
            End If
            If G104C12MaskedTextBox.Text.Length <> 10 Or Mid(G104C12MaskedTextBox.Text, 3, 1) <> "/" Or Mid(G104C12MaskedTextBox.Text, 6, 1) <> "/" Then
                MyMsg_Box.Presentation("Officine !", "Erreur Date de Naissance", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                Exit Sub
            End If
            If Not IsNumeric(G104C13TextBox.Text) Or Val(G104C13TextBox.Text) = 0 Then
                MyMsg_Box.Presentation("Officine !", "Erreur Rang de Naissance", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                Exit Sub
            End If
            If G104C14ComboBox.Text = "" Then
                MyMsg_Box.Presentation("Officine !", "Erreur Qualité Assuré", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                Exit Sub
            End If
            If G104C1TextBox.Text = "" Then
                MyMsg_Box.Presentation("Officine !", "Indiquez un Nom", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                Exit Sub
            End If
            '___________________________________________________________________________________________________

            'verif data
            If G104C1TextBox.Text = "" Or G104C3TextBox.Text = "" Or G101C7ComboBox.Text = "" Or G101C10TextBox.Text = "" Or G1420C3_TextBox.Text = "" Or G101C12TextBox.Text = "" Then
                MyMsg_Box.Presentation("Officine !", "Champ obligatoire non renseigné", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                Exit Sub
            End If
            For r = 0 To DataGridView_Couverture.Rows.Count - 1
                If DataGridView_Couverture.Item(Code_Situation.Index, r).Value <> "" Then
                    If DataGridView_Couverture.Item(Date_Début.Index, r).Value = "" Or DataGridView_Couverture.Item(Date_Fin.Index, r).Value = "" Then
                        MyMsg_Box.Presentation("Officine !", "Indiquez les dates de période de couverture.", False, "", False, "", True, "OK")
                        MyMsg_Box.ShowDialog()
                        Exit Sub
                    End If
                End If
            Next
            '___________________________________________________________________________
            'verif data G1420
            If G1420C1_ComboBox.Text = "" Then
                MyMsg_Box.Presentation("Officine !", "Nature de la pièce justificative pour l'AMO obligatoire", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                Exit Sub
            End If
            If G1420C2_MaskedTextBox.Text = "" Or G1420C2_MaskedTextBox.Text = "  /  /" Then
                MyMsg_Box.Presentation("Officine !", "Indiquez date de validité pour la pièce justificative pour l'AMO obligatoire", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                Exit Sub
            End If
            If G1420C3_TextBox.Text.Length > 3 Then
                MyMsg_Box.Presentation("Officine !", "Origine de la pièce justificative pour l'AMO: 3 caractères max", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                Exit Sub
            End If
            If Mid(G1420C1_ComboBox.Text, 1, 1) = "2" And G1420C3_TextBox.Text = "" Then
                MyMsg_Box.Presentation("Officine !", "Indiquez Origine de la pièce justificative pour l'AMO", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                Exit Sub
            End If
            '___________________________________________________________________________  
            ''mettre les code ALD à 0 si ald pas checké
            'For i = 0 To 6
            '    If DataGridView_Couverture.Item(ALD.Index, i).Value = False Then
            '        DataGridView_Couverture.Item(Code_ALD.Index, i).Value = "0"
            '    Else
            '        If Val(DataGridView_Couverture.Item(Code_ALD.Index, i).Value.ToString) <> 0 Then
            '            DataGridView_Couverture.Item(Code_ALD.Index, i).Value = DataGridView_Couverture.Item(Code_ALD.Index, i).Value.ToString
            '        Else
            '            DataGridView_Couverture.Item(Code_ALD.Index, i).Value = "1"
            '        End If
            '    End If
            'Next
            '___________________________________________________________________________
            'Groupes 106
            Dim CodeSituation() As String
            ReDim CodeSituation(7)
            Dim CodeALD() As String
            ReDim CodeALD(7)
            Dim Datedebut() As String
            ReDim Datedebut(7)
            Dim Datefin() As String
            ReDim Datefin(7)
            Dim aucun_code_situation_renseigné As Boolean = True
            For i = 0 To 6
                If DataGridView_Couverture.Item(Code_Situation.Index, i).Value.ToString <> "" Then
                    aucun_code_situation_renseigné = False
                    CodeSituation(i) = DataGridView_Couverture.Item(Code_Situation.Index, i).Value.ToString
                    If DataGridView_Couverture.Item(ALD.Index, i).Value = True Then
                        CodeALD(i) = "1"
                    Else
                        CodeALD(i) = "0"
                    End If

                    Datedebut(i) = Format_Date_Vitale(DataGridView_Couverture.Item(Date_Début.Index, i).Value.ToString)
                    Datefin(i) = Format_Date_Vitale(DataGridView_Couverture.Item(Date_Fin.Index, i).Value.ToString)
                End If
            Next
            If aucun_code_situation_renseigné = True Then
                MyMsg_Box.Presentation("Officine ?", "Aucun code situation n'est renseigné," + Chr(10) + "les taux de remboursement AMO seront les taux de base pour le régime.", True, "OUI", True, "NON", False, "")
                If MyMsg_Box.ShowDialog() = MsgBoxResult.No Then
                    Exit Sub
                End If
            End If
            '___________________________________________________________________________
            'Alsace-Moselle
            Dim G1511C2 As String
            If G1511C2_CheckBox1.Checked = True Then
                G1511C2 = "O"
            Else
                G1511C2 = "N"
            End If

            If G101C1ComboBox.SelectedItem Is Nothing Then
                G101C1ComboBox.SelectedIndex = 0
                Application.DoEvents()
            End If

            '___________________________________________________________________________
            Select Case BDD.VitaleTableAdapter.GetDataBénéficiaire(G101C8TextBox.Text, DateString_To_DateVitale(G104C12MaskedTextBox.Text), G104C13TextBox.Text, Val(Mid(G104C14ComboBox.Text, 1, 2)).ToString).Rows.Count
                Case 0
                    BDD.VitaleTableAdapter.InsertDégradé(G101C1ComboBox.Text, Mid(G101C7ComboBox.Text, 1, 1), G101C8TextBox.Text, G101C9TextBox.Text, G101C10TextBox.Text, _
                                                         G101C11TextBox.Text, G101C12TextBox.Text, G104C1TextBox.Text, "", G104C3TextBox.Text, _
                                                         Format_Date_Vitale(G104C12MaskedTextBox.Text), G104C13TextBox.Text, Val(Mid(G104C14ComboBox.Text, 1, 2)).ToString, _
                                                         Datedebut(0), Datefin(0), CodeALD(0), CodeSituation(0), _
                                                         Datedebut(1), Datefin(1), CodeALD(1), CodeSituation(1), _
                                                         Datedebut(2), Datefin(2), CodeALD(2), CodeSituation(2), _
                                                         Datedebut(3), Datefin(3), CodeALD(3), CodeSituation(3), _
                                                         Datedebut(4), Datefin(4), CodeALD(4), CodeSituation(4), _
                                                         Datedebut(5), Datefin(5), CodeALD(5), CodeSituation(5), _
                                                         Datedebut(6), Datefin(6), CodeALD(6), CodeSituation(6), _
                                                         "0", AssuréTextBox.Text, Mid(G1420C1_ComboBox.SelectedItem.ToString, 1, 1), _
                                                         Format_Date_Vitale(G1420C2_MaskedTextBox.Text), G1420C3_TextBox.Text, G1511C2, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", _
                                                         Vitale_adresse_RichTextBox.Text, Vitale_téléphone_TextBox.Text, Vitale_e_mail_TextBox.Text, Vitale_Notes_perso_RichTextBox.Text, _
                                                         Organisme_payeur_MyCombobox.Text, BDD.VitaleTableAdapter.SELECT_Max_Num + 1, 0, Organisme_residence_MyCombobox.Text)
                    Me.Close()
                Case 1
                    MyMsg_Box.Presentation("Officine ?", "Remplacer les données existantes ?", True, "OUI", True, "NON", False, "")
                    If MyMsg_Box.ShowDialog() = Windows.Forms.DialogResult.Yes Then
                        BDD.VitaleTableAdapter.UpdateDegrade(G101C1ComboBox.Text, Mid(G101C7ComboBox.Text, 1, 1), G101C8TextBox.Text, G101C9TextBox.Text, G101C10TextBox.Text, _
                                                             G101C11TextBox.Text, G101C12TextBox.Text, G104C1TextBox.Text, "", G104C3TextBox.Text, _
                                                             Format_Date_Vitale(G104C12MaskedTextBox.Text), G104C13TextBox.Text, Val(Mid(G104C14ComboBox.Text, 1, 2)).ToString, _
                                                             Datedebut(0), Datefin(0), CodeALD(0), CodeSituation(0), _
                                                             Datedebut(1), Datefin(1), CodeALD(1), CodeSituation(1), _
                                                             Datedebut(2), Datefin(2), CodeALD(2), CodeSituation(2), _
                                                             Datedebut(3), Datefin(3), CodeALD(3), CodeSituation(3), _
                                                             Datedebut(4), Datefin(4), CodeALD(4), CodeSituation(4), _
                                                             Datedebut(5), Datefin(5), CodeALD(5), CodeSituation(5), _
                                                             Datedebut(6), Datefin(6), CodeALD(6), CodeSituation(6), _
                                                             AssuréTextBox.Text, Mid(G1420C1_ComboBox.SelectedItem.ToString, 1, 1), _
                                                             Format_Date_Vitale(G1420C2_MaskedTextBox.Text), G1420C3_TextBox.Text, G1511C2, _
                                                             Vitale_adresse_RichTextBox.Text, Vitale_téléphone_TextBox.Text, Vitale_e_mail_TextBox.Text, Vitale_Notes_perso_RichTextBox.Text, _
                                                             Organisme_payeur_MyCombobox.Text, Organisme_residence_MyCombobox.Text)
                        Me.Close()
                    End If
                Case Else
                    MyMsg_Box.Presentation("Officine !", "Il existe plusieurs enregistrements avec des valeurs similaires, les données ne peuvent pas être enregistrées." & vbCrLf & "Vérifiez votre base patient et supprimmez les doublons." & vbCrLf & "Enregistrement abandonné.", False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()

            End Select

        Else
            If BDD.VitaleTableAdapter.GetData_Compte_Client_NonSV(G104C1TextBox.Text, G104C3TextBox.Text).Rows.Count > 0 Then
                MyMsg_Box.Presentation("Officine ?", "Remplacer les données existantes ?", True, "OUI", True, "NON", False, "")
                If MyMsg_Box.ShowDialog() = Windows.Forms.DialogResult.Yes Then
                    Dim Patient = BDD.VitaleTableAdapter.GetData_Compte_Client_NonSV(G104C1TextBox.Text, G104C3TextBox.Text).Item(0)
                    BDD.VitaleTableAdapter.Delete_Compte_Client_NonSV(G104C1TextBox.Text, G104C3TextBox.Text)
                    BDD.VitaleTableAdapter.Insert("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", G104C1TextBox.Text, "", G104C3TextBox.Text, "", "", "", "", "", "", "", "", "", "", "", "", "", _
                                                  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", _
                                                  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", _
                                                  Vitale_adresse_RichTextBox.Text, Vitale_Notes_perso_RichTextBox.Text, Vitale_téléphone_TextBox.Text, Vitale_e_mail_TextBox.Text, Organisme_payeur_MyCombobox.Text, _
                                                  Patient.Num, Patient.Chronique_Periode, Organisme_residence_MyCombobox.Text, Patient.Chronique_Periode_decalage_jour, "", "")
                    Me.Close()
                End If
            Else
                BDD.VitaleTableAdapter.Insert("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", G104C1TextBox.Text, "", G104C3TextBox.Text, "", "", "", "", "", "", "", "", "", "", "", "", "", _
                                                 "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", _
                                                 "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", _
                                                 Vitale_adresse_RichTextBox.Text, Vitale_Notes_perso_RichTextBox.Text, Vitale_téléphone_TextBox.Text, Vitale_e_mail_TextBox.Text, Organisme_payeur_MyCombobox.Text, _
                                                  BDD.VitaleTableAdapter.SELECT_Max_Num + 1, 0, Organisme_residence_MyCombobox.Text, 0, "", "")
                Me.Close()
            End If
        End If
    End Sub

    'verif NIR 
    Private Sub G101C8TextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles G101C8TextBox.Validated
        If G101C8TextBox.Text.Length <> 0 Then
            If G101C8TextBox.Text.Length <> 13 Then
                MyMsg_Box.Presentation("Officine !", "ERREUR N° IMMATRICULATION", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()

                G101C8TextBox.Text = ""
            End If
            If Not IsNumeric(G101C8TextBox.Text) Then
                G101C8TextBox.Text = UCase(G101C8TextBox.Text)
                If Mid(G101C8TextBox.Text, 7, 1) <> "A" And Mid(G101C8TextBox.Text, 7, 1) <> "B" Then
                    MyMsg_Box.Presentation("Officine !", "ERREUR N° IMMATRICULATION", False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()

                    G101C8TextBox.Text = ""
                    Exit Sub
                End If
                If Not IsNumeric(Mid(G101C8TextBox.Text, 1, 6)) Or Not IsNumeric(Mid(G101C8TextBox.Text, 8, 6)) Then
                    MyMsg_Box.Presentation("Officine !", "ERREUR N° IMMATRICULATION", False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()

                    G101C8TextBox.Text = ""
                    Exit Sub
                End If
            End If
        End If
    End Sub

    'verif cle du NIR
    Private Sub G101C9TextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles G101C9TextBox.Validated
        If G101C8TextBox.Text.Length <> 0 Then
            If G101C9TextBox.Text.Length <> 2 Then
                MyMsg_Box.Presentation("Officine !", "ERREUR N° IMMATRICULATION", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                G101C9TextBox.Text = ""
                Exit Sub
            End If
            If IsNumeric(G101C8TextBox.Text) Then
                If Val(G101C9TextBox.Text) <> (97 - (Val(G101C8TextBox.Text) Mod 97)) Then
                    MyMsg_Box.Presentation("Officine !", "CLE INVALIDE", False, "", False, "", True, "OK")
                    MyMsg_Box.ShowDialog()
                    G101C9TextBox.Text = ""
                    Exit Sub
                End If
            Else
                Select Case Mid(G101C8TextBox.Text, 7, 1)
                    Case "A"
                        Dim Nir As String = Mid(G101C8TextBox.Text, 1, 6) + "0" + Mid(G101C8TextBox.Text, 8, 6)
                        If G101C9TextBox.Text <> (97 - ((Val(Nir) - 1000000) Mod 97)).ToString Then
                            MyMsg_Box.Presentation("Officine !", "CLE INVALIDE", False, "", False, "", True, "OK")
                            MyMsg_Box.ShowDialog()
                            G101C9TextBox.Text = ""
                            Exit Sub
                        End If
                    Case "B"
                        Dim Nir As String = Mid(G101C8TextBox.Text, 1, 6) + "0" + Mid(G101C8TextBox.Text, 8, 6)
                        If G101C9TextBox.Text <> (97 - ((Val(Nir) - 2000000) Mod 97)).ToString Then
                            MyMsg_Box.Presentation("Officine !", "CLE INVALIDE", False, "", False, "", True, "OK")
                            MyMsg_Box.ShowDialog()
                            G101C9TextBox.Text = ""
                            Exit Sub
                        End If
                    Case Else
                        MyMsg_Box.Presentation("Officine !", "ERREUR N° IMMATRICULATION", False, "", False, "", True, "OK")
                        MyMsg_Box.ShowDialog()
                        G101C8TextBox.Text = ""
                        G101C9TextBox.Text = ""
                End Select
            End If
        End If
    End Sub



    'code centre gestionnaire
    Private Sub G101C12TextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles G101C12TextBox.Validated
        If Not IsNumeric(G101C12TextBox.Text) And G101C12TextBox.Text <> "" Then
            G101C12TextBox.Text = ""
            MyMsg_Box.Presentation("Officine !", "VALEUR NUMERIQUE OBLIGATOIRE", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        End If
        Select Case G101C12TextBox.TextLength
            Case 3
                If G101C10TextBox.Text = "03" Then
                    G101C12TextBox.Text = G101C12TextBox.Text & "0"
                Else
                    G101C12TextBox.Text = "0" & G101C12TextBox.Text
                End If
            Case 4
            Case Else
                MyMsg_Box.Presentation("Officine !", "CODE CENTRE GESTIONNAIRE INVALIDE", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
                G101C12TextBox.Text = ""
        End Select


    End Sub







    'mise en forme date dans datagrid
    Private Sub DataGridView_Couverture_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView_Couverture.CellValidated

        If DataGridView_Couverture.Item(e.ColumnIndex, e.RowIndex).Value Is Nothing Then Exit Sub

        Select Case DataGridView_Couverture.Columns(e.ColumnIndex).Name

            Case "Date_Début", "Date_Fin"
                Select Case DataGridView_Couverture.Item(e.ColumnIndex, e.RowIndex).Value.ToString.Length
                    Case 0
                    Case 8
                        Dim mydate As String
                        mydate = Mid(DataGridView_Couverture.Item(e.ColumnIndex, e.RowIndex).Value.ToString, 1, 2) + "/" + Mid(DataGridView_Couverture.Item(e.ColumnIndex, e.RowIndex).Value.ToString, 3, 2) + "/" + Mid(DataGridView_Couverture.Item(e.ColumnIndex, e.RowIndex).Value.ToString, 5, 4)
                        DataGridView_Couverture.Item(e.ColumnIndex, e.RowIndex).Value = mydate
                    Case 10
                        Dim valeur As String
                        valeur = DataGridView_Couverture.Item(e.ColumnIndex, e.RowIndex).Value.ToString
                        If Not IsNumeric(Mid(valeur, 1, 2)) Or Not IsNumeric(Mid(valeur, 4, 2)) Or Not IsNumeric(Mid(valeur, 7, 4)) Or Mid(valeur, 3, 1) <> "/" Or Mid(valeur, 6, 1) <> "/" Then
                            MyMsg_Box.Presentation("Officine !", "date incorrecte, entrez JJMMAAAA", False, "", False, "", True, "OK")
                            MyMsg_Box.ShowDialog()
                        End If
                    Case Else
                        MyMsg_Box.Presentation("Officine !", "date incorrecte, entrez JJMMAAAA", False, "", False, "", True, "OK")
                        MyMsg_Box.ShowDialog()
                End Select


        End Select
    End Sub

    'choix remboursement
    Private Sub DataGridView_Couverture_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView_Couverture.CellContentClick
        Select Case DataGridView_Couverture.CurrentCellAddress.X
            Case Editer.Index
                Dim my_Choix_remboursement_amo As New Choix_remboursement_amo
                my_Choix_remboursement_amo.Code_Régime = G101C10TextBox.Text
                my_Choix_remboursement_amo.caller = Me
                my_Choix_remboursement_amo.ShowDialog()
                DataGridView_Couverture.Item(Code_Situation.Index, DataGridView_Couverture.CurrentCellAddress.Y).Value = CodeSituation_from_Choix_remboursement_amo
        End Select
    End Sub



    'remplissage automatique G104C3
    Private Sub G104C3TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles G104C3TextBox.TextChanged
        If G104C3TextBox.Text.Length = 1 Then
            AssuréTextBox.Text = AssuréTextBox.Text + " "
        End If
        If G104C3TextBox.Text.Length <> 0 Then
            If AssuréTextBox.Text = G104C1TextBox.Text + " " + Mid(G104C3TextBox.Text, 1, G104C3TextBox.Text.Length - 1) Then
                AssuréTextBox.Text = G104C1TextBox.Text + " " + G104C3TextBox.Text
            End If
        End If
    End Sub




    Private Sub G101C11TextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles G1420C3_TextBox.Validated
        If Not IsNumeric(G1420C3_TextBox.Text) And G1420C3_TextBox.Text <> "" Then
            G1420C3_TextBox.Text = ""
            MyMsg_Box.Presentation("Officine !", "VALEUR NUMERIQUE OBLIGATOIRE", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        End If
    End Sub

    Private Sub G101C10TextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles G101C10TextBox.Validated
        If (Not IsNumeric(G101C10TextBox.Text) Or G101C10TextBox.TextLength <> 2) And G101C10TextBox.Text <> "" Then
            MyMsg_Box.Presentation("CODE RÉGIME", "Le code régime doit être" & vbCrLf & vbCrLf & "2 Chiffres", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            G101C10TextBox.Focus()
            G101C10TextBox.SelectAll()
            Exit Sub
        End If
        If G101C10TextBox.Text = "09" Then
            MyMsg_Box.Presentation("Officine !", "Les spécificités du régime 09 (Banque de France) ont disparu (bénéficiaires rattachés au régime des fonctionnaires).", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            G101C10TextBox.Text = ""
        ElseIf G101C10TextBox.Text = "12" Then
            MyMsg_Box.Presentation("Officine !", "Suppression du régime RSAM-CCIP (12) au 01/01/2013", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            G101C10TextBox.Text = ""
        End If
        Label_Régime.Text = TraductionCodeRégime(G101C10TextBox.Text)
    End Sub

    Private Sub TextBox1_Validated(sender As Object, e As EventArgs) Handles TextBox1.Validated
        If TextBox1.Text = "" Then Exit Sub

        Recherche_DataGridView.Rows.Clear()
        Dim items = BDD.VitaleTableAdapter.Form_Sesam_Vitale_Recherche_assurés(TextBox1.Text)
        For r = 0 To items.Rows.Count - 1
            Recherche_DataGridView.Rows.Add()
        Next
        For r = 0 To items.Rows.Count - 1
            Recherche_DataGridView.Item(Recherche_Nom.Index, r).Value = items(r).G104C1
            Recherche_DataGridView.Item(Recherche_Prénom.Index, r).Value = items(r).G104C3
            Recherche_DataGridView.Item(Recherche_Date_Naissance.Index, r).Value = DateVitale_To_DateString(items(r).G104C12)
            If Not items(r).IsG104C9Null Then
                If items(r).G104C9 <> "" Then
                    Recherche_DataGridView.Item(Recherche_NIR.Index, r).Value = items(r).G104C9
                End If
            End If
            If Recherche_DataGridView.Item(Recherche_NIR.Index, r).Value = "" Then
                Recherche_DataGridView.Item(Recherche_NIR.Index, r).Value = items(r).G101C8
            End If
        Next
        If Recherche_DataGridView.Rows.Count > 0 Then
            Recherche_DataGridView.CurrentCell = Recherche_DataGridView.Item(0, 0)
            Recherche_DataGridView.Focus()
        End If
    End Sub

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            TextBox1_Validated(Nothing, Nothing)
        End If
    End Sub



   


    Private Sub Recherche_DataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles Recherche_DataGridView.SelectionChanged

        If Recherche_DataGridView.Rows.Count = 0 Then Exit Sub
        If Recherche_DataGridView.CurrentRow Is Nothing Then Exit Sub
        If Recherche_DataGridView.CurrentRow.Index < 0 Then Exit Sub
        If Recherche_DataGridView.Item(0, 0).Value = "" Then Exit Sub

        DataGridView_Couverture.Rows.Clear()
        For r = 0 To 6
            DataGridView_Couverture.Rows.Add()
        Next
        For r = 0 To 6
            DataGridView_Couverture.Item(Code_Situation.Index, r).Value = ""
        Next
        G104C12MaskedTextBox.Clear()
        G1420C2_MaskedTextBox.Clear()
        G104C13TextBox.Clear()
        G104C14ComboBox.SelectedIndex = Nothing
        AssuréTextBox.Clear()
        G101C8TextBox.Clear()
        G101C9TextBox.Clear()
        G101C7ComboBox.SelectedIndex = Nothing
        G101C10TextBox.Clear()
        G101C11TextBox.Clear()
        G101C12TextBox.Clear()
        G1420C1_ComboBox.SelectedIndex = Nothing
        G1420C3_TextBox.Clear()

            If Recherche_DataGridView.Item(Recherche_NIR.Index, Recherche_DataGridView.CurrentRow.Index).Value <> "" Then
                Compte_Sesam_Vitale_CheckBox.Checked = True
                Dim assuré = BDD.VitaleTableAdapter.Form_Sesam_Vitale_Selection_Assuré(Recherche_DataGridView.Item(Recherche_NIR.Index, Recherche_DataGridView.CurrentRow.Index).Value, _
                    Recherche_DataGridView.Item(Recherche_Nom.Index, Recherche_DataGridView.CurrentRow.Index).Value, _
                    Recherche_DataGridView.Item(Recherche_Prénom.Index, Recherche_DataGridView.CurrentRow.Index).Value, _
                    DateString_To_DateVitale(Recherche_DataGridView.Item(Recherche_Date_Naissance.Index, Recherche_DataGridView.CurrentRow.Index).Value)).Item(0)






            G101C1ComboBox.Text = assuré.G101C1
            G101C7ComboBox.SelectedIndex = Nothing
                For i = 0 To G101C7ComboBox.Items.Count - 1
                    If InStr(G101C7ComboBox.Items.Item(i).ToString, assuré.G101C7) = 1 Then
                        G101C7ComboBox.SelectedIndex = i
                        Exit For
                    End If
                Next
                G101C8TextBox.Text = assuré.G101C8
                G101C9TextBox.Text = assuré.G101C9
                G104C1TextBox.Text = assuré.G104C1
                G104C3TextBox.Text = assuré.G104C3
                G101C7ComboBox.Text = assuré.G101C7
                G101C10TextBox.Text = assuré.G101C10
                G101C11TextBox.Text = assuré.G101C11
            G101C12TextBox.Text = assuré.G101C12
            If assuré.G104C12 <> "" Then
                G104C12MaskedTextBox.Text = DateVitale_To_DateString(assuré.G104C12)
            Else
                G104C12MaskedTextBox.Clear()
            End If
            G104C13TextBox.Text = assuré.G104C13

            G104C14ComboBox.SelectedIndex = Nothing
            For i = 0 To G104C14ComboBox.Items.Count - 1
                If Val(Mid(G104C14ComboBox.Items(i).ToString, 1, 2)) = Val(assuré.G104C14) Then
                    G104C14ComboBox.SelectedIndex = i
                    Exit For
                End If
            Next

            G1420C1_ComboBox.SelectedIndex = Nothing
            If Not assuré.IsG1420C1Null Then
                For i = 0 To G1420C1_ComboBox.Items.Count - 1
                    If Val(Mid(G1420C1_ComboBox.Items.Item(i).ToString, 1, 1)) = Val(assuré.G1420C1) Then
                        G1420C1_ComboBox.SelectedIndex = i
                        Exit For
                    End If
                Next
            Else
                G1420C1_ComboBox.SelectedIndex = Nothing
                G1420C1_ComboBox.Text = ""
            End If

            G1420C2_MaskedTextBox.Clear()
            If Not assuré.IsG1420C2Null Then
                If assuré.G1420C2 <> "" Then
                    G1420C2_MaskedTextBox.Text = DateVitale_To_DateString(assuré.G1420C2)
                End If
            End If

            G1420C3_TextBox.Text = ""
            If Not assuré.IsG1420C3Null Then
                G1420C3_TextBox.Text = assuré.G1420C3
            End If

            AssuréTextBox.Text = ""
            If Not assuré.IsAssuréNull Then
                AssuréTextBox.Text = assuré.Assuré
            End If

            '''''
           
            Dim occurence_G106 As Integer = 0
            If Not assuré.IsG106C1O1Null Then
                If assuré.G106C1O1 <> "" Then
                    DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = DateVitale_To_DateString(assuré.G106C1O1)
                Else
                    DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = ""
                End If
            Else
                DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = ""
            End If
            If Not assuré.IsG106C2O1Null Then
                If assuré.G106C2O1 <> "" Then
                    DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = DateVitale_To_DateString(assuré.G106C2O1)
                Else
                    DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = ""
                End If
            Else
                DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = ""
            End If
            If Not assuré.IsG106C3O1Null Then
                If Val(assuré.G106C3O1) > 0 Then
                    DataGridView_Couverture.Item(ALD.Index, occurence_G106).Value = True
                End If
            End If
            If Not assuré.IsG106C4O1Null Then
                DataGridView_Couverture.Item(Code_Situation.Index, occurence_G106).Value = assuré.G106C4O1
            End If
            '''''
            occurence_G106 = occurence_G106 + 1
            If Not assuré.IsG106C1O2Null Then
                If assuré.G106C1O2 <> "" Then
                    DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = DateVitale_To_DateString(assuré.G106C1O2)
                Else
                    DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = ""
                End If
            Else
                DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = ""
            End If
            If Not assuré.IsG106C2O2Null Then
                If assuré.G106C2O2 <> "" Then
                    DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = DateVitale_To_DateString(assuré.G106C2O2)
                Else
                    DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = ""
                End If
            Else
                DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = ""
            End If
            If Not assuré.IsG106C3O2Null Then
                If Val(assuré.G106C3O2) > 0 Then
                        DataGridView_Couverture.Item(ALD.Index, occurence_G106).Value = True
                End If
            End If
            If Not assuré.IsG106C4O2Null Then
                DataGridView_Couverture.Item(Code_Situation.Index, occurence_G106).Value = assuré.G106C4O2
            End If
            '''''
            occurence_G106 = occurence_G106 + 1
            If Not assuré.IsG106C1O3Null Then
                If assuré.G106C1O3 <> "" Then
                    DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = DateVitale_To_DateString(assuré.G106C1O3)
                Else
                    DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = ""
                End If
            Else
                DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = ""
            End If
            If Not assuré.IsG106C2O3Null Then
                If assuré.G106C2O3 <> "" Then
                    DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = DateVitale_To_DateString(assuré.G106C2O3)
                Else
                    DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = ""
                End If
            Else
                DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = ""
            End If
            If Not assuré.IsG106C3O3Null Then
                If Val(assuré.G106C3O3) > 0 Then
                    DataGridView_Couverture.Item(ALD.Index, occurence_G106).Value = True
                End If
            End If
            If Not assuré.IsG106C4O3Null Then
                DataGridView_Couverture.Item(Code_Situation.Index, occurence_G106).Value = assuré.G106C4O3
            End If
            '''''
            occurence_G106 = occurence_G106 + 1
            If Not assuré.IsG106C1O4Null Then
                If assuré.G106C1O4 <> "" Then
                    DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = DateVitale_To_DateString(assuré.G106C1O4)
                Else
                    DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = ""
                End If
            Else
                DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = ""
            End If
            If Not assuré.IsG106C2O4Null Then
                If assuré.G106C2O4 <> "" Then
                    DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = DateVitale_To_DateString(assuré.G106C2O4)
                Else
                    DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = ""
                End If
            Else
                DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = ""
            End If
            If Not assuré.IsG106C3O4Null Then
                If Val(assuré.G106C3O4) > 0 Then
                    DataGridView_Couverture.Item(ALD.Index, occurence_G106).Value = True
                End If
            End If
            If Not assuré.IsG106C4O4Null Then
                DataGridView_Couverture.Item(Code_Situation.Index, occurence_G106).Value = assuré.G106C4O4
            End If
            '''''
            occurence_G106 = occurence_G106 + 1
            If Not assuré.IsG106C1O5Null Then
                If assuré.G106C1O5 <> "" Then
                    DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = DateVitale_To_DateString(assuré.G106C1O5)
                Else
                    DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = ""
                End If
            Else
                DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = ""
            End If
            If Not assuré.IsG106C2O5Null Then
                If assuré.G106C2O5 <> "" Then
                    DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = DateVitale_To_DateString(assuré.G106C2O5)
                Else
                    DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = ""
                End If
            Else
                DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = ""
            End If
            If Not assuré.IsG106C3O5Null Then
                If Val(assuré.G106C3O5) > 0 Then
                    DataGridView_Couverture.Item(ALD.Index, occurence_G106).Value = True
                End If
            End If
            If Not assuré.IsG106C4O5Null Then
                DataGridView_Couverture.Item(Code_Situation.Index, occurence_G106).Value = assuré.G106C4O5
            End If
            '''''
            occurence_G106 = occurence_G106 + 1
            If Not assuré.IsG106C1O6Null Then
                If assuré.G106C1O6 <> "" Then
                    DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = DateVitale_To_DateString(assuré.G106C1O6)
                Else
                    DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = ""
                End If
            Else
                DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = ""
            End If
            If Not assuré.IsG106C2O6Null Then
                If assuré.G106C2O6 <> "" Then
                    DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = DateVitale_To_DateString(assuré.G106C2O6)
                Else
                    DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = ""
                End If
            Else
                DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = ""
            End If
            If Not assuré.IsG106C3O6Null Then
                If Val(assuré.G106C3O6) > 0 Then
                    DataGridView_Couverture.Item(ALD.Index, occurence_G106).Value = True
                End If
            End If
            If Not assuré.IsG106C4O6Null Then
                DataGridView_Couverture.Item(Code_Situation.Index, occurence_G106).Value = assuré.G106C4O6
            End If
            '''''
            occurence_G106 = occurence_G106 + 1
            If Not assuré.IsG106C1O7Null Then
                If assuré.G106C1O7 <> "" Then
                    DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = DateVitale_To_DateString(assuré.G106C1O7)
                Else
                    DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = ""
                End If
            Else
                DataGridView_Couverture.Item(Date_Début.Index, occurence_G106).Value = ""
            End If
            If Not assuré.IsG106C2O7Null Then
                If assuré.G106C2O7 <> "" Then
                    DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = DateVitale_To_DateString(assuré.G106C2O7)
                Else
                    DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = ""
                End If
            Else
                DataGridView_Couverture.Item(Date_Fin.Index, occurence_G106).Value = ""
            End If
            If Not assuré.IsG106C3O7Null Then
                If Val(assuré.G106C3O7) > 0 Then
                    DataGridView_Couverture.Item(ALD.Index, occurence_G106).Value = True
                End If
            End If
            If Not assuré.IsG106C4O7Null Then
                DataGridView_Couverture.Item(Code_Situation.Index, occurence_G106).Value = assuré.G106C4O7
            End If

            Vitale_adresse_RichTextBox.Text = ""
            If Not assuré.IsAdresseNull Then
                Vitale_adresse_RichTextBox.Text = assuré.Adresse
            End If

            Vitale_téléphone_TextBox.Text = ""
            If Not assuré.IsTéléphoneNull Then
                Vitale_téléphone_TextBox.Text = assuré.Téléphone
            End If

            Vitale_e_mail_TextBox.Text = ""
            If Not assuré.Ise_mailNull Then
                Vitale_e_mail_TextBox.Text = assuré.e_mail
            End If

            Vitale_Notes_perso_RichTextBox.Text = ""
            If Not assuré.IsNotes_persoNull Then
                Vitale_Notes_perso_RichTextBox.Text = assuré.Notes_perso
            End If

            Organisme_payeur_MyCombobox.Text = ""
            If Not assuré.IsOrganismeNull Then
                Organisme_payeur_MyCombobox.Text = assuré.Organisme
            End If

            Organisme_residence_MyCombobox.Text = ""
            If Not assuré.IsOrganisme_de_residenceNull Then
                Organisme_residence_MyCombobox.Text = assuré.Organisme_de_residence
            End If
        Else
        




            Compte_Sesam_Vitale_CheckBox.Checked = False
            Dim compte_NonSV = BDD.VitaleTableAdapter.GetData_Compte_Client_NonSV(Recherche_DataGridView.Item(Recherche_Nom.Index, Recherche_DataGridView.CurrentRow.Index).Value, _
                Recherche_DataGridView.Item(Recherche_Prénom.Index, Recherche_DataGridView.CurrentRow.Index).Value).Item(0)
            G104C1TextBox.Text = compte_NonSV.G104C1
            G104C3TextBox.Text = compte_NonSV.G104C3

            Vitale_adresse_RichTextBox.Text = ""
            If Not compte_NonSV.IsAdresseNull Then
                Vitale_adresse_RichTextBox.Text = compte_NonSV.Adresse
            End If
            Vitale_téléphone_TextBox.Text = ""
            If Not compte_NonSV.IsTéléphoneNull Then
                Vitale_téléphone_TextBox.Text = compte_NonSV.Téléphone
            End If

            Vitale_e_mail_TextBox.Text = ""
            If Not compte_NonSV.Ise_mailNull Then
                Vitale_e_mail_TextBox.Text = compte_NonSV.e_mail
            End If

            Vitale_Notes_perso_RichTextBox.Text = ""
            If Not compte_NonSV.IsNotes_persoNull Then
                Vitale_Notes_perso_RichTextBox.Text = compte_NonSV.Notes_perso
            End If

            Organisme_payeur_MyCombobox.Text = ""
            If Not compte_NonSV.IsOrganismeNull Then
                Organisme_payeur_MyCombobox.Text = compte_NonSV.Organisme
            End If

            Organisme_residence_MyCombobox.Text = ""
            If Not compte_NonSV.IsOrganisme_de_residenceNull Then
                Organisme_residence_MyCombobox.Text = compte_NonSV.Organisme_de_residence
            End If
        End If



    End Sub

   

    Private Sub Supprimmer_Button_Click(sender As Object, e As EventArgs) Handles Supprimmer_Button.Click
        MyMsg_Box.Presentation("Officine ?", "Voulez-vous supprimer ce compte ?", True, "Oui", True, "Non", False, "")
        If MyMsg_Box.ShowDialog = Windows.Forms.DialogResult.Yes Then
            If Compte_Sesam_Vitale_CheckBox.Checked = True Then
                BDD.VitaleTableAdapter.Delete_CompteSV(G101C8TextBox.Text, G101C10TextBox.Text, G101C11TextBox.Text, G101C12TextBox.Text, G104C1TextBox.Text, G104C3TextBox.Text, _
                                                       DateString_To_DateVitale(G104C12MaskedTextBox.Text), G104C13TextBox.Text, CInt(Mid(G104C14ComboBox.Text, 1, InStr(G104C14ComboBox.Text, " ") - 1)))
            Else
                BDD.VitaleTableAdapter.Delete_Compte_Client_NonSV(G104C1TextBox.Text, G104C3TextBox.Text)
            End If
            Me.Close()
        End If
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub Recherche_DataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles Recherche_DataGridView.CellContentClick

    End Sub

    Public Sub affiche_compte_from_another_form(ByVal Nir As String, ByVal Nom As String, ByVal Prénom As String, ByVal Date_Naissance As String)
        Recherche_DataGridView.Rows.Clear()
        Recherche_DataGridView.Rows.Add()
        Recherche_DataGridView.Item(Recherche_NIR.Index, 0).Value = Nir
        Recherche_DataGridView.Item(Recherche_Nom.Index, 0).Value = Nom
        Recherche_DataGridView.Item(Recherche_Prénom.Index, 0).Value = Prénom
        Recherche_DataGridView.Item(Recherche_Date_Naissance.Index, 0).Value = Date_Naissance
       
    End Sub

    Private Sub Comptes_Clients_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        If Me.Modal = True Then
            If Recherche_DataGridView.Rows.Count > 0 Then
                Recherche_DataGridView.CurrentCell = Recherche_DataGridView.Item(0, 0)
                Recherche_DataGridView_SelectionChanged(Nothing, Nothing)
            End If
            Me.TopMost = True
            Me.TopLevel = True
        End If
        
    End Sub

    Private Sub G101C10TextBox_TextChanged(sender As Object, e As EventArgs) Handles G101C10TextBox.TextChanged

    End Sub

    Private Sub GroupBox1_Enter(sender As Object, e As EventArgs) Handles GroupBox1.Enter

    End Sub
End Class