<?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>
    <a href="/accounting/portfolios" class="btn btn-outline">العودة</a>
</div>

<div class="card">
    <div style="padding:20px;">
        <form method="POST" action="/accounting/portfolios">
            <?= csrf_field() ?>
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:15px;margin-bottom:20px;">
                <div>
                    <label class="form-label">نوع الحافظة <span style="color:#DC2626;">*</span></label>
                    <select name="portfolio_type" class="form-select" required>
                        <?php foreach (InstrumentPortfolio::$typeLabels as $k => $v): ?>
                            <option value="<?= $k ?>"><?= $v ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="form-label">التاريخ <span style="color:#DC2626;">*</span></label>
                    <input type="date" name="portfolio_date" class="form-input" required value="<?= date('Y-m-d') ?>" dir="ltr">
                </div>
                <div>
                    <label class="form-label">الحساب البنكي</label>
                    <select name="bank_account_id" class="form-select">
                        <option value="">— بدون —</option>
                        <?php foreach ($bankAccounts as $ba): ?>
                            <option value="<?= (int)$ba['id'] ?>"><?= e($ba['account_name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div style="grid-column:1/-1;">
                    <label class="form-label">وصف</label>
                    <input type="text" name="description" class="form-input" placeholder="وصف الحافظة (اختياري)">
                </div>
            </div>

            <h4 style="margin:0 0 10px;">اختر الأوراق</h4>
            <?php if (!empty($availableInstruments)): ?>
            <div class="table-responsive" style="max-height:400px;overflow-y:auto;">
                <table class="data-table" style="width:100%;">
                    <thead>
                        <tr>
                            <th style="width:40px;"><input type="checkbox" id="select-all"></th>
                            <th>رقم الورقة</th>
                            <th>المبلغ</th>
                            <th>تاريخ الاستحقاق</th>
                            <th>الساحب</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($availableInstruments as $inst): ?>
                            <tr>
                                <td><input type="checkbox" name="instrument_ids[]" value="<?= (int)$inst['id'] ?>"></td>
                                <td style="direction:ltr;text-align:right;"><?= e($inst['instrument_number']) ?></td>
                                <td style="font-weight:600;"><?= number_format((float)$inst['amount'], 2) ?></td>
                                <td style="direction:ltr;text-align:right;"><?= e($inst['due_date']) ?></td>
                                <td><?= e($inst['drawer_name'] ?? '—') ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php else: ?>
            <p style="color:#6B7280;text-align:center;padding:20px;">لا توجد أوراق متاحة (في الخزينة)</p>
            <?php endif; ?>

            <div style="margin-top:20px;display:flex;gap:8px;">
                <button type="submit" class="btn btn-primary">إنشاء الحافظة</button>
                <a href="/accounting/portfolios" class="btn btn-outline">إلغاء</a>
            </div>
        </form>
    </div>
</div>

<script>
(function(){
    var sa = document.getElementById('select-all');
    if(sa) sa.addEventListener('change', function(){
        document.querySelectorAll('[name="instrument_ids[]"]').forEach(function(cb){ cb.checked = sa.checked; });
    });
})();
</script>
<?php $__template->endSection(); ?>
