<?php
require_once '../../config/config.php';
require_once '../../includes/auth.php';

requireAdminLogin();

// Filtreleme
$status_filter = $_GET['status'] ?? 'pending';
$company_filter = $_GET['company'] ?? '';

$where = ['1=1'];
$params = [];

if ($status_filter) {
    $where[] = "r.status = ?";
    $params[] = $status_filter;
}

if ($company_filter) {
    $where[] = "r.company_id = ?";
    $params[] = $company_filter;
}

$whereClause = implode(' AND ', $where);

// Yorumları listele
$sql = "
    SELECT r.*, c.company_name 
    FROM reviews r
    LEFT JOIN companies c ON r.company_id = c.id
    WHERE $whereClause
    ORDER BY r.created_at DESC
";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$reviews = $stmt->fetchAll();

// Firmalar listesi (filtre için)
$stmt = $pdo->query("SELECT id, company_name FROM companies ORDER BY company_name");
$companies = $stmt->fetchAll();

$page_title = 'Yorumlar';
include '../includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3">Yorumlar</h1>
</div>

<!-- Filtreleme -->
<div class="card mb-4">
    <div class="card-body">
        <form method="GET" class="row g-3">
            <div class="col-md-4">
                <select class="form-select" name="status">
                    <option value="">Tüm Durumlar</option>
                    <option value="pending" <?= $status_filter === 'pending' ? 'selected' : '' ?>>Onay Bekleyen</option>
                    <option value="approved" <?= $status_filter === 'approved' ? 'selected' : '' ?>>Onaylanan</option>
                    <option value="rejected" <?= $status_filter === 'rejected' ? 'selected' : '' ?>>Reddedilen</option>
                </select>
            </div>
            <div class="col-md-4">
                <select class="form-select" name="company">
                    <option value="">Tüm Firmalar</option>
                    <?php foreach ($companies as $company): ?>
                        <option value="<?= $company['id'] ?>" <?= $company_filter == $company['id'] ? 'selected' : '' ?>>
                            <?= e($company['company_name']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary w-100">
                    <i class="fas fa-search me-2"></i>Filtrele
                </button>
            </div>
            <div class="col-md-2">
                <a href="index.php" class="btn btn-secondary w-100">Temizle</a>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <?php if (empty($reviews)): ?>
            <p class="text-muted text-center py-5">Yorum bulunamadı.</p>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Kullanıcı</th>
                            <th>E-posta</th>
                            <th>Firma</th>
                            <th>Puan</th>
                            <th>Yorum</th>
                            <th>Durum</th>
                            <th>Tarih</th>
                            <th>İşlemler</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($reviews as $review): ?>
                            <tr>
                                <td><?= $review['id'] ?></td>
                                <td><?= e($review['name'] . ' ' . $review['surname']) ?></td>
                                <td><?= e($review['email']) ?></td>
                                <td><?= e($review['company_name']) ?></td>
                                <td>
                                    <?= displayStars($review['rating']) ?>
                                    <span class="ms-2"><?= $review['rating'] ?>/5</span>
                                </td>
                                <td><?= excerpt($review['comment'], 50) ?></td>
                                <td>
                                    <span class="badge bg-<?= $review['status'] === 'approved' ? 'success' : ($review['status'] === 'pending' ? 'warning' : 'danger') ?>">
                                        <?= $review['status'] === 'approved' ? 'Onaylı' : ($review['status'] === 'pending' ? 'Beklemede' : 'Reddedildi') ?>
                                    </span>
                                    <?php if ($review['admin_edited']): ?>
                                        <span class="badge bg-info ms-1">Düzenlendi</span>
                                    <?php endif; ?>
                                </td>
                                <td><?= formatDate($review['created_at'], 'd.m.Y H:i') ?></td>
                                <td>
                                    <a href="edit.php?id=<?= $review['id'] ?>" class="btn btn-sm btn-warning">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <?php if ($review['status'] === 'pending'): ?>
                                        <a href="approve.php?id=<?= $review['id'] ?>&action=approve" class="btn btn-sm btn-success">
                                            <i class="fas fa-check"></i>
                                        </a>
                                        <a href="approve.php?id=<?= $review['id'] ?>&action=reject" class="btn btn-sm btn-danger" onclick="return confirm('Bu yorumu reddetmek istediğinize emin misiniz?')">
                                            <i class="fas fa-times"></i>
                                        </a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

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





