<?php $__template->layout('Layout.main'); ?>

<?php $__template->section('title'); ?>السنوات المالية<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/accounting/fiscal-years/create" class="btn btn-primary">+ سنة مالية جديدة</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;">
        <div style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
            <div style="flex:2;">
                <label class="form-label" style="font-size:12px;">بحث</label>
                <input type="text" id="fy-search" class="form-input" placeholder="اسم السنة المالية...">
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">الحالة</label>
                <select id="fy-status" class="form-select">
                    <option value="">الكل</option>
                    <option value="open">مفتوحة</option>
                    <option value="closing">قيد الإقفال</option>
                    <option value="closed">مغلقة</option>
                </select>
            </div>
        </div>
    </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>
                </tr>
            </thead>
            <tbody id="fy-tbody">
                <?php foreach ($years as $fy): ?>
                    <?php
                    $statusColor = match($fy['status']) {
                        'open' => '#059669',
                        'closing' => '#F59E0B',
                        'closed' => '#6B7280',
                        default => '#374151',
                    };
                    $statusLabel = match($fy['status']) {
                        'open' => 'مفتوحة',
                        'closing' => 'قيد الإقفال',
                        'closed' => 'مغلقة',
                        default => $fy['status'],
                    };
                    ?>
                    <tr data-name="<?= e($fy['name_ar'] . ' ' . ($fy['name_en'] ?? '')) ?>" data-status="<?= e($fy['status']) ?>">
                        <td style="font-weight:600;"><?= e($fy['name_ar']) ?></td>
                        <td><?= e($fy['start_date']) ?></td>
                        <td><?= e($fy['end_date']) ?></td>
                        <td><span style="color:<?= $statusColor ?>;font-weight:600;"><?= $statusLabel ?></span></td>
                        <td><?= (int)$fy['is_current'] ? '<span style="color:#059669;font-weight:700;">نعم</span>' : '—' ?></td>
                        <td><a href="/accounting/fiscal-years/<?= (int)$fy['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($years)): ?>
                    <tr><td colspan="6" style="text-align:center;color:#6B7280;padding:30px;">لا توجد سنوات مالية</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<script>
(function(){
    var s=document.getElementById('fy-search'),st=document.getElementById('fy-status');
    var rows=document.querySelectorAll('#fy-tbody tr[data-name]');
    function f(){var q=(s.value||'').toLowerCase(),sv=st.value;
        rows.forEach(function(r){var ms=!q||(r.getAttribute('data-name')||'').toLowerCase().indexOf(q)!==-1;
            var mt=!sv||r.getAttribute('data-status')===sv;r.style.display=ms&&mt?'':'none';});}
    if(s)s.addEventListener('input',f);if(st)st.addEventListener('change',f);
})();
</script>
<?php $__template->endSection(); ?>
