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






 

    Private Sub Edition_Prescripteurs_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        COMPTOIR_MAIN.DP_Affiche_Msg()
    End Sub





    Private Sub ComboBox_Spécialités_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox_Spécialités.SelectedIndexChanged
        If IsNumeric(Mid(ComboBox_Spécialités.Text, ComboBox_Spécialités.Text.Length - 1)) Then
            CodeSpécialitéTextBox.Text = Mid(ComboBox_Spécialités.Text, ComboBox_Spécialités.Text.Length - 1)
        End If
    End Sub



    Private Sub NumidentificationTextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumidentificationTextBox.Validated
        If NumidentificationTextBox.Text = "" Then Exit Sub

        NumidentificationTextBox.Text = UCase(NumidentificationTextBox.Text)
        If NumidentificationTextBox.Text.Length <> 9 Then
            MyMsg_Box.Presentation("Officine", "9 caractères obligatoires", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            NumidentificationTextBox.Text = ""
        End If
        If Not IsNumeric(Mid(NumidentificationTextBox.Text, 9, 1)) Then
            MyMsg_Box.Presentation("Officine", "numéro invalide", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            NumidentificationTextBox.Text = ""
        End If
        If NumidentificationTextBox.Text = "" Then Exit Sub



        Dim chars() As String
        ReDim chars(7)
        For i = 0 To 7
            chars(i) = Mid(NumidentificationTextBox.Text, i + 1, 1)
        Next
        For i = 0 To 7
            Select Case chars(i)
                Case "A"
                    chars(i) = "1"
                Case "B"
                    chars(i) = "2"
                Case "C"
                    chars(i) = "3"
                Case "D"
                    chars(i) = "4"
                Case "E"
                    chars(i) = "5"
                Case "F"
                    chars(i) = "6"
                Case "G"
                    chars(i) = "7"
                Case "H"
                    chars(i) = "8"
                Case "I"
                    chars(i) = "9"
                Case "J"
                    chars(i) = "10"
                Case "K"
                    chars(i) = "11"
                Case "L"
                    chars(i) = "12"
                Case "M"
                    chars(i) = "13"
                Case "N"
                    chars(i) = "14"
                Case "O"
                    chars(i) = "15"
                Case "P"
                    chars(i) = "16"
                Case "Q"
                    chars(i) = "17"
                Case "R"
                    chars(i) = "18"
                Case "S"
                    chars(i) = "19"
                Case "T"
                    chars(i) = "20"
                Case "U"
                    chars(i) = "21"
                Case "V"
                    chars(i) = "22"
                Case "W"
                    chars(i) = "23"
                Case "X"
                    chars(i) = "24"
                Case "Y"
                    chars(i) = "25"
                Case "z"
                    chars(i) = "26"
            End Select
        Next

        For i = 0 To 7
            If i Mod 2 = 1 Then
                chars(i) = (2 * Val(chars(i))).ToString
            End If
        Next
        Dim total As Integer
        For i = 0 To 7
            If chars(i).Length = 2 Then
                total = total + Val(Mid(chars(i), 1, 1)) + Val(Mid(chars(i), 2, 1))
            Else
                total = total + Val(chars(i))
            End If
        Next
        Dim total_string As String = total.ToString
        total_string = Mid(total_string, total_string.Length, 1)

        Dim clé_calculée As Integer = 10 - Val(total_string)
        If clé_calculée = 10 Then clé_calculée = 0

        If Val(Mid(NumidentificationTextBox.Text, 9, 1)) <> clé_calculée Then
            MyMsg_Box.Presentation("Officine", "Clé invalide", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            NumidentificationTextBox.Text = ""
        End If

    End Sub



    Private Sub NumRPPSTextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumRPPSTextBox.Validated
        If NumRPPSTextBox.Text = "" Then Exit Sub

        NumRPPSTextBox.Text = UCase(NumRPPSTextBox.Text)
        If NumRPPSTextBox.Text.Length <> 11 Then
            MyMsg_Box.Presentation("Officine", "11 caractères obligatoires", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            NumRPPSTextBox.Text = ""
        End If
        If Not IsNumeric(Mid(NumRPPSTextBox.Text, 11, 1)) Then
            MyMsg_Box.Presentation("Officine", "numéro invalide", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            NumRPPSTextBox.Text = ""
        End If
        If NumRPPSTextBox.Text = "" Then Exit Sub




        'Dim chars() As String
        'ReDim chars(10)
        'For i = 0 To 10
        '    chars(i) = Mid(NumRPPSTextBox.Text, i + 1, 1)
        'Next
        'For i = 0 To 10
        '    Select Case chars(i)
        '        Case "A"
        '            chars(i) = "1"
        '        Case "B"
        '            chars(i) = "2"
        '        Case "C"
        '            chars(i) = "3"
        '        Case "D"
        '            chars(i) = "4"
        '        Case "E"
        '            chars(i) = "5"
        '        Case "F"
        '            chars(i) = "6"
        '        Case "G"
        '            chars(i) = "7"
        '        Case "H"
        '            chars(i) = "8"
        '        Case "I"
        '            chars(i) = "9"
        '        Case "J"
        '            chars(i) = "10"
        '        Case "K"
        '            chars(i) = "11"
        '        Case "L"
        '            chars(i) = "12"
        '        Case "M"
        '            chars(i) = "13"
        '        Case "N"
        '            chars(i) = "14"
        '        Case "O"
        '            chars(i) = "15"
        '        Case "P"
        '            chars(i) = "16"
        '        Case "Q"
        '            chars(i) = "17"
        '        Case "R"
        '            chars(i) = "18"
        '        Case "S"
        '            chars(i) = "19"
        '        Case "T"
        '            chars(i) = "20"
        '        Case "U"
        '            chars(i) = "21"
        '        Case "V"
        '            chars(i) = "22"
        '        Case "W"
        '            chars(i) = "23"
        '        Case "X"
        '            chars(i) = "24"
        '        Case "Y"
        '            chars(i) = "25"
        '        Case "z"
        '            chars(i) = "26"
        '    End Select
        'Next

        'For i = 0 To 10
        '    If i Mod 2 = 1 Then
        '        chars(i) = (2 * Val(chars(i))).ToString
        '    End If
        'Next
        'Dim total As Integer
        'For i = 0 To 10
        '    If chars(i).Length = 2 Then
        '        total = total + Val(Mid(chars(i), 1, 1)) + Val(Mid(chars(i), 2, 1))
        '    Else
        '        total = total + Val(chars(i))
        '    End If
        'Next
        'Dim total_string As String = total.ToString
        'total_string = Mid(total_string, total_string.Length, 1)

        'Dim clé_calculée As Integer = 10 - Val(total_string)
        'If clé_calculée = 10 Then clé_calculée = 0

        'If Val(Mid(NumRPPSTextBox.Text, 11, 1)) <> clé_calculée Then
        '    NumRPPSTextBox.Text = ""
        'End If
    End Sub



    Private Sub Ajouter_Button_Click(sender As Object, e As EventArgs) Handles Ajouter_Button.Click
        If NumidentificationTextBox.Text.Length <> 9 Then
            MyMsg_Box.Presentation("Officine", "Erreur de numéro d'identification.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        ElseIf CodeSpécialitéTextBox.Text.Length <> 2 Or Not IsNumeric(CodeSpécialitéTextBox.Text) Then
            MyMsg_Box.Presentation("Officine", "Erreur de code spécialité.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        ElseIf NomPrénomTextBox.Text.Length = 0 Then
            MyMsg_Box.Presentation("Officine", "Pas de nom pour le prescripteur", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        ElseIf StatutComboBox.Text = "" Then
            MyMsg_Box.Presentation("Officine", "Renseigner le statut du prescripteur.", False, "", False, "", True, "OK")
            MyMsg_Box.ShowDialog()
            Exit Sub
        End If
        If Me.PrescripteursTableAdapter.GetDataBy_Numidentification_NumRPPS(NumidentificationTextBox.Text, NumRPPSTextBox.Text).Rows.Count > 0 Then
            MyMsg_Box.Presentation("Officine", "Numéro d'identification existant." & Chr(10) & "Remplacer l'enregistrement?", True, "OUI", True, "NON", False, "")
            If MyMsg_Box.ShowDialog() = Windows.Forms.DialogResult.Yes Then
                Me.PrescripteursTableAdapter.UpdateById(Me.PrescripteursTableAdapter.GetDataBy_Numidentification_NumRPPS(NumidentificationTextBox.Text, NumRPPSTextBox.Text).Item(0).IDinterne, NomPrénomTextBox.Text, _
                                                    AdresseTextBox.Text, NumidentificationTextBox.Text, StatutComboBox.Text, CodeSpécialitéTextBox.Text, _
                                                    NumRPPSTextBox.Text, NumStructureTextBox.Text, Notes_RichTextBox.Text)
            End If
        Else
            PrescripteursTableAdapter.Insert1(NumidentificationTextBox.Text & NumRPPSTextBox.Text, NomPrénomTextBox.Text, AdresseTextBox.Text, NumidentificationTextBox.Text, _
                                               StatutComboBox.Text, CodeSpécialitéTextBox.Text, NumRPPSTextBox.Text, NumStructureTextBox.Text, Notes_RichTextBox.Text)
        End If

        If COMPTOIR_MAIN.Visible = True Then
            COMPTOIR_MAIN.PrescripteurMyCombobox_Validated(Nothing, Nothing)
        End If
        Me.Close()
    End Sub

    Private Sub CodeSpécialitéTextBox_TextChanged(sender As Object, e As EventArgs) Handles CodeSpécialitéTextBox.TextChanged
        For i = 0 To ComboBox_Spécialités.Items.Count - 1
            If Mid(ComboBox_Spécialités.Items.Item(i).ToString, ComboBox_Spécialités.Items.Item(i).ToString.Length - 1) = CodeSpécialitéTextBox.Text Then
                ComboBox_Spécialités.SelectedIndex = i
                Exit Sub
            End If
        Next
    End Sub

   
    Private Sub NumidentificationTextBox_TextChanged(sender As Object, e As EventArgs) Handles NumidentificationTextBox.TextChanged
        If Not IsNumeric(NumidentificationTextBox.Text) Then
            NumidentificationTextBox.Text = ""
        
        End If
    End Sub

    Private Sub NumRPPSTextBox_TextChanged(sender As Object, e As EventArgs) Handles NumRPPSTextBox.TextChanged
        If Not IsNumeric(NumRPPSTextBox.Text) Then
            NumRPPSTextBox.Text = ""
        
        End If
    End Sub

    Private Sub NumStructureTextBox_TextChanged(sender As Object, e As EventArgs) Handles NumStructureTextBox.TextChanged
        If NumStructureTextBox.Text.Length = 14 Then
            Ajouter_Button.Focus()
        End If
    End Sub


End Class