<?php
session_start();
if (!isset($_SESSION['unohs'])) {
    header("location:index.php?msg=unauthorized");
    exit;
}
date_default_timezone_set("Asia/Kolkata");

include 'conn.php'; // <-- apna database connection file

// Insert/Update logic
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $bannerId = intval($_POST['bannerId']);
    $title = trim($_POST['title']);
    $img = trim($_POST['img']);
    $coverUrl = trim($_POST['coverUrl']);
    $jumpType = intval($_POST['jumpType']);

    if (isset($_POST['edit_id']) && !empty($_POST['edit_id'])) {
        // Update
        $id = intval($_POST['edit_id']);
        $stmt = $conn->prepare("UPDATE activity_banner_custom_data SET bannerId=?, title=?, img=?, coverUrl=?, jumpType=? WHERE id=?");
        $stmt->bind_param("isssii", $bannerId, $title, $img, $coverUrl, $jumpType, $id);
        $stmt->execute();
    } else {
        // Insert
        $stmt = $conn->prepare("INSERT INTO activity_banner_custom_data (bannerId, title, img, coverUrl, jumpType) VALUES (?, ?, ?, ?, ?)");
        $stmt->bind_param("isssi", $bannerId, $title, $img, $coverUrl, $jumpType);
        $stmt->execute();
    }

    header("Location: manage_custom_activitybanner.php?msg=success");
    exit;
}

// Delete logic
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    $conn->query("DELETE FROM activity_banner_custom_data WHERE id=$id");
    header("Location: manage_custom_activitybanner.php?msg=deleted");
    exit;
}

// Edit fetch
$editData = null;
if (isset($_GET['edit'])) {
    $id = intval($_GET['edit']);
    $get = $conn->query("SELECT * FROM activity_banner_custom_data WHERE id=$id");
    $editData = $get->fetch_assoc();
}
?>



<?php include 'header.php'; ?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Manage Banners</title>
</head>
<body class="bg-light">

<div class="container py-5">
    <h2 class="mb-4 fw-bold text-primary">🎯 Manage Banners</h2>

    <!-- Add/Edit Banner Form -->
    <div class="card p-4 mb-5 shadow-sm">
        <form method="POST">
            <?php if ($editData): ?>
                <input type="hidden" name="edit_id" value="<?= $editData['id'] ?>">
            <?php endif; ?>
            <div class="row g-3">
                <div class="col-md-6">
                    <label class="form-label">Banner ID</label>
                    <input type="number" name="bannerId" class="form-control" value="<?= $editData['bannerId'] ?? '' ?>" required>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Jump Type</label>
                    <select name="jumpType" class="form-select" required>
                        <option value="1" <?= (isset($editData['jumpType']) && $editData['jumpType'] == 1) ? "selected" : "" ?>>Internal</option>
                        <option value="2" <?= (isset($editData['jumpType']) && $editData['jumpType'] == 2) ? "selected" : "" ?>>External</option>
                        <option value="3" <?= (isset($editData['jumpType']) && $editData['jumpType'] == 3) ? "selected" : "" ?>>Image List (JSON)</option>
                    </select>
                </div>
                <div class="col-md-12">
                    <label class="form-label">Title</label>
                    <input type="text" name="title" class="form-control" value="<?= htmlspecialchars($editData['title'] ?? '') ?>" required>
                </div>
                <!-- Banner Form Field -->
<div class="col-md-12">
    <label class="form-label">Image / HTML / JSON</label>
    <textarea name="img" class="form-control" rows="10"><?= htmlspecialchars($editData['img'] ?? '') ?></textarea>
</div>


<script>
    tinymce.init({
        selector: 'textarea[name="img"]',
        height: 300,
        plugins: 'image link lists code',
        toolbar: 'undo redo | bold italic underline | alignleft aligncenter alignright | bullist numlist | link image | code',
        menubar: false,
        branding: false
    });
</script>
                <div class="col-md-12">
                    <label class="form-label">Cover URL</label>
                    <input type="text" name="coverUrl" class="form-control" value="<?= htmlspecialchars($editData['coverUrl'] ?? '') ?>" required>
                </div>
            </div>
            <div class="mt-4">
                <button type="submit" class="btn btn-success"><?= $editData ? '✅ Update' : '➕ Add' ?> Banner</button>
                <?php if ($editData): ?>
                    <a href="manage_custom_activitybanner.php" class="btn btn-secondary ms-2">❌ Cancel</a>
                <?php endif; ?>
            </div>
        </form>
    </div>
    
    
    <div class="alert alert-info shadow-sm mb-5" role="alert">
    <h5 class="fw-bold mb-3">📋 Banner Management Instructions</h5>
    <ul class="mb-0">
        <li>🔹 <strong>Banner ID</strong> must be unique for each banner.</li>
        <li>🔹 <strong>Title</strong> should be short and meaningful.</li>
        <li>🔹 <strong>Jump Type:</strong> 
            <ul>
                <li>1 = Internal Page</li>
                <li>2 = External URL (Website/Link)</li>
                <li>3 = JSON List (Multiple Images)</li>
            </ul>
        </li>
        <li>🔹 <strong>Image/HTML/JSON</strong> field supports:
            <ul>
                <li>Simple HTML content</li>
                <li>Single Image tag (&lt;img&gt;)</li>
                <li>OR JSON List format for multiple images.</li>
            </ul>
        </li>
        <li>🔹 <strong>Cover URL</strong> should be a valid image link.</li>
        <li>🔹 Fill all fields carefully before submitting.</li>
        <li>🔹 After adding, new banners will auto-activate.</li>
    </ul>
</div>


    <!-- Banner List -->
<div class="card p-3 shadow-sm">
    <h5 class="fw-bold mb-3">📋 All Banners</h5>
    <div class="table-responsive">
        <table class="table table-bordered table-hover bg-white text-center align-middle">
            <thead class="table-primary">
                <tr>
                    <th>ID</th>
                    <th>Banner ID</th>
                    <th>Title</th>
                    <th>Jump Type</th>
                    <th>Cover</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $getAll = $conn->query("SELECT * FROM activity_banner_custom_data ORDER BY id DESC");
                while ($row = $getAll->fetch_assoc()):
                ?>
                <tr>
                    <td><?= $row['id'] ?></td>
                    <td><?= $row['bannerId'] ?></td>
                    <td><?= htmlspecialchars($row['title']) ?></td>
                    <td><?= $row['jumpType'] ?></td>
                    <td>
                        <div style="width: 100px; height: 100px; overflow: hidden; border-radius: 8px; margin: auto;">
                            <img src="<?= $row['coverUrl'] ?>" style="width: 100%; height: 100%; object-fit: cover;" onerror="this.src='https://via.placeholder.com/100x100?text=No+Image';">
                        </div>
                    </td>
                    <td>
                        <a href="?edit=<?= $row['id'] ?>" class="btn btn-sm btn-primary">✏️ Edit</a>
                        <a href="?delete=<?= $row['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure to delete this banner?');">🗑️ Delete</a>
                    </td>
                </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>
</div>


</div>



</body>
</html>
