<?php
$admin_page = 'slider';
$admin_title = 'Hero / Slider';
include 'admin_header.php';

$action = $_GET['action'] ?? 'list';
$id = (int)($_GET['id'] ?? 0);
$msg = ''; $msg_type = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title     = trim($_POST['title'] ?? '');
    $subtitle  = trim($_POST['subtitle'] ?? '');
    $label     = trim($_POST['label'] ?? '');
    $btn_text  = trim($_POST['button_text'] ?? '');
    $btn_link  = trim($_POST['button_link'] ?? '');
    $video_url = trim($_POST['video_url'] ?? '');
    $status    = $_POST['status'] ?? 'active';
    $sort_order= (int)($_POST['sort_order'] ?? 0);

    $image = $_POST['existing_image'] ?? '';
    if (!empty($_FILES['image']['name'])) {
        $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
        if (in_array($ext,['jpg','jpeg','png','webp'])) {
            $fname = uniqid('slide_').'.'.$ext;
            if (move_uploaded_file($_FILES['image']['tmp_name'], '../assets/uploads/'.$fname)) $image = $fname;
        }
    }
    try {
        if ($_POST['edit_id'] ?? 0) {
            $eid = (int)$_POST['edit_id'];
            db()->prepare("UPDATE vs_hero_slides SET title=?,subtitle=?,label=?,button_text=?,button_link=?,video_url=?,image=?,status=?,sort_order=? WHERE id=?")
                ->execute([$title,$subtitle,$label,$btn_text,$btn_link,$video_url,$image,$status,$sort_order,$eid]);
            $msg='Güncellendi.'; $msg_type='success';
        } else {
            db()->prepare("INSERT INTO vs_hero_slides (title,subtitle,label,button_text,button_link,video_url,image,status,sort_order) VALUES (?,?,?,?,?,?,?,?,?)")
                ->execute([$title,$subtitle,$label,$btn_text,$btn_link,$video_url,$image,$status,$sort_order]);
            $msg='Eklendi.'; $msg_type='success';
        }
        $action='list';
    } catch(Exception $e) { $msg='Hata: '.$e->getMessage(); $msg_type='error'; }
}

if ($action==='delete' && $id) { db()->prepare("DELETE FROM vs_hero_slides WHERE id=?")->execute([$id]); $action='list'; $msg='Silindi.'; $msg_type='success'; }

$edit = null;
if ($action==='edit' && $id) { $s=db()->prepare("SELECT * FROM vs_hero_slides WHERE id=?"); $s->execute([$id]); $edit=$s->fetch(PDO::FETCH_ASSOC); if(!$edit) $action='list'; }

$slides = [];
if ($action==='list') { try { $slides = db()->query("SELECT * FROM vs_hero_slides ORDER BY sort_order")->fetchAll(PDO::FETCH_ASSOC); } catch(Exception $e) {} }
?>
<?php if($msg): ?><div class="alert alert-<?= $msg_type ?>"><?= clean($msg) ?></div><?php endif; ?>

<?php if ($action==='list'): ?>
<div class="page-header"><h1>Hero / Slider</h1><a href="?action=add" class="btn btn-primary">+ Yeni Slide</a></div>
<div class="admin-card">
    <div style="padding:16px 24px;font-size:12px;color:var(--gray);border-bottom:1px solid rgba(255,255,255,0.06);">
        💡 Video URL alanına MP4 dosyanızın sunucu yolunu girin (örn: /assets/uploads/hero.mp4). Görsel yoksa video arka plan olarak gösterilir.
    </div>
    <table>
        <thead><tr><th>Başlık</th><th>Video</th><th>Durum</th><th>İşlemler</th></tr></thead>
        <tbody>
        <?php foreach($slides as $s): ?>
        <tr>
            <td><strong><?= clean($s['title']) ?></strong></td>
            <td style="color:var(--gray);font-size:12px;"><?= $s['video_url'] ? '✓ '.clean(mb_substr($s['video_url'],0,40)) : '—' ?></td>
            <td><span class="badge <?= $s['status']==='active'?'badge-green':'badge-gray' ?>"><?= $s['status']==='active'?'Aktif':'Taslak' ?></span></td>
            <td><a href="?action=edit&id=<?= $s['id'] ?>" class="btn btn-outline btn-sm">Düzenle</a> <a href="?action=delete&id=<?= $s['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Sil?')">Sil</a></td>
        </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</div>
<?php else: ?>
<div class="page-header"><h1><?= $action==='edit'?'Slide Düzenle':'Yeni Slide' ?></h1><a href="?action=list" style="color:var(--gray);font-size:13px;">← Geri</a></div>
<div class="admin-card" style="padding:28px;max-width:700px;">
    <form method="POST" enctype="multipart/form-data">
        <?php if($action==='edit'): ?><input type="hidden" name="edit_id" value="<?= $edit['id'] ?>"><?php endif; ?>
        <div style="display:flex;flex-direction:column;gap:16px;">
            <div class="admin-form-group"><label>Başlık</label><input type="text" name="title" value="<?= clean($edit['title']??'') ?>"></div>
            <div class="admin-form-group"><label>Alt Başlık</label><textarea name="subtitle"><?= clean($edit['subtitle']??'') ?></textarea></div>
            <div class="form-grid">
                <div class="admin-form-group"><label>Etiket (Badge)</label><input type="text" name="label" value="<?= clean($edit['label']??'') ?>"></div>
                <div class="admin-form-group"><label>Buton Metni</label><input type="text" name="button_text" value="<?= clean($edit['button_text']??'') ?>"></div>
                <div class="admin-form-group"><label>Buton Linki</label><input type="text" name="button_link" value="<?= clean($edit['button_link']??'') ?>"></div>
                <div class="admin-form-group"><label>Durum</label><select name="status"><option value="active" <?= ($edit['status']??'active')==='active'?'selected':'' ?>>Aktif</option><option value="draft" <?= ($edit['status']??'')==='draft'?'selected':'' ?>>Taslak</option></select></div>
            </div>
            <div class="admin-form-group">
                <label>Video URL (MP4) — Sunucu yolu veya harici URL</label>
                <input type="text" name="video_url" value="<?= clean($edit['video_url']??'') ?>" placeholder="/assets/uploads/hero-video.mp4">
            </div>
            <div class="admin-form-group">
                <label>Arkaplan Görseli (Video yoksa gösterilir)</label>
                <?php if(!empty($edit['image'])): ?>
                <img src="/volker/assets/uploads/<?= clean($edit['image']) ?>" style="width:100%;max-height:180px;object-fit:cover;border-radius:4px;margin-bottom:8px;">
                <input type="hidden" name="existing_image" value="<?= clean($edit['image']) ?>">
                <?php endif; ?>
                <input type="file" name="image" accept="image/*">
            </div>
            <div class="admin-form-group"><label>Sıralama</label><input type="number" name="sort_order" value="<?= (int)($edit['sort_order']??0) ?>"></div>
            <button type="submit" class="btn btn-primary">Kaydet</button>
        </div>
    </form>
</div>
<?php endif; ?>
<?php include 'admin_footer.php'; ?>
