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

    Private Sub FlowLayoutPanel1_Paint(sender As Object, e As PaintEventArgs) Handles FlowLayoutPanel1.Paint

    End Sub

    Private Sub VenteForm_Moyen_paiement2_Load(sender As Object, e As EventArgs) Handles Me.Load
        FlowLayoutPanel1.Controls.Clear()
        Dim Types = BDD.Type_PaiementTableAdapter.GetData
        For r = 0 To Types.Rows.Count - 1
            Dim but As New Button
            but.Size = New Size(150, 100)
            but.ForeColor = Color.White
            but.Text = Types(r).Type
            AddHandler but.Click, AddressOf Button_Click_paiement
            FlowLayoutPanel1.Controls.Add(but)
        Next
        FlowLayoutPanel2.Controls.Clear()
        Dim Organismes = BDD.OrganismesTableAdapter.GetDataBy_Type_Encaissement(True)
        For r = 0 To Organismes.Rows.Count - 1
            Dim but As New Button
            but.Size = New Size(150, 100)
            but.ForeColor = Color.White
            but.Text = Organismes(r).Nom
            AddHandler but.Click, AddressOf Button_Click_organisme
            FlowLayoutPanel2.Controls.Add(but)
        Next
        Me.TopMost = True
        Me.BringToFront()
        Me.Select()
        Me.Activate()
    End Sub

    Private Sub Button_Click_paiement(sender As Object, e As EventArgs)
        VenteForm.Paiement_Type_Button.Text = sender.text
        If VenteForm.Paiement_Type_Button.Text = "Crédit" Or VenteForm.Paiement_Type_Button.Text = "Organisme" Then
            For idx = 0 To VenteForm.Crédits_CheckedListBox1.Items.Count - 1
                VenteForm.Crédits_CheckedListBox1.SetItemChecked(idx, False)
            Next
            VenteForm.afficheinfovente()
            VenteForm.Crédits_CheckedListBox1.Enabled = False
        Else
            VenteForm.Crédits_CheckedListBox1.Enabled = True
        End If
        If VenteForm.Paiement_Type_Button.Text = "Chèque" Then
            VenteForm.Impression_Chèque_LinkLabel.Visible = True
        Else
            VenteForm.Impression_Chèque_LinkLabel.Visible = False
        End If
        Me.Close()
    End Sub
    Private Sub Button_Click_organisme(sender As Object, e As EventArgs)
        VenteForm.Paiement_Type_Button.Text = sender.text
        For idx = 0 To VenteForm.Crédits_CheckedListBox1.Items.Count - 1
            VenteForm.Crédits_CheckedListBox1.SetItemChecked(idx, False)
        Next
        VenteForm.afficheinfovente()
        VenteForm.Crédits_CheckedListBox1.Enabled = False
        If CurrentBenéficiaire.Organisme <> sender.text Then
            MyMsg_Box.Presentation("Officine ?", "Voulez vous definir cette organisme comme  organisme par defaut pour ce patient?", True, "Oui", True, "Non", False, "")
            If MyMsg_Box.ShowDialog = Windows.Forms.DialogResult.Yes Then
                If CurrentBenéficiaire.G101C8 <> "" Then
                    BDD.VitaleTableAdapter.Set_Organisme(sender.text, CurrentBenéficiaire.G101C8, CurrentBenéficiaire.G101C9, CurrentBenéficiaire.G104C12, CurrentBenéficiaire.G104C13, CurrentBenéficiaire.G104C14)
                ElseIf CurrentBenéficiaire.G104C1 <> "" Then
                    BDD.VitaleTableAdapter.Set_Organisme_Dégradé(sender.text, CurrentBenéficiaire.G104C1, CurrentBenéficiaire.G104C3)
                End If
            End If
        End If
        Me.Close()
    End Sub

    Private Sub FlowLayoutPanel2_Paint(sender As Object, e As PaintEventArgs) Handles FlowLayoutPanel2.Paint

    End Sub
End Class