<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>طلبات الأذونات<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('hr.permissions.create')): ?>
<a href="/hr/permissions/create" class="btn btn-primary" style="display:inline-flex;align-items:center;gap:6px;">
    <i data-lucide="plus" style="width:16px;height:16px;"></i> طلب إذن جديد
</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:20px;">
    <form method="GET" action="/hr/permissions" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;padding:16px;">
        <div style="min-width:160px;">
            <label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">الشهر</label>
            <input type="month" name="month" value="<?= e($month) ?>" class="form-control">
        </div>
        <div style="min-width:140px;">
            <label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">الحالة</label>
            <select name="status" class="form-control">
                <option value="">الكل</option>
                <option value="pending" <?= $status === 'pending' ? 'selected' : '' ?>>معلّق</option>
                <option value="approved" <?= $status === 'approved' ? 'selected' : '' ?>>معتمد</option>
                <option value="rejected" <?= $status === 'rejected' ? 'selected' : '' ?>>مرفوض</option>
            </select>
        </div>
        <button type="submit" class="btn btn-secondary">بحث</button>
    </form>
</div>

<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr><th>الموظف</th><th>الرقم الوظيفي</th><th>التاريخ</th><th>من</th><th>إلى</th><th>الساعات</th><th>السبب</th><th>الحالة</th><th>المعتمد</th><th>إجراءات</th></tr>
            </thead>
            <tbody>
                <?php if (empty($permissions)): ?>
                <tr><td colspan="10" style="text-align:center;padding:40px;color:#9CA3AF;">لا يوجد طلبات</td></tr>
                <?php else: ?>
                <?php foreach ($permissions as $p): ?>
                <?php
                $statusMap = ['pending' => ['معلّق', '#FEF3C7;color:#92400E'], 'approved' => ['معتمد', '#DEF7EC;color:#03543F'], 'rejected' => ['مرفوض', '#FDE8E8;color:#9B1C1C']];
                $st = $statusMap[$p['status']] ?? ['—', '#E5E7EB;color:#374151'];
                ?>
                <tr>
                    <td><?= e($p['employee_name'] ?? '-') ?></td>
                    <td><code><?= e($p['employee_number'] ?? '-') ?></code></td>
                    <td><?= e($p['permission_date']) ?></td>
                    <td><?= e(substr($p['start_time'], 0, 5)) ?></td>
                    <td><?= e(substr($p['end_time'], 0, 5)) ?></td>
                    <td><?= number_format((float)$p['hours'], 1) ?></td>
                    <td style="max-width:200px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"><?= e($p['reason'] ?? '') ?></td>
                    <td><span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:<?= $st[1] ?>;"><?= $st[0] ?></span></td>
                    <td><?= e($p['approved_by_name'] ?? '-') ?></td>
                    <td style="white-space:nowrap;">
                        <?php if ($p['status'] === 'pending' && can('hr.permissions.approve')): ?>
                        <form method="POST" action="/hr/permissions/<?= (int)$p['id'] ?>/approve" style="display:inline;">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm btn-success" title="اعتماد" style="padding:2px 8px;font-size:12px;">✓</button>
                        </form>
                        <form method="POST" action="/hr/permissions/<?= (int)$p['id'] ?>/reject" style="display:inline;margin-inline-start:4px;">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm btn-danger" title="رفض" style="padding:2px 8px;font-size:12px;">✗</button>
                        </form>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<script>if(typeof lucide!=='undefined')lucide.createIcons();</script>
<?php $__template->endSection(); ?>
