<?php
/**
 * modules/token/print_token.php
 * Premium Official Layout for SLCMC - OFFLINE READY
 */

// Clean output buffer so the main index.php layout does not print
ob_clean();

$ticket_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($ticket_id == 0) {
    die("Invalid Ticket ID");
}

// Update Print Count securely
$db->query("UPDATE TBL_WEIGHBRIDGE_TICKETS SET PRINT_COUNT = PRINT_COUNT + 1 WHERE TICKET_ID = $ticket_id");

// Fetch Ticket Data
$sql = "SELECT w.*, c.CONTRACTOR_NAME, m.MINE_NUMBER, b.BLOCK_NAME 
        FROM TBL_WEIGHBRIDGE_TICKETS w
        LEFT JOIN TBL_CONTRACTORS c ON w.CONTRACTOR_ID = c.CONTRACTOR_ID
        LEFT JOIN TBL_MINES m ON w.MINE_ID = m.MINE_ID
        LEFT JOIN TBL_BLOCKS b ON m.BLOCK_ID = b.BLOCK_ID
        WHERE w.TICKET_ID = $ticket_id";
$result = $db->query($sql);
$data = mysqli_fetch_assoc($result);

if (!$data) {
    die("Ticket not found.");
}

$is_duplicate = ($data['PRINT_COUNT'] > 1);
$qr_data = urlencode("TK:" . $data['TICKET_NO'] . "|VH:" . $data['VEHICLE_NO'] . "|DT:" . date('Ymd', strtotime($data['FIRST_WEIGHT_TIME'])));
// Currently using API for QR. Can be replaced with local phpqrcode library later for 100% offline.
$qr_url = "https://api.qrserver.com/v1/create-qr-code/?size=120x120&data=" . $qr_data . "&margin=0";

// Local Offline Image Paths
$sindh_logo_url = "assets/img/sindh-gov-logo.png";
$slcmc_logo_url = "assets/img/slmc-logo.png";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Official Token - <?php echo $data['TICKET_NO']; ?></title>
    <!-- Local AdminLTE CSS -->
    <link rel="stylesheet" href="assets/dist/css/adminlte.min.css">
    <!-- Local FontAwesome CSS (assuming you have it downloaded in plugins) -->
    <link rel="stylesheet" href="assets/plugins/fontawesome-free/css/all.min.css">
    
    <style>
        /* Screen & Print Base Settings */
        body { background-color: #555; font-family: 'Arial', sans-serif; padding: 20px; }
        
        /* The Physical Paper Container */
        .token-paper { 
            background-color: #fff; 
            width: 210mm; /* A4 Width */
            min-height: 148mm; /* A5 Height (Half A4) */
            margin: 0 auto; 
            padding: 40px 50px; 
            box-shadow: 0 10px 30px rgba(0,0,0,0.5); 
            position: relative; 
            overflow: hidden;
            border: 1px solid #ccc;
        }
        
        /* Typography & Layout */
        .header-section { display: flex; justify-content: space-between; align-items: center; border-bottom: 4px double #111; padding-bottom: 15px; margin-bottom: 20px; position: relative; z-index: 2; }
        .logo-img { height: 90px; width: auto; object-fit: contain; }
        .company-title { font-size: 26px; font-weight: 900; text-align: center; text-transform: uppercase; font-family: 'Times New Roman', serif; letter-spacing: 1.5px; color: #000; margin: 0; line-height: 1.2; }
        
        .token-badge { background-color: #000; color: #fff; padding: 6px 20px; font-size: 20px; font-weight: bold; border-radius: 4px; display: inline-block; margin-top: 10px; letter-spacing: 1px; -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
        
        .meta-strip { display: flex; justify-content: space-between; font-size: 16px; font-weight: bold; border-bottom: 2px solid #eee; padding-bottom: 10px; margin-bottom: 25px; position: relative; z-index: 2;}
        
        /* Field Rows */
        .content-z { position: relative; z-index: 2; }
        .data-row { display: flex; align-items: flex-end; margin-bottom: 18px; font-size: 17px; }
        .data-label { font-weight: bold; margin-right: 15px; white-space: nowrap; color: #222; }
        .data-value { flex-grow: 1; border-bottom: 2px dotted #555; font-family: 'Courier New', Courier, monospace; font-size: 22px; font-weight: bold; text-align: center; color: #000; padding-bottom: 2px; }
        
        /* Central Watermark */
        .watermark { position: absolute; top: 55%; left: 50%; transform: translate(-50%, -50%); opacity: 0.06; width: 350px; pointer-events: none; z-index: 1; -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important;}
        
        /* Duplicate Warning Overlay */
        .duplicate-stamp { position: absolute; top: 35%; left: 10%; font-size: 70px; font-weight: 900; color: rgba(255, 0, 0, 0.15); transform: rotate(-25deg); pointer-events: none; border: 8px solid rgba(255, 0, 0, 0.15); padding: 10px 30px; border-radius: 15px; z-index: 3; font-family: 'Impact', sans-serif; text-transform: uppercase; -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }

        .qr-wrapper { text-align: center; padding: 5px; border: 2px solid #000; border-radius: 5px; display: inline-block; background: #fff;}

        /* Print Settings */
        @media print {
            body { background-color: #fff; padding: 0; }
            .no-print { display: none !important; }
            .token-paper { box-shadow: none; border: none; margin: 0; padding: 20px; width: 100%; height: auto; }
            @page { size: A5 landscape; margin: 0; }
        }
    </style>
</head>
<body onload="window.print()">

    <!-- UI Controls (Hidden on actual print) -->
    <div class="text-center mb-4 no-print">
        <button onclick="window.print()" class="btn btn-primary btn-lg px-5 mr-3 font-weight-bold shadow"><i class="fas fa-print mr-2"></i> Print Official Token</button>
        <button onclick="window.close()" class="btn btn-dark btn-lg px-4 shadow"><i class="fas fa-times mr-2"></i> Close Screen</button>
    </div>

    <!-- Official Paper Layout -->
    <div class="token-paper">
        
        <!-- Watermark Image (Local) -->
        <img src="<?php echo $sindh_logo_url; ?>" class="watermark" alt="Watermark">

        <!-- Duplicate Tracking Stamp -->
        <?php if($is_duplicate): ?>
            <div class="duplicate-stamp">DUPLICATE<br><span style="font-size: 28px; text-align: center; display: block; letter-spacing: 5px; margin-top: 10px;">Reprint Count: <?php echo str_pad($data['PRINT_COUNT'], 2, '0', STR_PAD_LEFT); ?></span></div>
        <?php endif; ?>

        <!-- Official Header -->
        <div class="header-section">
            <div style="width: 15%;">
                <img src="<?php echo $sindh_logo_url; ?>" class="logo-img" alt="Sindh Govt Logo">
            </div>
            <div style="width: 70%; text-align: center;">
                <h1 class="company-title">SINDH LAKHRA COAL MINING COMPANY</h1>
                <div class="token-badge">TOKEN NO. <?php echo htmlspecialchars($data['TICKET_NO']); ?></div>
            </div>
            <div style="width: 15%; text-align: right;">
                <img src="<?php echo $slcmc_logo_url; ?>" class="logo-img" alt="SLCMC Logo">
            </div>
        </div>

        <div class="content-z">
            <!-- Meta Strip -->
            <div class="meta-strip">
                <div>Daily S.No: <span style="font-size: 20px; text-decoration: underline;"><?php echo str_pad($data['DAILY_SERIAL'], 3, '0', STR_PAD_LEFT); ?></span></div>
                <?php if($is_duplicate): ?><div style="color: #d9534f; font-weight: 900; border: 1px solid #d9534f; padding: 2px 10px; border-radius: 4px;">*** DUPLICATE COPY ***</div><?php endif; ?>
                <div>Dated: <span style="font-size: 20px; text-decoration: underline;"><?php echo date('d/m/Y', strtotime($data['FIRST_WEIGHT_TIME'])); ?></span></div>
            </div>

            <div class="row">
                <!-- Left Column (Main Data) -->
                <div class="col-9">
                    
                    <div class="row">
                        <div class="col-7">
                            <div class="data-row">
                                <span class="data-label">Truck No.</span>
                                <span class="data-value"><?php echo htmlspecialchars($data['VEHICLE_NO']); ?></span>
                            </div>
                        </div>
                        <div class="col-5">
                            <div class="data-row">
                                <span class="data-label">Expected Tons:</span>
                                <span class="data-value"><?php echo $data['EXPECTED_TONS'] ? $data['EXPECTED_TONS'] : '-'; ?></span>
                            </div>
                        </div>
                    </div>

                    <div class="data-row mt-2">
                        <span class="data-label">Initial (Tare) Weight:</span>
                        <span class="data-value" style="font-size: 24px;"><?php echo number_format($data['FIRST_WEIGHT'], 2); ?> TONS</span>
                    </div>

                    <div class="data-row mt-3">
                        <span class="data-label" style="color: #777;">Second (Gross) Weight:</span>
                        <span class="data-value"></span>
                    </div>

                    <div class="data-row mt-3">
                        <span class="data-label" style="color: #777;">Net Dispatched Weight:</span>
                        <span class="data-value"></span>
                    </div>

                    <div class="row mt-3">
                        <div class="col-6">
                            <div class="data-row">
                                <span class="data-label">Target Block:</span>
                                <span class="data-value" style="font-size: 18px;"><?php echo htmlspecialchars($data['BLOCK_NAME']); ?></span>
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="data-row">
                                <span class="data-label">Target Mine:</span>
                                <span class="data-value" style="font-size: 18px;"><?php echo htmlspecialchars($data['MINE_NUMBER']); ?></span>
                            </div>
                        </div>
                    </div>

                </div>

                <!-- Right Column (QR & Signatures) -->
                <div class="col-3 text-center d-flex flex-column justify-content-between pb-3">
                    <div class="mt-2">
                        <div class="qr-wrapper shadow-sm">
                            <img src="<?php echo $qr_url; ?>" alt="Secure QR Code">
                        </div>
                        <div style="font-size: 10px; font-weight: bold; margin-top: 5px; font-family: monospace;">SCAN TO VERIFY</div>
                    </div>
                    
                    <div style="border-top: 2px solid #000; margin-top: auto; padding-top: 5px; font-weight: bold;">
                        Authorized Signature<br>
                        <small>Weighbridge Officer</small>
                    </div>
                </div>
            </div>

            <hr style="border-top: 2px dashed #ccc; margin: 25px 0;">

            <!-- Contractor Info -->
            <div class="row">
                <div class="col-8">
                    <div class="data-row">
                        <span class="data-label">Contractor Name:</span>
                        <span class="data-value" style="font-size: 18px;"><?php echo htmlspecialchars($data['CONTRACTOR_NAME']); ?></span>
                    </div>
                    <div class="data-row mt-2">
                        <span class="data-label">Driver Name:</span>
                        <span class="data-value" style="font-size: 18px;"><?php echo htmlspecialchars($data['DRIVER_NAME']); ?></span>
                    </div>
                </div>
                <div class="col-4">
                    <div class="data-row">
                        <span class="data-label">Cont. PIN:</span>
                        <span class="data-value" style="letter-spacing: 4px; font-size: 18px;"><?php echo htmlspecialchars($data['CONTRACTOR_PASSWORD']); ?></span>
                    </div>
                    <div class="data-row mt-2">
                        <span class="data-label">Mobile:</span>
                        <span class="data-value" style="font-size: 18px;"><?php echo htmlspecialchars($data['DRIVER_MOBILE']); ?></span>
                    </div>
                </div>
            </div>

        </div>
    </div>

</body>
</html>
<?php exit; ?>