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

requireAdminLogin();

$id = $_GET['id'] ?? 0;

$stmt = $pdo->prepare("
    SELECT r.*, c.company_name 
    FROM reviews r
    LEFT JOIN companies c ON r.company_id = c.id
    WHERE r.id = ?
");
$stmt->execute([$id]);
$review = $stmt->fetch();

if (!$review) {
    setFlashMessage('error', 'Yorum bulunamadı.');
    redirect('index.php');
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $surname = trim($_POST['surname'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $rating = intval($_POST['rating'] ?? 0);
    $comment = trim($_POST['comment'] ?? '');
    $status = $_POST['status'] ?? 'pending';
    
    if (empty($name) || empty($surname)) {
        $error = 'Ad ve soyad gereklidir.';
    } elseif (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Geçerli bir e-posta adresi giriniz.';
    } elseif ($rating < 1 || $rating > 5) {
        $error = 'Puan 1-5 arası olmalıdır.';
    } else {
        try {
            $stmt = $pdo->prepare("
                UPDATE reviews 
                SET name = ?, surname = ?, email = ?, rating = ?, comment = ?, status = ?, admin_edited = 1
                WHERE id = ?
            ");
            $stmt->execute([$name, $surname, $email, $rating, $comment, $status, $id]);
            
            setFlashMessage('success', 'Yorum başarıyla güncellendi.');
            redirect('index.php');
        } catch (PDOException $e) {
            $error = 'Yorum güncellenirken bir hata oluştu.';
        }
    }
}

$page_title = 'Yorum Düzenle';
include '../includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3">Yorum Düzenle</h1>
    <a href="index.php" class="btn btn-secondary">
        <i class="fas fa-arrow-left me-2"></i>Geri Dön
    </a>
</div>

<?php if ($error): ?>
    <div class="alert alert-danger"><?= e($error) ?></div>
<?php endif; ?>

<div class="card">
    <div class="card-body">
        <form method="POST">
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label class="form-label">Firma</label>
                        <input type="text" class="form-control" value="<?= e($review['company_name']) ?>" disabled>
                    </div>
                    
                    <div class="mb-3">
                        <label for="name" class="form-label">Ad <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="name" name="name" value="<?= e($review['name']) ?>" required>
                    </div>
                    
                    <div class="mb-3">
                        <label for="surname" class="form-label">Soyad <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="surname" name="surname" value="<?= e($review['surname']) ?>" required>
                    </div>
                    
                    <div class="mb-3">
                        <label for="email" class="form-label">E-posta <span class="text-danger">*</span></label>
                        <input type="email" class="form-control" id="email" name="email" value="<?= e($review['email']) ?>" required>
                        <small class="text-muted">Bu bilgi sadece admin tarafından görülebilir.</small>
                    </div>
                </div>
                
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="rating" class="form-label">Puan <span class="text-danger">*</span></label>
                        <select class="form-select" id="rating" name="rating" required>
                            <option value="1" <?= $review['rating'] == 1 ? 'selected' : '' ?>>1 Yıldız</option>
                            <option value="2" <?= $review['rating'] == 2 ? 'selected' : '' ?>>2 Yıldız</option>
                            <option value="3" <?= $review['rating'] == 3 ? 'selected' : '' ?>>3 Yıldız</option>
                            <option value="4" <?= $review['rating'] == 4 ? 'selected' : '' ?>>4 Yıldız</option>
                            <option value="5" <?= $review['rating'] == 5 ? 'selected' : '' ?>>5 Yıldız</option>
                        </select>
                        <div class="mt-2">
                            <?= displayStars($review['rating']) ?>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label for="status" class="form-label">Durum</label>
                        <select class="form-select" id="status" name="status">
                            <option value="pending" <?= $review['status'] === 'pending' ? 'selected' : '' ?>>Beklemede</option>
                            <option value="approved" <?= $review['status'] === 'approved' ? 'selected' : '' ?>>Onaylı</option>
                            <option value="rejected" <?= $review['status'] === 'rejected' ? 'selected' : '' ?>>Reddedildi</option>
                        </select>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Oluşturulma Tarihi</label>
                        <input type="text" class="form-control" value="<?= formatDate($review['created_at']) ?>" disabled>
                    </div>
                </div>
            </div>
            
            <div class="mb-3">
                <label for="comment" class="form-label">Yorum</label>
                <textarea class="form-control" id="comment" name="comment" rows="6" required><?= e($review['comment']) ?></textarea>
            </div>
            
            <?php if ($review['admin_edited']): ?>
                <div class="alert alert-info">
                    <i class="fas fa-info-circle me-2"></i>Bu yorum daha önce admin tarafından düzenlenmiştir.
                </div>
            <?php endif; ?>
            
            <div class="mt-4">
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-save me-2"></i>Güncelle
                </button>
                <a href="index.php" class="btn btn-secondary">İptal</a>
            </div>
        </form>
    </div>
</div>

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





