<?php
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/includes/auth.php'; // ensure logged in
$lang = $_SESSION['lang'] ?? 'en';
require_once __DIR__ . '/lang.php';

function __($key) {
    global $lang, $translations;
    return $translations[$lang][$key] ?? $key;
}

$id = $_GET['id'] ?? null;
if (!$id) die("No Token ID");

$db = db();
$stmt = $db->prepare("
    SELECT q.*, 
           p.name as patient_name,
           p.patient_number,
           d.name_$lang as doctor_name,
           d.room_number,
           dept.name_$lang as department_name,
           td.name_$lang as target_department_name
    FROM patient_queue q
    JOIN patients p ON q.patient_id = p.id
    JOIN departments dept ON q.department_id = dept.id
    LEFT JOIN departments td ON q.target_department_id = td.id
    LEFT JOIN employees d ON q.doctor_id = d.id
    WHERE q.id = ?
");
$stmt->execute([$id]);
$token = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$token) die("Token not found");
?>
<!DOCTYPE html>
<html lang="<?php echo $lang; ?>" dir="<?php echo $lang === 'ar' ? 'rtl' : 'ltr'; ?>">
<head>
    <meta charset="UTF-8">
    <title>Print Token</title>
    <style>
        body {
            font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
            font-size: 14px;
            margin: 0;
            padding: 10px;
            width: 58mm; /* standard 58mm thermal printer width */
            color: #000;
        }
        .center { text-align: center; }
        .clinic-name { font-size: 16px; font-weight: bold; margin-bottom: 5px; }
        .token-num { font-size: 42px; font-weight: bold; margin: 10px 0; }
        hr { border: none; border-top: 1px dashed #000; margin: 10px 0; }
        .details { text-align: left; }
        .details div { margin-bottom: 5px; }
        .footer { font-size: 10px; text-align: center; margin-top: 15px; }
        @media print {
            @page { margin: 0; }
            body { margin: 1cm; }
        }
    </style>
</head>
<body onload="window.print(); window.setTimeout(window.close, 500);">
    <div class="center">
        <div class="clinic-name">HMS Clinic</div>
        <hr>
        <h2><?php echo __('token'); ?></h2>
        <div class="token-num">#<?php echo $token['token_number']; ?></div>
        <div style="font-size: 16px; font-weight: bold;"><?php echo htmlspecialchars($token['department_name']); ?></div>
        <?php if ($token['target_department_name']): ?>
        <div style="font-size: 12px; margin-top: 2px;">
            <?php echo __('target_clinic'); ?>: <?php echo htmlspecialchars($token['target_department_name']); ?>
        </div>
        <?php endif; ?>
    </div>
    
    <hr>
    
    <div class="details">
        <div><strong><?php echo __('patient'); ?>:</strong> <?php echo htmlspecialchars($token['patient_name']); ?></div>
        <?php if ($token['doctor_name']): ?>
        <div><strong><?php echo __('doctor'); ?>:</strong> <?php echo htmlspecialchars($token['doctor_name']); ?></div>
        <?php endif; ?>
        <?php if ($token['room_number']): ?>
        <div style="font-size: 16px; margin-top: 8px;"><strong><?php echo __('room_number'); ?>:</strong> <?php echo htmlspecialchars($token['room_number']); ?></div>
        <?php endif; ?>
        <div style="margin-top: 8px; font-size: 12px;"><strong><?php echo __('date'); ?>:</strong> <?php echo date('Y-m-d H:i', strtotime($token['created_at'])); ?></div>
    </div>
    
    <hr>
    
    <div class="footer">
        <?php echo __('please_wait_for_token'); ?>
    </div>
</body>
</html>