<?php
require '../config/db.php';

if (!isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin') {
    header("Location: ../index.php"); exit;
}

$message = "";

// Lógica de Guardado
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
    // 1. Guardar ajustes de texto y colores
    if (isset($_POST['settings'])) {
        foreach ($_POST['settings'] as $key => $value) {
            $stmt = $pdo->prepare("INSERT INTO site_settings (setting_key, setting_value) 
                                   VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
            $stmt->execute([$key, $value, $value]);
        }
    }

    // 2. Lógica para eliminar el logo de imagen (para volver a texto)
    if (isset($_POST['remove_logo'])) {
        $stmt = $pdo->prepare("UPDATE site_settings SET setting_value = '' WHERE setting_key = 'logo_url'");
        $stmt->execute();
        $message = "Imagen removida. Ahora se usará el logo por texto.";
    }

    // 3. Lógica para subir el Logo
    if (!empty($_FILES['logo']['name'])) {
        $target_dir = "../assets/img/";
        if (!is_dir($target_dir)) mkdir($target_dir, 0777, true);
        
        $target_file = $target_dir . "logo_main.png";
        if (move_uploaded_file($_FILES['logo']['tmp_name'], $target_file)) {
            $stmt = $pdo->prepare("INSERT INTO site_settings (setting_key, setting_value) 
                                   VALUES ('logo_url', ?) ON DUPLICATE KEY UPDATE setting_value = ?");
            $stmt->execute(['assets/img/logo_main.png', 'assets/img/logo_main.png']);
            $message = "¡Logo e imagen actualizados!";
        }
    } else if(empty($message)) {
        $message = "Ajustes de diseño actualizados!";
    }
}

// Cargar ajustes actuales
$settings_raw = $pdo->query("SELECT * FROM site_settings")->fetchAll(PDO::FETCH_KEY_PAIR);
include '../includes/header.php';
?>

<div style="max-width: 900px; margin: 50px auto; color: white; padding: 0 20px;">
    <h2 style="color: var(--primary); font-family: 'Syncopate', sans-serif;"><i class="fa-solid fa-palette"></i> Theme Customizer</h2>

    <?php if($message): ?>
        <div style="background: rgba(0,255,136,0.1); border: 1px solid var(--primary); padding: 15px; border-radius: 8px; margin-bottom: 20px; font-size: 14px;">
            <i class="fa-solid fa-check-circle"></i> <?php echo $message; ?>
        </div>
    <?php endif; ?>

    <form method="POST" enctype="multipart/form-data" style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
        
        <div class="v-card" style="background: #161b22; border: 1px solid #30363d; padding: 25px; border-radius: 15px;">
            <h3 style="font-size: 12px; color: var(--primary); text-transform: uppercase; margin-bottom: 20px; letter-spacing: 1px;">General Colors</h3>
            
            <div style="margin-bottom: 15px;">
                <label style="display: block; font-weight: bold; margin-bottom: 8px; font-size: 11px; color: #8b949e;">PRIMARY NEON COLOR</label>
                <input type="color" name="settings[primary_color]" value="<?php echo $settings_raw['primary_color'] ?? '#00ff88'; ?>" style="width: 100%; height: 40px; border: 1px solid #30363d; background: #000; cursor: pointer; border-radius: 5px;">
            </div>

            <div style="margin-bottom: 15px;">
                <label style="display: block; font-weight: bold; margin-bottom: 8px; font-size: 11px; color: #8b949e;">BACKGROUND COLOR</label>
                <input type="color" name="settings[background_color]" value="<?php echo $settings_raw['background_color'] ?? '#0d1117'; ?>" style="width: 100%; height: 40px; border: 1px solid #30363d; background: #000; cursor: pointer; border-radius: 5px;">
            </div>

            <div style="margin-bottom: 15px;">
                <label style="display: block; font-weight: bold; margin-bottom: 8px; font-size: 11px; color: #8b949e;">CARD / NAV BACKGROUND</label>
                <input type="color" name="settings[nav_bg]" value="<?php echo $settings_raw['nav_bg'] ?? '#161b22'; ?>" style="width: 100%; height: 40px; border: 1px solid #30363d; background: #000; cursor: pointer; border-radius: 5px;">
            </div>
        </div>

        <div class="v-card" style="background: #161b22; border: 1px solid #30363d; padding: 25px; border-radius: 15px;">
            <h3 style="font-size: 12px; color: var(--primary); text-transform: uppercase; margin-bottom: 20px; letter-spacing: 1px;">Logo Settings</h3>
            
            <div style="display: flex; gap: 10px; margin-bottom: 15px;">
                <div style="flex: 1;">
                    <label style="display: block; font-weight: bold; margin-bottom: 8px; font-size: 11px; color: #8b949e;">TEXT PART 1</label>
                    <input type="text" name="settings[logo_text_1]" value="<?php echo $settings_raw['logo_text_1'] ?? 'V'; ?>" class="v-input" style="margin:0;">
                </div>
                <div style="flex: 2;">
                    <label style="display: block; font-weight: bold; margin-bottom: 8px; font-size: 11px; color: #8b949e;">TEXT PART 2</label>
                    <input type="text" name="settings[logo_text_2]" value="<?php echo $settings_raw['logo_text_2'] ?? 'VAULT'; ?>" class="v-input" style="margin:0;">
                </div>
            </div>

            <div style="display: flex; gap: 10px; margin-bottom: 20px;">
                <div style="flex: 1;">
                    <label style="display: block; font-weight: bold; margin-bottom: 8px; font-size: 11px; color: #8b949e;">COLOR 1</label>
                    <input type="color" name="settings[logo_color_1]" value="<?php echo $settings_raw['logo_color_1'] ?? '#00ff88'; ?>" style="width: 100%; height: 30px; border: none; background: none; cursor: pointer;">
                </div>
                <div style="flex: 1;">
                    <label style="display: block; font-weight: bold; margin-bottom: 8px; font-size: 11px; color: #8b949e;">COLOR 2</label>
                    <input type="color" name="settings[logo_color_2]" value="<?php echo $settings_raw['logo_color_2'] ?? '#ffffff'; ?>" style="width: 100%; height: 30px; border: none; background: none; cursor: pointer;">
                </div>
            </div>

            <hr style="border: 0; border-top: 1px solid #30363d; margin: 20px 0;">

            <label style="display: block; font-weight: bold; margin-bottom: 10px; font-size: 11px; color: #8b949e;">USE IMAGE LOGO (OVERRIDE TEXT)</label>
            <div style="background: #0d1117; padding: 15px; border-radius: 8px; border: 1px dashed #30363d;">
                <?php if(!empty($settings_raw['logo_url'])): ?>
                    <div style="text-align: center; margin-bottom: 10px;">
                        <img src="../<?php echo $settings_raw['logo_url']; ?>" style="max-height: 40px;">
                        <br>
                        <button type="submit" name="remove_logo" style="background:none; border:none; color:#ff4d4d; font-size:10px; cursor:pointer; margin-top:5px; font-weight:bold;">[ REMOVE IMAGE ]</button>
                    </div>
                <?php endif; ?>
                <input type="file" name="logo" style="font-size: 10px; width: 100%;">
            </div>
        </div>

        <div style="grid-column: span 2;">
            <button type="submit" style="background: var(--primary); color: black; border: none; padding: 18px; width: 100%; border-radius: 10px; font-weight: 900; cursor: pointer; text-transform: uppercase; letter-spacing: 1px; box-shadow: 0 5px 15px rgba(0, 255, 136, 0.2);">
                Update VVault Design
            </button>
        </div>
    </form>
</div>