<?php
session_start();
if (!isset($_SESSION['admin_id'])) {
    header("Location: login.php");
    exit();
}
include '../include/config.php';

$patient_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$patient = null;
$qr_image = '';
$pin = '';
$card_number = '';

if ($patient_id) {
    $result = $conn->query("SELECT * FROM patients WHERE patient_id = $patient_id");
    $patient = $result->fetch_assoc();
    
    if ($patient) {
        // Generate unique card number
        $card_number = 'UHD' . date('Y') . str_pad($patient_id, 6, '0', STR_PAD_LEFT);
        
        // Get or generate 4-digit PIN for this patient
        $pin_check = $conn->query("SELECT pin FROM patient_pins WHERE patient_id = $patient_id");
        if ($pin_check && $pin_check->num_rows > 0) {
            $pin_row = $pin_check->fetch_assoc();
            $pin = $pin_row['pin'];
        } else {
            // Generate random 4-digit PIN
            $pin = str_pad(rand(1000, 9999), 4, '0', STR_PAD_LEFT);
            $conn->query("INSERT INTO patient_pins (patient_id, pin) VALUES ($patient_id, '$pin')");
        }
        
        // IMPORTANT: Create a proper URL for QR code - NOT JSON
        // This URL will open the PIN login page directly when scanned
        $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
        $domain = $_SERVER['HTTP_HOST'];
        $login_url = $protocol . $domain . "/patient/pin_login.php?patient_id=" . $patient['patient_id'];
        
        // Generate QR code image with the URL
        $qr_image = "https://api.qrserver.com/v1/create-qr-code/?size=250x250&data=" . urlencode($login_url);
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Print Patient Card - Detomi Specialist Hospital</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        /* Card size: 85.6mm x 54mm (standard plastic card) */
        @page {
            size: 85.6mm 54mm;
            margin: 0;
        }
        
        body {
            margin: 0;
            padding: 20px;
            background: #2c1a0e;
            font-family: 'Segoe UI', 'Arial', sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
        }
        
        .card-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 20px;
        }
        
        /* The actual card */
        .patient-card {
            width: 85.6mm;
            height: 54mm;
            background: linear-gradient(135deg, #fff8e7, #fff0d0);
            border-radius: 5mm;
            box-shadow: 0 4px 10px rgba(0,0,0,0.2);
            position: relative;
            overflow: hidden;
        }
        
        .patient-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: repeating-linear-gradient(45deg, rgba(212,160,23,0.05) 0px, rgba(212,160,23,0.05) 2px, transparent 2px, transparent 8px);
            pointer-events: none;
        }
        
        .card-content {
            padding: 4mm;
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
        
        .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #d4a017;
            padding-bottom: 2mm;
        }
        
        .hospital-name {
            font-size: 5mm;
            font-weight: bold;
            color: #0a3d2f;
        }
        
        .uhd-logo {
            font-size: 4mm;
            color: #d4a017;
            font-weight: bold;
        }
        
        .patient-info {
            margin: 3mm 0;
        }
        
        .patient-name {
            font-size: 5.5mm;
            font-weight: bold;
            color: #1a0f08;
        }
        
        .patient-id {
            font-size: 3.5mm;
            color: #555;
            margin-top: 1mm;
        }
        
        .pin-section {
            background: #f5f5f5;
            border-radius: 3mm;
            padding: 2mm;
            margin: 2mm 0;
            text-align: center;
            border: 1px solid #d4a017;
        }
        
        .pin-label {
            font-size: 3mm;
            color: #666;
        }
        
        .pin-number {
            font-size: 7mm;
            font-weight: bold;
            color: #0a3d2f;
            letter-spacing: 3mm;
            font-family: 'Courier New', monospace;
        }
        
        .qr-section {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 2mm;
        }
        
        .qr-code {
            width: 16mm;
            height: 16mm;
            background: white;
            padding: 1mm;
            border-radius: 2mm;
            border: 1px solid #ddd;
        }
        
        .qr-code img {
            width: 100%;
            height: 100%;
        }
        
        .qr-instruction {
            font-size: 2.5mm;
            color: #555;
            text-align: right;
        }
        
        .card-footer {
            font-size: 2.5mm;
            color: #777;
            text-align: center;
            border-top: 0.5px solid #ddd;
            padding-top: 1.5mm;
            margin-top: 2mm;
        }
        
        .print-btn {
            background: #d4a017;
            color: #1a0f08;
            border: none;
            padding: 12px 30px;
            border-radius: 10px;
            font-weight: bold;
            cursor: pointer;
            font-size: 16px;
            transition: 0.3s;
        }
        
        .print-btn:hover {
            background: #b8860b;
            color: white;
            transform: scale(1.05);
        }
        
        .back-btn {
            background: #064e3b;
            color: white;
            border: none;
            padding: 12px 30px;
            border-radius: 10px;
            font-weight: bold;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
        }
        
        .back-btn:hover {
            background: #0a5a45;
            color: white;
        }
        
        .instruction-box {
            background: #1a0f08;
            border: 1px solid #d4a017;
            border-radius: 10px;
            padding: 15px;
            max-width: 350px;
            margin-top: 20px;
        }
        
        .instruction-box h6 {
            color: #d4a017;
            margin-bottom: 10px;
        }
        
        .instruction-box small {
            color: #c0c0c0;
            line-height: 1.6;
        }
        
        @media print {
            .print-btn, .back-btn, .instruction-box, .button-group {
                display: none;
            }
            body {
                background: white;
                padding: 0;
                margin: 0;
            }
            .patient-card {
                box-shadow: none;
                margin: 0;
                page-break-after: avoid;
            }
        }
    </style>
</head>
<body>
<div class="card-container">
    <?php if ($patient && $qr_image): ?>
        <!-- The Physical Card -->
        <div class="patient-card" id="printArea">
            <div class="card-content">
                <div class="card-header">
                    <span class="hospital-name">Detomi Specialist Hospital</span>
                    <span class="uhd-logo">UHD‑Lite</span>
                </div>
                
                <div class="patient-info">
                    <div class="patient-name"><?= htmlspecialchars($patient['first_name'] . ' ' . $patient['last_name']) ?></div>
                    <div class="patient-id">Patient ID: <?= $patient['patient_id'] ?></div>
                    <div class="patient-id">Card No: <?= $card_number ?></div>
                </div>
                
                <div class="pin-section">
                    <div class="pin-label"><i class="fas fa-lock"></i> SECURE LOGIN PIN</div>
                    <div class="pin-number"><?= $pin ?></div>
                </div>
                
                <div class="qr-section">
                    <div class="qr-code">
                        <img src="<?= $qr_image ?>" alt="QR Code">
                    </div>
                    <div class="qr-instruction">
                        <i class="fas fa-qrcode"></i> Scan QR<br>then enter PIN
                    </div>
                </div>
                
                <div class="card-footer">
                    Valid throughout Nigeria • www.uhd.com.ng
                </div>
            </div>
        </div>
        
        <!-- Button Group -->
        <div class="button-group">
            <button class="print-btn" onclick="window.print();">
                <i class="fas fa-print"></i> Print Card
            </button>
            <a href="manage_patients.php" class="back-btn ms-2">
                <i class="fas fa-arrow-left"></i> Back to Patients
            </a>
        </div>
        
        <!-- Instructions for Admin -->
        <div class="instruction-box">
            <h6><i class="fas fa-info-circle"></i> Card Instructions</h6>
            <small>
                ✓ Print on plastic card stock (85.6mm x 54mm)<br>
                ✓ Cut along edges<br>
                ✓ Give card to patient<br>
                ✓ Patient scans QR code → Opens login page<br>
                ✓ Patient enters <?= $pin ?> as PIN<br>
                ✓ Patient accesses their dashboard<br>
                <br>
                <strong>QR Code contains:</strong><br>
                <?php 
                $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
                $domain = $_SERVER['HTTP_HOST'];
                echo $protocol . $domain . "/patient/pin_login.php?patient_id=" . $patient['patient_id'];
                ?>
            </small>
        </div>
        
    <?php else: ?>
        <div class="alert alert-danger text-center">
            <i class="fas fa-exclamation-circle"></i> Patient not found!
        </div>
        <a href="manage_patients.php" class="back-btn">Go back</a>
    <?php endif; ?>
</div>

<script>
    function printCard() {
        window.print();
    }
</script>
</body>
</html>