<?php
/**
 * Admin - Kayan Yazı Düzenle
 * Konum: /magaza/admin/marquee/duzenle.php
 */
require_once '../../includes/db.php';
require_once '../../includes/functions.php';
adminCheck();

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header("Location: index.php");
    exit;
}

$id = (int)$_GET['id'];
$mesaj = '';

// Mevcut veriyi çek
$stmt = $db->prepare("SELECT * FROM marquees WHERE id = ?");
$stmt->execute([$id]);
$marquee = $stmt->fetch();

if (!$marquee) {
    die("Düzenlenecek kayıt bulunamadı.");
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $page_type = trim($_POST['page_type']);
    $content = trim($_POST['content']);
    $status = isset($_POST['status']) ? 1 : 0;

    if (empty($content)) {
        $mesaj = "<div class='alert alert-warning shadow-sm'>Lütfen kayan yazı içeriğini boş bırakmayın.</div>";
    } else {
        try {
            $upd = $db->prepare("UPDATE marquees SET page_type = ?, content = ?, status = ? WHERE id = ?");
            $upd->execute([$page_type, $content, $status, $id]);
            
            $mesaj = "<div class='alert alert-success shadow-sm'><i class='bi bi-check-circle-fill me-2'></i> Kayan yazı başarıyla güncellendi!</div>";
            
            // Veriyi formda güncel göstermek için tekrar çek
            $stmt->execute([$id]);
            $marquee = $stmt->fetch();
        } catch (Exception $e) {
            $mesaj = "<div class='alert alert-danger shadow-sm'>Hata: " . $e->getMessage() . "</div>";
        }
    }
}

require_once '../header.php';
?>

<div class="row mb-4 align-items-center text-dark">
    <div class="col-md-6">
        <h2 class="fw-bold mb-0">
            <a href="index.php" class="btn btn-sm btn-outline-secondary me-2 border-2"><i class="bi bi-arrow-left"></i> Geri</a>
            Kayan Yazı Düzenle
        </h2>
    </div>
</div>

<?= $mesaj ?>

<form action="" method="POST">
    <div class="row text-dark">
        <div class="col-lg-8">
            <div class="card shadow-sm border-0 rounded-4 mb-4">
                <div class="card-body p-4 p-md-5">
                    
                    <div class="mb-4">
                        <label class="form-label fw-bold">Gösterim Konumu (Sayfa) <span class="text-danger">*</span></label>
                        <select name="page_type" class="form-select form-select-lg border-2 fw-bold" required>
                            <option value="home" <?= $marquee['page_type'] == 'home' ? 'selected' : '' ?>>🏠 Ana Sayfa</option>
                            <option value="category" <?= $marquee['page_type'] == 'category' ? 'selected' : '' ?>>🏷️ Kategori Sayfaları</option>
                            <option value="product" <?= $marquee['page_type'] == 'product' ? 'selected' : '' ?>>📦 Ürün Detay Sayfaları</option>
                            <option value="header">🔥 Sitenin En Üstü (Top Bar)</option>
                        </select>
                    </div>

                    <div class="mb-4">
                        <label class="form-label fw-bold">Kayan Yazı İçeriği <span class="text-danger">*</span></label>
                        <textarea name="content" class="form-control border-2 font-monospace" rows="5" required><?= htmlspecialchars($marquee['content']) ?></textarea>
                    </div>

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

        <div class="col-lg-4">
            <div class="card shadow-sm border-0 rounded-4 mb-4 h-100">
                <div class="card-header bg-white py-3 border-bottom"><h6 class="mb-0 fw-bold"><i class="bi bi-gear-fill text-secondary me-2"></i>Yayın Ayarları</h6></div>
                <div class="card-body p-4">
                    <div class="form-check form-switch mb-4 fs-5">
                        <input class="form-check-input cursor-pointer" type="checkbox" name="status" id="statusSwitch" <?= $marquee['status'] == 1 ? 'checked' : '' ?>>
                        <label class="form-check-label fs-6 ms-2 fw-bold" for="statusSwitch">Sitede Göster</label>
                    </div>

                    <button type="submit" class="btn btn-success btn-lg w-100 fw-bold shadow-sm rounded-pill">
                        <i class="bi bi-arrow-repeat me-1"></i> Değişiklikleri Uygula
                    </button>
                </div>
            </div>
        </div>
    </div>
</form>

<?php require_once '../footer.php'; ?>