
' ============================================
' frmCustomer - إضافة عميل
' ============================================

Option Explicit

Private Sub UserForm_Initialize()
    ClearFields
End Sub

' مسح الحقول
Private Sub ClearFields()
    txtCodCustomer.Value = ""
    txtCustomerName.Value = ""
    txtphone.Value = ""
    txtEmail.Value = ""
    txtadress.Value = ""
    txtNot.Value = ""
End Sub

' حفظ العميل
Private Sub btnAddSave_Click()
    ' التحقق من البيانات
    If Trim(txtCodCustomer.Value) = "" Then
        MsgBox "يرجى إدخال كود العميل", vbExclamation
        txtCodCustomer.SetFocus
        Exit Sub
    End If
    
    If Trim(txtCustomerName.Value) = "" Then
        MsgBox "يرجى إدخال اسم العميل", vbExclamation
        txtCustomerName.SetFocus
        Exit Sub
    End If
    
    Dim tbl As ListObject
    Dim i As Long
    
    Set tbl = GetTableByName("tlbcustomer")
    If tbl Is Nothing Then Exit Sub
    
    ' التحقق من عدم وجود الكود
    For i = 1 To tbl.ListRows.count
        If UCase(Trim(tbl.ListRows(i).Range(1).Value)) = UCase(Trim(txtCodCustomer.Value)) Then
            MsgBox "كود العميل موجود مسبقاً", vbExclamation
            Exit Sub
        End If
    Next i
    
    ' إضافة العميل
    Dim newRow As ListRow
    Set newRow = tbl.ListRows.Add
    newRow.Range(1).Value = txtCodCustomer.Value
    newRow.Range(2).Value = txtCustomerName.Value
    newRow.Range(3).Value = txtphone.Value
    newRow.Range(4).Value = txtEmail.Value
    newRow.Range(5).Value = txtadress.Value
    newRow.Range(6).Value = txtNot.Value
    
    MsgBox "تم إضافة العميل بنجاح", vbInformation
    ClearFields
    Me.Hide
End Sub


