<?php
/**
 * @var object $salaries
 * @var string $employeeId
 * @var string $month
 * @var string $year
 * @var string $status
 * @var array $months
 * @var array $years
 * @var array $employees
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-file-invoice-dollar me-2 text-gold"></i>Salaries</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">Salary Records</h6>
        </div>
        <div class="card-header-right">
            <?php if (can('salaries_create')): ?>
                <a href="<?= url('salary/create') ?>" class="btn btn-sm btn-gold">
                    <i class="fas fa-plus me-1"></i>Generate Salary
                </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-3">
            <div class="col-md-10">
                <form method="GET" action="<?= url('salary') ?>" class="d-flex gap-2 flex-wrap">
                    <select name="employee_id" class="form-select form-select-sm bg-transparent" style="width:auto;">
                        <option value="">All Employees</option>
                        <?php foreach ($employees as $emp): ?>
                            <option value="<?= $emp->id ?>" <?= $employeeId == $emp->id ? 'selected' : '' ?>>
                                <?= e($emp->first_name . ' ' . $emp->last_name) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <select name="month" class="form-select form-select-sm bg-transparent" style="width:auto;">
                        <option value="">All Months</option>
                        <?php foreach ($months as $val => $label): ?>
                            <option value="<?= $val ?>" <?= $month === $val ? 'selected' : '' ?>><?= $label ?></option>
                        <?php endforeach; ?>
                    </select>
                    <select name="year" class="form-select form-select-sm bg-transparent" style="width:auto;">
                        <option value="">All Years</option>
                        <?php foreach ($years as $y): ?>
                            <option value="<?= $y ?>" <?= $year == $y ? 'selected' : '' ?>><?= $y ?></option>
                        <?php endforeach; ?>
                    </select>
                    <select name="status" class="form-select form-select-sm bg-transparent" style="width:auto;">
                        <option value="">All Status</option>
                        <option value="pending" <?= $status === 'pending' ? 'selected' : '' ?>>Pending</option>
                        <option value="paid" <?= $status === 'paid' ? 'selected' : '' ?>>Paid</option>
                    </select>
                    <button type="submit" class="btn btn-sm btn-gold">
                        <i class="fas fa-filter me-1"></i>Filter
                    </button>
                    <a href="<?= url('salary') ?>" class="btn btn-sm btn-outline-secondary">
                        <i class="fas fa-times"></i>
                    </a>
                </form>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Employee</th>
                        <th>Period</th>
                        <th class="text-end">Basic</th>
                        <th class="text-end">Commission</th>
                        <th class="text-end">Bonus</th>
                        <th class="text-end">Deductions</th>
                        <th class="text-end">Net Salary</th>
                        <th>Status</th>
                        <th>Paid At</th>
                        <th width="100" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($salaries->data)): ?>
                        <tr>
                            <td colspan="11" class="text-center text-muted py-4">
                                <i class="fas fa-file-invoice-dollar fa-2x mb-2"></i>
                                <p class="mb-0">No salary records found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($salaries->currentPage - 1) * $salaries->perPage + 1; ?>
                        <?php foreach ($salaries->data as $salary): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td>
                                    <a href="<?= url('employees/show/' . $salary->employee_id) ?>" class="text-gold text-decoration-none">
                                        <strong><?= e($salary->first_name . ' ' . $salary->last_name) ?></strong>
                                    </a>
                                </td>
                                <td><?= e(date('F Y', strtotime($salary->salary_date))) ?></td>
                                <td class="text-end"><?= currency($salary->basic_salary) ?></td>
                                <td class="text-end"><?= currency($salary->commission_total) ?></td>
                                <td class="text-end"><?= currency($salary->bonus) ?></td>
                                <td class="text-end"><?= currency($salary->deductions) ?></td>
                                <td class="text-end"><strong><?= currency($salary->net_salary) ?></strong></td>
                                <td>
                                    <span class="badge bg-<?= statusBadge($salary->payment_status) ?>">
                                        <?= e(ucfirst($salary->payment_status)) ?>
                                    </span>
                                </td>
                                <td><?= !empty($salary->payment_date) ? formatDateTime($salary->payment_date) : '-' ?></td>
                                <td class="text-center">
                                    <div class="btn-group btn-group-sm">
                                        <a href="<?= url('salary/payslip/' . $salary->id) ?>" class="btn btn-outline-info" title="Payslip">
                                            <i class="fas fa-file-invoice"></i>
                                        </a>
                                        <?php if ($salary->payment_status === 'pending' && can('salaries_edit')): ?>
                                            <a href="<?= url('salary/pay/' . $salary->id) ?>" class="btn btn-outline-success"
                                               onclick="return confirm('Mark this salary as paid?')" title="Pay">
                                                <i class="fas fa-check"></i>
                                            </a>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($salaries->totalPages > 1): ?>
            <div class="card-footer d-flex justify-content-between align-items-center">
                <div class="text-muted small">
                    Showing <?= (($salaries->currentPage - 1) * $salaries->perPage) + 1 ?>
                    to <?= min($salaries->currentPage * $salaries->perPage, $salaries->total) ?>
                    of <?= $salaries->total ?> entries
                </div>
                <?= paginateLinks($salaries, url('salary')) ?>
            </div>
        <?php endif; ?>
    </div>
</div>
