<?php include __DIR__ . '/../../layouts/header.php'; ?>
<?php include __DIR__ . '/../../layouts/sidebar.php'; ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2 class="mb-0"><?= __('reviews') ?></h2>
</div>

<div class="table-container">
    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th><?= __('id') ?></th>
                    <th><?= __('customer') ?></th>
                    <th>Target</th>
                    <th>Rating</th>
                    <th>Comment</th>
                    <th><?= __('date') ?></th>
                    <th><?= __('status') ?></th>
                    <th class="text-end"><?= __('actions') ?></th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($reviews)): ?>
                    <tr>
                        <td colspan="8" class="text-center py-5 text-muted"><?= __('no_data_found') ?></td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($reviews as $review): ?>
                        <tr>
                            <td class="text-secondary fw-semibold">#<?= htmlspecialchars($review['id']) ?></td>
                            <td>
                                <span class="fw-bold text-dark">
                                    <i class="fas fa-user-circle text-secondary" style="<?= isRTL() ? 'margin-left: 4px;' : 'margin-right: 4px;' ?>"></i> <?= htmlspecialchars($review['customer_name']) ?>
                                </span>
                            </td>
                            <td>
                                <?php if($review['maid_id']): ?>
                                    <span class="badge badge-light text-primary border fw-normal"><i class="fas fa-user-nurse"></i> <?= htmlspecialchars($review['maid_name']) ?></span>
                                <?php else: ?>
                                    <span class="badge badge-light text-dark border fw-normal"><i class="fas fa-building"></i> <?= htmlspecialchars($review['agency_name']) ?></span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <div class="text-warning">
                                    <?php 
                                        $rating = (float)$review['rating'];
                                        for($i = 1; $i <= 5; $i++) {
                                            if($i <= $rating) echo '<i class="fas fa-star"></i>';
                                            elseif($i - 0.5 <= $rating) echo '<i class="fas fa-star-half-alt"></i>';
                                            else echo '<i class="far fa-star"></i>';
                                        }
                                    ?>
                                    <span class="text-dark fw-bold ms-1" style="font-size:12px;"><?= $rating ?></span>
                                </div>
                            </td>
                            <td style="max-width: 250px;" class="text-truncate" title="<?= htmlspecialchars($review['comment']) ?>">
                                <?= htmlspecialchars($review['comment'] ?: '-') ?>
                            </td>
                            <td class="text-secondary"><?= date('M d, Y', strtotime($review['created_at'])) ?></td>
                            <td>
                                <?php if ($review['status'] === 'approved'): ?>
                                    <span class="badge badge-success"><?= __('approved') ?></span>
                                <?php elseif ($review['status'] === 'rejected'): ?>
                                    <span class="badge badge-danger"><?= __('rejected') ?></span>
                                <?php else: ?>
                                    <span class="badge badge-warning"><?= htmlspecialchars(ucfirst($review['status'])) ?></span>
                                <?php endif; ?>
                            </td>
                            <td class="text-end">
                                <button class="btn btn-light btn-sm text-success me-1" title="Approve"><i class="fas fa-check"></i></button>
                                <button class="btn btn-light btn-sm text-danger" title="Reject"><i class="fas fa-times"></i></button>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if ($totalPages > 1): ?>
    <div class="d-flex justify-content-end mt-4">
        <ul class="pagination mb-0 gap-2">
            <li class="page-item <?= $page <= 1 ? 'disabled' : '' ?>">
                <a class="page-link rounded-circle border-0 text-secondary bg-light" href="?page=reviews&p=<?= $page - 1 ?>" style="width:40px; height:40px; display:flex; align-items:center; justify-content:center;"><i class="fas <?= isRTL() ? 'fa-chevron-right' : 'fa-chevron-left' ?>"></i></a>
            </li>
            <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                <li class="page-item <?= $i == $page ? 'active' : '' ?>">
                    <a class="page-link rounded-circle border-0 <?= $i == $page ? 'bg-primary text-white' : 'text-secondary bg-light' ?>" href="?page=reviews&p=<?= $i ?>" style="width:40px; height:40px; display:flex; align-items:center; justify-content:center;"><?= $i ?></a>
                </li>
            <?php endfor; ?>
            <li class="page-item <?= $page >= $totalPages ? 'disabled' : '' ?>">
                <a class="page-link rounded-circle border-0 text-secondary bg-light" href="?page=reviews&p=<?= $page + 1 ?>" style="width:40px; height:40px; display:flex; align-items:center; justify-content:center;"><i class="fas <?= isRTL() ? 'fa-chevron-left' : 'fa-chevron-right' ?>"></i></a>
            </li>
        </ul>
    </div>
    <?php endif; ?>
</div>

<?php include __DIR__ . '/../../layouts/footer.php'; ?>
