<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include(__DIR__ . '/includes/functions.php');
$static_url = dirname($_SERVER['SCRIPT_NAME']) . '/assets/';
include 'session_check.php';

// Verificar y crear la tabla si no existe
$tableExists = $db->select("sqlite_master", "name", "type = 'table' AND name = 'qr_code'");
if (empty($tableExists)) {
    $db->exec("CREATE TABLE qr_code (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        url TEXT NOT NULL DEFAULT 'https://tudominio.com',
        name TEXT NOT NULL DEFAULT 'https://apps-rgvip.com'
    )");
    
    // Insertar registro inicial
    $db->insert('qr_code', [
        'url' => 'https://tudominio.com',
        'name' => 'https://apps-rgvip.com'
    ]);
}

// Obtener datos del QR
$qr_code = $db->select('qr_code', '*', 'id = :id', '', [':id' => '1']);

if (empty($qr_code)) {
    // Insertar registro si no existe
    $db->insert('qr_code', [
        'id' => 1,
        'url' => 'https://tudominio.com',
        'name' => 'https://apps-rgvip,com'
    ]);
    $qr_code = $db->select('qr_code', '*', 'id = :id', '', [':id' => '1']);
}

$qrCodeURL = $qr_code[0]['url'] ?? 'https://tudominio.com';
$qrCodename = $qr_code[0]['name'] ?? 'https://apps-rgvip.com';

$page_title = "Rgvip Rebrand";
$current_url = $qrCodeURL;
$current_label = $qrCodename;

$success_message = "";
$error_message = "";

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["web_label"])) {
    $name = trim($_POST["web_label"]);
    
    if (!empty($name)) {
        $result = $db->update('qr_code', ['name' => $name], 'id = :id', [':id' => '1']);
        
        if ($result) {
            $success_message = "¡Nombre actualizado correctamente!";
            $current_label = $name;
        } else {
            $error_message = "Error al guardar en la base de datos";
        }
    } else {
        $error_message = "El nombre no puede estar vacío";
    }
}
// Construir contenido de la página
$page_content = '<div class="container mt-5">
    <div class="card mb-5 mb-xl-12">
        <div class="card-body py-12">
            <h2 class="mb-9">Texto o Link sobre la Mac</h2>';

if (!empty($success_message)) {
    $page_content .= '<div class="alert alert-success" role="alert">' . htmlspecialchars($success_message) . '</div>';
}

if (!empty($error_message)) {
    $page_content .= '<div class="alert alert-danger" role="alert">' . htmlspecialchars($error_message) . '</div>';
}

$page_content .= '<div class="row mb-12">
                <div class="col-xl-12 mb-15 mb-xl-0 pe-5">
                    <form method="POST" action="">
                        <div class="form-group mb-3">
                            <label for="web_label" class="form-label">Texto Actual:</label>
                            <input 
                                type="text" 
                                class="form-control" 
                                name="web_label" 
                                id="web_label" 
                                placeholder="Ingresar Texto" 
                                value="' . htmlspecialchars($current_label) . '" 
                                required>
                        </div>
                        <div class="text-center mt-4">
                            <button type="submit" name="submit" class="btn btn-primary btn-block">
                                Actualizar
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>';

include 'includes/layout.php';
?>