' 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 CréerAyantDroit

    Private Sub OK_Button_Click(sender As Object, e As EventArgs) Handles OK_Button.Click
        Dim Sesam_Vitale_form As New Comptes_Clients
        Sesam_Vitale_form.G101C8TextBox.Text = COMPTOIR_MAIN.NirTextBox.Text
        Sesam_Vitale_form.G101C9TextBox.Text = COMPTOIR_MAIN.CléduNIRTextBox.Text
        Sesam_Vitale_form.G104C12MaskedTextBox.Text = Format(G104C12_DateTimePicker.Value, "dd/MM/yyyy")
        Sesam_Vitale_form.G104C13TextBox.Text = G104C13TextBox.Text
        Sesam_Vitale_form.G104C14ComboBox.SelectedItem = G104C14ComboBox.Text
        Sesam_Vitale_form.Text = Me.Text
        Sesam_Vitale_form.Compte_Sesam_Vitale_CheckBox.Checked = True
        Me.Hide()
        Dim assuré_rows = BDD.VitaleTableAdapter.GetDataBénéficiaire(Sesam_Vitale_form.G101C8TextBox.Text, Mid(Sesam_Vitale_form.G104C12MaskedTextBox.Text, 7, 4) + Mid(Sesam_Vitale_form.G104C12MaskedTextBox.Text, 4, 2) + Mid(Sesam_Vitale_form.G104C12MaskedTextBox.Text, 1, 2) + "0000", Sesam_Vitale_form.G104C13TextBox.Text, Sesam_Vitale_form.G104C14ComboBox.Text)
        If assuré_rows.Rows.Count > 0 Then
            MyMsg_Box.Presentation("Officine !", "Assuré existant: " & assuré_rows.Item(0).G104C1 & assuré_rows.Item(0).G104C3, False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
        Else
            Sesam_Vitale_form.Visible = True
            Sesam_Vitale_form.BringToFront()
            Sesam_Vitale_form.TopMost = True
            Sesam_Vitale_form.G101C1ComboBox.SelectedItem = "R"
            Sesam_Vitale_form.AssuréTextBox.Text = COMPTOIR_MAIN.ComboBoxLECTURE_VITALE.Text
            Sesam_Vitale_form.G101C7ComboBox.SelectedIndex = 0
            Sesam_Vitale_form.G101C10TextBox.Text = CurrentBenéficiaire.G101C10
            Sesam_Vitale_form.G101C11TextBox.Text = CurrentBenéficiaire.G101C11
            Sesam_Vitale_form.G101C12TextBox.Text = CurrentBenéficiaire.G101C12
            Sesam_Vitale_form.G1420C3_TextBox.Text = CurrentBenéficiaire.G101C11
            Sesam_Vitale_form.G104C1TextBox.Text = G104C1TextBox.Text
            Sesam_Vitale_form.G104C3TextBox.Text = G104C3TextBox.Text

            Select Case Sesam_Vitale_form.G101C10TextBox.Text
                Case "01", "03", "04", "05", "06", "08", "09", "12", "14", "15", "16", "17", "80", "90", "91", "92", "93", "94", "95", "96", "99"
                    Sesam_Vitale_form.DataGridView_Couverture.Item(Sesam_Vitale_form.Code_Situation.Index, 0).Value = "0100"
                Case "02" 'AGRICOLE
                    Sesam_Vitale_form.DataGridView_Couverture.Item(Sesam_Vitale_form.Code_Situation.Index, 0).Value = "0100"
                Case "10" 'CRPCEN
                    Sesam_Vitale_form.DataGridView_Couverture.Item(Sesam_Vitale_form.Code_Situation.Index, 0).Value = "1000"
                Case "07" 'Régime minier
                    Sesam_Vitale_form.DataGridView_Couverture.Item(Sesam_Vitale_form.Code_Situation.Index, 0).Value = "0700"
            End Select

        End If

        Me.Close()

    End Sub

    Private Sub G104C1TextBox_Validated(sender As Object, e As EventArgs) Handles G104C1TextBox.Validated
        G104C1TextBox.Text = UCase(G104C1TextBox.Text)
    End Sub

    Private Sub G104C3TextBox_Validated(sender As Object, e As EventArgs) Handles G104C3TextBox.Validated
        G104C3TextBox.Text = UCase(G104C3TextBox.Text)
    End Sub

    Private Sub G104C13TextBox_TextChanged(sender As Object, e As EventArgs) Handles G104C13TextBox.Validated
        If Not IsNumeric(G104C13TextBox.Text) Then
            MyMsg_Box.Presentation("Officine !", "valeur numérique seulement", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            G104C13TextBox.Text = "1"
        ElseIf G104C13TextBox.Text = "0" Then
            MyMsg_Box.Presentation("Officine !", "Rang 0 invalide", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            G104C13TextBox.Text = "1"
        End If
    End Sub
End Class