<?php
require_once __DIR__ . '/src/auth.php';
require_once __DIR__ . '/src/db.php';
require_once __DIR__ . '/src/history.php';
require_login();

$id = $_GET['id'] ?? '';
if (empty($id)) {
    die("ID de assinatura inválido.");
}

$pdo = get_db_connection();
$stmt = $pdo->prepare("
    SELECT cs.*, c.title as contract_title, c.contract_number, c.supplier, u.name as unit_name 
    FROM contract_signatures cs 
    JOIN contracts c ON cs.contract_id = c.id 
    JOIN units u ON c.unit_id = u.id
    WHERE cs.id = ? AND cs.is_signed = 1
");
$stmt->execute([$id]);
$sig = $stmt->fetch();

if (!$sig) {
    die("Assinatura não encontrada ou pendente.");
}
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <title>Certificado de Assinatura Digital - <?= htmlspecialchars($sig['contract_title']) ?></title>
    <style>
        body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; color: #333; line-height: 1.6; background-color: #f4f7f6; }
        .certificate { width: 800px; margin: 40px auto; border: 1px solid #ddd; background-color: #fff; padding: 50px; position: relative; box-shadow: 0 0 20px rgba(0,0,0,0.1); border-radius: 8px; }
        .header { text-align: center; border-bottom: 2px solid #004a99; padding-bottom: 20px; margin-bottom: 30px; }
        .title { font-size: 26px; font-weight: bold; color: #004a99; text-transform: uppercase; letter-spacing: 1px; }
        .subtitle { font-size: 14px; color: #666; margin-top: 5px; }
        .section { margin-bottom: 30px; padding: 15px; background-color: #f9f9f9; border-radius: 6px; border-left: 4px solid #004a99; }
        .label { font-weight: bold; color: #555; font-size: 11px; text-transform: uppercase; margin-bottom: 4px; display: block; }
        .value { font-size: 15px; color: #222; font-weight: 500; }
        .signature-area { text-align: center; margin-top: 40px; border: 2px dashed #004a99; padding: 30px; border-radius: 10px; background-color: #fff; }
        .signature-img { max-height: 100px; display: block; margin: 0 auto 15px; filter: grayscale(100%) contrast(150%); }
        .signature-line { width: 300px; height: 1px; background-color: #333; margin: 10px auto; }
        .signer-info { font-weight: bold; font-size: 18px; color: #004a99; }
        .seal { position: absolute; top: 40px; right: 40px; width: 100px; height: 100px; opacity: 0.1; }
        .footer { font-size: 11px; color: #888; text-align: center; margin-top: 40px; border-top: 1px solid #eee; padding-top: 15px; }
        .stamp-text { font-size: 10px; color: #004a99; font-weight: bold; text-transform: uppercase; margin-top: 5px; }
        @media print {
            body { background-color: #fff; }
            .no-print { display: none; }
            .certificate { border: 2px solid #004a99; margin: 0; padding: 40px; width: 100%; box-shadow: none; }
        }
    </style>
</head>
<body>
    <div class="no-print" style="text-align: center; margin-top: 20px;">
        <button onclick="window.print()" style="padding: 12px 25px; cursor: pointer; background: #004a99; color: white; border: none; border-radius: 50px; font-weight: bold; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
            <i class="fas fa-print"></i> Imprimir Comprovante de Assinatura
        </button>
        <a href="contracts.php" style="margin-left: 15px; color: #666; text-decoration: none;">Voltar ao Sistema</a>
    </div>

    <div class="certificate">
        <div class="header">
            <div class="title">Comprovante de Assinatura Eletrônica</div>
            <div class="subtitle">Este documento certifica a integridade e a autoria da assinatura digital</div>
        </div>

        <div class="section">
            <div class="label">Documento Vinculado</div>
            <div class="value"><?= htmlspecialchars($sig['contract_title']) ?></div>
            <div style="font-size: 12px; color: #666; margin-top: 4px;">
                Nº Contrato: <?= htmlspecialchars($sig['contract_number'] ?: 'N/A') ?> | 
                Unidade: <?= htmlspecialchars($sig['unit_name']) ?> | 
                Fornecedor: <?= htmlspecialchars($sig['supplier']) ?>
            </div>
        </div>

        <div class="row" style="display: flex; gap: 20px;">
            <div class="section" style="flex: 1;">
                <div class="label">Dados do Assinante</div>
                <div class="value"><?= htmlspecialchars($sig['signer_name']) ?></div>
                <div style="font-size: 13px; color: #666;"><?= htmlspecialchars($sig['signer_email']) ?></div>
            </div>
            <div class="section" style="flex: 1;">
                <div class="label">Registro de Auditoria</div>
                <div class="value"><?= date('d/m/Y \à\s H:i:s', strtotime($sig['signed_at'])) ?></div>
                <div style="font-size: 13px; color: #666;">IP: <?= $sig['signer_ip'] ?></div>
            </div>
        </div>

        <div class="section">
            <div class="label">Assinatura Digital (Token)</div>
            <div class="value" style="font-family: 'Courier New', Courier, monospace; font-size: 12px; word-break: break-all; color: #004a99;">
                <?= $sig['token'] ?>
            </div>
        </div>

        <div class="signature-area">
            <div class="label" style="margin-bottom: 15px;">Assinatura do Documento</div>
            <img src="<?= $sig['signature_image'] ?>" class="signature-img">
            <div class="signature-line"></div>
            <div class="signer-info"><?= htmlspecialchars($sig['signer_name']) ?></div>
            <div class="stamp-text">Assinado Eletronicamente em <?= date('d/m/Y', strtotime($sig['signed_at'])) ?></div>
            <div style="font-size: 10px; color: #888; margin-top: 5px;">Conformidade com a Medida Provisória nº 2.200-2/2001</div>
        </div>

        <div class="footer">
            Este comprovante deve ser mantido junto ao arquivo original do contrato para fins de verificação legal.
            <br>Autenticado pelo Sistema de Gestão de Contratos em <?= date('d/m/Y H:i:s') ?>
        </div>
    </div>
</body>
</html>
