' ============================================
' frmProduct - إدارة المنتجات
' ============================================

Option Explicit
Private selectedProductCode As String

Private Sub UserForm_Initialize()
    LoadProducts
End Sub

' تحميل المنتجات
Public Sub LoadProducts()
    Dim tbl As ListObject
    Dim i As Long
    Dim currentStock As Double
    Dim r As Long
    
    ListBox1.Clear
    ListBox1.ColumnCount = 8
    ListBox1.ColumnWidths = "100;100;100;100;100;100;100;100"
    
    ' صف العناوين
    ListBox1.AddItem "رمز الصنف"
    r = ListBox1.ListCount - 1
    ListBox1.List(r, 1) = "اسم الصنف"
    ListBox1.List(r, 2) = "الوحدة"
    ListBox1.List(r, 3) = "الرصيد الحالي"
    ListBox1.List(r, 4) = "سعر الشراء"
    ListBox1.List(r, 5) = "سعر البيع"
    ListBox1.List(r, 6) = "الحالة"
    ListBox1.List(r, 7) = "ملاحظات"
    
    Set tbl = GetTableByName("tlbproduct")
    If tbl Is Nothing Then Exit Sub
    
    For i = 1 To tbl.ListRows.count
        ' الحصول على الرصيد الحالي
        currentStock = GetCurrentStock(CStr(tbl.ListRows(i).Range(1).Value))
        
        ListBox1.AddItem tbl.ListRows(i).Range(1).Value
        r = ListBox1.ListCount - 1
        ListBox1.List(r, 1) = tbl.ListRows(i).Range(2).Value
        ListBox1.List(r, 2) = tbl.ListRows(i).Range(3).Value
        ListBox1.List(r, 3) = Format(currentStock, "#,##0.00")
        ListBox1.List(r, 4) = Format(tbl.ListRows(i).Range(4).Value, "#,##0.00")
        ListBox1.List(r, 5) = Format(tbl.ListRows(i).Range(5).Value, "#,##0.00")
        ListBox1.List(r, 6) = tbl.ListRows(i).Range(7).Value
        ListBox1.List(r, 7) = tbl.ListRows(i).Range(11).Value
    Next i
End Sub

' تحديث البيانات
Private Sub cmdRefresh_Click()
    LoadProducts
End Sub

' استيراد منتجات
Private Sub cmdImport_Click()
    frmImport.Show vbModal
    ' تحديث القائمة بعد إغلاق فورم الاستيراد
    LoadProducts
End Sub

' إضافة منتج جديد
Private Sub cmdAddNew_Click()
    frmItimeData.Show
    LoadProducts
End Sub

' تعديل منتج
Private Sub cmdEdit_Click()
    If ListBox1.ListIndex < 0 Or ListBox1.ListIndex = 0 Then
        MsgBox "يرجى اختيار منتج للتعديل", vbExclamation
        Exit Sub
    End If
    
    selectedProductCode = ListBox1.List(ListBox1.ListIndex, 0)
    Dim productData As Variant
    productData = GetProductByCode(selectedProductCode)
    
    If Not IsEmpty(productData) Then
        frmItimeData.txtProductCode.Value = productData(1)
        frmItimeData.txtProductName.Value = productData(2)
        frmItimeData.txtUnit.Value = productData(3)
        frmItimeData.txtPurchasePrice.Value = productData(4)
        frmItimeData.txtSalePrice.Value = productData(5)
        frmItimeData.txtNotes.Value = productData(11)
        frmItimeData.isEditMode = True
        frmItimeData.editProductCode = selectedProductCode
        frmItimeData.Show
        LoadProducts
    End If
End Sub

' حذف منتج
Private Sub cmdDelete_Click()
    If ListBox1.ListIndex < 0 Or ListBox1.ListIndex = 0 Then
        MsgBox "يرجى اختيار منتج للحذف", vbExclamation
        Exit Sub
    End If
    
    Dim response As VbMsgBoxResult
    response = MsgBox("هل أنت متأكد من حذف هذا المنتج؟", vbYesNo + vbQuestion, "تأكيد الحذف")
    
    If response = vbYes Then
        Dim tbl As ListObject
        Dim i As Long
        
        Set tbl = GetTableByName("tlbproduct")
        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(ListBox1.List(ListBox1.ListIndex, 0))) Then
                tbl.ListRows(i).Delete
                MsgBox "تم حذف المنتج بنجاح", vbInformation
                LoadProducts
                Exit Sub
            End If
        Next i
    End If
End Sub

' تصدير HTML
Private Sub cmdExportHTML_Click()
    GenerateProductReportHTML
End Sub

' إغلاق
Private Sub cmdClose_Click()
    Me.Hide
End Sub

' تقرير المنتجات HTML
Private Sub GenerateProductReportHTML()
    Dim html As String
    Dim tbl As ListObject
    Dim i As Long
    Dim currentStock As Double
    
    Set tbl = GetTableByName("tlbproduct")
    If tbl Is Nothing Then Exit Sub
    
    html = GetHTMLHeader("تقرير المنتجات")
    html = html & "<div class='container'>"
    html = html & "<h1>تقرير المنتجات</h1>"
    html = html & "<p><strong>التاريخ:</strong> " & Format(Date, "yyyy/mm/dd") & "</p>"
    html = html & "<table class='data-table'>"
    html = html & "<thead><tr>"
    html = html & "<th>رمز الصنف</th>"
    html = html & "<th>اسم الصنف</th>"
    html = html & "<th>الوحدة</th>"
    html = html & "<th>الرصيد الحالي</th>"
    html = html & "<th>سعر الشراء</th>"
    html = html & "<th>سعر البيع</th>"
    html = html & "<th>الحالة</th>"
    html = html & "<th>ملاحظات</th>"
    html = html & "</tr></thead><tbody>"
    
    For i = 1 To tbl.ListRows.count
        currentStock = GetCurrentStock(CStr(tbl.ListRows(i).Range(1).Value))
        
        html = html & "<tr>"
        html = html & "<td>" & tbl.ListRows(i).Range(1).Value & "</td>"
        html = html & "<td>" & tbl.ListRows(i).Range(2).Value & "</td>"
        html = html & "<td>" & tbl.ListRows(i).Range(3).Value & "</td>"
        html = html & "<td>" & Format(currentStock, "#,##0.00") & "</td>"
        html = html & "<td>" & Format(tbl.ListRows(i).Range(4).Value, "#,##0.00") & "</td>"
        html = html & "<td>" & Format(tbl.ListRows(i).Range(5).Value, "#,##0.00") & "</td>"
        html = html & "<td>" & tbl.ListRows(i).Range(7).Value & "</td>"
        html = html & "<td>" & tbl.ListRows(i).Range(11).Value & "</td>"
        html = html & "</tr>"
    Next i
    
    html = html & "</tbody></table>"
    html = html & "</div>" & GetHTMLFooter
    
    SaveAndOpenHTML html, "ProductReport"
End Sub

Private Function GetHTMLHeader(title As String) As String
    GetHTMLHeader = Module_HTMLReports.GetHTMLHeader(title)
End Function

Private Function GetHTMLFooter() As String
    GetHTMLFooter = Module_HTMLReports.GetHTMLFooter
End Function

Private Sub SaveAndOpenHTML(htmlContent As String, fileName As String)
    Module_HTMLReports.SaveAndOpenHTML htmlContent, fileName
End Sub



