<?php
use Core\App\Models\ProductReviewModel;
$reviewModel = new ProductReviewModel();
$reviews = $reviewModel->getAll();
include __DIR__ . '/../header.php';
?>

<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">

<div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-4">
    <div>
        <h3 class="mb-1 fw-bold"><i class="bi bi-star me-2 text-warning"></i><?= __('reviews.admin_title','Product Reviews') ?></h3>
        <p class="text-muted mb-0 small"><?= __('reviews.subtitle','Manage and moderate customer reviews') ?></p>
    </div>
    <?php
    $approved = count(array_filter($reviews, fn($r)=>$r['status']==='approved'));
    $pending  = count($reviews) - $approved;
    ?>
    <div class="d-flex gap-2">
        <?php if ($pending > 0): ?>
        <span class="badge bg-warning text-dark fs-6 px-3 py-2">
            <i class="bi bi-hourglass-split me-1"></i><?= $pending ?> <?= __('reviews.pending_count','Pending') ?>
        </span>
        <?php endif; ?>
        <span class="badge bg-success fs-6 px-3 py-2">
            <i class="bi bi-check-circle me-1"></i><?= $approved ?> <?= __('reviews.approved_count','Approved') ?>
        </span>
    </div>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white border-bottom d-flex justify-content-between align-items-center py-3">
        <h6 class="mb-0 fw-semibold"><i class="bi bi-list-ul me-2 text-muted"></i><?= __('reviews.list','All Reviews') ?></h6>
        <span class="badge bg-secondary rounded-pill"><?= count($reviews) ?></span>
    </div>
    <div class="card-body p-0">
        <?php if (empty($reviews)): ?>
        <div class="text-center py-5 text-muted">
            <i class="bi bi-chat-square-dots display-4 d-block mb-2 opacity-50"></i>
            <p class="mb-0"><?= __('reviews.empty','No reviews yet.') ?></p>
        </div>
        <?php else: ?>
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th class="ps-3"><?= __('reviews.table.product','Product') ?></th>
                        <th><?= __('reviews.table.name','Customer') ?></th>
                        <th><?= __('reviews.table.rating','Rating') ?></th>
                        <th><?= __('reviews.table.status','Status') ?></th>
                        <th><?= __('reviews.table.created','Date') ?></th>
                        <th class="text-end pe-3"><?= __('reviews.table.actions','Actions') ?></th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($reviews as $review): ?>
                <tr>
                    <td class="ps-3 fw-semibold small"><?= htmlspecialchars($review['product_title'] ?? '#'.$review['product_id']) ?></td>
                    <td>
                        <div class="fw-semibold small"><?= htmlspecialchars($review['name']) ?></div>
                        <div class="text-muted" style="font-size:.72rem"><?= htmlspecialchars($review['email']) ?></div>
                    </td>
                    <td>
                        <span class="text-warning fw-bold"><?= str_repeat('★',(int)$review['rating']) ?></span>
                        <span class="text-muted"><?= str_repeat('☆', 5-(int)$review['rating']) ?></span>
                    </td>
                    <td>
                        <?php if ($review['status']==='approved'): ?>
                            <span class="badge bg-success-subtle text-success border border-success-subtle">
                                <i class="bi bi-check-circle me-1"></i><?= __('reviews.approved','Approved') ?>
                            </span>
                        <?php else: ?>
                            <span class="badge bg-warning-subtle text-warning border border-warning-subtle">
                                <i class="bi bi-hourglass-split me-1"></i><?= __('reviews.pending','Pending') ?>
                            </span>
                        <?php endif; ?>
                    </td>
                    <td class="text-muted small"><?= date('d/m/Y', strtotime($review['created_at'])) ?></td>
                    <td class="text-end pe-3">
                        <button type="button" class="btn btn-sm btn-outline-primary btn-review-preview"
                                data-bs-toggle="modal" data-bs-target="#reviewModal"
                                data-name="<?= htmlspecialchars($review['name'],ENT_QUOTES) ?>"
                                data-email="<?= htmlspecialchars($review['email'],ENT_QUOTES) ?>"
                                data-rating="<?= (int)$review['rating'] ?>"
                                data-comment="<?= htmlspecialchars($review['comment'],ENT_QUOTES) ?>"
                                data-images='<?= json_encode($review['images']??[],JSON_UNESCAPED_SLASHES) ?>'>
                            <i class="bi bi-eye"></i>
                        </button>
                        <?php if ($review['status']!=='approved'): ?>
                        <form method="post" action="/admin/reviews/approve/<?= (int)$review['id'] ?>" class="d-inline">
                            <?= \Core\System\Csrf::field() ?>
                            <button 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)$review['id'] ?>" class="d-inline"
                              onsubmit="return confirm('<?= __('reviews.confirm_delete','Are you sure?') ?>')">
                            <?= \Core\System\Csrf::field() ?>
                            <button class="btn btn-sm btn-outline-danger"><i class="bi bi-trash3"></i></button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div>

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

<!-- Modal -->
<div class="modal fade" id="reviewModal" tabindex="-1">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content border-0 shadow">
            <div class="modal-header border-bottom">
                <h5 class="modal-title fw-semibold"><i class="bi bi-star me-2 text-warning"></i><?= __('reviews.preview_title','Review Preview') ?></h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="row g-3 mb-3">
                    <div class="col-sm-6"><label class="text-muted small"><?= __('reviews.name','Name') ?></label><div id="pName" class="fw-semibold"></div></div>
                    <div class="col-sm-6"><label class="text-muted small"><?= __('reviews.email','Email') ?></label><div id="pEmail" class="fw-semibold"></div></div>
                </div>
                <div class="mb-3"><label class="text-muted small"><?= __('reviews.rating','Rating') ?></label><div id="pRating" class="text-warning fw-bold fs-5"></div></div>
                <div class="mb-3"><label class="text-muted small"><?= __('reviews.comment','Comment') ?></label><p id="pComment" class="mt-1 text-dark"></p></div>
                <div id="pImages" class="d-flex gap-2 flex-wrap"></div>
            </div>
            <div class="modal-footer"><button class="btn btn-secondary" data-bs-dismiss="modal"><?= __('actions.close','Close') ?></button></div>
        </div>
    </div>
</div>

<script>
document.getElementById('reviewModal')?.addEventListener('show.bs.modal', function(e){
    const b = e.relatedTarget;
    document.getElementById('pName').textContent    = b.dataset.name||'';
    document.getElementById('pEmail').textContent   = b.dataset.email||'';
    document.getElementById('pRating').textContent  = '★'.repeat(+b.dataset.rating||0)+' ('+b.dataset.rating+'/5)';
    document.getElementById('pComment').textContent = b.dataset.comment||'';
    const box = document.getElementById('pImages'); box.innerHTML='';
    try{ JSON.parse(b.dataset.images||'[]').forEach(src=>{
        const img=document.createElement('img'); img.src=src;
        img.className='rounded border'; img.style.cssText='width:90px;height:90px;object-fit:cover';
        box.appendChild(img);
    }); }catch(e){}
});
</script>

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