' ============================================
' frmItimeData - إضافة/تعديل منتج
' ============================================

Option Explicit
Public isEditMode As Boolean
Public editProductCode As String

Private Sub UserForm_Initialize()
    isEditMode = False
    editProductCode = ""
    txtProductCode.Value = ""
    txtProductName.Value = ""
    txtUnit.Value = ""
    txtPurchasePrice.Value = ""
    txtSalePrice.Value = ""
    txtNotes.Value = ""
End Sub

' حفظ المنتج
Private Sub btnAddSave_Click()
    ' التحقق من البيانات
    If Trim(txtProductCode.Value) = "" Then
        MsgBox "يرجى إدخال رمز الصنف", vbExclamation
        txtProductCode.SetFocus
        Exit Sub
    End If
    
    If Trim(txtProductName.Value) = "" Then
        MsgBox "يرجى إدخال اسم الصنف", vbExclamation
        txtProductName.SetFocus
        Exit Sub
    End If
    
    If Not IsNumeric(txtPurchasePrice.Value) Or CDbl(txtPurchasePrice.Value) < 0 Then
        MsgBox "يرجى إدخال سعر شراء صحيح", vbExclamation
        txtPurchasePrice.SetFocus
        Exit Sub
    End If
    
    If Not IsNumeric(txtSalePrice.Value) Or CDbl(txtSalePrice.Value) < 0 Then
        MsgBox "يرجى إدخال سعر بيع صحيح", vbExclamation
        txtSalePrice.SetFocus
        Exit Sub
    End If
    
    Dim tbl As ListObject
    Dim i As Long
    Dim found As Boolean
    
    Set tbl = GetTableByName("tlbproduct")
    If tbl Is Nothing Then Exit Sub
    
    If isEditMode Then
        ' تحديث المنتج
        found = False
        For i = 1 To tbl.ListRows.count
            If UCase(Trim(tbl.ListRows(i).Range(1).Value)) = UCase(Trim(editProductCode)) Then
                tbl.ListRows(i).Range(1).Value = txtProductCode.Value
                tbl.ListRows(i).Range(2).Value = txtProductName.Value
                tbl.ListRows(i).Range(3).Value = txtUnit.Value
                tbl.ListRows(i).Range(4).Value = CDbl(txtPurchasePrice.Value)
                tbl.ListRows(i).Range(5).Value = CDbl(txtSalePrice.Value)
                tbl.ListRows(i).Range(8).Value = Date ' تاريخ آخر تحديث
                tbl.ListRows(i).Range(11).Value = txtNotes.Value
                found = True
                MsgBox "تم تحديث المنتج بنجاح", vbInformation
                Exit For
            End If
        Next i
        
        If Not found Then
            MsgBox "لم يتم العثور على المنتج", vbExclamation
        End If
    Else
        ' التحقق من عدم وجود المنتج
        For i = 1 To tbl.ListRows.count
            If UCase(Trim(tbl.ListRows(i).Range(1).Value)) = UCase(Trim(txtProductCode.Value)) Then
                MsgBox "رمز الصنف موجود مسبقاً", vbExclamation
                Exit Sub
            End If
        Next i
        
        ' إضافة منتج جديد
        Dim newRow As ListRow
        Set newRow = tbl.ListRows.Add
        newRow.Range(1).Value = txtProductCode.Value
        newRow.Range(2).Value = txtProductName.Value
        newRow.Range(3).Value = txtUnit.Value
        newRow.Range(4).Value = CDbl(txtPurchasePrice.Value)
        newRow.Range(5).Value = CDbl(txtSalePrice.Value)
        newRow.Range(6).Value = Date ' تاريخ الإضافة
        newRow.Range(7).Value = "نشط" ' حالة المنتج
        newRow.Range(8).Value = Date ' تاريخ آخر تحديث
        newRow.Range(9).Value = CDbl(txtPurchasePrice.Value) ' آخر سعر شراء
        newRow.Range(10).Value = CDbl(txtSalePrice.Value) ' آخر سعر بيع
        newRow.Range(11).Value = txtNotes.Value
        
        ' إضافة سجل في Inventory
        Dim invTbl As ListObject
        Set invTbl = GetTableByName("tlbInventory")
        If Not invTbl Is Nothing Then
            Dim invRow As ListRow
            Set invRow = invTbl.ListRows.Add
            invRow.Range(1).Value = txtProductCode.Value
            invRow.Range(2).Value = txtProductName.Value
            invRow.Range(3).Value = 0 ' المخزون أول الفترة
            invRow.Range(4).Value = CDbl(txtPurchasePrice.Value) ' سعر الشراء أول الفترة
            invRow.Range(5).Value = 0 ' الكمية الواردة
            invRow.Range(6).Value = 0 ' الكمية الصادرة
            invRow.Range(7).Value = 0 ' المخزون الحالي
            invRow.Range(8).Value = 0 ' قيمة المخزون الحالي
            invRow.Range(9).Value = Date ' آخر حركة
            invRow.Range(10).Value = 10 ' حد التنبية
            invRow.Range(11).Value = "كافي" ' الحالة
        End If
        
        MsgBox "تم إضافة المنتج بنجاح", vbInformation
    End If
    
    ' إعادة تعيين الحقول
    isEditMode = False
    editProductCode = ""
    txtProductCode.Value = ""
    txtProductName.Value = ""
    txtUnit.Value = ""
    txtPurchasePrice.Value = ""
    txtSalePrice.Value = ""
    txtNotes.Value = ""
    
    Me.Hide
End Sub


