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

<div class="container-fluid py-4">

    <!-- PAGE HEADER -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h3 class="mb-1">
                <i class="bi bi-star-fill me-2"></i>
                <?= __('reviews.admin_title', 'Product Reviews') ?>
            </h3>
            <small class="text-muted">
                <?= __('reviews.admin_breadcrumb', 'Catalog → Reviews') ?>
            </small>
        </div>
    </div>

    <?php if (empty($reviews)): ?>

        <!-- EMPTY STATE -->
        <div class="card border-0 shadow-sm">
            <div class="card-body text-center py-5">
                <i class="bi bi-chat-left-text fs-1 text-muted mb-3 d-block"></i>
                <p class="text-muted mb-0">
                    <?= __('reviews.empty', 'No reviews yet') ?>
                </p>
            </div>
        </div>

    <?php else: ?>

        <!-- TABLE -->
        <div class="card border-0 shadow-sm">
            <div class="card-body p-0">

                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">

                        <thead class="table-light">
                        <tr>
                            <th><?= __('reviews.table.product', 'Product') ?></th>
                            <th><?= __('reviews.table.name', 'Name') ?></th>
                            <th><?= __('reviews.table.rating', 'Rating') ?></th>
                            <th><?= __('reviews.table.status', 'Status') ?></th>
                            <th><?= __('reviews.table.created', 'Created') ?></th>
                            <th class="text-end"><?= __('reviews.table.actions', 'Actions') ?></th>
                        </tr>
                        </thead>

                        <tbody>
                        <?php foreach ($reviews as $r): ?>
                            <tr>

                                <!-- PRODUCT -->
                                <td>
                                    <strong>
                                        <?= htmlspecialchars($r['product_title'] ?? ('#' . (int)$r['product_id'])) ?>
                                    </strong>
                                </td>

                                <!-- NAME -->
                                <td>
                                    <?= htmlspecialchars($r['name']) ?>
                                </td>

                                <!-- RATING -->
                                <td>
                                    <span class="text-warning fw-bold">
                                        <?= (int)$r['rating'] ?>/5
                                    </span>
                                </td>

                                <!-- STATUS -->
                                <td>
                                    <?php if ($r['status'] === 'approved'): ?>
                                        <span class="badge bg-success">
                                            <?= __('reviews.approved', 'Approved') ?>
                                        </span>
                                    <?php else: ?>
                                        <span class="badge bg-warning text-dark">
                                            <?= __('reviews.pending', 'Pending') ?>
                                        </span>
                                    <?php endif; ?>
                                </td>

                                <!-- CREATED -->
                                <td>
                                    <?= htmlspecialchars($r['created_at']) ?>
                                </td>

                                <!-- ACTIONS -->
                                <td class="text-end">

                                    <?php if ($r['status'] !== 'approved'): ?>
                                        <form method="post"
                                              action="/admin/reviews/approve/<?= (int)$r['id'] ?>"
                                              style="display:inline-block">
                                            <?= \Core\System\Csrf::field(); ?>
                                            <button type="submit"
                                                    class="btn btn-sm btn-outline-success"
                                                    title="<?= __('reviews.approve', 'Approve') ?>">
                                                <i class="bi bi-check-lg"></i>
                                            </button>
                                        </form>
                                    <?php endif; ?>

                                    <form method="post"
                                          action="/admin/reviews/delete/<?= (int)$r['id'] ?>"
                                          style="display:inline-block"
                                          onsubmit="return confirm('<?= __('reviews.confirm_delete', 'Delete this review?') ?>')">
                                        <?= \Core\System\Csrf::field(); ?>
                                        <button type="submit"
                                                class="btn btn-sm btn-outline-danger"
                                                title="<?= __('common.delete', 'Delete') ?>">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>

                                </td>

                            </tr>
                        <?php endforeach; ?>
                        </tbody>

                    </table>
                </div>

            </div>
        </div>

    <?php endif; ?>

</div>

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