' 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 PrescripteursBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrescripteursBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.PrescripteursBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.PharmaDataSet)

    End Sub

    Private Sub Edition_Prescripteurs_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO : cette ligne de code charge les données dans la table 'PharmaDataSet.Prescripteurs'. Vous pouvez la déplacer ou la supprimer selon vos besoins.
        Me.PrescripteursTableAdapter.Fill(Me.PharmaDataSet.Prescripteurs)

    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
        CodeSpécialitéTextBox.Text = Mid(ComboBox_Spécialités.Text, ComboBox_Spécialités.Text.Length - 1)
    End Sub



    Private Sub CléTextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles CléTextBox.Validated

        If Not IsNumeric(CléTextBox.Text) Then CléTextBox.Text = ""

        If CléTextBox.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(CléTextBox.Text) <> clé_calculée Then
            MsgBox("Clé invalide", MsgBoxStyle.Exclamation)
            CléTextBox.Text = ""
        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 <> 8 Then
            MsgBox("8 caractères obligatoires", MsgBoxStyle.Exclamation)
            NumidentificationTextBox.Text = ""
        End If
    End Sub

    Private Sub CléRPPSTextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles CléRPPSTextBox.Validated

        If Not IsNumeric(CléRPPSTextBox.Text) Then CléRPPSTextBox.Text = ""

        If CléRPPSTextBox.Text = "" Then Exit Sub


        Dim chars() As String
        ReDim chars(7)
        For i = 0 To 7
            chars(i) = Mid(NumRPPSTextBox.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(CléRPPSTextBox.Text) <> clé_calculée Then
            MsgBox("Clé invalide", MsgBoxStyle.Exclamation)
            CléRPPSTextBox.Text = ""
        End If
    End Sub
End Class