<?php
$db = getDB();
$message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = $_POST['title'] ?? '';
    $subtitle = $_POST['subtitle'] ?? '';
    $image_url = $_POST['image_url'] ?? '';
    $sort_order = (int)($_POST['sort_order'] ?? 0);
    $id = $_POST['id'] ?? '';

    if (!verifyCsrfToken($_POST['csrf_token'] ?? '')) {
        $message = 'Invalid security token.';
    } else {
        try {
            if ($id) {
                $stmt = $db->prepare("UPDATE sliders SET title=?, subtitle=?, image_url=?, sort_order=? WHERE id=?");
                $stmt->execute([$title, $subtitle, $image_url, $sort_order, $id]);
                $message = 'Slider updated successfully.';
            } else {
                $stmt = $db->prepare("INSERT INTO sliders (title, subtitle, image_url, sort_order) VALUES (?, ?, ?, ?)");
                $stmt->execute([$title, $subtitle, $image_url, $sort_order]);
                $message = 'Slider added successfully.';
            }
        } catch (PDOException $e) {
            $message = 'Database error: ' . htmlspecialchars($e->getMessage());
        }
    }
}

$sliders = $db->query("SELECT * FROM sliders ORDER BY sort_order ASC")->fetchAll();
$editSlider = null;
if (isset($_GET['edit'])) {
    $stmt = $db->prepare("SELECT * FROM sliders WHERE id = ?");
    $stmt->execute([$_GET['edit']]);
    $editSlider = $stmt->fetch();
}

include __DIR__ . '/admin_header.php';
?>

<div class="admin-topbar">
    <h2>Manage Sliders</h2>
</div>

<?php if ($message): ?>
<div class="alert alert-success"><?= htmlspecialchars($message) ?></div>
<?php endif; ?>

<div class="admin-card">
    <h3><?= $editSlider ? 'Edit Slider' : 'Add New Slider' ?></h3>
    <form method="post">
        <?= csrfField() ?>
        <?php if ($editSlider): ?>
        <input type="hidden" name="id" value="<?= $editSlider['id'] ?>">
        <?php endif; ?>
        <div class="form-group">
            <label>Title</label>
            <input type="text" name="title" value="<?= htmlspecialchars($editSlider['title'] ?? '') ?>" required>
        </div>
        <div class="form-group">
            <label>Subtitle</label>
            <input type="text" name="subtitle" value="<?= htmlspecialchars($editSlider['subtitle'] ?? '') ?>">
        </div>
        <div class="form-group">
            <label>Image URL</label>
            <input type="url" name="image_url" value="<?= htmlspecialchars($editSlider['image_url'] ?? '') ?>" required>
            <div style="margin-top:8px; display:flex; gap:8px; align-items:center;">
                <input type="file" id="file_upload_slider_image" accept="image/*,.pdf" style="font-size:13px;">
                <button type="button" onclick="uploadFile('file_upload_slider_image', 'image_url')" class="btn-sm btn-sm-success"><i class="fas fa-upload"></i> Upload</button>
                <span id="upload_status_file_upload_slider_image" style="font-size:12px;color:#666;"></span>
            </div>
        </div>
        <div class="form-group">
            <label>Sort Order</label>
            <input type="number" name="sort_order" value="<?= htmlspecialchars($editSlider['sort_order'] ?? '0') ?>">
        </div>
        <button type="submit" class="btn-sm btn-sm-primary"><?= $editSlider ? 'Update' : 'Add' ?> Slider</button>
        <?php if ($editSlider): ?>
        <a href="/admin/sliders" class="btn-sm btn-sm-success">Cancel</a>
        <?php endif; ?>
    </form>
</div>

<div class="admin-card">
    <h3>All Sliders</h3>
    <table>
        <thead>
            <tr><th>Image</th><th>Title</th><th>Subtitle</th><th>Order</th><th>Actions</th></tr>
        </thead>
        <tbody>
        <?php foreach ($sliders as $s): ?>
            <tr>
                <td><img src="<?= htmlspecialchars($s['image_url']) ?>" class="admin-img-preview"></td>
                <td><?= htmlspecialchars($s['title']) ?></td>
                <td><?= htmlspecialchars($s['subtitle']) ?></td>
                <td><?= $s['sort_order'] ?></td>
                <td class="actions">
                    <a href="/admin/sliders?edit=<?= $s['id'] ?>" class="btn-sm btn-sm-primary">Edit</a>
                    <form method="post" action="/admin/delete" style="display:inline" onsubmit="return confirm('Are you sure?')"><input type="hidden" name="type" value="sliders"><input type="hidden" name="delete_id" value="<?= $s['id'] ?>"><?= csrfField() ?><button type="submit" class="btn-sm btn-sm-danger">Delete</button></form>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</div>

<?php include __DIR__ . '/admin_footer.php'; ?>
