<?php
/**
 * @var object $salary
 * @var array $commissions
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-file-invoice me-2 text-gold"></i>Payslip</h5>
</div>

<div class="glass-card p-0 mb-4">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-building card-icon"></i>
            <h6 class="card-title"><?= e(APP_NAME) ?></h6>
        </div>
        <div class="card-header-right">
            <span class="text-muted small">Payslip for <?= e(date('F Y', strtotime($salary->salary_date))) ?></span>
        </div>
    </div>
    <div class="card-body">
        <div class="row g-4">
            <div class="col-md-6">
                <h6 class="text-gold mb-3"><i class="fas fa-user-tie me-2"></i>Employee Information</h6>
                <table class="table table-sm table-borderless">
                    <tr>
                        <td class="text-muted" width="140">Name</td>
                        <td><strong><?= e($salary->first_name . ' ' . $salary->last_name) ?></strong></td>
                    </tr>
                    <tr>
                        <td class="text-muted">Job Title</td>
                        <td><?= e($salary->job_title ?? '-') ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted">Email</td>
                        <td><?= e($salary->email ?? '-') ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted">Phone</td>
                        <td><?= e($salary->phone ?? '-') ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted">Hire Date</td>
                        <td><?= !empty($salary->hire_date) ? formatDate($salary->hire_date) : '-' ?></td>
                    </tr>
                    <tr>
                        <td class="text-muted">Salary Type</td>
                        <td><?= e(ucwords(str_replace('_', ' ', $salary->salary_type ?? '-'))) ?></td>
                    </tr>
                </table>
            </div>
            <div class="col-md-6">
                <h6 class="text-gold mb-3"><i class="fas fa-calendar-alt me-2"></i>Payment Details</h6>
                <table class="table table-sm table-borderless">
                    <tr>
                        <td class="text-muted" width="140">Period</td>
                        <td><strong><?= e(date('F Y', strtotime($salary->salary_date))) ?></strong></td>
                    </tr>
                    <tr>
                        <td class="text-muted">Status</td>
                        <td>
                            <span class="badge bg-<?= statusBadge($salary->payment_status) ?>">
                                <?= e(ucfirst($salary->payment_status)) ?>
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-muted">Paid At</td>
                        <td><?= !empty($salary->payment_date) ? formatDateTime($salary->payment_date) : '-' ?></td>
                    </tr>
                    <?php if (!empty($salary->processed_by_first_name)): ?>
                        <tr>
                            <td class="text-muted">Processed By</td>
                            <td><?= e($salary->processed_by_first_name . ' ' . $salary->processed_by_last_name) ?></td>
                        </tr>
                    <?php endif; ?>
                </table>
            </div>
        </div>

        <hr class="border-secondary">

        <h6 class="text-gold mb-3"><i class="fas fa-calculator me-2"></i>Salary Breakdown</h6>

        <?php if (!empty($commissions)): ?>
            <div class="table-responsive mb-3">
                <table class="table table-dark table-sm table-hover mb-0">
                    <thead>
                        <tr>
                            <th>Commission Details</th>
                            <th class="text-end">Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($commissions as $comm): ?>
                            <tr>
                                <td><?= e($comm->service_name ?? 'Commission') ?></td>
                                <td class="text-end"><?= currency($comm->amount) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>

        <div class="table-responsive">
            <table class="table table-dark table-borderless mb-0">
                <tbody>
                    <tr>
                        <td width="200">Basic Salary</td>
                        <td class="text-end"><?= currency($salary->basic_salary) ?></td>
                    </tr>
                    <tr>
                        <td>Commissions</td>
                        <td class="text-end">+ <?= currency($salary->commission_total) ?></td>
                    </tr>
                    <tr>
                        <td>Bonus</td>
                        <td class="text-end">+ <?= currency($salary->bonus) ?></td>
                    </tr>
                    <tr>
                        <td>Deductions</td>
                        <td class="text-end">- <?= currency($salary->deductions) ?></td>
                    </tr>
                    <tr class="border-top border-gold">
                        <td><strong class="text-gold">Net Salary</strong></td>
                        <td class="text-end">
                            <strong class="text-gold fs-5"><?= currency($salary->net_salary) ?></strong>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>

        <?php if (!empty($salary->notes)): ?>
            <hr class="border-secondary">
            <h6 class="text-gold mb-2"><i class="fas fa-sticky-note me-2"></i>Notes</h6>
            <p class="mb-0"><?= nl2br(e($salary->notes)) ?></p>
        <?php endif; ?>
    </div>
</div>

<div class="d-flex gap-2">
    <a href="<?= url('salary') ?>" class="btn btn-outline-secondary">
        <i class="fas fa-arrow-left me-1"></i>Back to Salaries
    </a>
    <button onclick="window.print()" class="btn btn-gold">
        <i class="fas fa-print me-1"></i>Print Payslip
    </button>
</div>
