<?php
session_start();
$uploadDir = 'uploads/';

// Crear directorio si no existe
if (!is_dir($uploadDir)) {
    mkdir($uploadDir, 0777, true);
}

$message = '';
$messageType = '';
$watermarkImageDir = 'watermark_logos/';

// Crear directorio para logos si no existe
if (!is_dir($watermarkImageDir)) {
    mkdir($watermarkImageDir, 0777, true);
}

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['images'])) {
    $files = $_FILES['images'];
    $watermarkType = isset($_POST['watermark_type']) ? $_POST['watermark_type'] : 'text';
    $watermarkText = isset($_POST['watermark_text']) ? $_POST['watermark_text'] : 'Descubre San Martín';
    $watermarkPosition = isset($_POST['watermark_position']) ? $_POST['watermark_position'] : 'bottom-right';
    $watermarkOpacity = isset($_POST['watermark_opacity']) ? intval($_POST['watermark_opacity']) : 50;
    $watermarkSize = isset($_POST['watermark_size']) ? intval($_POST['watermark_size']) : 24;
    
    $uploadedCount = 0;
    $totalFiles = count($files['name']);
    
    for ($i = 0; $i < $totalFiles; $i++) {
        if ($files['error'][$i] == 0) {
            $fileName = basename($files['name'][$i]);
            $targetFile = $uploadDir . time() . '_' . $fileName;
            $imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
            
            // Verificar si es una imagen válida
            $check = getimagesize($files['tmp_name'][$i]);
            if ($check !== false) {
                // Cargar la imagen según su tipo
                switch ($imageFileType) {
                    case 'jpg':
                    case 'jpeg':
                        $image = imagecreatefromjpeg($files['tmp_name'][$i]);
                        break;
                    case 'png':
                        $image = imagecreatefrompng($files['tmp_name'][$i]);
                        break;
                    case 'gif':
                        $image = imagecreatefromgif($files['tmp_name'][$i]);
                        break;
                    case 'webp':
                        $image = imagecreatefromwebp($files['tmp_name'][$i]);
                        break;
                    default:
                        continue 2;
                }
                
                // Obtener dimensiones de la imagen
                $width = imagesx($image);
                $height = imagesy($image);
                
                if ($watermarkType === 'text') {
                    // Marca de agua de TEXTO
                    $opacity = 127 - round($watermarkOpacity * 1.27);
                    $white = imagecolorallocatealpha($image, 255, 255, 255, $opacity);
                    
                    $fontSize = $watermarkSize;
                    $font = 5;
                    
                    $textWidth = imagefontwidth($font) * strlen($watermarkText);
                    $textHeight = imagefontheight($font);
                    $padding = 20;
                    
                    switch ($watermarkPosition) {
                        case 'top-left':
                            $x = $padding;
                            $y = $padding;
                            break;
                        case 'top-center':
                            $x = ($width - $textWidth) / 2;
                            $y = $padding;
                            break;
                        case 'top-right':
                            $x = $width - $textWidth - $padding;
                            $y = $padding;
                            break;
                        case 'center-left':
                            $x = $padding;
                            $y = ($height - $textHeight) / 2;
                            break;
                        case 'center':
                            $x = ($width - $textWidth) / 2;
                            $y = ($height - $textHeight) / 2;
                            break;
                        case 'center-right':
                            $x = $width - $textWidth - $padding;
                            $y = ($height - $textHeight) / 2;
                            break;
                        case 'bottom-left':
                            $x = $padding;
                            $y = $height - $textHeight - $padding;
                            break;
                        case 'bottom-center':
                            $x = ($width - $textWidth) / 2;
                            $y = $height - $textHeight - $padding;
                            break;
                        case 'bottom-right':
                        default:
                            $x = $width - $textWidth - $padding;
                            $y = $height - $textHeight - $padding;
                            break;
                    }
                    
                    imagestring($image, $font, $x, $y, $watermarkText, $white);
                    
                } else {
                    // Marca de agua de IMAGEN/LOGO
                    $watermarkImagePath = $watermarkImageDir . 'logo.png';
                    
                    if (file_exists($watermarkImagePath)) {
                        $watermarkImg = imagecreatefrompng($watermarkImagePath);
                        
                        // Preservar transparencia del logo
                        imagealphablending($watermarkImg, true);
                        imagesavealpha($watermarkImg, true);
                        
                        $wmWidth = imagesx($watermarkImg);
                        $wmHeight = imagesy($watermarkImg);
                        
                        // Redimensionar logo según el tamaño configurado
                        $scaleFactor = $watermarkSize / 100;
                        $newWmWidth = round($width * $scaleFactor);
                        $newWmHeight = round(($wmHeight / $wmWidth) * $newWmWidth);
                        
                        // Crear imagen redimensionada con transparencia
                        $resizedWatermark = imagecreatetruecolor($newWmWidth, $newWmHeight);
                        imagealphablending($resizedWatermark, false);
                        imagesavealpha($resizedWatermark, true);
                        
                        // Llenar con color transparente
                        $transparent = imagecolorallocatealpha($resizedWatermark, 0, 0, 0, 127);
                        imagefill($resizedWatermark, 0, 0, $transparent);
                        
                        // Redimensionar manteniendo transparencia
                        imagecopyresampled($resizedWatermark, $watermarkImg, 0, 0, 0, 0, 
                                         $newWmWidth, $newWmHeight, $wmWidth, $wmHeight);
                        
                        $padding = 20;
                        
                        // Calcular posición
                        switch ($watermarkPosition) {
                            case 'top-left':
                                $destX = $padding;
                                $destY = $padding;
                                break;
                            case 'top-center':
                                $destX = ($width - $newWmWidth) / 2;
                                $destY = $padding;
                                break;
                            case 'top-right':
                                $destX = $width - $newWmWidth - $padding;
                                $destY = $padding;
                                break;
                            case 'center-left':
                                $destX = $padding;
                                $destY = ($height - $newWmHeight) / 2;
                                break;
                            case 'center':
                                $destX = ($width - $newWmWidth) / 2;
                                $destY = ($height - $newWmHeight) / 2;
                                break;
                            case 'center-right':
                                $destX = $width - $newWmWidth - $padding;
                                $destY = ($height - $newWmHeight) / 2;
                                break;
                            case 'bottom-left':
                                $destX = $padding;
                                $destY = $height - $newWmHeight - $padding;
                                break;
                            case 'bottom-center':
                                $destX = ($width - $newWmWidth) / 2;
                                $destY = $height - $newWmHeight - $padding;
                                break;
                            case 'bottom-right':
                            default:
                                $destX = $width - $newWmWidth - $padding;
                                $destY = $height - $newWmHeight - $padding;
                                break;
                        }
                        
                        // Habilitar alpha blending en la imagen principal
                        imagealphablending($image, true);
                        
                        // Si la opacidad es 100%, copiar directamente
                        if ($watermarkOpacity >= 100) {
                            imagecopy($image, $resizedWatermark, $destX, $destY, 0, 0, 
                                    $newWmWidth, $newWmHeight);
                        } else {
                            // Aplicar opacidad pixel por pixel
                            for ($y = 0; $y < $newWmHeight; $y++) {
                                for ($x = 0; $x < $newWmWidth; $x++) {
                                    $color = imagecolorat($resizedWatermark, $x, $y);
                                    $alpha = ($color >> 24) & 0xFF;
                                    
                                    // Ajustar alpha según opacidad configurada
                                    $newAlpha = $alpha + (127 - $alpha) * (1 - $watermarkOpacity / 100);
                                    $newAlpha = min(127, max(0, $newAlpha));
                                    
                                    $r = ($color >> 16) & 0xFF;
                                    $g = ($color >> 8) & 0xFF;
                                    $b = $color & 0xFF;
                                    
                                    $newColor = imagecolorallocatealpha($image, $r, $g, $b, $newAlpha);
                                    
                                    if ($newAlpha < 127) { // Solo dibujar si no es completamente transparente
                                        imagesetpixel($image, $destX + $x, $destY + $y, $newColor);
                                    }
                                }
                            }
                        }
                        
                        imagedestroy($watermarkImg);
                        imagedestroy($resizedWatermark);
                    }
                }
                
                // Guardar la imagen con marca de agua
                switch ($imageFileType) {
                    case 'jpg':
                    case 'jpeg':
                        imagejpeg($image, $targetFile, 90);
                        break;
                    case 'png':
                        imagepng($image, $targetFile, 9);
                        break;
                    case 'gif':
                        imagegif($image, $targetFile);
                        break;
                    case 'webp':
                        imagewebp($image, $targetFile, 90);
                        break;
                }
                
                imagedestroy($image);
                $uploadedCount++;
            }
        }
    }
    
    if ($uploadedCount > 0) {
        $message = "¡$uploadedCount imagen(es) subida(s) exitosamente con marca de agua!";
        $messageType = 'success';
    } else {
        $message = "Error al subir las imágenes.";
        $messageType = 'error';
    }
}
?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Subir Imágenes con Marca de Agua</title>
    <style>
 * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    padding: 20px;
}

.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 15px 30px;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    margin-bottom: 30px;
}

.header img {
    height: 50px;
}

.header nav a {
    margin-left: 25px;
    text-decoration: none;
    color: #333;
    font-weight: 600;
    transition: color 0.3s;
}

.header nav a:hover {
    color: #667eea;
}

.container {
    max-width: 900px;
    margin: 0 auto;
    background-color: #fff;
    padding: 40px;
    border-radius: 15px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}

h1 {
    color: #333;
    text-align: center;
    margin-bottom: 30px;
    font-size: 2em;
}

.message {
    margin-bottom: 20px;
    padding: 15px 20px;
    border-radius: 8px;
    text-align: center;
    font-weight: 500;
}

.success {
    background-color: #d4edda;
    color: #155724;
    border: 1px solid #c3e6cb;
}

.error {
    background-color: #f8d7da;
    color: #721c24;
    border: 1px solid #f5c6cb;
}

.upload-section {
    margin-bottom: 30px;
}

.file-input-wrapper {
    position: relative;
    overflow: hidden;
    display: inline-block;
    width: 100%;
}

.file-input-wrapper input[type=file] {
    position: absolute;
    left: -9999px;
}

.file-input-label {
    display: block;
    padding: 40px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    text-align: center;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.3s;
    font-size: 1.1em;
    font-weight: 600;
}

.file-input-label:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}

.watermark-settings {
    background-color: #f8f9fa;
    padding: 25px;
    border-radius: 10px;
    margin-bottom: 25px;
}

.watermark-settings h3 {
    color: #333;
    margin-bottom: 20px;
    font-size: 1.2em;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    color: #555;
    font-weight: 600;
}

.form-group input[type="text"],
.form-group input[type="number"] {
    width: 100%;
    padding: 12px;
    border: 2px solid #ddd;
    border-radius: 8px;
    font-size: 15px;
    transition: border-color 0.3s;
}

.form-group input:focus {
    outline: none;
    border-color: #667eea;
}

.position-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    margin-top: 10px;
}

.position-option {
    position: relative;
}

.position-option input[type="radio"] {
    position: absolute;
    opacity: 0;
}

.position-option label {
    display: block;
    padding: 15px;
    text-align: center;
    background-color: #fff;
    border: 2px solid #ddd;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s;
    font-weight: 500;
    font-size: 0.9em;
}

.position-option input[type="radio"]:checked + label {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-color: #667eea;
}

.position-option label:hover {
    border-color: #667eea;
}

.type-toggle {
    display: flex;
    gap: 10px;
    margin-top: 10px;
}

.type-option {
    flex: 1;
    position: relative;
}

.type-option input[type="radio"] {
    position: absolute;
    opacity: 0;
}

.type-option label {
    display: block;
    padding: 15px;
    text-align: center;
    background-color: #fff;
    border: 2px solid #ddd;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s;
    font-weight: 600;
}

.type-option input[type="radio"]:checked + label {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-color: #667eea;
}

.type-option label:hover {
    border-color: #667eea;
}

.file-input-wrapper-small {
    position: relative;
    overflow: hidden;
    display: inline-block;
    width: 100%;
}

.file-input-wrapper-small input[type=file] {
    position: absolute;
    left: -9999px;
}

.file-input-label-small {
    display: block;
    padding: 12px 20px;
    background-color: #f0f0f0;
    border: 2px solid #ddd;
    color: #333;
    text-align: center;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s;
    font-weight: 600;
}

.file-input-label-small:hover {
    background-color: #e0e0e0;
    border-color: #667eea;
}

.slider-container {
    margin-top: 10px;
}

.slider {
    width: 100%;
    height: 8px;
    border-radius: 5px;
    background: #ddd;
    outline: none;
    -webkit-appearance: none;
}

.slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #667eea;
    cursor: pointer;
}

.slider::-moz-range-thumb {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #667eea;
    cursor: pointer;
    border: none;
}

.slider-value {
    display: inline-block;
    margin-left: 10px;
    color: #667eea;
    font-weight: 600;
}

.preview-section {
    background-color: #f8f9fa;
    padding: 25px;
    border-radius: 10px;
    margin-bottom: 25px;
    text-align: center;
}

.preview-section h3 {
    color: #333;
    margin-bottom: 15px;
}

.preview-box {
    position: relative;
    width: 100%;
    max-width: 400px;
    height: 300px;
    margin: 0 auto;
    background: linear-gradient(45deg, #f0f0f0 25%, transparent 25%, transparent 75%, #f0f0f0 75%, #f0f0f0),
                linear-gradient(45deg, #f0f0f0 25%, transparent 25%, transparent 75%, #f0f0f0 75%, #f0f0f0);
    background-size: 20px 20px;
    background-position: 0 0, 10px 10px;
    border-radius: 10px;
    overflow: hidden;
    border: 2px solid #ddd;
}

.preview-watermark {
    position: absolute;
    color: white;
    font-weight: bold;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
    pointer-events: none;
    transition: all 0.3s;
}

.preview-watermark-img {
    position: absolute;
    max-width: 30%;
    height: auto;
    pointer-events: none;
    transition: all 0.3s;
}

.button {
    width: 100%;
    padding: 15px;
    font-size: 16px;
    font-weight: 600;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    background: linear-gradient(135deg, #00b09b 0%, #96c93d 100%);
    color: white;
    transition: all 0.3s;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.button:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}

.button:disabled {
    opacity: 0.7;
    cursor: not-allowed;
}

.link-button {
    display: inline-block;
    margin-top: 20px;
    padding: 12px 24px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    text-decoration: none;
    border-radius: 8px;
    font-weight: 600;
    transition: all 0.3s;
}

.link-button:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}

/* ========================================
   ESTILOS PARA LA BARRA DE PROGRESO
   ======================================== */

.progress-container {
    display: none;
    background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
    padding: 30px;
    border-radius: 15px;
    margin: 25px 0;
    border: 2px solid #dee2e6;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.progress-container.active {
    display: block;
    animation: fadeIn 0.4s ease;
}

.progress-bar-wrapper {
    width: 100%;
    height: 35px;
    background-color: #e9ecef;
    border-radius: 20px;
    overflow: hidden;
    position: relative;
    margin-bottom: 20px;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
}

.progress-bar {
    height: 100%;
    background: linear-gradient(90deg, #667eea 0%, #764ba2 50%, #667eea 100%);
    background-size: 200% 100%;
    width: 0%;
    transition: width 0.4s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-weight: 700;
    font-size: 15px;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
    animation: gradientShift 2s linear infinite;
    border-radius: 20px;
}

@keyframes gradientShift {
    0% {
        background-position: 0% 50%;
    }
    100% {
        background-position: 100% 50%;
    }
}

.progress-text {
    text-align: center;
    color: #333;
    font-weight: 700;
    font-size: 18px;
    margin-bottom: 15px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
}

.progress-details {
    text-align: center;
    color: #666;
    font-size: 15px;
    font-weight: 600;
    margin-top: 10px;
    padding: 10px;
    background-color: rgba(255, 255, 255, 0.7);
    border-radius: 8px;
}

.upload-complete {
    display: none;
    background: linear-gradient(135deg, #00b09b 0%, #96c93d 100%);
    color: white;
    padding: 25px 30px;
    border-radius: 15px;
    text-align: center;
    margin: 25px 0;
    font-weight: 700;
    font-size: 20px;
    box-shadow: 0 6px 20px rgba(0, 176, 155, 0.3);
    border: 3px solid rgba(255, 255, 255, 0.3);
}

.upload-complete.show {
    display: block;
    animation: slideInBounce 0.6s ease;
}

@keyframes slideInBounce {
    0% {
        opacity: 0;
        transform: translateY(-30px) scale(0.9);
    }
    60% {
        opacity: 1;
        transform: translateY(5px) scale(1.02);
    }
    100% {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.spinner {
    display: inline-block;
    width: 18px;
    height: 18px;
    border: 3px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    border-top-color: white;
    animation: spin 0.8s linear infinite;
    vertical-align: middle;
}

@keyframes spin {
    to { 
        transform: rotate(360deg); 
    }
}

/* Efecto de pulso para el texto de progreso */
.progress-text .spinner {
    animation: spin 0.8s linear infinite, pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.6;
    }
}

/* ========================================
   RESPONSIVE
   ======================================== */

@media (max-width: 768px) {
    .container {
        padding: 20px;
    }
    
    .position-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 5px;
    }
    
    .position-option label {
        padding: 10px;
        font-size: 0.8em;
    }
    
    .header {
        flex-direction: column;
        gap: 15px;
    }
    
    .header nav {
        display: flex;
        gap: 15px;
    }
    
    .header nav a {
        margin-left: 0;
    }
    
    h1 {
        font-size: 1.5em;
    }
    
    .progress-bar {
        font-size: 13px;
    }
    
    .progress-text {
        font-size: 16px;
    }
    
    .upload-complete {
        font-size: 18px;
        padding: 20px;
    }
}

@media (max-width: 480px) {
    .type-toggle {
        flex-direction: column;
    }
    
    .position-grid {
        gap: 8px;
    }
    
    .position-option label {
        padding: 8px;
        font-size: 0.75em;
    }
    
    .preview-box {
        height: 200px;
    }
}
    </style>
</head>
<body>
    <div class="header">
        <img src="http://descubresanmartin.com/images/Desc.SanMartin2.png" alt="Logo">
        <nav>
            <a href="ver.php">🖼️ Galería</a>
            <a href="about.php">ℹ️ Acerca de</a>
            <a href="contact.php">📧 Contacto</a>
        </nav>
    </div>

    <div class="container">
        <h1>📤 Subir Imágenes con Marca de Agua</h1>
        
        <?php if ($message): ?>
            <div class="message <?php echo $messageType; ?>">
                <?php echo $message; ?>
            </div>
        <?php endif; ?>
        
        <form method="post" enctype="multipart/form-data">
            <div class="watermark-settings">
                <h3>⚙️ Configuración de Marca de Agua</h3>
                
                <div class="form-group">
                    <label>Tipo de marca de agua:</label>
                    <div class="type-toggle">
                        <div class="type-option">
                            <input type="radio" id="type-text" name="watermark_type" value="text" checked>
                            <label for="type-text">📝 Texto</label>
                        </div>
                        <div class="type-option">
                            <input type="radio" id="type-image" name="watermark_type" value="image">
                            <label for="type-image">🖼️ Logo/Imagen</label>
                        </div>
                    </div>
                </div>
                
                <div id="text-options">
                    <div class="form-group">
                        <label for="watermark_text">Texto de la marca de agua:</label>
                        <input type="text" id="watermark_text" name="watermark_text" value="Descubre San Martín">
                    </div>
                </div>
                
                <div id="image-options" style="display: none;">
                    <div class="form-group">
                        <label for="watermark_logo">Subir logo (PNG con transparencia recomendado):</label>
                        <div class="file-input-wrapper-small">
                            <input type="file" id="watermark_logo" accept="image/png,image/jpeg,image/jpg">
                            <label for="watermark_logo" class="file-input-label-small">
                                📁 Seleccionar Logo
                            </label>
                        </div>
                        <small style="color: #666; display: block; margin-top: 5px;">
                            El logo se guardará como "logo.png" en la carpeta watermark_logos/
                        </small>
                    </div>
                </div>
                
                <div class="form-group">
                    <label>Posición de la marca de agua:</label>
                    <div class="position-grid">
                        <div class="position-option">
                            <input type="radio" id="pos-tl" name="watermark_position" value="top-left">
                            <label for="pos-tl">↖️ Arriba Izq.</label>
                        </div>
                        <div class="position-option">
                            <input type="radio" id="pos-tc" name="watermark_position" value="top-center">
                            <label for="pos-tc">⬆️ Arriba Centro</label>
                        </div>
                        <div class="position-option">
                            <input type="radio" id="pos-tr" name="watermark_position" value="top-right">
                            <label for="pos-tr">↗️ Arriba Der.</label>
                        </div>
                        <div class="position-option">
                            <input type="radio" id="pos-cl" name="watermark_position" value="center-left">
                            <label for="pos-cl">⬅️ Centro Izq.</label>
                        </div>
                        <div class="position-option">
                            <input type="radio" id="pos-c" name="watermark_position" value="center">
                            <label for="pos-c">🎯 Centro</label>
                        </div>
                        <div class="position-option">
                            <input type="radio" id="pos-cr" name="watermark_position" value="center-right">
                            <label for="pos-cr">➡️ Centro Der.</label>
                        </div>
                        <div class="position-option">
                            <input type="radio" id="pos-bl" name="watermark_position" value="bottom-left">
                            <label for="pos-bl">↙️ Abajo Izq.</label>
                        </div>
                        <div class="position-option">
                            <input type="radio" id="pos-bc" name="watermark_position" value="bottom-center">
                            <label for="pos-bc">⬇️ Abajo Centro</label>
                        </div>
                        <div class="position-option">
                            <input type="radio" id="pos-br" name="watermark_position" value="bottom-right" checked>
                            <label for="pos-br">↘️ Abajo Der.</label>
                        </div>
                    </div>
                </div>
                
                <div class="form-group">
                    <label for="watermark_opacity">Opacidad: <span class="slider-value" id="opacity-value">50%</span></label>
                    <input type="range" id="watermark_opacity" name="watermark_opacity" min="10" max="100" value="50" class="slider">
                </div>
                
                <div class="form-group">
                    <label for="watermark_size">
                        <span id="size-label-text">Tamaño:</span>
                        <span id="size-label-image" style="display: none;">Tamaño (% del ancho):</span>
                        <span class="slider-value" id="size-value">24px</span>
                    </label>
                    <input type="range" id="watermark_size" name="watermark_size" min="12" max="48" value="24" class="slider">
                </div>
            </div>
            
            <div class="preview-section">
                <h3>👁️ Vista Previa</h3>
                <div class="preview-box">
                    <div class="preview-watermark" id="preview-watermark">Descubre San Martín</div>
                    <img class="preview-watermark-img" id="preview-watermark-img" src="" style="display: none;">
                </div>
            </div>
            
            <div class="upload-section">
                <div class="file-input-wrapper">
                    <input type="file" name="images[]" id="images" accept="image/*" multiple required>
                    <label for="images" class="file-input-label">
                        📁 Seleccionar Imágenes<br>
                        <small>(Puede seleccionar múltiples archivos)</small>
                    </label>
                </div>
            </div>
            
            <button type="submit" class="button">🚀 Subir con Marca de Agua</button>
        </form>
        
        <div style="text-align: center;">
            <a href="ver.php" class="link-button">🖼️ Ver Galería de Imágenes</a>
        </div>
    </div>

    <script>
        // Actualizar preview en tiempo real
        const watermarkText = document.getElementById('watermark_text');
        const watermarkPosition = document.getElementsByName('watermark_position');
        const watermarkOpacity = document.getElementById('watermark_opacity');
        const watermarkSize = document.getElementById('watermark_size');
        const watermarkType = document.getElementsByName('watermark_type');
        const watermarkLogo = document.getElementById('watermark_logo');
        const previewWatermark = document.getElementById('preview-watermark');
        const previewWatermarkImg = document.getElementById('preview-watermark-img');
        const opacityValue = document.getElementById('opacity-value');
        const sizeValue = document.getElementById('size-value');
        const textOptions = document.getElementById('text-options');
        const imageOptions = document.getElementById('image-options');
        const sizeLabelText = document.getElementById('size-label-text');
        const sizeLabelImage = document.getElementById('size-label-image');
        
        // Cambiar entre texto e imagen
        watermarkType.forEach(radio => {
            radio.addEventListener('change', function() {
                if (this.value === 'text') {
                    textOptions.style.display = 'block';
                    imageOptions.style.display = 'none';
                    previewWatermark.style.display = 'block';
                    previewWatermarkImg.style.display = 'none';
                    sizeLabelText.style.display = 'inline';
                    sizeLabelImage.style.display = 'none';
                    watermarkSize.min = 12;
                    watermarkSize.max = 48;
                    watermarkSize.value = 24;
                } else {
                    textOptions.style.display = 'none';
                    imageOptions.style.display = 'block';
                    previewWatermark.style.display = 'none';
                    previewWatermarkImg.style.display = 'block';
                    sizeLabelText.style.display = 'none';
                    sizeLabelImage.style.display = 'inline';
                    watermarkSize.min = 10;
                    watermarkSize.max = 50;
                    watermarkSize.value = 20;
                }
                updatePreview();
            });
        });
        
        // Cargar logo
        watermarkLogo.addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                // Mostrar inmediatamente en el preview
                const reader = new FileReader();
                reader.onload = function(event) {
                    previewWatermarkImg.src = event.target.result;
                    updatePreview();
                    
                    // Subir el logo al servidor
                    const formData = new FormData();
                    formData.append('logo', file);
                    
                    fetch('upload_logo.php', {
                        method: 'POST',
                        body: formData
                    })
                    .then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            console.log('Logo subido correctamente:', data.path);
                        } else {
                            console.error('Error:', data.message);
                            alert('Error al subir el logo: ' + data.message);
                        }
                    })
                    .catch(error => {
                        console.error('Error de red:', error);
                    });
                };
                reader.readAsDataURL(file);
            }
        });
        
        function updatePreview() {
            const opacity = watermarkOpacity.value / 100;
            const size = watermarkSize.value;
            let position = 'bottom-right';
            let currentType = 'text';
            
            watermarkType.forEach(radio => {
                if (radio.checked) currentType = radio.value;
            });
            
            watermarkPosition.forEach(radio => {
                if (radio.checked) position = radio.value;
            });
            
            if (currentType === 'text') {
                const text = watermarkText.value;
                previewWatermark.textContent = text;
                previewWatermark.style.opacity = opacity;
                previewWatermark.style.fontSize = size + 'px';
                
                positionElement(previewWatermark, position);
                sizeValue.textContent = size + 'px';
            } else {
                previewWatermarkImg.style.opacity = opacity;
                previewWatermarkImg.style.maxWidth = size + '%';
                
                positionElement(previewWatermarkImg, position);
                sizeValue.textContent = size + '%';
            }
            
            opacityValue.textContent = watermarkOpacity.value + '%';
        }
        
        function positionElement(element, position) {
            const padding = '20px';
            element.style.top = 'auto';
            element.style.bottom = 'auto';
            element.style.left = 'auto';
            element.style.right = 'auto';
            element.style.transform = 'none';
            
            switch(position) {
                case 'top-left':
                    element.style.top = padding;
                    element.style.left = padding;
                    break;
                case 'top-center':
                    element.style.top = padding;
                    element.style.left = '50%';
                    element.style.transform = 'translateX(-50%)';
                    break;
                case 'top-right':
                    element.style.top = padding;
                    element.style.right = padding;
                    break;
                case 'center-left':
                    element.style.top = '50%';
                    element.style.left = padding;
                    element.style.transform = 'translateY(-50%)';
                    break;
                case 'center':
                    element.style.top = '50%';
                    element.style.left = '50%';
                    element.style.transform = 'translate(-50%, -50%)';
                    break;
                case 'center-right':
                    element.style.top = '50%';
                    element.style.right = padding;
                    element.style.transform = 'translateY(-50%)';
                    break;
                case 'bottom-left':
                    element.style.bottom = padding;
                    element.style.left = padding;
                    break;
                case 'bottom-center':
                    element.style.bottom = padding;
                    element.style.left = '50%';
                    element.style.transform = 'translateX(-50%)';
                    break;
                case 'bottom-right':
                    element.style.bottom = padding;
                    element.style.right = padding;
                    break;
            }
        }
        
        watermarkText.addEventListener('input', updatePreview);
        watermarkOpacity.addEventListener('input', updatePreview);
        watermarkSize.addEventListener('input', updatePreview);
        watermarkPosition.forEach(radio => {
            radio.addEventListener('change', updatePreview);
        });
        
        // Mostrar nombre de archivos seleccionados
        document.getElementById('images').addEventListener('change', function(e) {
            const label = document.querySelector('.file-input-label');
            const fileCount = e.target.files.length;
            if (fileCount > 0) {
                label.innerHTML = `📁 ${fileCount} imagen(es) seleccionada(s)<br><small>Listo para subir</small>`;
            }
        });
        
        // Inicializar preview
        updatePreview();
    </script>
    
    
    
    
    
</body>
</html>