<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>فترات الرواتب<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:20px;">
    <form method="GET" action="/hr/payroll/periods" style="display:flex;gap:10px;align-items:end;padding:16px;">
        <div><label style="display:block;margin-bottom:4px;font-size:13px;">السنة</label><input type="number" name="year" value="<?= e($filters['year']) ?>" class="form-control" style="max-width:100px;"></div>
        <div><label style="display:block;margin-bottom:4px;font-size:13px;">الحالة</label>
            <select name="status" class="form-control"><option value="">الكل</option>
            <option value="open" <?= $filters['status'] === 'open' ? 'selected' : '' ?>>مفتوح</option>
            <option value="calculated" <?= $filters['status'] === 'calculated' ? 'selected' : '' ?>>محسوب</option>
            <option value="approved" <?= $filters['status'] === 'approved' ? 'selected' : '' ?>>معتمد</option>
            <option value="paid" <?= $filters['status'] === 'paid' ? 'selected' : '' ?>>مدفوع</option>
            <option value="closed" <?= $filters['status'] === 'closed' ? 'selected' : '' ?>>مغلق</option>
            </select>
        </div>
        <button type="submit" class="btn btn-secondary">بحث</button>
    </form>
</div>
<?php if (can('hr.payroll.process')): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;font-size:16px;">إنشاء فترة جديدة</h3></div>
    <form method="POST" action="/hr/payroll/periods" style="display:flex;gap:10px;align-items:end;padding:16px;">
        <?= csrf_field() ?>
        <div><label style="display:block;margin-bottom:4px;font-size:13px;">السنة</label><input type="number" name="year" value="<?= date('Y') ?>" class="form-control" style="max-width:100px;"></div>
        <div><label style="display:block;margin-bottom:4px;font-size:13px;">الشهر</label>
            <?php $mn = [1=>'يناير',2=>'فبراير',3=>'مارس',4=>'أبريل',5=>'مايو',6=>'يونيو',7=>'يوليو',8=>'أغسطس',9=>'سبتمبر',10=>'أكتوبر',11=>'نوفمبر',12=>'ديسمبر']; ?>
            <select name="month" class="form-control"><?php for ($m = 1; $m <= 12; $m++): ?><option value="<?= $m ?>" <?= (int) date('m') === $m ? 'selected' : '' ?>><?= $mn[$m] ?></option><?php endfor; ?></select>
        </div>
        <button type="submit" class="btn btn-primary">إنشاء</button>
    </form>
</div>
<?php endif; ?>
<div class="card">
    <?php $statusLabels = ['open'=>'مفتوح','processing'=>'جاري','calculated'=>'محسوب','approved'=>'معتمد','paid'=>'مدفوع','closed'=>'مغلق']; ?>
    <?php $statusColors = ['open'=>'#DBEAFE;color:#1E40AF','processing'=>'#FEF3C7;color:#92400E','calculated'=>'#E0E7FF;color:#3730A3','approved'=>'#DEF7EC;color:#03543F','paid'=>'#D1FAE5;color:#065F46','closed'=>'#E5E7EB;color:#374151']; ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead><tr><th>الكود</th><th>الشهر</th><th>الحالة</th><th>إجراءات</th></tr></thead>
            <tbody>
                <?php if (empty($periods)): ?>
                <tr><td colspan="4" style="text-align:center;padding:40px;color:#9CA3AF;">لا توجد فترات</td></tr>
                <?php else: ?>
                <?php foreach ($periods as $p): ?>
                <tr>
                    <td><a href="/hr/payroll/periods/<?= (int) $p['id'] ?>"><?= e($p['period_code']) ?></a></td>
                    <td><?= e(($mn[(int) $p['month']] ?? '') . ' ' . $p['year']) ?></td>
                    <td><span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:<?= $statusColors[$p['status']] ?? '#E5E7EB;color:#374151' ?>;"><?= e($statusLabels[$p['status']] ?? $p['status']) ?></span></td>
                    <td><a href="/hr/payroll/periods/<?= (int) $p['id'] ?>" style="color:#2563EB;"><i data-lucide="eye" style="width:16px;height:16px;"></i></a></td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    <?php if (!empty($pagination)): ?><?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?><?php endif; ?>
</div>
<script>if(typeof lucide!=='undefined')lucide.createIcons();</script>
<?php $__template->endSection(); ?>
