' 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 Choix_Bénéficiaire

    Private beneficiaire_selected As Boolean

    Private Sub Choix_Bénéficiaire_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If beneficiaire_selected = False Then
            CurrentBenéficiaire.clear()
            COMPTOIR_MAIN.ComboBoxLECTURE_VITALE.Text = ""
        End If
    End Sub

    Private Sub Choix_Bénéficiaire_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        beneficiaire_selected = False
        Choix_BénéficiaireDataGridView.Rows.Clear()
        Dim mydatatable = Me.VitaleTableAdapter1.GetDataByNom(Replace(COMPTOIR_MAIN.ComboBoxLECTURE_VITALE.Text, " ", "%"))

        For r = 0 To mydatatable.Rows.Count - 1
            Choix_BénéficiaireDataGridView.Rows.Add()
        Next
        For r = 0 To mydatatable.Rows.Count - 1
            Try
                Choix_BénéficiaireDataGridView.Item(Nom.Index, r).Value = mydatatable(r).G104C1
                Choix_BénéficiaireDataGridView.Item(Prénom.Index, r).Value = mydatatable(r).G104C3
                Choix_BénéficiaireDataGridView.Item(DateNaissance.Index, r).Value = DateVitale_To_DateString(mydatatable(r).G104C12)
                Choix_BénéficiaireDataGridView.Item(Rang.Index, r).Value = mydatatable(r).G104C13
                Choix_BénéficiaireDataGridView.Item(NIR.Index, r).Value = mydatatable(r).G101C8
                Choix_BénéficiaireDataGridView.Item(Qualité.Index, r).Value = mydatatable(r).G104C14
            Catch ex As Exception
            End Try


        Next
    End Sub

    Private Sub Choix_BénéficiaireDataGridView_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Choix_BénéficiaireDataGridView.CellClick
        select_benef()
    End Sub


    Private Sub select_benef()
        If Choix_BénéficiaireDataGridView.RowCount = 0 Then Exit Sub
        If Choix_BénéficiaireDataGridView.CurrentRow.Index < 0 Then Exit Sub

        If Choix_BénéficiaireDataGridView.Item(NIR.Index, Choix_BénéficiaireDataGridView.CurrentRow.Index).Value <> "" Then
            CurrentBenéficiaire.G101C8 = Choix_BénéficiaireDataGridView.Item(NIR.Index, Choix_BénéficiaireDataGridView.CurrentRow.Index).Value
            CurrentBenéficiaire.G104C12 = DateString_To_DateVitale(Choix_BénéficiaireDataGridView.Item(DateNaissance.Index, Choix_BénéficiaireDataGridView.CurrentRow.Index).Value)
            CurrentBenéficiaire.G104C13 = Choix_BénéficiaireDataGridView.Item(Rang.Index, Choix_BénéficiaireDataGridView.CurrentRow.Index).Value
            CurrentBenéficiaire.G104C14 = Choix_BénéficiaireDataGridView.Item(Qualité.Index, Choix_BénéficiaireDataGridView.CurrentRow.Index).Value

            COMPTOIR_MAIN.GetDataBénéficiaire()
            COMPTOIR_MAIN.ModeSecur = "5"
            COMPTOIR_MAIN.CheckBoxModeDegradé.Enabled = True
            COMPTOIR_MAIN.CheckBoxModeDegradé.Checked = True
            COMPTOIR_MAIN.ventetype_defaut = "assurance"
            COMPTOIR_MAIN.afficheDonnéesAssuré()
            COMPTOIR_MAIN.AMC_Piècejustificative_ComboBox.SelectedIndex = 0
            'COMPTOIR_MAIN.Assuré_détail_Button.Visible = True
            COMPTOIR_MAIN.DP_form = New DP
            COMPTOIR_MAIN.DP_form.Affichage_histo_local()

        Else
            CurrentBenéficiaire.G101C8 = ""
            CurrentBenéficiaire.G104C1 = Choix_BénéficiaireDataGridView.Item(Nom.Index, Choix_BénéficiaireDataGridView.CurrentRow.Index).Value
            CurrentBenéficiaire.G104C3 = Choix_BénéficiaireDataGridView.Item(Prénom.Index, Choix_BénéficiaireDataGridView.CurrentRow.Index).Value
            COMPTOIR_MAIN.ventetype_defaut = "Vente Directe"
            COMPTOIR_MAIN.GetDataBénéficiaire()

        End If

        beneficiaire_selected = True

        If COMPTOIR_MAIN.NatureAssurance_Button.Text = "Choix Nature Assurance" Then
            COMPTOIR_MAIN.NatureAssurance_Button.Text = "MALADIE"
            COMPTOIR_MAIN.CheckBox_Facturation_AMC.Enabled = True
        End If
        Me.Close()

    End Sub

    Private Sub Choix_BénéficiaireDataGridView_KeyDown(sender As Object, e As KeyEventArgs) Handles Choix_BénéficiaireDataGridView.KeyDown
        If e.KeyCode = Keys.Enter Then
            select_benef()
        End If
    End Sub

    Private Sub Choix_BénéficiaireDataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles Choix_BénéficiaireDataGridView.CellContentClick

    End Sub
End Class