' 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/>.
Imports System.IO
Public Class Recup_Lgpi_AMC





    Private Sub affiche_lgpi_data()
        Dim sr As New StreamReader(Application.StartupPath & "\lgpi data\clients.csv", System.Text.Encoding.Default)

        Dim line = sr.ReadLine
        While Not line Is Nothing


            If Mid(CSV_Champ(line, 3), 1, 13) = COMPTOIR_MAIN.NirTextBox.Text And InStr(COMPTOIR_MAIN.ComboBoxLECTURE_VITALE.Text, (CSV_Champ(line, 1))) <> 0 And InStr(COMPTOIR_MAIN.ComboBoxLECTURE_VITALE.Text, (CSV_Champ(line, 2))) <> 0 Then
                DataGridView1.Rows.Add()
            End If
            line = sr.ReadLine
        End While
        sr.Close()
        sr = New StreamReader(Application.StartupPath & "\lgpi data\client_amc_data.csv")
        line = sr.ReadLine
        Dim row = 0
        While Not line Is Nothing
            If Mid(CSV_Champ(line, 3), 1, 13) = COMPTOIR_MAIN.NirTextBox.Text And InStr(COMPTOIR_MAIN.ComboBoxLECTURE_VITALE.Text, (CSV_Champ(line, 1))) <> 0 And InStr(COMPTOIR_MAIN.ComboBoxLECTURE_VITALE.Text, (CSV_Champ(line, 2))) <> 0 Then
                DataGridView1.Item(0, row).Value = CSV_Champ(line, 4)
                DataGridView1.Item(1, row).Value = CSV_Champ(line, 5)
                DataGridView1.Item(2, row).Value = CSV_Champ(line, 6)
                DataGridView1.Item(3, row).Value = CSV_Champ(line, 7)
                DataGridView1.Item(4, row).Value = CSV_Champ(line, 8)
                DataGridView1.Item(5, row).Value = CSV_Champ(line, 9)
                row = row + 1
            End If
            line = sr.ReadLine
        End While
        sr.Close()

        If DataGridView1.RowCount = 0 Then Me.Close()
    End Sub

    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick



        CurrentBenéficiaire.AMC_NumOrganisme = DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value
        COMPTOIR_MAIN.AMC_NumAMC_TextBox.Text = DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value
        COMPTOIR_MAIN.AMC_NumAMC_TextBox.Enabled = True
        COMPTOIR_MAIN.AMC_NumAMC_TextBox.ReadOnly = False
        COMPTOIR_MAIN.AMC_Couverture_TextBox.Text = DataGridView1.Item(2, DataGridView1.CurrentRow.Index).Value
        COMPTOIR_MAIN.AMC_Couverture_TextBox.Enabled = True
        COMPTOIR_MAIN.AMC_Couverture_TextBox.ReadOnly = False
        COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Text = DataGridView1.Item(3, DataGridView1.CurrentRow.Index).Value
        COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.Enabled = True
        COMPTOIR_MAIN.AMC_DateDebut_MaskedTextBox.ReadOnly = False
        COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Text = DataGridView1.Item(4, DataGridView1.CurrentRow.Index).Value
        COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.Enabled = True
        COMPTOIR_MAIN.AMC_DateFin_MaskedTextBox.ReadOnly = False
        COMPTOIR_MAIN.AMC_Numadhérent_TextBox.Text = DataGridView1.Item(5, DataGridView1.CurrentRow.Index).Value
        COMPTOIR_MAIN.AMC_Numadhérent_TextBox.Enabled = True
        COMPTOIR_MAIN.AMC_Numadhérent_TextBox.ReadOnly = False
        COMPTOIR_MAIN.AMC_Piècejustificative_ComboBox.SelectedIndex = 0
        Me.Close()
    End Sub


    Private Sub Recup_Lgpi_Load(sender As Object, e As EventArgs) Handles Me.Load
        affiche_lgpi_data()

    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub
End Class