Imports Store_Managment.DB, Store_Managment.Messages

Public Class FrmLogin

#Region "Frm Settings"

    Private MouseDown As Boolean = False
    Private LastLoc As Point
    Private Sub pnlMain_MouseDown(sender As Object, e As MouseEventArgs) Handles pnlMain.MouseDown
        MouseDown = True
        pnlMain.Cursor = Cursors.NoMove2D
        LastLoc = e.Location
    End Sub
    Private Sub pnlMain_MouseMove(sender As Object, e As MouseEventArgs) Handles pnlMain.MouseMove
        If (MouseDown) Then
            Dim NewX As Integer = (Me.Location.X - LastLoc.X) + e.X
            Dim NewY As Integer = (Me.Location.Y - LastLoc.Y) + e.Y
            Me.Location = New Point(NewX, NewY)
        End If
    End Sub
    Private Sub pnlMain_MouseUp(sender As Object, e As MouseEventArgs) Handles pnlMain.MouseUp
        MouseDown = False
        pnlMain.Cursor = Cursors.Default
    End Sub
    Private Sub btnMinimuz_Click(sender As Object, e As EventArgs) Handles btnMinimuz.Click
        WindowState = FormWindowState.Minimized
    End Sub
#End Region

    Private Sub FormLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Icon = frmMain.Icon
        'فتح الداتا بيز
        DB.Open()
        'الوقوف علي اسم المستخدم
        txtusername.Select()
    End Sub


    'زر فتح فورم إغلاق البرنامج
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Dim Close As New FrmCloseYesNo
        Close.ShowDialog()
    End Sub

    ' زر تسجيل الدخول
    Private Sub btnLogin_Click(sender As Object, e As EventArgs)
        Try
            Dim LoginSecure As DataTable = DB.GetData("Select * from Login_Secure where User_Name='" + txtusername.Text + "' and User_Password ='" + txtpassword.Text + "' ")
            Dim UserNameCheck As DataTable = DB.GetData("Select User_Name From Login_Secure where User_Name ='" + txtusername.Text + "'")
            Dim PasswordCheck As DataTable = DB.GetData("Select User_Password From Login_Secure where User_Password = '" + txtpassword.Text + "' and User_Name= '" + txtusername.Text + "'")

            If (String.IsNullOrEmpty(txtusername.Text)) Then
                MsgError("لا يمكنك ترك حقل اسم المستخدم فارغ")
                txtusername.Select()
            ElseIf (String.IsNullOrEmpty(txtpassword.Text)) Then
                MsgError("من فضلك قم بإدخال كلمة المرور")
                txtpassword.Select()
            ElseIf (UserNameCheck.Rows.Count < 1) Then
                MsgError("اسم المستخدم خطأ او غير موجود")
                txtusername.Clear()
                txtpassword.Clear()
                txtusername.Select()
            ElseIf (PasswordCheck.Rows.Count < 1) Then
                MsgError("من فضلك تأكد من كلمة المرور مرة أخري")
                txtpassword.Clear()
                txtpassword.Select()
            ElseIf (LoginSecure.Rows.Count > 0) Then
                Parameters.UserNameLogin = LoginSecure.Rows(0)(0).ToString()
                Dim Main As New frmMain()
                Main.Show()
                Me.Hide()
            Else
                MsgError("بيانات غير متطابقة")
            End If
        Catch ex As Exception
            MsgError(ex.Message)
        End Try
    End Sub
    'إذا تم الضغط علي زر إنتر يذهب إلي كلمة المرور
    Private Sub txtusername_KeyDown(sender As Object, e As KeyEventArgs)
        If (e.KeyData = Keys.Enter) Then
            txtpassword.Focus()
        End If

    End Sub

    'إذا تم الضغط علي زر إنتر يقوم بتنفيذ زر تسجيل الدخول
    Private Sub txtpassword_KeyDown(sender As Object, e As KeyEventArgs)
        If (e.KeyData = Keys.Enter) Then
            btnLogin.PerformClick()
        End If

    End Sub




    ' تم تصميم هذا الاسلوب من الحماية لأن إغلاق المشروع من فورم ليست رئيسية'
    ' سوف يجعل المشروع يعمل في الخلفية لأن الفورم الرئيسي'
    ' لم يتم اغلاقه لانه فورم خفي يدير البرنامج فقط
    'متغير نوعة منطقي حالته علي الوضع حقيقي
    Dim NoCloseProject As Boolean = True
    'حدث حين يتم إغلاق الفورم
    Private Sub FormLogin_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        ' الحدث مساوي للمتغير
        e.Cancel = NoCloseProject
        ' لو كان الحدث حقيقي قم بإظهار رسالة
        If (e.Cancel = True) Then
            MessageBox.Show("من الافضل أغلاق البرنامج من زر الأغلاق لتجنب حدوث مشاكل", "معلومات الأمان", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If

    End Sub
    Private Sub panel11_Paint(sender As Object, e As PaintEventArgs) Handles panel11.Paint

    End Sub

    Private Sub btnLogin_Click_1(sender As Object, e As EventArgs) Handles btnLogin.Click

    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs)
        Try
            Dim LoginSecure As DataTable = DB.GetData("Select * from Login_Secure where User_Name='" + txtusername.Text + "' and User_Password ='" + txtpassword.Text + "' ")
            Dim UserNameCheck As DataTable = DB.GetData("Select User_Name From Login_Secure where User_Name ='" + txtusername.Text + "'")
            Dim PasswordCheck As DataTable = DB.GetData("Select User_Password From Login_Secure where User_Password = '" + txtpassword.Text + "' and User_Name= '" + txtusername.Text + "'")

            If (String.IsNullOrEmpty(txtusername.Text)) Then
                MsgError("لا يمكنك ترك حقل اسم المستخدم فارغ")
                txtusername.Select()
            ElseIf (String.IsNullOrEmpty(txtpassword.Text)) Then
                MsgError("من فضلك قم بإدخال كلمة المرور")
                txtpassword.Select()
            ElseIf (UserNameCheck.Rows.Count < 1) Then
                MsgError("اسم المستخدم خطأ او غير موجود")
                txtusername.Clear()
                txtpassword.Clear()
                txtusername.Select()
            ElseIf (PasswordCheck.Rows.Count < 1) Then
                MsgError("من فضلك تأكد من كلمة المرور مرة أخري")
                txtpassword.Clear()
                txtpassword.Select()
            ElseIf (LoginSecure.Rows.Count > 0) Then
                Parameters.UserNameLogin = LoginSecure.Rows(0)(0).ToString()
                Dim Main As New frmMain()
                Main.Show()
                Me.Hide()
            Else
                MsgError("بيانات غير متطابقة")
            End If
        Catch ex As Exception
            MsgError(ex.Message)
        End Try
    End Sub

    Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
        Try
            Dim LoginSecure As DataTable = DB.GetData("Select * from Login_Secure where User_Name='" + txtusername.Text + "' and User_Password ='" + txtpassword.Text + "' ")
            Dim UserNameCheck As DataTable = DB.GetData("Select User_Name From Login_Secure where User_Name ='" + txtusername.Text + "'")
            Dim PasswordCheck As DataTable = DB.GetData("Select User_Password From Login_Secure where User_Password = '" + txtpassword.Text + "' and User_Name= '" + txtusername.Text + "'")

            If (String.IsNullOrEmpty(txtusername.Text)) Then
                MsgError("لا يمكنك ترك حقل اسم المستخدم فارغ")
                txtusername.Select()
            ElseIf (String.IsNullOrEmpty(txtpassword.Text)) Then
                MsgError("من فضلك قم بإدخال كلمة المرور")
                txtpassword.Select()
            ElseIf (UserNameCheck.Rows.Count < 1) Then
                MsgError("اسم المستخدم خطأ او غير موجود")
                txtusername.Clear()
                txtpassword.Clear()
                txtusername.Select()
            ElseIf (PasswordCheck.Rows.Count < 1) Then
                MsgError("من فضلك تأكد من كلمة المرور مرة أخري")
                txtpassword.Clear()
                txtpassword.Select()
            ElseIf (LoginSecure.Rows.Count > 0) Then
                Parameters.UserNameLogin = LoginSecure.Rows(0)(0).ToString()
                Dim Main As New frmMain()
                Main.Show()
                Me.Hide()
            Else
                MsgError("بيانات غير متطابقة")
            End If
        Catch ex As Exception
            MsgError(ex.Message)
        End Try
    End Sub

    Private Sub cbxShowPassword_ValueChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            txtpassword.UseSystemPasswordChar = False
        Else
            txtpassword.UseSystemPasswordChar = True

        End If
    End Sub

    Private Sub txtpassword_MouseClick(sender As Object, e As MouseEventArgs) Handles txtpassword.MouseClick
        If txtpassword.Text = "ادخل كلمة المرور" Then
            txtpassword.Clear()
        End If
    End Sub

    Private Sub txtpassword_TextChanged(sender As Object, e As EventArgs) Handles txtpassword.TextChanged

    End Sub

    Private Sub PictureBox6_Click(sender As Object, e As EventArgs) Handles PictureBox6.Click
        If (conn.State = ConnectionState.Open) Then
            lblConnState.ForeColor = Color.Green
            lblConnState.Text = ""
            picwrong.Visible = False
            picRight.Visible = True
        Else
            lblConnState.ForeColor = Color.Maroon
            lblConnState.Text = ""
            picRight.Visible = False
            picwrong.Visible = True
        End If
    End Sub
End Class
