<?php
/**
 * @var object $reviews
 * @var string $employeeId
 * @var array $employees
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-chart-line me-2 text-gold"></i>Performance Reviews</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">All Reviews</h6>
        </div>
        <div class="card-header-right">
            <?php if (can('performance_create')): ?>
                <button type="button" class="btn btn-sm btn-gold" data-bs-toggle="modal" data-bs-target="#createReviewModal">
                    <i class="fas fa-plus me-1"></i>New Review
                </button>
            <?php endif; ?>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-3">
            <div class="col-md-6">
                <form method="GET" action="<?= url('performance') ?>" class="d-flex gap-2">
                    <select name="employee_id" class="form-select form-select-sm bg-transparent" style="width:auto;" onchange="this.form.submit()">
                        <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>
                    <a href="<?= url('performance') ?>" 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>Review Date</th>
                        <th>Rating</th>
                        <th>Feedback</th>
                        <th>Strengths</th>
                        <th>Areas to Improve</th>
                        <th>Reviewed By</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($reviews->data)): ?>
                        <tr>
                            <td colspan="8" 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 $i = ($reviews->currentPage - 1) * $reviews->perPage + 1; ?>
                        <?php foreach ($reviews->data as $review): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td>
                                    <a href="<?= url('employees/show/' . $review->employee_id) ?>" class="text-gold text-decoration-none">
                                        <strong><?= e($review->first_name . ' ' . $review->last_name) ?></strong>
                                    </a>
                                </td>
                                <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" style="max-width:250px;">
                                    <?= e(truncate($review->feedback, 100)) ?>
                                </td>
                                <td class="small" style="max-width:200px;">
                                    <?= e(truncate($review->strengths ?? '-', 80)) ?>
                                </td>
                                <td class="small" style="max-width:200px;">
                                    <?= e(truncate($review->areas_for_improvement ?? '-', 80)) ?>
                                </td>
                                <td class="small">
                                    <?= e(($review->reviewer_first_name ?? '') . ' ' . ($review->reviewer_last_name ?? '')) ?: '-' ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

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

<?php if (can('performance_create')): ?>
<div class="modal fade" id="createReviewModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content bg-dark">
            <div class="modal-header border-secondary">
                <h5 class="modal-title"><i class="fas fa-star text-gold me-2"></i>New Performance Review</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="<?= url('performance/store') ?>">
                <div class="modal-body">
                    <?= csrf_field() ?>
                    <div class="row g-3">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Employee <span class="text-danger">*</span></label>
                                <select name="employee_id" class="form-select bg-transparent" required>
                                    <option value="">Select Employee</option>
                                    <?php foreach ($employees as $emp): ?>
                                        <option value="<?= $emp->id ?>"><?= e($emp->first_name . ' ' . $emp->last_name) ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="mb-3">
                                <label class="form-label">Rating <span class="text-danger">*</span></label>
                                <select name="rating" class="form-select bg-transparent" required>
                                    <option value="">Select</option>
                                    <option value="1">1 - Poor</option>
                                    <option value="2">2 - Below Average</option>
                                    <option value="3">3 - Average</option>
                                    <option value="4">4 - Good</option>
                                    <option value="5">5 - Excellent</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="mb-3">
                                <label class="form-label">Review Date <span class="text-danger">*</span></label>
                                <input type="date" name="review_date" class="form-control bg-transparent"
                                       value="<?= date('Y-m-d') ?>" required>
                            </div>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Feedback <span class="text-danger">*</span></label>
                        <textarea name="feedback" class="form-control bg-transparent" rows="4"
                                  maxlength="5000" required placeholder="Detailed feedback..."></textarea>
                    </div>
                    <div class="row g-3">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Strengths</label>
                                <textarea name="strengths" class="form-control bg-transparent" rows="3"
                                          maxlength="2000" placeholder="Key strengths..."></textarea>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Areas for Improvement</label>
                                <textarea name="areas_for_improvement" class="form-control bg-transparent" rows="3"
                                          maxlength="2000" placeholder="Areas to work on..."></textarea>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer border-secondary">
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-gold">
                        <i class="fas fa-save me-1"></i>Save Review
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php endif; ?>
