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

include 'conn.php';

// HTML structure template
$baseTemplate = '
<div style="padding: 20px; font-family: Arial, sans-serif; font-size: 16px; background: #fff; border-radius: 10px; box-shadow: 0 0 15px rgba(0,0,0,0.1); max-width: 600px; margin: auto;">
  <h3 style="color: #0d6efd; text-align: center;">📢 {popup_heading}</h3>
  <p style="text-align: center; font-weight: bold;">{announcement}</p>
  <p style="text-align: center;">{body_text}</p>
  <p style="text-align: center; font-weight: bold;">{reward_text}</p>
  <div style="text-align: center; margin-top: 15px;">
    <a href="{vip_link}" style="display: inline-block; padding: 10px 25px; background: #0d6efd; color: white; border-radius: 6px; text-decoration: none;">{button_text}</a>
  </div>
</div>
';

$success = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title         = $_POST['title'];
    $popup_heading = $_POST['popup_heading'];
    $announcement  = $_POST['announcement'];
    $body_text     = $_POST['body_text'];
    $reward_text   = $_POST['reward_text'];
    $vip_link      = $_POST['vip_link'];
    $button_text   = $_POST['button_text'];

    $finalHTML = str_replace(
        ['{popup_heading}', '{announcement}', '{body_text}', '{reward_text}', '{vip_link}', '{button_text}'],
        [$popup_heading, $announcement, $body_text, $reward_text, $vip_link, $button_text],
        $baseTemplate
    );

    $stmt = $conn->prepare("UPDATE site_messages SET title = ?, siteMessage = ? WHERE id = 1");
    $stmt->bind_param("ss", $title, $finalHTML);
    $stmt->execute();
    $success = true;
}

// Load existing
$data = $conn->query("SELECT * FROM site_messages WHERE id = 1")->fetch_assoc();
$siteHTML = $data['siteMessage'];
$title    = $data['title'];

// DOM extractors
function extractDomTagText($html, $tag, $index = 0, $isBoldOnly = false) {
    libxml_use_internal_errors(true);
    $doc = new DOMDocument();
    $doc->loadHTML('<?xml encoding="utf-8" ?>' . $html);
    $tags = $doc->getElementsByTagName($tag);
    if ($tags->length > $index) {
        $node = $tags->item($index);
        if ($isBoldOnly) {
            foreach ($node->childNodes as $child) {
                if ($child->nodeName === 'b') {
                    return $child->textContent;
                }
            }
        }
        return $node->textContent;
    }
    return '';
}

function extractButton($html) {
    if (preg_match('/<a[^>]+href="([^"]+)"[^>]*>(.*?)<\/a>/i', $html, $match)) {
        return [$match[1], $match[2]];
    }
    return ['', ''];
}

// Extract values
$popup_heading = extractDomTagText($siteHTML, 'h3', 0);
$announcement  = extractDomTagText($siteHTML, 'p', 0, true);
$body_text     = extractDomTagText($siteHTML, 'p', 1);
$reward_text   = extractDomTagText($siteHTML, 'p', 2, true);
list($vip_link, $button_text) = extractButton($siteHTML);
?>


  <style>
 .card.custom-editor {
    max-width: 100%;
    width: 100%;
    padding: 20px;
    margin-top: 30px;
    border-radius: 16px;
    box-shadow: 0 0 12px rgba(0, 0, 0, 0.05);
}

.card-header.bg-primary {
    border-top-left-radius: 16px;
    border-top-right-radius: 16px;
}

.card-body input,
.card-body textarea {
    background-color: #f9f9f9;
    border: 1px solid #ddd;
}

    .custom-editor-card {
    max-width: 1500px;
    width: 100%;
    margin: 40px auto;                /* Center horizontally */
    padding: 25px;
    border-radius: 16px;
    background-color: #ffffff;
    box-shadow: 0 0 18px rgba(0, 0, 0, 0.08);
    border: 1px solid #eee;
}

/* Extra wrapper to control spacing from sidebar */
.editor-wrapper {
    padding: 30px 20px; /* top/bottom 30px, left/right 20px */
    background: #f6f8fa;
}

    }
    .form-label {
        font-weight: 600;
    }
    .form-control {
        border-radius: 8px;
    }


	.cool-input {
        border: 2px solid #007bff;
        border-radius: 0.25rem;
        padding: 0.5rem 1rem;
        font-size: 1rem;
        transition: all 0.3s ease;
    }
    .cool-input:focus {
        border-color: #0056b3;
        box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
    }
    .cool-input::placeholder {
        color: #6c757d;
        opacity: 1;
    }
	.cool-button {
        padding: 0.5rem 1rem;
        font-size: 1rem;
        border-radius: 0.25rem;
        transition: all 0.3s ease;
    }
    .cool-button:hover {
        background-color: #0056b3;
        color: #fff;
    }
    .cool-button.btn-secondary:hover {
        background-color: #343a40;
        color: #fff;
    }
	#copied{
		visibility: hidden;
		z-index: 1;
		position: fixed;
		bottom: 50%;
		background-color: #333;
		color: #fff;
		border-radius: 6px;
		padding: 16px;
		max-width: 250px;
		font-size: 17px;
	}	   
	#copied.show {
		visibility: visible;
		-webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
		animation: fadein 0.5s, fadeout 0.5s 2.5s;
	}
	
  </style>



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

      </nav>
<div class="card shadow custom-editor-card">
    <div class="card shadow">
        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
            <h5 class="mb-0">🎯 Popup Message Editor (Structured)</h5>
            <span class="badge bg-light text-dark">Edit Text & Link</span>
        </div>
        <div class="card-body">
            <?php if ($success): ?>
                <div class="alert alert-success">✅ Message saved successfully!</div>
            <?php endif; ?>
            <form method="POST">
                <div class="mb-3">
                    <label class="form-label">Popup Title</label>
                    <input type="text" name="title" class="form-control" required value="<?= htmlspecialchars($title) ?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">Popup Heading</label>
                    <input type="text" name="popup_heading" class="form-control" required value="<?= htmlspecialchars($popup_heading) ?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">Announcement</label>
                    <input type="text" name="announcement" class="form-control" value="<?= htmlspecialchars($announcement) ?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">Body Message</label>
                    <textarea name="body_text" class="form-control" rows="3"><?= htmlspecialchars($body_text) ?></textarea>
                </div>
                <div class="mb-3">
                    <label class="form-label">Reward Text</label>
                    <input type="text" name="reward_text" class="form-control" value="<?= htmlspecialchars($reward_text) ?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">VIP Link (URL/Image Link/Text Link)</label>
                    <input type="text" name="vip_link" class="form-control" value="<?= htmlspecialchars($vip_link) ?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">Button Text</label>
                    <input type="text" name="button_text" class="form-control" value="<?= htmlspecialchars($button_text) ?>">
                </div>
                <button type="submit" class="btn btn-success w-100"> Save Changes</button>
            </form>
        </div>
    </div>
</div>



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