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

requireAdminLogin();

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

$stmt = $pdo->prepare("SELECT * FROM sliders WHERE id = ?");
$stmt->execute([$id]);
$slider = $stmt->fetch();

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

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = trim($_POST['title'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $link = trim($_POST['link'] ?? '');
    $sort_order = intval($_POST['sort_order'] ?? 0);
    $status = $_POST['status'] ?? 'active';
    
    // Resim yükleme
    $image = $slider['image'];
    if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
        $upload = uploadImage($_FILES['image'], 'sliders');
        if ($upload['success']) {
            // Eski resmi sil
            if ($image) {
                $oldPath = str_replace(UPLOAD_URL, UPLOAD_PATH, $image);
                deleteImage($oldPath);
            }
            $image = $upload['url'];
        } else {
            $error = $upload['message'];
        }
    }
    
    if (empty($error)) {
        try {
            $stmt = $pdo->prepare("
                UPDATE sliders 
                SET title = ?, description = ?, image = ?, link = ?, sort_order = ?, status = ?
                WHERE id = ?
            ");
            $stmt->execute([$title, $description, $image, $link, $sort_order, $status, $id]);
            
            setFlashMessage('success', 'Slider başarıyla güncellendi.');
            redirect('index.php');
        } catch (PDOException $e) {
            $error = 'Slider güncellenirken bir hata oluştu.';
        }
    }
}

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

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3">Slider 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" enctype="multipart/form-data">
            <div class="row">
                <div class="col-md-8">
                    <div class="mb-3">
                        <label for="title" class="form-label">Başlık</label>
                        <input type="text" class="form-control" id="title" name="title" value="<?= e($slider['title']) ?>">
                    </div>
                    
                    <div class="mb-3">
                        <label for="description" class="form-label">Açıklama</label>
                        <textarea class="form-control" id="description" name="description" rows="3"><?= e($slider['description']) ?></textarea>
                    </div>
                    
                    <div class="mb-3">
                        <label for="link" class="form-label">Link</label>
                        <input type="url" class="form-control" id="link" name="link" value="<?= e($slider['link']) ?>" placeholder="https://...">
                    </div>
                </div>
                
                <div class="col-md-4">
                    <div class="mb-3">
                        <label for="image" class="form-label">Resim</label>
                        <?php if ($slider['image']): ?>
                            <div class="mb-2">
                                <img src="<?= e(getImageUrl($slider['image'])) ?>" alt="" class="img-thumbnail w-100">
                            </div>
                        <?php endif; ?>
                        <input type="file" class="form-control" id="image" name="image" accept="image/*">
                        <small class="text-muted">Yeni resim yüklemek için seçin</small>
                    </div>
                    
                    <div class="mb-3">
                        <label for="sort_order" class="form-label">Sıra</label>
                        <input type="number" class="form-control" id="sort_order" name="sort_order" value="<?= $slider['sort_order'] ?>" min="0">
                    </div>
                    
                    <div class="mb-3">
                        <label for="status" class="form-label">Durum</label>
                        <select class="form-select" id="status" name="status">
                            <option value="active" <?= $slider['status'] === 'active' ? 'selected' : '' ?>>Aktif</option>
                            <option value="inactive" <?= $slider['status'] === 'inactive' ? 'selected' : '' ?>>Pasif</option>
                        </select>
                    </div>
                </div>
            </div>
            
            <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'; ?>




