<!-- TAB 4: INVESTMENTS -->
    <div class="tab-pane fade <?= $activeTab=='investments'?'show active':'' ?>" id="investments" role="tabpanel">
        <div class="d-flex justify-content-between align-items-center mb-4 animate-slide-up delay-1">
            <h4 class="fw-bold mb-0">المحفظة الاستثمارية الذكية</h4>
            <button class="btn btn-dark btn-premium shadow-sm fw-bold" data-bs-toggle="modal" data-bs-target="#investmentModal"><i class="fa-solid fa-building-trend-up"></i> أصل استثماري جديد</button>
        </div>

        <div class="row">
            <?php foreach($investments as $inv): 
                $buyTotal = $inv->buy_price * $inv->quantity;
                $currentTotal = $inv->current_value * $inv->quantity;
                $profit = $currentTotal - $buyTotal;
                $p_str = $profit >= 0 ? '+' . number_format($profit, 2) : number_format($profit, 2);
                $p_per = ($buyTotal > 0) ? ($profit / $buyTotal) * 100 : 0;
                
                $theme = 'secondary';
                $emoji = '<i class="fa-solid fa-minus"></i>';
                if ($profit > 0) { $theme = 'success'; $emoji = '<i class="fa-solid fa-arrow-trend-up"></i>'; }
                if ($profit < 0) { $theme = 'danger'; $emoji = '<i class="fa-solid fa-arrow-trend-down"></i>'; }
            ?>
            <div class="col-md-4 mb-4">
                <div class="card card-saas h-100 overflow-hidden">
                    <div class="bg-<?= $theme ?> py-2 text-center text-white fw-bold shadow-sm">
                        <?= $emoji ?> <?= htmlspecialchars($inv->asset_type) ?>
                    </div>
                    <div class="card-body text-center position-relative">
                        <div class="position-absolute top-0 end-0 m-2">
                             <a href="<?= BASE_URL ?>/finance/delete/finance_investments/<?= $inv->id ?>" class="text-danger opacity-50 hover-opacity-100" onclick="return confirm('تأكيد الحذف؟');"><i class="fa-solid fa-trash"></i></a>
                        </div>
                        <h4 class="fw-bold text-dark mt-2 mb-0"><?= htmlspecialchars($inv->asset_name) ?></h4>
                        <div class="mb-3"><span class="badge bg-light text-dark border">الكمية: <?= number_format($inv->quantity, 4) ?></span></div>

                        <div class="d-flex justify-content-between align-items-center bg-light rounded p-3 mb-2 shadow-inner">
                            <div class="text-start">
                                <small class="text-muted d-block mb-1">الشراء الإجمالي</small>
                                <div class="fw-bold fs-5"><?= number_format($buyTotal, 2) ?></div>
                            </div>
                            <div class="text-end">
                                <small class="text-muted d-block mb-1">القيمة الحالية الإجمالية</small>
                                <div class="fw-bold fs-4 text-<?= $theme ?>"><?= number_format($currentTotal, 2) ?></div>
                            </div>
                        </div>
                        
                        <div class="d-flex justify-content-center align-items-center mb-4">
                            <span class="fs-6 fw-bold text-<?= $theme ?> bg-<?= $theme ?>-subtle px-3 py-1 rounded-pill">
                                الربح/الخسارة: <?= $p_str ?> (<?= number_format($p_per, 2) ?>%)
                            </span>
                        </div>
                        
                        <form action="<?= BASE_URL ?>/finance/updateInvestment/<?= $inv->id ?>" method="POST" class="loader-form">
                            <?= csrf_field() ?>
                            <label class="text-muted small text-start w-100 fw-bold mb-1">تحديث السعر الحالي للحبة/للوحدة</label>
                            <div class="input-group">
                                <span class="input-group-text"><i class="fa-solid fa-pen-to-square"></i></span>
                                <input type="number" step="0.0001" name="current_value" class="form-control" value="<?= $inv->current_value ?>" required>
                                <button class="btn btn-<?= $theme ?> btn-premium py-2 px-3 text-white fw-bold" type="submit">تحديث</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>

</div>


