<?php
require_once __DIR__ . '/includes/admin_bootstrap.php';

if (isset($_GET['toggle_system'])) {
    $cur = getSetting($pdo,'review_enabled');
    $pdo->prepare("UPDATE settings SET setting_value=? WHERE setting_key='review_enabled'")->execute([$cur=='1'?'0':'1']);
    redirect(BASE_URL.'/admin/reviews.php');
}
if (isset($_GET['approve'])) {
    $pdo->prepare("UPDATE reviews SET status='approved' WHERE id=?")->execute([(int)$_GET['approve']]);
    redirect(BASE_URL.'/admin/reviews.php?action_done=approve');
}
if (isset($_GET['reject'])) {
    $pdo->prepare("UPDATE reviews SET status='rejected' WHERE id=?")->execute([(int)$_GET['reject']]);
    redirect(BASE_URL.'/admin/reviews.php?action_done=reject');
}
if (isset($_GET['delete'])) {
    $pdo->prepare("DELETE FROM reviews WHERE id=?")->execute([(int)$_GET['delete']]);
    redirect(BASE_URL.'/admin/reviews.php?action_done=delete');
}

$reviewEnabled  = getSetting($pdo,'review_enabled') == '1';
$statusFilter   = $_GET['status'] ?? '';
$where  = $statusFilter ? "WHERE r.status=?" : '';
$params = $statusFilter ? [$statusFilter] : [];
$stmt = $pdo->prepare("SELECT r.*,u.name as user_name,p.name as product_name FROM reviews r LEFT JOIN users u ON r.user_id=u.id LEFT JOIN products p ON r.product_id=p.id $where ORDER BY r.created_at DESC");
$stmt->execute($params);
$reviews = $stmt->fetchAll();


// ── HTML output starts ──
include __DIR__ . '/includes/admin_header.php';

?>
<h2>রিভিউ ব্যবস্থাপনা</h2>

<?php if(isset($_GET['action_done'])): ?><div class="alert alert-success"><i class="fas fa-check-circle"></i> সম্পন্ন হয়েছে।</div><?php endif; ?>

<div class="card" style="margin-bottom:20px;">
    <div class="card-body" style="display:flex;align-items:center;justify-content:space-between;padding:14px 20px;flex-wrap:wrap;gap:12px;">
        <div><strong>রিভিউ সিস্টেম:</strong>
            <span class="badge <?= $reviewEnabled?'badge-active':'badge-inactive' ?>" style="margin-left:8px;"><?= $reviewEnabled?'চালু':'বন্ধ' ?></span>
        </div>
        <a href="?toggle_system=1" class="topbar-btn <?= $reviewEnabled?'btn-red':'btn-green' ?>">
            <i class="fas fa-<?= $reviewEnabled?'toggle-off':'toggle-on' ?>"></i>
            <?= $reviewEnabled?'বন্ধ করুন':'চালু করুন' ?>
        </a>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <h3>সকল রিভিউ</h3>
        <form method="GET" style="display:flex;gap:8px;">
            <select class="form-control" name="status" style="max-width:160px;">
                <option value="">সব রিভিউ</option>
                <option value="pending"  <?= $statusFilter==='pending' ?'selected':''?>>অপেক্ষমান</option>
                <option value="approved" <?= $statusFilter==='approved'?'selected':''?>>অনুমোদিত</option>
                <option value="rejected" <?= $statusFilter==='rejected'?'selected':''?>>প্রত্যাখ্যাত</option>
            </select>
            <button type="submit" class="topbar-btn btn-navy">ফিল্টার</button>
        </form>
    </div>
    <div class="table-wrap">
        <table>
            <thead><tr><th>পণ্য</th><th>ব্যবহারকারী</th><th>রেটিং</th><th>মন্তব্য</th><th>অবস্থা</th><th>তারিখ</th><th>অ্যাকশন</th></tr></thead>
            <tbody>
            <?php foreach($reviews as $rv): ?>
            <tr>
                <td><?= e($rv['product_name']) ?></td>
                <td><?= e($rv['user_name']) ?></td>
                <td style="color:var(--gold);"><?= str_repeat('★',$rv['rating']) ?></td>
                <td style="max-width:180px;"><?= e(mb_substr($rv['comment'],0,60)) ?>...</td>
                <td><span class="badge badge-<?=$rv['status']?>"><?= match($rv['status']){'pending'=>'অপেক্ষমান','approved'=>'অনুমোদিত','rejected'=>'প্রত্যাখ্যাত',default=>$rv['status']} ?></span></td>
                <td><?= date('d/m/Y',strtotime($rv['created_at'])) ?></td>
                <td>
                    <div class="actions">
                        <?php if($rv['status']!=='approved'): ?><a href="?approve=<?=$rv['id']?>" class="topbar-btn btn-green"><i class="fas fa-check"></i></a><?php endif; ?>
                        <?php if($rv['status']!=='rejected'): ?><a href="?reject=<?=$rv['id']?>"  class="topbar-btn btn-gray"><i class="fas fa-ban"></i></a><?php endif; ?>
                        <a href="?delete=<?=$rv['id']?>" class="topbar-btn btn-red" data-confirm="মুছবেন?"><i class="fas fa-trash"></i></a>
                    </div>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php include __DIR__ . '/includes/admin_footer.php'; ?>
