<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>أرصدة الإجازات <?= $year ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?><a href="/hr/leaves" class="btn btn-secondary">رجوع</a><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:20px;">
    <form method="GET" action="/hr/leaves/balances" 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="<?= $year ?>" class="form-control" style="max-width:100px;"></div>
        <div><label style="display:block;margin-bottom:4px;font-size:13px;">القسم</label>
            <select name="department_id" class="form-control"><option value="">الكل</option>
            <?php foreach ($departments as $d): ?><option value="<?= (int) $d['id'] ?>" <?= $departmentId == $d['id'] ? 'selected' : '' ?>><?= e($d['name_ar']) ?></option><?php endforeach; ?>
            </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>
                    <?php foreach ($leaveTypes as $lt): ?><th style="font-size:11px;text-align:center;"><?= e($lt['name_ar']) ?></th><?php endforeach; ?>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($employees)): ?>
                <tr><td colspan="<?= 2 + count($leaveTypes) ?>" style="text-align:center;padding:40px;color:#9CA3AF;">لا يوجد موظفين</td></tr>
                <?php else: ?>
                <?php foreach ($employees as $emp): ?>
                <tr>
                    <td><a href="/hr/leaves/balances/<?= (int) $emp['id'] ?>?year=<?= $year ?>"><?= e($emp['first_name_ar'] . ' ' . $emp['last_name_ar']) ?></a></td>
                    <td><?= e($emp['department_name'] ?? '-') ?></td>
                    <?php foreach ($leaveTypes as $lt): ?>
                    <?php $bal = $balancesMatrix[$emp['id']][$lt['id']] ?? null; ?>
                    <td style="text-align:center;font-size:12px;">
                        <?php if ($bal): ?>
                        <span title="المتبقي / المستحق"><?= number_format((float) ($bal['remaining_days'] ?? 0), 0) ?>/<?= number_format((float) ($bal['entitled_days'] ?? 0), 0) ?></span>
                        <?php else: ?>-<?php endif; ?>
                    </td>
                    <?php endforeach; ?>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>
