<?php
require_once("../db.php");

if (!isset($_SESSION['rol']) || $_SESSION['rol'] !== 'admin') {
    header("Location: ../login.php?error=acceso_denegado");
    exit();
}

$archivo = basename($_GET['file'] ?? '');
$ruta = 'comprobantes/' . $archivo;

if (empty($archivo) || !file_exists($ruta)) {
    die("
    <div style='font-family:\"Segoe UI\", sans-serif; text-align:center; padding:50px; background:#fffef8; height:100vh; box-sizing:border-box;'>
        <div style='max-width:450px; margin:0 auto; background:white; padding:30px; border-radius:20px; box-shadow:0 10px 30px rgba(0,0,0,0.05); border-top: 4px solid #e02828;'>
            <h3 style='color:#e02828; margin-bottom:10px;'>Error de Archivo</h3>
            <p style='color:#666; font-size:0.95rem; line-height:1.5;'>El comprobante no existe en el servidor o el nombre del archivo es inválido.</p>
            <button onclick='window.close()' style='margin-top:20px; background:#333; color:white; border:none; padding:10px 20px; border-radius:50px; cursor:pointer; font-weight:bold;'>Cerrar Ventana</button>
        </div>
    </div>");
}

// Obtenemos la extensión para saber si es PDF o Imagen
$extension = strtolower(pathinfo($ruta, PATHINFO_EXTENSION));
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Imprimir Comprobante - La Pecosa</title>
    <link rel="stylesheet" href="../css/all.min.css">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Segoe UI', sans-serif; background: #f4f6f9; color: #333; padding: 30px 20px; }
        
        .no-print { 
            max-width: 800px;
            margin: 0 auto 20px auto;
            background: white; 
            padding: 12px 25px; 
            border-radius: 15px; 
            box-shadow: 0 4px 20px rgba(0,0,0,0.03); 
            display: flex; 
            justify-content: space-between; 
            align-items: center; 
        }
        .panel-info { font-size: 0.95rem; color: #64748b; font-weight: 500; }
        .panel-info i { color: #e02828; margin-right: 5px; }
        .button-group { display: flex; gap: 10px; }
        
        .btn { 
            padding: 10px 22px; 
            font-weight: bold; 
            font-size: 0.9rem;
            border: none; 
            border-radius: 50px; 
            cursor: pointer; 
            transition: all 0.2s ease; 
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        .btn-print { background: #e02828; color: white; box-shadow: 0 4px 10px rgba(224,40,40,0.15); }
        .btn-print:hover { background: #c02020; transform: translateY(-1px); }
        .btn-close { background: #f1f5f9; color: #475569; }
        .btn-close:hover { background: #e2e8f0; color: #1e293b; }
        
        .view-container { 
            max-width: 800px; 
            margin: 0 auto; 
            background: white; 
            padding: 20px; 
            border-radius: 15px; 
            box-shadow: 0 4px 25px rgba(0,0,0,0.04); 
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        img { max-width: 100%; height: auto; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.02); }
        iframe { width: 100%; height: 78vh; border: none; border-radius: 8px; }
        
        @media print { 
            .no-print { display: none !important; } 
            body { background: white; padding: 0; } 
            .view-container { box-shadow: none; padding: 0; max-width: 100%; } 
            img { box-shadow: none; border-radius: 0; }
        }
    </style>
</head>
<body onload="window.print()">

    <div class="no-print">
        <div class="panel-info">
            <i class="fa-solid fa-file-invoice"></i> Vista de Comprobante
        </div>
        <div class="button-group">
            <button class="btn btn-close" onclick="window.close()">
                <i class="fa-solid fa-xmark"></i> Cerrar
            </button>
            <button class="btn btn-print" onclick="window.print()">
                <i class="fa-solid fa-print"></i> Imprimir Ticket
            </button>
        </div>
    </div>

    <div class="view-container">
        <?php if ($extension === 'pdf'): ?>
            <iframe src="<?php echo $ruta; ?>"></iframe>
        <?php else: ?>
            <img src="<?php echo $ruta; ?>" alt="Comprobante de Pago">
        <?php endif; ?>
    </div>

</body>
</html>