<?php
/**
 * @var object $employee
 * @var array $schedule
 * @var object $attendanceStats
 * @var array $commissions
 * @var array $reviews
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-user-circle me-2 text-gold"></i><?= e($employee->first_name . ' ' . $employee->last_name) ?></h5>
</div>

<div class="row g-4 mb-4">
    <div class="col-lg-4">
        <div class="glass-card text-center">
            <div class="card-body py-4">
                <?php if (!empty($employee->photo)): ?>
                    <img src="<?= uploadUrl($employee->photo) ?>" alt="" class="rounded-circle mb-3"
                         width="120" height="120" style="object-fit:cover;border:3px solid var(--gold);">
                <?php else: ?>
                    <div class="rounded-circle d-inline-flex align-items-center justify-content-center mb-3"
                         style="width:120px;height:120px;background:var(--gold);color:#000;font-size:48px;font-weight:600;">
                        <?= strtoupper(substr($employee->first_name ?? '?', 0, 1) . substr($employee->last_name ?? '', 0, 1)) ?>
                    </div>
                <?php endif; ?>
                <h5 class="mb-1"><?= e($employee->first_name . ' ' . $employee->last_name) ?></h5>
                <p class="text-muted small mb-2"><?= e($employee->job_title ?? '') ?></p>
                <span class="badge bg-<?= $employee->is_active ? 'success' : 'danger' ?> mb-3">
                    <?= $employee->is_active ? 'Active' : 'Inactive' ?>
                </span>
                <div class="d-flex justify-content-center gap-2 mt-2">
                    <?php if (can('employees_edit')): ?>
                        <a href="<?= url('employees/edit/' . $employee->id) ?>" class="btn btn-sm btn-outline-gold">
                            <i class="fas fa-edit me-1"></i>Edit
                        </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-8">
        <div class="row g-3">
            <div class="col-md-3 col-6">
                <div class="glass-card text-center py-3">
                    <div class="fs-3 text-gold fw-bold"><?= (int)$attendanceStats->present_days ?></div>
                    <div class="small text-muted">Present</div>
                </div>
            </div>
            <div class="col-md-3 col-6">
                <div class="glass-card text-center py-3">
                    <div class="fs-3 text-warning fw-bold"><?= (int)$attendanceStats->late_days ?></div>
                    <div class="small text-muted">Late</div>
                </div>
            </div>
            <div class="col-md-3 col-6">
                <div class="glass-card text-center py-3">
                    <div class="fs-3 text-danger fw-bold"><?= (int)$attendanceStats->absent_days ?></div>
                    <div class="small text-muted">Absent</div>
                </div>
            </div>
            <div class="col-md-3 col-6">
                <div class="glass-card text-center py-3">
                    <div class="fs-3 text-info fw-bold"><?= currency(array_sum(array_column($commissions, 'amount'))) ?></div>
                    <div class="small text-muted">Commissions</div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <ul class="nav nav-tabs card-header-tabs" id="employeeTabs" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link active" id="overview-tab" data-bs-toggle="tab" data-bs-target="#overview"
                        type="button" role="tab" aria-selected="true">
                    <i class="fas fa-info-circle me-1"></i>Overview
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="schedule-tab" data-bs-toggle="tab" data-bs-target="#schedule"
                        type="button" role="tab" aria-selected="false">
                    <i class="fas fa-clock me-1"></i>Schedule
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="commissions-tab" data-bs-toggle="tab" data-bs-target="#commissions"
                        type="button" role="tab" aria-selected="false">
                    <i class="fas fa-coins me-1"></i>Commissions
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="performance-tab" data-bs-toggle="tab" data-bs-target="#performance"
                        type="button" role="tab" aria-selected="false">
                    <i class="fas fa-chart-line me-1"></i>Performance
                </button>
            </li>
        </ul>
    </div>
    <div class="card-body">
        <div class="tab-content" id="employeeTabsContent">

            <!-- Overview Tab -->
            <div class="tab-pane fade show active" id="overview" role="tabpanel" aria-labelledby="overview-tab">
                <div class="row g-4">
                    <div class="col-md-6">
                        <h6 class="text-gold mb-3"><i class="fas fa-address-card me-2"></i>Personal Information</h6>
                        <table class="table table-sm table-borderless">
                            <tr>
                                <td class="text-muted" width="140">First Name</td>
                                <td><?= e($employee->first_name) ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Last Name</td>
                                <td><?= e($employee->last_name) ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Email</td>
                                <td><?= e($employee->email ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Phone</td>
                                <td><?= e($employee->phone ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Mobile</td>
                                <td><?= e($employee->mobile ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Date of Birth</td>
                                <td><?= !empty($employee->date_of_birth) ? formatDate($employee->date_of_birth) : '-' ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Gender</td>
                                <td><?= e(ucfirst($employee->gender ?? '-')) ?></td>
                            </tr>
                        </table>
                    </div>
                    <div class="col-md-6">
                        <h6 class="text-gold mb-3"><i class="fas fa-map-marker-alt me-2"></i>Address</h6>
                        <table class="table table-sm table-borderless">
                            <tr>
                                <td class="text-muted" width="140">Address</td>
                                <td><?= nl2br(e($employee->address ?? '-')) ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">City</td>
                                <td><?= e($employee->city ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">State</td>
                                <td><?= e($employee->state ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">ZIP Code</td>
                                <td><?= e($employee->zip ?? '-') ?></td>
                            </tr>
                        </table>

                        <h6 class="text-gold mb-3 mt-4"><i class="fas fa-briefcase me-2"></i>Employment</h6>
                        <table class="table table-sm table-borderless">
                            <tr>
                                <td class="text-muted" width="140">Job Title</td>
                                <td><?= e($employee->job_title ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Hire Date</td>
                                <td><?= !empty($employee->hire_date) ? formatDate($employee->hire_date) : '-' ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Salary Type</td>
                                <td><?= e(ucwords(str_replace('_', ' ', $employee->salary_type ?? '-'))) ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Salary Amount</td>
                                <td><?= currency($employee->salary_amount ?? 0) ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Commission</td>
                                <td><?= ($employee->commission_percent ?? 0) . '%' ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Target</td>
                                <td><?= currency($employee->target_amount ?? 0) ?></td>
                            </tr>
                        </table>
                    </div>
                </div>
                <div class="row mt-3">
                    <div class="col-12">
                        <h6 class="text-gold mb-3"><i class="fas fa-phone-alt me-2"></i>Emergency Contact</h6>
                        <table class="table table-sm table-borderless">
                            <tr>
                                <td class="text-muted" width="140">Name</td>
                                <td><?= e($employee->emergency_contact ?? '-') ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Phone</td>
                                <td><?= e($employee->emergency_phone ?? '-') ?></td>
                            </tr>
                        </table>
                    </div>
                </div>
                <?php if (!empty($employee->notes)): ?>
                    <div class="row mt-3">
                        <div class="col-12">
                            <h6 class="text-gold mb-3"><i class="fas fa-sticky-note me-2"></i>Notes</h6>
                            <p><?= nl2br(e($employee->notes)) ?></p>
                        </div>
                    </div>
                <?php endif; ?>
            </div>

            <!-- Schedule Tab -->
            <div class="tab-pane fade" id="schedule" role="tabpanel" aria-labelledby="schedule-tab">
                <div class="table-responsive">
                    <table class="table table-dark table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Day</th>
                                <th>Start Time</th>
                                <th>End Time</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($schedule)): ?>
                                <tr>
                                    <td colspan="4" class="text-center text-muted py-4">
                                        <i class="fas fa-calendar-times fa-2x mb-2"></i>
                                        <p class="mb-0">No schedule defined</p>
                                    </td>
                                </tr>
                            <?php else: ?>
                                <?php $days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; ?>
                                <?php foreach ($schedule as $sched): ?>
                                    <tr>
                                        <td><?= e($days[$sched->day_of_week - 1] ?? 'Day ' . $sched->day_of_week) ?></td>
                                        <td><?= e($sched->start_time ? date(TIME_FORMAT, strtotime($sched->start_time)) : '-') ?></td>
                                        <td><?= e($sched->end_time ? date(TIME_FORMAT, strtotime($sched->end_time)) : '-') ?></td>
                                        <td>
                                            <span class="badge bg-<?= ($sched->is_active ?? 1) ? 'success' : 'secondary' ?>">
                                                <?= ($sched->is_active ?? 1) ? 'Active' : 'Inactive' ?>
                                            </span>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- Commissions Tab -->
            <div class="tab-pane fade" id="commissions" role="tabpanel" aria-labelledby="commissions-tab">
                <div class="table-responsive">
                    <table class="table table-dark table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Service</th>
                                <th class="text-end">Amount</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($commissions)): ?>
                                <tr>
                                    <td colspan="4" class="text-center text-muted py-4">
                                        <i class="fas fa-coins fa-2x mb-2"></i>
                                        <p class="mb-0">No commissions found</p>
                                    </td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($commissions as $comm): ?>
                                    <tr>
                                        <td><?= formatDateTime($comm->created_at) ?></td>
                                        <td><?= e($comm->service_name ?? '-') ?></td>
                                        <td class="text-end"><?= currency($comm->amount) ?></td>
                                        <td>
                                            <span class="badge bg-<?= statusBadge($comm->status) ?>">
                                                <?= e(ucfirst($comm->status)) ?>
                                            </span>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
                <div class="mt-3">
                    <a href="<?= url('commissions?employee_id=' . $employee->id) ?>" class="btn btn-sm btn-outline-gold">
                        <i class="fas fa-external-link-alt me-1"></i>View All Commissions
                    </a>
                </div>
            </div>

            <!-- Performance Tab -->
            <div class="tab-pane fade" id="performance" role="tabpanel" aria-labelledby="performance-tab">
                <div class="table-responsive">
                    <table class="table table-dark table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Rating</th>
                                <th>Strengths</th>
                                <th>Areas to Improve</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($reviews)): ?>
                                <tr>
                                    <td colspan="4" class="text-center text-muted py-4">
                                        <i class="fas fa-chart-line fa-2x mb-2"></i>
                                        <p class="mb-0">No performance reviews found</p>
                                    </td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($reviews as $review): ?>
                                    <tr>
                                        <td><?= formatDate($review->review_date ?? $review->created_at) ?></td>
                                        <td>
                                            <span class="text-gold">
                                                <?php for ($s = 1; $s <= 5; $s++): ?>
                                                    <i class="fas fa-star<?= $s <= $review->rating ? '' : '-o text-muted' ?>"></i>
                                                <?php endfor; ?>
                                            </span>
                                            <span class="ms-1">(<?= e($review->rating) ?>/5)</span>
                                        </td>
                                        <td class="small"><?= nl2br(e($review->strengths ?? '-')) ?></td>
                                        <td class="small"><?= nl2br(e($review->areas_for_improvement ?? '-')) ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
                <div class="mt-3">
                    <a href="<?= url('performance?employee_id=' . $employee->id) ?>" class="btn btn-sm btn-outline-gold">
                        <i class="fas fa-external-link-alt me-1"></i>View All Reviews
                    </a>
                </div>
            </div>

        </div>
    </div>
</div>
