<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>أرصدة إجازات: <?= e($profile->first_name_ar . ' ' . $profile->last_name_ar) ?> - <?= $year ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?><a href="/hr/leaves/balances" class="btn btn-secondary">رجوع</a><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:20px;">
    <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 foreach ($balances as $b): ?>
                <?php $ltName = '-'; foreach ($leaveTypes as $lt) { if ((int) $lt['id'] === (int) $b['leave_type_id']) { $ltName = $lt['name_ar']; break; } } ?>
                <tr>
                    <td><?= e($ltName) ?></td>
                    <td><?= number_format((float) $b['entitled_days'], 1) ?></td>
                    <td><?= number_format((float) ($b['carried_over_days'] ?? 0), 1) ?></td>
                    <td><?= number_format((float) ($b['adjustment_days'] ?? 0), 1) ?></td>
                    <td><?= number_format((float) ($b['used_days'] ?? 0), 1) ?></td>
                    <td><?= number_format((float) ($b['pending_days'] ?? 0), 1) ?></td>
                    <td style="font-weight:700;color:#059669;"><?= number_format((float) ($b['remaining_days'] ?? 0), 1) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?php if (!empty($recentRequests)): ?>
<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>
    <div class="table-responsive">
        <table class="data-table">
            <thead><tr><th>النوع</th><th>من</th><th>إلى</th><th>الأيام</th><th>الحالة</th></tr></thead>
            <tbody>
                <?php foreach ($recentRequests as $r): ?>
                <?php $sl = ['pending'=>'معلق','approved'=>'موافق','rejected'=>'مرفوض','cancelled'=>'ملغي']; ?>
                <tr>
                    <td><?= e($r['leave_type_name']) ?></td>
                    <td><?= e($r['start_date']) ?></td>
                    <td><?= e($r['end_date']) ?></td>
                    <td><?= number_format((float) $r['total_days'], 1) ?></td>
                    <td><?= e($sl[$r['status']] ?? $r['status']) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<div class="card" style="padding:16px;">
    <h4 style="margin:0 0 12px;">تعديل الرصيد</h4>
    <form method="POST" action="/hr/leaves/balances/adjust" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
        <?= csrf_field() ?>
        <input type="hidden" name="employee_profile_id" value="<?= (int) $profile->id ?>">
        <input type="hidden" name="year" value="<?= $year ?>">
        <div><label style="display:block;margin-bottom:4px;font-size:13px;">نوع الإجازة</label>
            <select name="leave_type_id" class="form-control" required>
            <?php foreach ($leaveTypes as $lt): ?><option value="<?= (int) $lt['id'] ?>"><?= e($lt['name_ar']) ?></option><?php endforeach; ?>
            </select>
        </div>
        <div><label style="display:block;margin-bottom:4px;font-size:13px;">التعديل (أيام)</label><input type="number" step="0.5" name="adjustment_days" class="form-control" style="max-width:100px;" required></div>
        <div><label style="display:block;margin-bottom:4px;font-size:13px;">السبب</label><input type="text" name="reason" class="form-control" style="min-width:200px;"></div>
        <button type="submit" class="btn btn-primary">تعديل</button>
    </form>
</div>
<?php $__template->endSection(); ?>
