
' ============================================
' frmHome - لوحة التحكم الرئيسية
' ============================================

Option Explicit


Private Sub btnCard_Click()
    frmItemCard.Show
End Sub

Private Sub btnItimeCard_Click()
    frmItemCard.Show
End Sub

Private Sub btnJard_Click()
    frmJard.Show
End Sub

Private Sub UserForm_Initialize()
    RefreshDashboard
End Sub

' تحديث لوحة التحكم
Public Sub RefreshDashboard()
    Dim totalStockValue As Double
    Dim lowStockCount As Long
    Dim totalProducts As Long
    Dim totalIn As Long
    Dim totalOut As Long
    Dim i As Long
    Dim tbl As ListObject
    
    ' حساب إجمالي قيمة المخزون
    Set tbl = GetTableByName("tlbInventory")
    If Not tbl Is Nothing Then
        totalStockValue = 0
        lowStockCount = 0
        totalProducts = tbl.ListRows.count
        
        For i = 1 To tbl.ListRows.count
            totalStockValue = totalStockValue + CDbl(tbl.ListRows(i).Range(8).Value)
            If UCase(Trim(tbl.ListRows(i).Range(11).Value)) = "منخفض" Then
                lowStockCount = lowStockCount + 1
            End If
        Next i
    End If
    
    ' حساب عدد حركات الوارد والصادر
    Set tbl = GetTableByName("tlbTransaction")
    If Not tbl Is Nothing Then
        totalIn = 0
        totalOut = 0
        For i = 1 To tbl.ListRows.count
            If UCase(Trim(tbl.ListRows(i).Range(3).Value)) = "وارد" Then
                totalIn = totalIn + 1
            ElseIf UCase(Trim(tbl.ListRows(i).Range(3).Value)) = "صادر" Then
                totalOut = totalOut + 1
            End If
        Next i
    End If
    
    ' تحديث Labels
    Label2.Caption = Format(totalStockValue, "#,##0.00") & " ريال"
    Label3.Caption = lowStockCount
    lblTotalProductNow.Caption = totalProducts
    Label5.Caption = totalIn
    Label6.Caption = totalOut
    
    ' تعبئة ListBoxfrmHome بآخر الحركات
    LoadRecentTransactions
End Sub

' تحميل آخر الحركات في ListBoxfrmHome (عرض أعمدة مثل ListView)
Private Sub LoadRecentTransactions()
    Dim tbl As ListObject
    Dim i As Long
    Dim count As Long
    Dim maxRows As Long
    Dim r As Long
    
    ListBoxfrmHome.Clear
    
    ' إعداد ListBox لعرض 8 أعمدة
    ListBoxfrmHome.ColumnCount = 8
    ListBoxfrmHome.ColumnWidths = "100;100;100;100;100;100;100;100"
    
    ' صف العناوين (كل عمود على حدة)
    ListBoxfrmHome.AddItem "رقم الحركة"
    r = ListBoxfrmHome.ListCount - 1
    ListBoxfrmHome.List(r, 1) = "التاريخ"
    ListBoxfrmHome.List(r, 2) = "نوع"
    ListBoxfrmHome.List(r, 3) = "اسم الصنف"
    ListBoxfrmHome.List(r, 4) = "الكمية"
    ListBoxfrmHome.List(r, 5) = "العميل/المورد"
    ListBoxfrmHome.List(r, 6) = "الإجمالي"
    ListBoxfrmHome.List(r, 7) = "رقم الفاتورة"
    
    Set tbl = GetTableByName("tlbTransaction")
    If tbl Is Nothing Then Exit Sub
    
    ' عرض آخر 50 حركة (من الأحدث للأقدم)
    maxRows = 50
    count = 0
    For i = tbl.ListRows.count To 1 Step -1
        If count >= maxRows Then Exit For
        
        ListBoxfrmHome.AddItem tbl.ListRows(i).Range(1).Value
        r = ListBoxfrmHome.ListCount - 1
        ListBoxfrmHome.List(r, 1) = Format(tbl.ListRows(i).Range(2).Value, "yyyy/mm/dd")
        ListBoxfrmHome.List(r, 2) = tbl.ListRows(i).Range(3).Value
        ListBoxfrmHome.List(r, 3) = tbl.ListRows(i).Range(5).Value
        ListBoxfrmHome.List(r, 4) = Format(tbl.ListRows(i).Range(6).Value, "#,##0.00")
        ListBoxfrmHome.List(r, 5) = tbl.ListRows(i).Range(10).Value
        ListBoxfrmHome.List(r, 6) = Format(tbl.ListRows(i).Range(8).Value, "#,##0.00")
        ListBoxfrmHome.List(r, 7) = tbl.ListRows(i).Range(11).Value
        
        count = count + 1
    Next i
End Sub

' فتح فورم المنتجات
Private Sub btnfrmproduct_Click()
    frmProduct.Show
End Sub

' فتح فورم الوارد
Private Sub btnWared_Click()
    frmWared.Show
End Sub

' فتح فورم الصادر
Private Sub btnSader_Click()
    frmSader.Show
End Sub

' فتح فورم التقارير
Private Sub btnReport_Click()
    frmReport.Show
End Sub

' فتح ملف Excel وإغلاق الفورم
Private Sub btnExcel_Click()
    Dim ws As Worksheet
    
    ' إظهار جميع أوراق العمل
    On Error Resume Next
    For Each ws In ThisWorkbook.Worksheets
        ws.Visible = xlSheetVisible
    Next ws
    On Error GoTo 0
    
    ' إظهار Excel
    Application.Visible = True
    ThisWorkbook.Activate
    
    ' إغلاق الفورم
    Me.Hide
    Unload Me
End Sub

' تسجيل الخروج
Private Sub btnOut_Click()
    Dim response As VbMsgBoxResult
    
    ' تأكيد الخروج
    response = MsgBox("هل أنت متأكد من الخروج؟", vbYesNo + vbQuestion, "تسجيل الخروج")
    If response = vbNo Then Exit Sub
    
    ' حفظ الملف
    ThisWorkbook.Save
    
    ' إغلاق الفورم
    Me.Hide
    Unload Me
    
    ' إغلاق Excel نهائياً
    Application.Quit
End Sub

' تحديث البيانات
Private Sub btnRefresh_Click()
    RefreshDashboard
End Sub





