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

include 'conn.php';

$isEdit = false;
$editData = null;

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

// Handle EDIT request
if (isset($_GET['edit'])) {
    $id = intval($_GET['edit']);
    $get = $conn->query("SELECT * FROM table_activity WHERE id = $id");
    $editData = $get->fetch_assoc();
    $isEdit = true;
}

// Handle INSERT/UPDATE request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = $_POST['bannerTitle'];
    $bannerID = $_POST['bannerID'];
    $url = $_POST['bannerUrl'];
    $jump = $_POST['jumpType'];
    $contents = $_POST['contents'];

    if (isset($_POST['edit_id'])) {
        $editId = $_POST['edit_id'];
        $stmt = $conn->prepare("UPDATE table_activity SET bannerTitle=?, bannerID=?, bannerUrl=?, jumpType=?, contents=? WHERE id=?");
        $stmt->bind_param("sisisi", $title, $bannerID, $url, $jump, $contents, $editId);
        $stmt->execute();
    } else {
        $stmt = $conn->prepare("INSERT INTO table_activity (bannerTitle, bannerID, bannerUrl, jumpType, contents) VALUES (?, ?, ?, ?, ?)");
        $stmt->bind_param("sisis", $title, $bannerID, $url, $jump, $contents);
        $stmt->execute();
    }

    header("Location: update_activitybanner.php?msg=saved");
    exit;
}
?>


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

        
        
        
<div class="container py-5">
    <h2 class="mb-4 text-primary fw-bold"><?= $isEdit ? "✏️ Edit" : "➕ Add New" ?> Banner</h2>

    <form method="POST" class="card p-4 shadow-sm mb-5 bg-white rounded border-primary">
        <?php if ($isEdit): ?>
            <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 Title</label>
                <input type="text" name="bannerTitle" class="form-control" value="<?= $editData['bannerTitle'] ?? '' ?>" required>
            </div>
            <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">Banner Image URL</label>
                <input type="text" name="bannerUrl" class="form-control" value="<?= $editData['bannerUrl'] ?? '' ?>" 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>
                </select>
            </div>
            <div class="col-md-12">
                <label class="form-label">Redirect URL / Contents (Optional)</label>
                <input type="text" name="contents" class="form-control" placeholder="Enter Redirect URL or leave blank" value="<?= $editData['contents'] ?? '' ?>">
            </div>
        </div>
        <div class="mt-4">
            <button type="submit" class="btn btn-success"><?= $isEdit ? "✅ Update" : "➕ Add" ?> Banner</button>
            <?php if ($isEdit): ?>
                <a href="update_activitybanner.php" class="btn btn-secondary ms-2">❌ Cancel</a>
            <?php endif; ?>
        </div>
    </form>



<h4 class="mb-4 fw-bold text-dark">📋 All Banners</h4>
<div class="table-responsive">
    <table class="table table-bordered table-hover align-middle bg-white shadow-sm">
        <thead class="table-primary text-center">
            <tr>
                <th>#</th>
                <th>🖼️ Image</th>
                <th>📛 Title</th>
                <th>🆔 Banner ID</th>
                <th>🔁 Jump Type</th>
                <th>🔗 Contents</th>
                <th>⚙️ Action</th>
            </tr>
        </thead>
        <tbody class="text-center">
            <?php
            $i = 1;
            $result = $conn->query("SELECT * FROM table_activity ORDER BY id DESC");
            while ($row = $result->fetch_assoc()):
            ?>
                <tr>
                    <td><?= $i++ ?></td>
                    <td>
                        <img src="<?= htmlspecialchars($row['bannerUrl']) ?>" 
                             onerror="this.src='https://via.placeholder.com/100x60?text=No+Image'" 
                             style="width: 100px; height: 60px; object-fit: cover; border-radius: 6px; box-shadow: 0 0 5px rgba(0,0,0,0.1);">
                    </td>
                    <td><?= htmlspecialchars($row['bannerTitle']) ?></td>
                    <td><?= htmlspecialchars($row['bannerID']) ?></td>
                    <td>
                        <span class="badge bg-<?= $row['jumpType'] == 1 ? 'info' : 'warning' ?>">
                            <?= $row['jumpType'] == 1 ? 'Internal' : 'External' ?>
                        </span>
                    </td>
                    <td class="text-break"><?= htmlspecialchars($row['contents']) ?></td>
                    <td>
                        <div class="d-flex justify-content-center gap-2">
                            <a href="?edit=<?= $row['id'] ?>" class="btn btn-sm btn-outline-primary">✏️ Edit</a>
                            <a href="?delete=<?= $row['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this banner?');">🗑️ Delete</a>
                        </div>
                    </td>
                </tr>
            <?php endwhile; ?>
        </tbody>
    </table>
</div>

</div>
</div>
    </div>
  
</body>
</html>
