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

<?php $__template->section('title'); ?><?= e($fiscal_year['name_ar']) ?><?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<?php
$statusColor = match($fiscal_year['status']) {
    'open' => '#059669',
    'closing' => '#F59E0B',
    'closed' => '#6B7280',
    default => '#374151',
};
$statusLabel = match($fiscal_year['status']) {
    'open' => 'مفتوحة',
    'closing' => 'قيد الإقفال',
    'closed' => 'مغلقة',
    default => $fiscal_year['status'],
};
?>

<div class="card" style="margin-bottom:15px;">
    <div style="padding:20px;">
        <div style="display:flex;justify-content:space-between;align-items:center;">
            <h2 style="margin:0;"><?= e($fiscal_year['name_ar']) ?></h2>
            <span style="padding:8px 20px;border-radius:20px;font-weight:700;color:white;background:<?= $statusColor ?>;"><?= $statusLabel ?></span>
        </div>
        <div style="display:grid;grid-template-columns:repeat(3,1fr);gap:15px;margin-top:15px;">
            <div><span style="color:#6B7280;">تاريخ البداية:</span><br><strong><?= e($fiscal_year['start_date']) ?></strong></div>
            <div><span style="color:#6B7280;">تاريخ النهاية:</span><br><strong><?= e($fiscal_year['end_date']) ?></strong></div>
            <div><span style="color:#6B7280;">الحالية:</span><br><strong><?= (int)$fiscal_year['is_current'] ? 'نعم' : 'لا' ?></strong></div>
        </div>
        <?php if ($fiscal_year['status'] === 'open' && can('accounting.fiscal_year.close')): ?>
            <div style="margin-top:15px;">
                <form method="POST" action="/accounting/fiscal-years/<?= (int)$fiscal_year['id'] ?>/close" style="display:inline;">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn" style="color:#DC2626;border-color:#DC2626;" onclick="return confirm('هل أنت متأكد من إقفال السنة المالية؟ سيتم إنشاء قيد إقفال وترحيل الأرصدة.')">إقفال السنة المالية</button>
                </form>
            </div>
        <?php endif; ?>
    </div>
</div>

<!-- Periods Grid -->
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;">الفترات الشهرية</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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($periods as $p): ?>
                    <?php
                    $pColor = match($p['status']) {
                        'open' => '#059669',
                        'closed' => '#6B7280',
                        'reopened' => '#F59E0B',
                        default => '#374151',
                    };
                    $pLabel = match($p['status']) {
                        'open' => 'مفتوحة',
                        'closed' => 'مغلقة',
                        'reopened' => 'أعيد فتحها',
                        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>
                            <?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="<?= (int)$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 endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>
