Imports System.Data.SqlClient
Imports System.IO, Store_Managment.Messages

Public Class frmItems

#Region "FrmSettings"

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

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

    Private MouseDown As Boolean = False
    Private LastLoc As Point
    Private Sub pnlMain_MouseMove(sender As Object, e As MouseEventArgs)
        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 Region
    Private Sub frmItems_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Icon = frmMain.Icon
        dgvitems.DataSource = DB.GetData("Select * from Item")
        dgvitems.ClearSelection()
        ClearAndAutoNumber()
        tsAdd.Enabled = True
        tsEdit.Enabled = False
        tsDelete.Enabled = False
    End Sub

    'مثال لعمل إضافة جديد
    Private Sub ClearAndAutoNumber()
        txtItemNumber.Text = DB.GetData("select max(Item_Number)+1 from Item").Rows(0)(0).ToString()
        If (txtItemNumber.Text.Trim() = "") Then txtItemNumber.Text = "1"
        txtItemName.Clear()
        txtItem_Quantity.Clear()
        txtItemBuyPrice.Text = ""
        txtItemSellPrice.Text = ""
        CategoryForItem.SetIndex(-1)
        txtItemName.Select()
    End Sub

    Private Sub txtItem_Quantity_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtItem_Quantity.KeyPress, txtItemSellPrice.KeyPress, txtItemBuyPrice.KeyPress
        If (Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar)) Then
            e.Handled = True
        End If
    End Sub



    Private Sub dgvitems_SelectionChanged(sender As Object, e As EventArgs) Handles dgvitems.SelectionChanged

        tsAdd.Enabled = False
        tsEdit.Enabled = True
        tsDelete.Enabled = True
        If (dgvitems.SelectedRows.Count > 0) Then
            txtItemNumber.Text = dgvitems.SelectedRows(0).Cells(0).Value.ToString()
            txtItemName.Text = dgvitems.SelectedRows(0).Cells(1).Value.ToString()
            CategoryForItem.GetItems().Text = dgvitems.SelectedRows(0).Cells(2).Value.ToString()
            txtItem_Quantity.Text = dgvitems.SelectedRows(0).Cells(3).Value.ToString()
            txtItemBuyPrice.Text = dgvitems.SelectedRows(0).Cells(4).Value.ToString()
            txtItemSellPrice.Text = dgvitems.SelectedRows(0).Cells(5).Value.ToString()
        End If


    End Sub


    Private Sub tsNew_Click(sender As Object, e As EventArgs) Handles tsNew.Click
        dgvitems.ClearSelection()
        ClearAndAutoNumber()
        tsAdd.Enabled = True
        tsEdit.Enabled = False
        tsDelete.Enabled = False
    End Sub

    Private Sub tsAdd_Click(sender As Object, e As EventArgs) Handles tsAdd.Click
        Try
            If (String.IsNullOrEmpty(txtItemNumber.Text)) Then
                MsgError("حدث خطأ ما سيتم محاولة إصلاحة وإذا استمر العطل تواصل مع المصمم")
                ClearAndAutoNumber()
            ElseIf (String.IsNullOrEmpty(txtItemName.Text)) Then
                MsgInformation("من فضلك قم بإدخال اسم الصنف أولاً")
                txtItemName.Select()
            ElseIf (txtItem_Quantity.Text.Trim() = "") Then
                MsgInformation("من فضلك قم بإدخال الرصيد الافتتاحي")
                txtItem_Quantity.Select()
            ElseIf (String.IsNullOrEmpty(txtItemBuyPrice.Text)) Then
                MsgInformation("من فضلك قم بإدخال سعر الشراء")
                txtItemBuyPrice.Select()
            ElseIf (txtItemSellPrice.Text.Trim() = "") Then
                MsgInformation("من فضلك قم بإدخال سعر البيع")
                txtItemSellPrice.Select()
            ElseIf (Convert.ToInt32(txtItemBuyPrice.Text) > Convert.ToInt32(txtItemSellPrice.Text)) Then
                MsgWarrning("سعر الشراء اكبر من سعر البيع وذلك سيسبب خسائر من فضلك تأكد من السعر")
                txtItemBuyPrice.Select()
                txtItemBuyPrice.SelectAll()
            ElseIf (Not CategoryForItem.IsSelected()) Then
                MsgInformation("من فضلك قم بإدخال تصنيف الصنف")
            Else
                DB.Run("Insert Into Item Values(" + txtItemNumber.Text + " , '" + txtItemName.Text + "' , '" + CategoryForItem.GetItems().Text + "' , " + txtItem_Quantity.Text + " , " + txtItemBuyPrice.Text + " , " + txtItemSellPrice.Text + ")")
                MsgInformation("عملية ناجحة تم إضافة الصنف بنجاح")
                ClearAndAutoNumber()
                dgvitems.DataSource = DB.GetData("Select * from Item")
            End If
        Catch ex As Exception
            MsgError(ex.Message())
            ClearAndAutoNumber()
            dgvitems.DataSource = DB.GetData("Select * from Item")
        End Try
    End Sub



    Private Sub tsEdit_Click(sender As Object, e As EventArgs) Handles tsEdit.Click
        Try
            If (String.IsNullOrEmpty(txtItemNumber.Text)) Then
                MsgError("حدث خطأ ما سيتم محاولة إصلاحة وإذا استمر العطل تواصل مع المصمم")
                ClearAndAutoNumber()
            ElseIf (String.IsNullOrEmpty(txtItemName.Text)) Then
                MsgInformation("لا يمكنك ترك اسم الصنف فارغ")
                txtItemName.Select()
            ElseIf (txtItem_Quantity.Text.Trim() = "") Then
                MsgInformation("يجب إدخال رصيد")
                txtItem_Quantity.Select()
            ElseIf (String.IsNullOrEmpty(txtItemBuyPrice.Text)) Then
                MsgInformation("لا يمكنك ترك سعر الشراء فارغ")
                txtItemBuyPrice.Select()
            ElseIf (txtItemSellPrice.Text.Trim() = "") Then
                MsgInformation("لا يمكنك ترك سعر البيع فارغ")
                txtItemSellPrice.Select()
            ElseIf (Convert.ToInt32(txtItemBuyPrice.Text) > Convert.ToInt32(txtItemSellPrice.Text)) Then
                MsgWarrning("سعر الشراء اكبر من سعر البيع وذلك سيسبب خسائر من فضلك تأكد من السعر")
                txtItemBuyPrice.Select()
                txtItemBuyPrice.SelectAll()
            ElseIf (Not CategoryForItem.IsSelected()) Then
                MsgInformation("من فضلك قم بإدخال تصنيف الصنف")
            Else
                DB.Run("Update  Item Set Item_Number=" + txtItemNumber.Text + " , Item_Name='" + txtItemName.Text + "' , Item_Category='" + CategoryForItem.GetItems().Text + "' , Item_Quantity=" + txtItem_Quantity.Text + " , Buy_Price=" + txtItemBuyPrice.Text + " , Sell_Price=" + txtItemSellPrice.Text + " where Item_Number = " + txtItemNumber.Text + " ")
                MsgInformation("عملية ناجحة تم تعديل الصنف بنجاح")
                dgvitems.DataSource = DB.GetData("Select * from Item")
                ClearAndAutoNumber()
                dgvitems.ClearSelection()
                tsAdd.Enabled = True
                tsDelete.Enabled = False
                tsEdit.Enabled = False
            End If
        Catch ex As Exception
            MsgError(ex.Message())
            ClearAndAutoNumber()
            dgvitems.DataSource = DB.GetData("Select * from Item")

        End Try
    End Sub

    Private Sub tsDelete_Click(sender As Object, e As EventArgs) Handles tsDelete.Click
        Try
            Dim Delete As DialogResult
            Delete = MessageBox.Show("هل متأكد من استمرار عملية الحذف؟", "حذف الصنف", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading)
            If (Delete = DialogResult.Yes) Then
                DB.Run("Delete from Item Where Item_Number=" + txtItemNumber.Text)
                MsgInformation("تم حذف الصنف بنجاح")
                dgvitems.DataSource = DB.GetData("Select * from Item")
                ClearAndAutoNumber()
                dgvitems.ClearSelection()
                tsAdd.Enabled = True
                tsDelete.Enabled = False
                tsEdit.Enabled = False
            Else
                Return
            End If



        Catch ex As Exception
            MsgError(ex.Message)
        End Try
    End Sub

    Private Sub btnAddCategory_Click(sender As Object, e As EventArgs)
        Dim Report As New FrmRptItem
        Report.ShowDialog()
    End Sub


End Class