<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/nabila/includes/config.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/nabila/includes/functions.php';

// Cek login
cek_login();

// Cek ID
if (!isset($_GET['id']) || empty($_GET['id'])) {
    header("Location: " . BASE_URL . "/modules/qrcode/index.php?error=" . urlencode("ID karyawan tidak valid!"));
    exit();
}

$id = (int)$_GET['id'];

// Ambil data karyawan
$query = "SELECT * FROM karyawan WHERE id = $id";
$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) == 0 || !file_exists(QR_DIR . $karyawan['qrcode'])) {
    header("Location: " . BASE_URL . "/modules/qrcode/index.php?error=" . urlencode("Data QR Code tidak ditemukan!"));
    exit();
}

$karyawan = mysqli_fetch_assoc($result);
$pengaturan = get_pengaturan();

// Pastikan qrcode ada
if (empty($karyawan['qrcode'])) {
    header("Location: " . BASE_URL . "/modules/qrcode/index.php?error=" . urlencode("QR Code belum dibuat!"));
    exit();
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cetak QR Code - <?php echo $karyawan['nama']; ?></title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <style>
        @media print {
            body {
                margin: 0;
                padding: 0;
            }
            .no-print {
                display: none !important;
            }
            .card {
                break-inside: avoid;
                page-break-inside: avoid;
            }
        }
    </style>
</head>
<body class="bg-gray-100 p-8">
    <div class="max-w-lg mx-auto">
        <div class="no-print mb-4 flex justify-between items-center">
            <h1 class="text-2xl font-bold">Cetak QR Code Karyawan</h1>
            <button onclick="window.print()" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
                <i class="fas fa-print mr-1"></i> Cetak
            </button>
        </div>
        
        <div class="bg-white p-6 rounded-lg shadow-md card mb-4">
            <div class="text-center mb-4">
                <h2 class="text-xl font-bold"><?php echo $pengaturan['nama_perusahaan']; ?></h2>
                <p class="text-sm text-gray-600"><?php echo $pengaturan['alamat']; ?></p>
            </div>
            
            <div class="flex justify-center mb-4">
                <img src="<?php echo BASE_URL; ?>/uploads/qrcode/<?php echo $karyawan['qrcode']; ?>" alt="QR Code" class="w-48 h-48">
            </div>
            
            <div class="text-center">
                <h3 class="text-lg font-bold"><?php echo $karyawan['nama']; ?></h3>
                <p class="text-sm text-gray-600"><?php echo $karyawan['nik']; ?></p>
                <p class="text-sm text-gray-600"><?php echo $karyawan['jabatan']; ?></p>
            </div>
            
            <div class="mt-4 text-center text-xs text-gray-500">
                <p>QR Code ini digunakan untuk absensi karyawan</p>
                <p>Scan QR Code pada aplikasi absensi untuk melakukan absensi</p>
            </div>
        </div>
        
        <div class="no-print mt-4 text-center">
            <a href="<?php echo BASE_URL; ?>/modules/qrcode/index.php" class="text-blue-600 hover:text-blue-800">
                <i class="fas fa-arrow-left mr-1"></i> Kembali ke Daftar QR Code
            </a>
        </div>
    </div>
    
    <script>
        // Auto print when page loads
        window.onload = function() {
            setTimeout(function() {
                window.print();
            }, 1000);
        };
    </script>
</body>
</html>