<?php
// comments.php - View All Comments
// File location: /bidwheels.net/admin/comments.php

require_once '../includes/auth.php';
require_once '../includes/functions.php';
require_once 'includes/admin_header.php';

requireAdmin();

// Handle delete comment
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    delete('comments', "id = $id");
    setSuccess('Comment deleted.');
    redirect('/admin/comments.php');
}

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * 50;

$comments = fetchAll("
    SELECT c.*, p.name as product_name, u.name as user_name, u.is_dummy
    FROM comments c
    LEFT JOIN products p ON c.product_id = p.id
    LEFT JOIN users u ON c.user_id = u.id
    ORDER BY c.created_at DESC
    LIMIT $offset, 50
");

$total = getCount('comments');
$total_pages = ceil($total / 50);
?>

<div class="admin-page">
    <h1 class="page-title">All Comments</h1>
    
    <div class="stats-box">
        <div class="stat">
            <span class="stat-label">Total Comments</span>
            <span class="stat-value"><?php echo $total; ?></span>
        </div>
        <div class="stat">
            <span class="stat-label">System Comments</span>
            <span class="stat-value"><?php echo getCount('comments', 'is_system_comment = 1'); ?></span>
        </div>
        <div class="stat">
            <span class="stat-label">User Comments</span>
            <span class="stat-value"><?php echo getCount('comments', 'is_system_comment = 0'); ?></span>
        </div>
    </div>
    
    <div class="table-responsive">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Product</th>
                    <th>User</th>
                    <th>Comment</th>
                    <th>Type</th>
                    <th>Time</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php if (count($comments) > 0): ?>
                    <?php foreach ($comments as $comment): ?>
                        <tr>
                            <td><?php echo $comment['id']; ?></td>
                            <td><a href="edit_product.php?id=<?php echo $comment['product_id']; ?>"><?php echo htmlspecialchars($comment['product_name']); ?></a></td>
                            <td>
                                <?php echo htmlspecialchars($comment['user_name'] ?? 'System'); ?>
                                <?php if ($comment['is_dummy']): ?>
                                    <span class="badge badge-info">Dummy</span>
                                <?php endif; ?>
                            </td>
                            <td style="max-width: 300px;"><?php echo htmlspecialchars(substr($comment['comment_text'], 0, 100)); ?>...</td>
                            <td>
                                <?php if ($comment['is_system_comment']): ?>
                                    <span class="badge badge-warning">Auto</span>
                                <?php else: ?>
                                    <span class="badge badge-success">User</span>
                                <?php endif; ?>
                            </td>
                            <td><?php echo date('M d, Y g:i A', strtotime($comment['created_at'])); ?></td>
                            <td>
                                <a href="?delete=<?php echo $comment['id']; ?>" class="btn-icon btn-danger" onclick="return confirm('Delete this comment?')"><i class="fas fa-trash"></i></a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="7" style="text-align: center;">No comments found.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    
    <?php if ($total_pages > 1): ?>
        <div class="pagination">
            <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                <a href="?page=<?php echo $i; ?>" class="<?php echo $i == $page ? 'active' : ''; ?>"><?php echo $i; ?></a>
            <?php endfor; ?>
        </div>
    <?php endif; ?>
</div>

<style>
.stats-box {
    background: white;
    padding: 20px;
    border-radius: 5px;
    margin-bottom: 25px;
    display: flex;
    justify-content: center;
    gap: 50px;
}
.stat {
    text-align: center;
}
.stat-label {
    display: block;
    font-size: 14px;
    color: #666;
}
.stat-value {
    display: block;
    font-size: 36px;
    font-weight: bold;
    color: #e63946;
}
.badge {
    padding: 3px 6px;
    border-radius: 3px;
    font-size: 10px;
    font-weight: bold;
    margin-left: 5px;
}
.badge-success {
    background: #d4edda;
    color: #155724;
}
.badge-warning {
    background: #fff3cd;
    color: #856404;
}
.badge-info {
    background: #d1ecf1;
    color: #0c5460;
}
</style>

<?php require_once 'includes/admin_footer.php'; ?>