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

    Private Sub UsersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
        Me.Validate()
        Me.UsersBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.PharmaDataSet)

    End Sub

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

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Me.UsersTableAdapter.GetDataBy_login(LoginTextBox.Text).Rows.Count > 0 Then
            Me.UsersTableAdapter.Update_User(PictureTextBox.Text, Comptage_boitesCheckBox.Checked, LoginTextBox.Text)
        Else
            Me.UsersTableAdapter.Insert(LoginTextBox.Text, "", PictureTextBox.Text, Comptage_boitesCheckBox.Checked)
        End If
        Me.Close()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Password.Changer_mdp_CheckBox.Checked = False
        Dim the_password As String = ""
        MyInptBox.my_showdialog("SUPPRESSION COMPTE " & LoginTextBox.Text & vbCrLf & vbCrLf & "Saisir mot de passe du compte.", VarType(the_password), 100, "")
        the_password = MyInptBox.result
        If Password.check_password(LoginTextBox.Text, the_password) = True Then
            Me.UsersTableAdapter.Delete_By_Login(LoginTextBox.Text)
            Me.Close()
        End If

    End Sub



    Private Sub PictureTextBox_TextChanged(sender As Object, e As EventArgs) Handles PictureTextBox.TextChanged
        Try
            If PictureTextBox.Text = "" Then
                user_picture.BackgroundImage = Nothing
                user_picture.Text = "cliquez pour charger image"
            Else
                user_picture.BackgroundImage = System.Drawing.Image.FromFile(Config.LocalNetwork_ServerName & "\images\users\" & PictureTextBox.Text)
                user_picture.Text = ""
            End If
        Catch ex As Exception

        End Try
       
    End Sub

    Private Sub user_picture_Click(sender As Object, e As EventArgs) Handles user_picture.Click
        OpenFileDialog1.InitialDirectory = Config.LocalNetwork_ServerName & "\images\users"
        OpenFileDialog1.ShowDialog()

        Dim file = OpenFileDialog1.FileName

        If InStr(file, Config.LocalNetwork_ServerName & "\images\users\") = 0 Then Exit Sub
       
        Try
            user_picture.BackgroundImage = System.Drawing.Image.FromFile(file)
            PictureTextBox.Text = Mid(file, InStrRev(file, "\") + 1)
        Catch ex As Exception

        End Try

    End Sub
End Class