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

<?php $__template->section('title'); ?>إقفال الفترات<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<!-- Year Selector -->
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;">
        <form method="GET" action="/accounting/period-closing" style="display:flex;gap:10px;align-items:end;">
            <div>
                <label class="form-label" style="font-size:12px;">السنة المالية</label>
                <select name="fiscal_year_id" class="form-select" onchange="this.form.submit()">
                    <option value="">— اختر —</option>
                    <?php foreach ($fiscal_years as $fy): ?>
                        <option value="<?= (int)$fy['id'] ?>" <?= (int)$fy['id'] === $fiscal_year_id ? 'selected' : '' ?>><?= e($fy['name_ar']) ?> (<?= e($fy['status']) ?>)</option>
                    <?php endforeach; ?>
                </select>
            </div>
        </form>
    </div>
</div>

<?php if ($fiscal_year): ?>
    <div class="card">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;"><?= e($fiscal_year['name_ar']) ?> — الفترات الشهرية</h3>
        </div>
        <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>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($periods as $p): ?>
                        <?php
                        $pColor = match($p['status']) {
                            'open' => '#059669',
                            'closed' => '#6B7280',
                            'reopened' => '#F59E0B',
                            'closing' => '#0284C7',
                            default => '#374151',
                        };
                        $pLabel = match($p['status']) {
                            'open' => 'مفتوحة',
                            'closed' => 'مغلقة',
                            'reopened' => 'أعيد فتحها',
                            'closing' => 'قيد الإغلاق',
                            default => $p['status'],
                        };
                        ?>
                        <tr>
                            <td style="font-weight:600;"><?= e($p['period']) ?></td>
                            <td><?= e($p['period_start']) ?></td>
                            <td><?= e($p['period_end']) ?></td>
                            <td style="text-align:center;"><?= (int)$p['journal_count'] ?></td>
                            <td style="direction:ltr;text-align:right;"><?= money($p['total_debit']) ?></td>
                            <td><span style="color:<?= $pColor ?>;font-weight:600;"><?= $pLabel ?></span></td>
                            <td style="font-size:12px;color:#6B7280;"><?= e($p['closed_at'] ?? '—') ?></td>
                            <td>
                                <?php if (($p['status'] === 'open' || $p['status'] === 'reopened') && can('accounting.period.close')): ?>
                                    <form method="POST" action="/accounting/period-closing/close-month" style="display:inline;">
                                        <?= csrf_field() ?>
                                        <input type="hidden" name="fiscal_year_id" value="<?= $fiscal_year_id ?>">
                                        <input type="hidden" name="period" value="<?= e($p['period']) ?>">
                                        <button type="submit" class="btn btn-sm btn-outline" onclick="return confirm('إغلاق الفترة <?= e($p['period']) ?>?')">إغلاق</button>
                                    </form>
                                <?php elseif ($p['status'] === 'closed' && can('accounting.period.reopen')): ?>
                                    <form method="POST" action="/accounting/period-closing/reopen-month" style="display:inline;" onsubmit="var r=prompt('سبب إعادة الفتح:');if(!r)return false;this.querySelector('[name=reason]').value=r;">
                                        <?= csrf_field() ?>
                                        <input type="hidden" name="fiscal_year_id" value="<?= $fiscal_year_id ?>">
                                        <input type="hidden" name="period" value="<?= e($p['period']) ?>">
                                        <input type="hidden" name="reason" value="">
                                        <button type="submit" class="btn btn-sm" style="color:#F59E0B;border-color:#F59E0B;">إعادة فتح</button>
                                    </form>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
<?php endif; ?>
<?php $__template->endSection(); ?>
