<?php
require_once __DIR__ . '/includes/header.php';

$message = '';
$message_type = '';

// Processar atualização
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $note_title = str_in($_POST['note_title'] ?? '');
    $note_content = str_in($_POST['note_content'] ?? '');
    
    if (empty($note_title) || empty($note_content)) {
        $message = 'Título e conteúdo são obrigatórios.';
        $message_type = 'error';
    } else {
        try {
            // Desativa todas as notas anteriores
            db()->exec("UPDATE app_notes SET is_active = 0");
            
            // Insere nova nota
            $stmt = db()->prepare("INSERT INTO app_notes (note_title, note_content, is_active) VALUES (?, ?, 1)");
            $stmt->execute([$note_title, $note_content]);
            
            $message = 'Aviso atualizado com sucesso!';
            $message_type = 'success';
        } catch (Exception $e) {
            $message = 'Erro ao atualizar aviso: ' . $e->getMessage();
            $message_type = 'error';
        }
    }
}

$notes = get_notes();
$current_title = $notes['note_title'] ?? '';
$current_content = $notes['note_content'] ?? '';
?>

<style>
    .page-header {
        margin-bottom: 30px;
    }
    
    .page-header h1 {
        font-size: 28px;
        margin-bottom: 8px;
        background: var(--primary-bg);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
    }
    
    .page-header p {
        color: #999;
        font-size: 14px;
    }
    
    .alert {
        padding: 15px 20px;
        border-radius: 8px;
        margin-bottom: 20px;
        animation: slideIn 0.3s ease;
    }
    
    .alert-success {
        background: rgba(46, 204, 113, 0.2);
        border: 1px solid #2ecc71;
        color: #2ecc71;
    }
    
    .alert-error {
        background: rgba(231, 76, 60, 0.2);
        border: 1px solid #e74c3c;
        color: #e74c3c;
    }
    
    .form-container {
        background: #1a1a1a;
        padding: 30px;
        border-radius: 12px;
        border: 1px solid rgba(255, 255, 255, 0.1);
        max-width: 900px;
    }
    
    .form-group {
        margin-bottom: 25px;
    }
    
    .form-group label {
        display: block;
        margin-bottom: 8px;
        color: #fff;
        font-weight: 500;
        font-size: 14px;
    }
    
    .form-group input,
    .form-group textarea {
        width: 100%;
        padding: 12px 15px;
        background: #111;
        border: 1px solid rgba(255, 255, 255, 0.2);
        border-radius: 6px;
        color: #fff;
        font-size: 14px;
        font-family: inherit;
        transition: all 0.2s ease;
    }
    
    .form-group textarea {
        min-height: 150px;
        resize: vertical;
    }
    
    .form-group input:focus,
    .form-group textarea:focus {
        outline: none;
        border-color: var(--primary-bg);
        box-shadow: 0 0 0 3px rgba(192, 57, 43, 0.1);
    }
    
    .submit-btn {
        background: var(--primary-bg);
        color: #fff;
        padding: 12px 30px;
        border: none;
        border-radius: 6px;
        font-size: 14px;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.2s ease;
    }
    
    .submit-btn:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    }
    
    .char-counter {
        font-size: 12px;
        color: #999;
        text-align: right;
        margin-top: 5px;
    }
</style>

<div class="page-header">
    <h1>📝 Gerenciar Aviso</h1>
    <p>Configure o aviso que aparecerá no aplicativo</p>
</div>

<?php if ($message): ?>
    <div class="alert alert-<?php echo $message_type; ?>">
        <?php echo htmlspecialchars($message); ?>
    </div>
<?php endif; ?>

<div class="form-container">
    <form method="POST">
        <div class="form-group">
            <label for="note_title">📌 Título do Aviso</label>
            <input type="text" 
                   id="note_title" 
                   name="note_title" 
                   value="<?php echo htmlspecialchars($current_title); ?>"
                   placeholder="Ex: Bem-vindo ao App"
                   maxlength="100"
                   required>
            <div class="char-counter">
                <span id="title-counter">0</span>/100 caracteres
            </div>
        </div>
        
        <div class="form-group">
            <label for="note_content">📄 Conteúdo do Aviso</label>
            <textarea id="note_content" 
                      name="note_content" 
                      placeholder="Digite aqui o conteúdo completo do aviso..."
                      maxlength="500"
                      required><?php echo htmlspecialchars($current_content); ?></textarea>
            <div class="char-counter">
                <span id="content-counter">0</span>/500 caracteres
            </div>
        </div>
        
        <button type="submit" class="submit-btn">💾 Salvar Aviso</button>
    </form>
</div>

<script>
    // Contador de caracteres
    const titleInput = document.getElementById('note_title');
    const contentInput = document.getElementById('note_content');
    const titleCounter = document.getElementById('title-counter');
    const contentCounter = document.getElementById('content-counter');
    
    function updateCounters() {
        titleCounter.textContent = titleInput.value.length;
        contentCounter.textContent = contentInput.value.length;
    }
    
    titleInput.addEventListener('input', updateCounters);
    contentInput.addEventListener('input', updateCounters);
    
    // Inicializa contadores
    updateCounters();
</script>

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