<?php
$statusBadge = ['DRAFT' => 'badge-gray', 'FINALIZED' => 'badge-gold', 'PAID' => 'badge-green'];
$monthNames = ['', 'January','February','March','April','May','June','July','August','September','October','November','December'];
?>
<div style="display:flex;justify-content:flex-end;margin-bottom:16px;">
    <?php if (Auth::can('payroll', 'add')): ?>
    <button type="button" class="btn btn-primary" onclick="rmsOpenModal('rmsGenerateModal')"><i class="fa-solid fa-plus"></i> Generate Payroll Run</button>
    <?php endif; ?>
</div>

<div class="panel" style="padding:0;overflow-x:auto;">
<table class="data-table">
<thead><tr><th>Period</th><th>Staff</th><th>Total Net Pay</th><th>Status</th><th>Generated By</th><th></th></tr></thead>
<tbody>
<?php foreach ($runs as $run): ?>
<tr>
    <td><strong><?= e($monthNames[$run['period_month']]) ?> <?= e($run['period_year']) ?></strong></td>
    <td><?= (int) $run['staff_count'] ?></td>
    <td><?= formatMoney((float) $run['total_net']) ?></td>
    <td><span class="badge <?= $statusBadge[$run['status']] ?? 'badge-gray' ?>"><?= e($run['status']) ?></span></td>
    <td><?= e($run['generated_by_name'] ?? 'System') ?></td>
    <td><a href="<?= Router::url('payroll/show?id=' . $run['id']) ?>" class="btn btn-secondary" style="padding:5px 12px;font-size:12px;"><i class="fa-solid fa-eye"></i> View</a></td>
</tr>
<?php endforeach; ?>
<?php if (empty($runs)): ?>
<tr><td colspan="6" style="text-align:center;color:var(--color-text-muted);padding:30px;">No payroll runs yet.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>

<?php if (Auth::can('payroll', 'add')): ?>
<div class="rms-modal-overlay" id="rmsGenerateModal">
    <div class="rms-modal" style="max-width:380px;">
        <div class="rms-modal-head">
            <h3><i class="fa-solid fa-users" style="color:var(--color-teal);margin-right:6px;"></i>Generate Payroll Run</h3>
            <button type="button" class="rms-modal-close" onclick="rmsCloseModal('rmsGenerateModal')"><i class="fa-solid fa-xmark"></i></button>
        </div>
        <form method="POST" action="<?= Router::url('payroll/generate') ?>">
            <?= $csrf ?>
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:10px;">
                <div class="form-group">
                    <label>Month</label>
                    <select name="period_month" class="form-control">
                        <?php for ($m = 1; $m <= 12; $m++): ?>
                        <option value="<?= $m ?>" <?= $m === $currentMonth ? 'selected' : '' ?>><?= e($monthNames[$m]) ?></option>
                        <?php endfor; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label>Year</label>
                    <input type="number" name="period_year" class="form-control" value="<?= $currentYear ?>">
                </div>
            </div>
            <div style="font-size:11.5px;color:var(--color-text-muted);margin-bottom:14px;">
                Calculates basic pay from attendance (Monthly staff get full salary, Daily-wage staff get daily_wage × days present). Additions/deductions can be edited after generating.
            </div>
            <button type="submit" class="btn btn-primary" style="width:100%;"><i class="fa-solid fa-check"></i> Generate</button>
        </form>
    </div>
</div>
<?php endif; ?>
