<?php
require_once 'includes/config.php';
require_once 'includes/db.php';
session_start();

// Verificar si el usuario está logueado
if (!isset($_SESSION['usuario_id'])) {
    header('Location: login.php');
    exit;
}

// Inicializar la base de datos
$db = new Database();

// Verificar si se proporcionó un comprobante
if (!isset($_GET['comprobante'])) {
    header('Location: caja.php');
    exit;
}

$comprobante = $_GET['comprobante'];
$motivo = isset($_GET['motivo']) ? $_GET['motivo'] : '';

// Obtener configuración
$config = $db->query("SELECT color_principal, color_secundario FROM configuracion WHERE id = 1")->fetch();
$colorPrincipal = $config['color_principal'] ?? '#ff2b1c';
$colorSecundario = $config['color_secundario'] ?? '#666666';
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ver Comprobante</title>
    <link rel="stylesheet" href="admin/css/bootstrap.min.css">
    <style>
        :root {
            --color-primary: <?= $colorPrincipal ?>;
            --color-secondary: <?= $colorSecundario ?>;
        }
        body {
            background: #f5f5f5;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            padding: 20px;
        }
        .actions {
            position: fixed;
            top: 20px;
            right: 20px;
            display: flex;
            gap: 10px;
        }
        .btn-volver, .btn-imprimir {
            background-color: var(--color-primary);
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            text-decoration: none;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .btn-volver:hover, .btn-imprimir:hover {
            background-color: var(--color-secondary);
            color: white;
        }
        @media print {
            .actions {
                display: none;
            }
            body {
                padding: 0;
                margin: 0;
            }
            .comprobante-container {
                max-width: 100%;
                margin: 0;
            }
            .motivo {
                margin: 20px 0;
            }
        }
        .comprobante-container {
            max-width: 90%;
            margin: 0 auto;
            text-align: center;
        }
        .comprobante-container img {
            max-width: 100%;
            height: auto;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
        }
        .motivo {
            color: var(--color-primary);
            margin-bottom: 20px;
            font-size: 1.2em;
        }
    </style>
</head>
<body>
    <div class="actions">
        <button onclick="window.print()" class="btn-imprimir">
            <img src="admin/icons/printer.svg" width="16" height="16" alt="Imprimir" style="filter: brightness(0) invert(1);">
            Imprimir
        </button>
        <a href="caja.php" class="btn-volver">
            <img src="admin/icons/x-lg.svg" width="16" height="16" alt="Cerrar" style="filter: brightness(0) invert(1);">
            Volver
        </a>
    </div>

    <div class="comprobante-container">
        <?php if ($motivo): ?>
            <h3 class="motivo"><?= htmlspecialchars($motivo) ?></h3>
        <?php endif; ?>
        <img src="<?= htmlspecialchars('uploads/comprobantes/' . $comprobante) ?>" 
             alt="Comprobante" 
             class="img-fluid">
    </div>
</body>
</html>
