<?php
include('includes/auth.php');

$table_name = 'welcome';
$res = $db->select($table_name, '*', 'id = :id', '', [':id' => 1]);

if (isset($_POST['submit'])) {
    if (!verify_token($_POST['csrf_token'] ?? '')) {
        header("Location: welcome.php?status=4");
        exit;
    }
    $updateData = [
        'message_one' => trim($_POST['message_one'] ?? ''),
        'message_two' => trim($_POST['message_two'] ?? ''),
        'message_three' => trim($_POST['message_three'] ?? ''),
    ];
    $db->update($table_name, $updateData, 'id = :id', [':id' => 1]);
    header("Location: welcome.php?status=1");
    exit;
}

// --- All redirects handled, now output HTML ---
include('includes/header.php');

$csrfToken = generate_token();
$res = $db->select($table_name, '*', 'id = :id', '', [':id' => 1]);
?>

<div class="md-card md-card--elevated" style="max-width: 700px; margin: 0 auto;">
    <div class="md-card__header">
        <?= icon('chat', 'md-card__header-icon') ?>
        <h2 class="md-card__title">Welcome Messages</h2>
    </div>
    <div class="md-card__content">
        <p class="md-helper-text" style="margin-bottom: 1.5rem;">
            These messages are displayed to users when they first open the app (before adding a playlist).
        </p>
        <form method="post">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken) ?>">

            <div class="md-text-field md-text-field--outlined">
                <input type="text" name="message_one" id="message_one" class="md-text-field__input" placeholder=" "
                       value="<?= htmlspecialchars($res[0]['message_one'] ?? '') ?>">
                <label for="message_one" class="md-text-field__label">Message 1 (Title)</label>
                <div class="md-text-field__indicator"></div>
            </div>

            <div class="md-text-field md-text-field--outlined">
                <input type="text" name="message_two" id="message_two" class="md-text-field__input" placeholder=" "
                       value="<?= htmlspecialchars($res[0]['message_two'] ?? '') ?>">
                <label for="message_two" class="md-text-field__label">Message 2 (Body)</label>
                <div class="md-text-field__indicator"></div>
            </div>

            <div class="md-text-field md-text-field--outlined">
                <input type="text" name="message_three" id="message_three" class="md-text-field__input" placeholder=" "
                       value="<?= htmlspecialchars($res[0]['message_three'] ?? '') ?>">
                <label for="message_three" class="md-text-field__label">Message 3 (Footer)</label>
                <div class="md-text-field__indicator"></div>
            </div>

            <div class="md-card__actions">
                <button type="submit" name="submit" class="md-button md-button--filled">
                    <?= icon('check') ?>
                    <span class="md-button__label">Save Messages</span>
                </button>
            </div>
        </form>
    </div>
</div>

<?php include('includes/footer.php'); ?>
