<?php
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/helpers.php';

require_once __DIR__ . '/includes/auth.php';
check_auth();

$db = db();
$lang = $_SESSION['lang'] ?? 'en';

$patient_id = $_GET['id'] ?? 0;

if (!$patient_id) {
    die("Invalid Patient ID");
}

// Fetch Patient
$stmt = $db->prepare("SELECT * FROM patients WHERE id = ?");
$stmt->execute([$patient_id]);
$patient = $stmt->fetch();

if (!$patient) {
    die("Patient not found");
}

// Fetch Settings
$stmt = $db->query("SELECT setting_key, setting_value FROM settings");
$settings = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $settings[$row['setting_key']] = $row['setting_value'];
}

$hospital_name = $settings['company_name'] ?? 'Hospital Management System';
$patient_no = str_pad($patient['id'], 6, '0', STR_PAD_LEFT);
$age = calculate_age($patient['dob']);
$gender_raw = strtolower($patient['gender'] ?? '');
$gender = $gender_raw == 'male' ? __('male') : ($gender_raw == 'female' ? __('female') : __('other'));

?>
<!DOCTYPE html>
<html lang="<?php echo $lang; ?>" dir="<?php echo $lang == 'ar' ? 'rtl' : 'ltr'; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Patient Label - <?php echo $patient_no; ?></title>
    <script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.5/dist/JsBarcode.all.min.js"></script>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
        }
        .label-container {
            width: 40mm;
            height: 25mm;
            margin: 20px auto;
            background: white;
            border: 1px solid #ddd;
            box-sizing: border-box;
            text-align: center;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            overflow: hidden;
            padding: 1mm;
        }
        .hospital-name {
            font-weight: bold;
            font-size: 8px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            width: 100%;
            margin-bottom: 1px;
        }
        .patient-name {
            font-weight: bold;
            font-size: 9px;
            margin: 1px 0;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            width: 100%;
        }
        .meta-info {
            font-size: 7px;
            color: #000;
            line-height: 1.1;
        }
        #barcode {
            max-width: 95%;
            height: 8mm; /* Reduced height for barcode */
        }
        
        @media print {
            @page {
                size: 40mm 25mm;
                margin: 0;
            }
            body {
                background: none;
                margin: 0;
            }
            .label-container {
                margin: 0;
                border: none;
                width: 40mm;
                height: 25mm;
                page-break-inside: avoid;
                position: absolute;
                top: 0;
                left: 0;
            }
            .no-print {
                display: none;
            }
        }
    </style>
</head>
<body onload="window.print()">

    <div class="no-print" style="text-align: center; margin-top: 10px;">
        <button onclick="window.print()" style="padding: 10px 20px; cursor: pointer;">Print Label</button>
        <button onclick="window.close()" style="padding: 10px 20px; cursor: pointer;">Close</button>
    </div>

    <div class="label-container">
        <div class="hospital-name"><?php echo htmlspecialchars($hospital_name); ?></div>
        
        <svg id="barcode"></svg>
        
        <div class="patient-name"><?php echo htmlspecialchars($patient['name']); ?></div>
        <div class="meta-info">
            Age: <?php echo $age; ?> | DOB: <?php echo $patient['dob']; ?> <br>
            <?php echo $gender; ?>
        </div>
    </div>

    <script>
        JsBarcode("#barcode", "<?php echo $patient_no; ?>", {
            format: "CODE128",
            width: 1, /* Thinner bars */
            height: 25, /* Shorter bars */
            displayValue: true,
            fontSize: 8,
            marginTop: 1,
            marginBottom: 1,
            margin: 0
        });
    </script>
</body>
</html>
