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

// ⚠️ এই path তোমার server-এ ঠিক আছে কিনা নিশ্চিত করো
// (Adminxxx78 ফোল্ডার থেকে deepanshu/api/webapi/ পর্যন্ত কত ধাপ উপরে যেতে হয়)
$bannerFilePath = __DIR__ . "/../deepanshu/api/webapi/GetBannerList.php";

$message = '';

// ─────────────────────────────────────────────
// বর্তমান file থেকে JSON পড়া
// ─────────────────────────────────────────────
function loadBanners($path) {
    if (!file_exists($path)) return null;
    $content = file_get_contents($path);
    $data = json_decode($content, true);
    return $data;
}

$bannerData = loadBanners($bannerFilePath);

if ($bannerData === null) {
    $message = "❌ File পড়া যায়নি। Path ঠিক আছে কিনা যাচাই করো: " . htmlspecialchars($bannerFilePath);
    $bannerData = ['data' => [], 'code' => 0, 'msg' => 'Succeed', 'msgCode' => 0, 'serviceNowTime' => date('Y-m-d H:i:s')];
}

// ─────────────────────────────────────────────
// নতুন Banner যোগ করা
// ─────────────────────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add') {
    $newBanner = [
        'url'          => trim($_POST['url'] ?? ''),
        'bannerUrl'    => trim($_POST['bannerUrl'] ?? ''),
        'categoryType' => 0,
        'jumpLinkType' => 0,
        'vendorCode'   => '',
        'gameCode'     => '',
        'gameNameEn'   => '',
    ];
    if (!empty($newBanner['bannerUrl'])) {
        $bannerData['data'][] = $newBanner;
        $bannerData['serviceNowTime'] = date('Y-m-d H:i:s');
        if (file_put_contents($bannerFilePath, json_encode($bannerData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES))) {
            $message = '✅ নতুন Banner যোগ হয়েছে।';
            $bannerData = loadBanners($bannerFilePath);
        } else {
            $message = '❌ File-এ লিখতে পারলাম না — Folder Permission (CHMOD) দেখো।';
        }
    }
}

// ─────────────────────────────────────────────
// Banner Delete করা (index অনুযায়ী)
// ─────────────────────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete') {
    $index = intval($_POST['index']);
    if (isset($bannerData['data'][$index])) {
        array_splice($bannerData['data'], $index, 1);
        $bannerData['serviceNowTime'] = date('Y-m-d H:i:s');
        if (file_put_contents($bannerFilePath, json_encode($bannerData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES))) {
            $message = '✅ Banner মুছে ফেলা হয়েছে।';
            $bannerData = loadBanners($bannerFilePath);
        } else {
            $message = '❌ File-এ লিখতে পারলাম না।';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Manage Banners (Real) - 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-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 (আসল, Live)</h3>
    <p style="color:#8A8270; font-size:12px;">এটা সরাসরি <code>GetBannerList.php</code> ফাইল বদলায় — যেটা তোমার Homepage-এর আসল Banner দেখায়।</p>

    <?php if ($message): ?>
        <div class="alert" style="background:#171512; border:1px solid #D4AF37; color:#F5EFDC;"><?= $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</label>
            <input type="text" name="bannerUrl" placeholder="https://example.com/your-banner.png" required>
            <label style="color:#8A8270; font-size:12px;">ক্লিক করলে কোথায় যাবে (ঐচ্ছিক)</label>
            <input type="text" name="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($bannerData['data'])): ?>
        <p style="color:#8A8270;">কোনো Banner নেই।</p>
    <?php else: ?>
        <?php foreach ($bannerData['data'] as $index => $b): ?>
            <div class="banner-card">
                <img src="<?= htmlspecialchars($b['bannerUrl']) ?>" class="banner-preview" alt="Banner">
                <div style="font-size:11.5px; color:#8A8270; margin-bottom:8px; word-break:break-all;">
                    🔗 <?= htmlspecialchars($b['bannerUrl']) ?><br>
                    ↳ Click: <?= htmlspecialchars($b['url'] ?: '(কিছু নেই)') ?>
                </div>
                <form method="POST" onsubmit="return confirm('এই Banner মুছে ফেলতে চান?');">
                    <input type="hidden" name="action" value="delete">
                    <input type="hidden" name="index" value="<?= $index ?>">
                    <button type="submit" class="btn btn-sm" style="background:#E8544A; color:#fff; border-radius:8px; font-weight:600;">🗑 Delete</button>
                </form>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>

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