<?php
session_start();
if (!isset($_SESSION['unohs'])) {
    header("Location: index.php");
    exit();
}
include "conn.php";

$message = '';

// ─────────────────────────────────────────────
// নতুন Banner যোগ করা
// ─────────────────────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add') {
    $bannerUrl = trim($_POST['banner_url']);
    $clickUrl  = trim($_POST['click_url']) ?: '/#/';

    if (!empty($bannerUrl)) {
        $stmt = $conn->prepare("INSERT INTO banners (url, banner_url, is_active) VALUES (?, ?, 1)");
        $stmt->bind_param("ss", $clickUrl, $bannerUrl);
        if ($stmt->execute()) {
            $message = '✅ নতুন Banner যোগ করা হয়েছে।';
        } else {
            $message = '❌ Banner যোগ করা যায়নি।';
        }
        $stmt->close();
    }
}

// ─────────────────────────────────────────────
// Banner Delete করা
// ─────────────────────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete') {
    $id = intval($_POST['banner_id']);
    $stmt = $conn->prepare("DELETE FROM banners WHERE id = ?");
    $stmt->bind_param("i", $id);
    if ($stmt->execute()) {
        $message = '✅ Banner মুছে ফেলা হয়েছে।';
    }
    $stmt->close();
}

// ─────────────────────────────────────────────
// Banner Enable/Disable Toggle করা
// ─────────────────────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'toggle') {
    $id = intval($_POST['banner_id']);
    $newStatus = intval($_POST['new_status']);
    $stmt = $conn->prepare("UPDATE banners SET is_active = ? WHERE id = ?");
    $stmt->bind_param("ii", $newStatus, $id);
    if ($stmt->execute()) {
        $message = $newStatus === 1 ? '✅ Banner Enable করা হয়েছে।' : '✅ Banner Disable করা হয়েছে।';
    }
    $stmt->close();
}

// ─────────────────────────────────────────────
// সব Banner-এর তালিকা আনা
// ─────────────────────────────────────────────
$banners = [];
$result = $conn->query("SELECT id, url, banner_url, is_active, created_at FROM banners ORDER BY id DESC");
if ($result) {
    while ($row = $result->fetch_assoc()) { $banners[] = $row; }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Manage Banners - RAJ365 Admin</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="admin-gold-theme.css">
    <style>
        body { background: #0A0A0A; padding: 20px; }
        .banner-card {
            background: #171512; border: 1px solid rgba(212,175,55,.25);
            border-radius: 14px; padding: 14px; margin-bottom: 14px;
        }
        .banner-card.disabled { opacity: .5; }
        .banner-preview {
            width: 100%; max-height: 140px; object-fit: cover;
            border-radius: 10px; margin-bottom: 10px; background: #000;
        }
        .form-box { background: #171512; border: 1px solid rgba(212,175,55,.3); border-radius: 14px; padding: 18px; margin-bottom: 24px; }
        .form-box input { background: #0A0A0A; border: 1px solid #3A3226; color: #F5EFDC; border-radius: 8px; padding: 8px 12px; width: 100%; margin-bottom: 10px; }
    </style>
</head>
<body>
<div class="container" style="max-width:720px;">
    <h3 style="color:#D4AF37;">🖼️ Manage Banners</h3>

    <?php if ($message): ?>
        <div class="alert" style="background:#171512; border:1px solid #D4AF37; color:#F5EFDC;"><?= htmlspecialchars($message) ?></div>
    <?php endif; ?>

    <div class="form-box">
        <div style="color:#D4AF37; font-weight:700; margin-bottom:10px;">➕ নতুন Banner যোগ করো</div>
        <form method="POST">
            <input type="hidden" name="action" value="add">
            <label style="color:#8A8270; font-size:12px;">Banner Image-এর Link (আগে থেকে কোথাও upload করা ছবির URL)</label>
            <input type="text" name="banner_url" placeholder="https://example.com/your-banner.jpg" required>
            <label style="color:#8A8270; font-size:12px;">ক্লিক করলে কোথায় যাবে (না দিলে Homepage-এ যাবে)</label>
            <input type="text" name="click_url" placeholder="/#/activity/Turntable">
            <button type="submit" class="btn" style="background:#D4AF37; color:#1A1400; font-weight:700; border-radius:10px;">Banner যোগ করো</button>
        </form>
    </div>

    <?php if (empty($banners)): ?>
        <p style="color:#8A8270;">কোনো Banner নেই।</p>
    <?php else: ?>
        <?php foreach ($banners as $b): ?>
            <?php $isActive = intval($b['is_active']) === 1; ?>
            <div class="banner-card <?= $isActive ? '' : 'disabled' ?>">
                <img src="<?= htmlspecialchars($b['banner_url']) ?>" class="banner-preview" alt="Banner Preview">
                <div style="font-size:11.5px; color:#8A8270; margin-bottom:8px; word-break:break-all;">
                    🔗 <?= htmlspecialchars($b['banner_url']) ?><br>
                    ↳ Click যাবে: <?= htmlspecialchars($b['url']) ?>
                </div>
                <div style="display:flex; gap:8px;">
                    <form method="POST" style="display:inline;">
                        <input type="hidden" name="action" value="toggle">
                        <input type="hidden" name="banner_id" value="<?= $b['id'] ?>">
                        <input type="hidden" name="new_status" value="<?= $isActive ? 0 : 1 ?>">
                        <button type="submit" class="btn btn-sm" style="background:<?= $isActive ? '#E8544A' : '#1FB979' ?>; color:#fff; border-radius:8px; font-weight:600;">
                            <?= $isActive ? 'Disable' : 'Enable' ?>
                        </button>
                    </form>
                    <form method="POST" style="display:inline;" onsubmit="return confirm('এই Banner সম্পূর্ণ মুছে ফেলতে চান?');">
                        <input type="hidden" name="action" value="delete">
                        <input type="hidden" name="banner_id" value="<?= $b['id'] ?>">
                        <button type="submit" class="btn btn-sm" style="background:#3A3226; color:#fff; border-radius:8px; font-weight:600;">🗑 Delete</button>
                    </form>
                </div>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>

    <a href="dashboard.php" style="color:#D4AF37;">← Dashboard-এ ফিরে যান</a>
</div>
</body>
</html>
