<?php
use App\Modules\Accounting\Models\InstrumentPortfolio;
$__template->layout('Layout.main');
?>

<?php $__template->section('title'); ?>حوافظ الأوراق التجارية<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:15px;">
    <h2 style="margin:0;">الحوافظ</h2>
    <div style="display:flex;gap:8px;">
        <a href="/accounting/instruments" class="btn btn-outline">الأوراق التجارية</a>
        <?php if (can('accounting.instruments.manage')): ?><a href="/accounting/portfolios/create" class="btn btn-primary">+ حافظة جديدة</a><?php endif; ?>
    </div>
</div>

<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;">
        <form method="GET" style="display:flex;gap:10px;align-items:end;">
            <div>
                <label class="form-label" style="font-size:12px;">نوع الحافظة</label>
                <select name="type" class="form-select" onchange="this.form.submit()">
                    <option value="">الكل</option>
                    <?php foreach (InstrumentPortfolio::$typeLabels as $k => $v): ?>
                        <option value="<?= $k ?>" <?= $type === $k ? 'selected' : '' ?>><?= $v ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="table-responsive">
        <table class="data-table" style="width:100%;">
            <thead>
                <tr>
                    <th>رقم الحافظة</th>
                    <th>النوع</th>
                    <th>التاريخ</th>
                    <th>عدد الأوراق</th>
                    <th>الإجمالي</th>
                    <th>الحالة</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($portfolios as $p): ?>
                    <tr>
                        <td style="font-weight:600;direction:ltr;text-align:right;"><?= e($p['portfolio_number']) ?></td>
                        <td><?= InstrumentPortfolio::$typeLabels[$p['portfolio_type']] ?? $p['portfolio_type'] ?></td>
                        <td style="direction:ltr;text-align:right;"><?= e($p['portfolio_date']) ?></td>
                        <td style="text-align:center;"><?= (int)$p['instrument_count'] ?></td>
                        <td style="font-weight:600;"><?= number_format((float)$p['total_amount'], 2) ?></td>
                        <td>
                            <span style="color:<?= $p['status'] === 'confirmed' ? '#059669' : '#D97706' ?>;font-weight:600;">
                                <?= $p['status'] === 'confirmed' ? 'مؤكدة' : 'مسودة' ?>
                            </span>
                        </td>
                        <td><a href="/accounting/portfolios/<?= (int)$p['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($portfolios)): ?>
                    <tr><td colspan="7" style="text-align:center;color:#6B7280;padding:30px;">لا توجد حوافظ</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>
