Public Class Categories
    Private Sub Categories_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Icon = frmMain.Icon
        ClearCategoryDataAndAutoNumber()
    End Sub

    Private Sub SuperTabItem1_Click(sender As Object, e As EventArgs) Handles SuperTabItem1.Click
        ClearCategoryDataAndAutoNumber()
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Hide()
    End Sub


    Private Sub btnMinimuz_Click(sender As Object, e As EventArgs) Handles btnMinimuz.Click
        WindowState = FormWindowState.Minimized
    End Sub

    Public Sub ClearCategoryDataAndAutoNumber()
        txtCatNo.Text = ""
        Try

            txtCatNo.Text = DB.GetData("select max(Category_Number)+1 from Category").Rows(0)(0).ToString()
            If (txtCatNo.Text.Trim() = "") Then
                txtCatNo.Text = "1"
            End If
            txtCatName.Text = ""
            txtCatName.Select()

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try

    End Sub


    Private Sub btnAddNewCat_Click(sender As Object, e As EventArgs) Handles btnAddNewCat.Click
        ClearCategoryDataAndAutoNumber()
    End Sub

    Private Sub btnAddCategory_Click(sender As Object, e As EventArgs) Handles btnAddCategory.Click
        If (txtCatNo.Text.Trim() = "") Then

            Messages.MsgInformation("يفترض ضغط *جديد لبدء عملية جديدة من إضافة العناصر تم إصلاح الخطأ")
            BtnNewCat.PerformClick()

        ElseIf (txtCatName.Text.Trim() = "") Then

            Messages.MsgInformation("لا يمكنك ترك أسم التفريغ فارغ")
            txtCatName.Select()

        Else

            Try
                txtCatNo.Text = DB.GetData("select max(Category_Number)+1 from Category").Rows(0)(0).ToString()
                If (txtCatNo.Text.Trim() = "") Then txtCatNo.Text = "1"

                DB.Run("insert into Category values (" + txtCatNo.Text.Replace("'", "") + ", '" +
                 txtCatName.Text.Replace("'", "") + "' )")

                Messages.MsgInformation("عملية ناجحة تم إضافة التصنيف")
                ClearCategoryDataAndAutoNumber()

            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

        End If

    End Sub


    Private Sub ClearCatEdit()
        OpenButton()
        ShowCategoryEdit()
        CategorySearchToEdit.SetCategoryAll()
        CategorySearchToEdit.SetIndex(-1)
        txtEditCategoryNumber.Clear()
        txtEditCategoryName.Clear()
    End Sub







    Private Sub cbxCategory_SelectedValueChanged(sender As Object, e As EventArgs) Handles CategorySearchToEdit.SelectedValueChanged
        ShowCategoryEdit()
    End Sub

    Private Sub OpenButton()
        If txtEditCategoryNumber.Text.Trim() = "" Then
            btnEditCategory.Enabled = False
            btnEditCategory.ForeColor = Color.Black
        Else
            btnEditCategory.Enabled = True
            btnEditCategory.ForeColor = Color.White
        End If
    End Sub
    Private Sub ShowCategoryEdit()
        Try

            If (CategorySearchToEdit.IsSelected()) Then

                Dim tblcategory As DataTable = DB.GetData("select * from Category where Category_Number = " + CategorySearchToEdit.GetValue())
                txtEditCategoryNumber.Text = tblcategory.Rows(0)(0).ToString()
                txtEditCategoryName.Text = tblcategory.Rows(0)(1).ToString()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try


    End Sub
    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        ShowCategoryEdit()
        OpenButton()
    End Sub

    Private Sub SuperTabItem2_Click(sender As Object, e As EventArgs) Handles SuperTabItem2.Click
        ClearCatEdit()
    End Sub

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

    End Sub

    Private Sub CategorySearchToEdit_SelectedValueChanged(sender As Object, e As EventArgs)
        ShowCategoryEdit()
    End Sub

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

    End Sub

    Private Sub btnEditCategory_Click(sender As Object, e As EventArgs) Handles btnEditCategory.Click
        Try
            If txtEditCategoryNumber.Text.Trim() = "" Then
                MessageBox.Show("حدث خطأ يرجي اعادة المحاولة بعد إعادة تشغيل البرنامج وإذا استمر العطل تواصل مع المبرمج", "رسائل الاخطاء", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading)
            ElseIf txtEditCategoryName.Text.Trim() = "" Then
                MessageBox.Show("لا يمكنك ترك اسم التصنيف فارغا", "رسائل الاخطاء", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading)
            Else
                DB.Run("Update Category Set Category_Name = '" + txtEditCategoryName.Text.Replace("'", "") + "' where Category_Number = " + txtEditCategoryNumber.Text)
                MessageBox.Show("تم تعديل التصنيف بنجاح", "رسائل الاعلامات", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading)
                ClearCatEdit()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            ClearCatEdit()
        Finally
            OpenButton()
        End Try

    End Sub

    Private Sub btnSeachCategoryToEdit_Click(sender As Object, e As EventArgs) Handles btnSeachCategoryToEdit.Click
        Dim frmnum As New SearchNumber
        frmnum.ShowDialog()
        txtEditCategoryNumber.Text = Parameters.CatNo
        txtEditCategoryName.Text = Parameters.CatName
        OpenButton()
    End Sub

    Private Sub btnCatSearchShowAll_Click(sender As Object, e As EventArgs) Handles btnCatSearchShowAll.Click
        dgvCatSearch.DataSource = DB.GetData("Select * from Category")
    End Sub

    Private Sub txtCatSearchByNameManual_TextChanged(sender As Object, e As EventArgs) Handles txtCatSearchByNameManual.TextChanged
        Try

            dgvCatSearch.DataSource = DB.GetData("Select * from Category where Category_Name like '%" + txtCatSearchByNameManual.Text + "%'")

        Catch ex As Exception
            MessageBox.Show(ex.Message)


        End Try
    End Sub

    Private Sub btnCatSearchByNum_Click(sender As Object, e As EventArgs) Handles btnCatSearchByNum.Click
        Dim frmnum As New SearchNumber
        frmnum.ShowDialog()
        dgvCatSearch.DataSource = DB.GetData("Select * from Category Where Category_Number = " + Parameters.CatNo.ToString())

    End Sub
    Private Sub ShowCategorySearch()
        Try

            If (CategoryForSearch.IsSelected()) Then

                Dim tblcategory As DataTable = DB.GetData("select * from Category where Category_Number = " + CategoryForSearch.GetValue())
                dgvCatSearch.DataSource = tblcategory
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try


    End Sub


    Private Sub btnCatSearch_Click(sender As Object, e As EventArgs) Handles btnCatSearch.Click
        ShowCategorySearch()
    End Sub

    Private Sub SuperTabItem3_Click(sender As Object, e As EventArgs) Handles SuperTabItem3.Click
        CategoryForSearch.SetCategoryAll()
        CategoryForSearch.SetIndex(-1)
        txtCatSearchByNameManual.Clear()
        btnCatSearchShowAll.PerformClick()
    End Sub

    Private Sub btnSearchByNumDelCat_Click(sender As Object, e As EventArgs) Handles btnSearchByNumDelCat.Click
        Dim frmnum As New SearchNumber
        frmnum.ShowDialog()
        txtCatNoDel.Text = Parameters.CatNo
        txtCatNameDel.Text = Parameters.CatName
        OpenButtonDelete()

    End Sub
    Private Sub OpenButtonDelete()
        If txtCatNoDel.Text.Trim() = "" Then
            btnCategoryDelete.Enabled = False
            btnCategoryDelete.ForeColor = Color.Black
        Else
            btnCategoryDelete.Enabled = True
            btnCategoryDelete.ForeColor = Color.White
        End If
    End Sub
    Private Sub ShowCategoryDelete()
        Try

            If (CategoryForDelete.IsSelected()) Then

                Dim tblcategory As DataTable = DB.GetData("select * from Category where Category_Number = " + CategoryForDelete.GetValue())
                txtCatNoDel.Text = tblcategory.Rows(0)(0).ToString()
                txtCatNameDel.Text = tblcategory.Rows(0)(1).ToString()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try


    End Sub
    Private Sub btnSearchCategoryForDelete_Click(sender As Object, e As EventArgs) Handles btnSearchCategoryForDelete.Click
        ShowCategoryDelete()
        OpenButtonDelete()
    End Sub

    Private Sub SuperTabItem4_Click(sender As Object, e As EventArgs) Handles SuperTabItem4.Click
        CategoryForDelete.SetCategoryAll()
        CategoryForDelete.SetIndex(-1)
        txtCatNoDel.Clear()
        txtCatNameDel.Clear()
    End Sub
    Private Sub ClearCatDelete()
        OpenButtonDelete()
        CategoryForDelete.SetCategoryAll()
        CategoryForDelete.SetIndex(-1)
        txtCatNoDel.Clear()
        txtCatNameDel.Clear()
    End Sub
    Private Sub btnCategoryDelete_Click(sender As Object, e As EventArgs) Handles btnCategoryDelete.Click
        Try
            If txtCatNoDel.Text.Trim() = "" Then
                MessageBox.Show("حدث خطأ يرجي اعادة المحاولة بعد إعادة تشغيل البرنامج وإذا استمر العطل تواصل مع المبرمج", "رسائل الاخطاء", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading)
            ElseIf String.IsNullOrEmpty(txtCatNameDel.Text) Then
                MessageBox.Show("حدث خطأ يرجي اعادة المحاولة بعد إعادة تشغيل البرنامج وإذا استمر العطل تواصل مع المبرمج", "رسائل الاخطاء", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading)
            Else
                Dim answer As MsgBoxResult
                answer = MsgBox("هل متأكد من إتمام عملية الحذف؟", vbExclamation + vbYesNo, "حذف التصنيف")
                If answer = MsgBoxResult.Yes Then
                    DB.Run("Delete from Category Where Category_Number =" + txtCatNoDel.Text)
                    MessageBox.Show("تم حذف التصنيف بنجاح", "رسائل الاعلامات", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading)
                    ClearCatDelete()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            ClearCatDelete()
        Finally
            OpenButtonDelete()
        End Try
    End Sub
    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_MouseUp(sender As Object, e As MouseEventArgs) Handles pnlMain.MouseUp
        MouseDown = False
        pnlMain.Cursor = Cursors.Default
    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
End Class