<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>طلبات العمل الإضافي<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (canAny(['hr.overtime.create', 'hr.overtime.manage'])): ?>
<a href="/hr/overtime/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> طلب عمل إضافي</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<?php
$statusLabels = ['pending' => 'معلق', 'approved' => 'موافق', 'rejected' => 'مرفوض'];
$statusColors = ['pending' => '#D97706', 'approved' => '#059669', 'rejected' => '#DC2626'];
$statusBgs = ['pending' => '#FFF7ED', 'approved' => '#ECFDF5', 'rejected' => '#FEE2E2'];
?>

<!-- Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/hr/overtime" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">الشهر</label>
            <input type="month" name="month" value="<?= e($month ?? '') ?>" class="form-input">
        </div>
        <div style="min-width:140px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-select">
                <option value="">الكل</option>
                <?php foreach ($statusLabels as $val => $label): ?>
                <option value="<?= e($val) ?>" <?= ($status ?? '') === $val ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/hr/overtime" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Overtime Requests Table -->
<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>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($requests)): ?>
                <tr><td colspan="7" style="text-align:center;padding:40px;color:#9CA3AF;">لا توجد طلبات عمل إضافي</td></tr>
                <?php else: ?>
                <?php foreach ($requests as $r): ?>
                <?php
                $st = $r['status'] ?? 'pending';
                $stColor = $statusColors[$st] ?? '#6B7280';
                $stBg = $statusBgs[$st] ?? '#F3F4F6';
                $stLabel = $statusLabels[$st] ?? $st;
                ?>
                <tr>
                    <td style="font-weight:600;"><?= e($r['employee_name'] ?? '—') ?></td>
                    <td><?= e($r['type_name'] ?? '—') ?></td>
                    <td style="font-size:13px;"><?= e($r['date'] ?? '—') ?></td>
                    <td style="text-align:center;font-weight:600;"><?= e($r['hours'] ?? '0') ?></td>
                    <td style="text-align:center;direction:ltr;">&times;<?= e($r['rate_multiplier'] ?? '1') ?></td>
                    <td>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $stBg ?>;color:<?= $stColor ?>;">
                            <?= e($stLabel) ?>
                        </span>
                    </td>
                    <td>
                        <?php if ($st === 'pending' && can('hr.overtime.approve')): ?>
                        <div style="display:flex;gap:4px;">
                            <form method="POST" action="/hr/overtime/<?= (int) $r['id'] ?>/approve" style="display:inline;">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-sm btn-primary" style="font-size:11px;padding:4px 8px;background:#059669;border-color:#059669;">
                                    <i data-lucide="check" style="width:12px;height:12px;vertical-align:middle;"></i> موافقة
                                </button>
                            </form>
                            <form method="POST" action="/hr/overtime/<?= (int) $r['id'] ?>/reject" style="display:inline;">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-sm btn-outline" style="font-size:11px;padding:4px 8px;color:#DC2626;border-color:#DC2626;">
                                    <i data-lucide="x" style="width:12px;height:12px;vertical-align:middle;"></i> رفض
                                </button>
                            </form>
                        </div>
                        <?php else: ?>
                        <a href="/hr/overtime/<?= (int) $r['id'] ?>" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                            <i data-lucide="eye" style="width:13px;height:13px;vertical-align:middle;"></i> عرض
                        </a>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<script>document.addEventListener('DOMContentLoaded', function() { if (typeof lucide !== 'undefined') lucide.createIcons(); });</script>
<?php $__template->endSection(); ?>
