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

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If CodeRégimeComboBox.Text = "" Then
            MsgBox("Erreur code régime", MsgBoxStyle.Information)
            Exit Sub
        End If

        If CaisseGestionnaireTextBox.TextLength <> 3 Then
            MsgBox("Erreur code Caisse Gestionnaire", MsgBoxStyle.Information)
            Exit Sub
        End If

        If CentregestionnaireTextBox.TextLength <> 4 And CentregestionnaireTextBox.TextLength <> 3 Then
            MsgBox("Erreur code Centre Gestionnaire", MsgBoxStyle.Information)
            Exit Sub
        End If

        If CentregestionnaireTextBox.TextLength = 3 Then
            Select Case CodeRégimeComboBox.Text
                Case "03"
                    CentregestionnaireTextBox.Text = CentregestionnaireTextBox.Text & "0"
                Case "99"
                    CentregestionnaireTextBox.Text = "0" & CentregestionnaireTextBox.Text
            End Select
        End If

        CodeRégimeComboBox.Enabled = False
        CaisseGestionnaireTextBox.Enabled = False
        CentregestionnaireTextBox.Enabled = False
        Button1.Enabled = False
        Button2.Enabled = True
        If Me.AmoTableAdapter1.SelectCount(CodeRégimeComboBox.Text, CaisseGestionnaireTextBox.Text, CentregestionnaireTextBox.Text) = 1 Then
            OrganismeDestinataireTextBox.Text = Me.AmoTableAdapter1.SelectOrganismeDestinataire(CodeRégimeComboBox.Text, CaisseGestionnaireTextBox.Text, CentregestionnaireTextBox.Text)
            CentreInformatiqueTextBox.Text = Me.AmoTableAdapter1.SelectCentreInformatique(CodeRégimeComboBox.Text, CaisseGestionnaireTextBox.Text, CentregestionnaireTextBox.Text)
            libelléTextBox.Text = Me.AmoTableAdapter1.SelectLibellé(CodeRégimeComboBox.Text, CaisseGestionnaireTextBox.Text, CentregestionnaireTextBox.Text)
        End If

        OrganismeDestinataireTextBox.Enabled = True
        CentreInformatiqueTextBox.Enabled = True
        libelléTextBox.Enabled = True

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If OrganismeDestinataireTextBox.TextLength <> 3 Then Exit Sub
        If CentreInformatiqueTextBox.TextLength <> 3 Then Exit Sub
        If libelléTextBox.Text = "" Then Exit Sub
        Select Case Me.AmoTableAdapter1.SelectCount(CodeRégimeComboBox.Text, CaisseGestionnaireTextBox.Text, CentregestionnaireTextBox.Text)
            Case 0
                Me.AmoTableAdapter1.Insert(CodeRégimeComboBox.Text, CaisseGestionnaireTextBox.Text, CentregestionnaireTextBox.Text, OrganismeDestinataireTextBox.Text, CentreInformatiqueTextBox.Text, libelléTextBox.Text)
            Case 1
                Me.AmoTableAdapter1.UpdateQuery(CodeRégimeComboBox.Text, CaisseGestionnaireTextBox.Text, CentregestionnaireTextBox.Text, OrganismeDestinataireTextBox.Text, CentreInformatiqueTextBox.Text, libelléTextBox.Text)
            Case Else
                MsgBox("IL EXISTE PLUSIEURS ENREGISTREMENT POUR CES CODE AMO" + Chr(10) + "CORRIGEZ CETTE ERREUR GRAVE", MsgBoxStyle.Critical)
        End Select
        Me.Close()
    End Sub


End Class