<?php
include("../db/cn.php");
include("common/tx.php");        // $tx = company name
ob_start();
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(0);

$orderId = isset($_GET['order_id']) ? mysqli_real_escape_string($connection, $_GET['order_id']) : null;
$orderItems = [];
$dateTime = $customerName = $customerPhone = $city = $transport = $userName = $totalQty = '';

if ($orderId) {
    // Fetch all items for this order_id
    $stmt = $connection->prepare("SELECT item_name, qty, package, date_time, customer_name, customer_phone, city, transport, total_qty, user_name 
                                  FROM gatepass 
                                  WHERE order_id = ?");
    $stmt->bind_param("s", $orderId);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result && $result->num_rows > 0) {
        $orderItems = $result->fetch_all(MYSQLI_ASSOC);
        $first = $orderItems[0];
        $dateTime       = $first['date_time'];
        $customerName   = $first['customer_name'];
        $customerPhone  = $first['customer_phone'];
        $city           = $first['city'];
        $transport      = $first['transport'];
        $totalQty       = $first['total_qty'];
        $userName       = $first['user_name'];
    }
    $stmt->close();
}

$content = ob_get_clean();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gate Pass | <?php echo htmlspecialchars($tx); ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary: #1e293b;
            --border: #e2e8f0;
            --bg-light: #f8fafc;
            --text: #0f172a;
            --text-muted: #64748b;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background: #f1f5f9;
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 40px 20px;
            color: var(--text);
        }

        .invoice {
            width: 210mm;
            max-width: 210mm;
            background: #ffffff;
            box-shadow: 0 10px 30px rgba(0,0,0,0.08);
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }

        /* ----- MODAL (screen only) ----- */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            z-index: 9999;
            justify-content: center;
            align-items: center;
        }
        .modal.active {
            display: flex;
        }
        .modal-dialog {
            background: white;
            border-radius: 12px;
            padding: 30px 25px;
            width: 90%;
            max-width: 380px;
            text-align: center;
            box-shadow: 0 15px 35px rgba(0,0,0,0.2);
        }
        .modal-dialog h2 {
            margin-bottom: 10px;
            font-size: 20px;
            color: #1e293b;
        }
        .modal-dialog p {
            font-size: 14px;
            color: #64748b;
            margin-bottom: 25px;
        }
        .modal-buttons {
            display: flex;
            flex-direction: column;
            gap: 10px;
        }
        .modal-btn {
            padding: 14px;
            border: none;
            border-radius: 8px;
            font-weight: 600;
            font-size: 16px;
            cursor: pointer;
            transition: background 0.2s;
        }
        .modal-btn.thermal {
            background: #0f172a;
            color: white;
        }
        .modal-btn.thermal:hover {
            background: #1e293b;
        }
        .modal-btn.a4 {
            background: #e2e8f0;
            color: #0f172a;
        }
        .modal-btn.a4:hover {
            background: #cbd5e1;
        }
        .modal-close {
            margin-top: 15px;
            background: transparent;
            border: none;
            color: #64748b;
            text-decoration: underline;
            cursor: pointer;
            font-size: 14px;
        }

        .invoice-header {
            background: var(--primary);
            color: white;
            padding: 24px 36px;
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            flex-wrap: wrap;
        }

        .company-info h1 {
            font-size: 28px;
            font-weight: 700;
            letter-spacing: -0.5px;
            margin-bottom: 4px;
        }

        .company-info .address {
            font-size: 13px;
            opacity: 0.85;
            line-height: 1.5;
        }

        .invoice-badge {
            text-align: right;
        }
        .invoice-badge .label {
            font-size: 12px;
            text-transform: uppercase;
            letter-spacing: 2px;
            opacity: 0.8;
        }
        .invoice-badge .number {
            font-size: 24px;
            font-weight: 700;
            margin-top: 4px;
        }

        .invoice-body {
            padding: 36px 36px 24px;
        }

        .meta-row {
            display: flex;
            flex-wrap: wrap;
            gap: 30px;
            margin-bottom: 28px;
        }

        .meta-box {
            flex: 1;
            min-width: 180px;
        }
        .meta-box h3 {
            font-size: 11px;
            text-transform: uppercase;
            letter-spacing: 1px;
            color: var(--text-muted);
            margin-bottom: 10px;
            border-bottom: 1px solid var(--border);
            padding-bottom: 6px;
        }
        .meta-box p {
            font-size: 14px;
            line-height: 1.6;
            font-weight: 500;
        }

        /* Items Table */
        .items-section {
            margin-top: 20px;
        }
        .items-section h3 {
            font-size: 11px;
            text-transform: uppercase;
            letter-spacing: 1px;
            color: var(--text-muted);
            margin-bottom: 10px;
            border-bottom: 1px solid var(--border);
            padding-bottom: 6px;
        }
        .items-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 14px;
        }
        .items-table th,
        .items-table td {
            border: 1px solid var(--border);
            padding: 8px 12px;
            text-align: left;
        }
        .items-table th {
            background: var(--bg-light);
            font-weight: 600;
        }
        .total-row td {
            font-weight: 700;
            background: var(--bg-light);
        }

        .footer {
            margin-top: 24px;
            text-align: center;
            font-size: 12px;
            color: var(--text-muted);
            border-top: 1px solid var(--border);
            padding-top: 20px;
        }

        .button-group {
            background: white;
            padding: 20px 36px;
            border-top: 1px solid var(--border);
            display: flex;
            gap: 14px;
            justify-content: flex-end;
        }
        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 8px;
            font-weight: 600;
            font-size: 14px;
            cursor: pointer;
            transition: all 0.2s;
            font-family: 'Inter', sans-serif;
        }
        .btn-primary {
            background: var(--primary);
            color: white;
        }
        .btn-primary:hover {
            background: #0f172a;
        }
        .btn-secondary {
            background: #e2e8f0;
            color: #334155;
        }
        .btn-secondary:hover {
            background: #cbd5e1;
        }

        @media screen and (max-width: 800px) {
            .invoice-header {
                flex-direction: column;
                gap: 16px;
            }
            .meta-row {
                flex-direction: column;
                gap: 20px;
            }
        }

        /* ================ PRINT STYLES ================ */
        @media print {
            .no-print, .modal, #printModal {
                display: none !important;
            }

            /* A4 layout */
            body:not(.print-thermal) {
                background: white;
                padding: 0;
                margin: 0;
            }
            body:not(.print-thermal) .invoice {
                width: 100%;
                max-width: 100%;
                box-shadow: none;
                border-radius: 0;
                margin: 0;
                padding: 0;
            }
            body:not(.print-thermal) .invoice-body {
                padding: 15mm 15mm 10mm;
            }
            body:not(.print-thermal) .invoice-header {
                padding: 15mm 15mm 5mm;
            }
            @page {
                size: A4;
                margin: 15mm;
            }

            /* Thermal layout */
            body.print-thermal {
                background: white;
                margin: 0;
                padding: 0;
            }
            body.print-thermal .invoice {
                width: 80mm;
                max-width: 80mm;
                box-shadow: none;
                border-radius: 0;
                margin: 0 auto;
                padding: 0;
                font-size: 10px;
                font-family: 'Courier New', monospace;
            }
            body.print-thermal .invoice-header {
                background: none !important;
                color: black;
                text-align: center;
                padding: 1mm 2mm 2mm;
                border-bottom: 1px dashed #000;
                flex-direction: column;
                gap: 1px;
            }
            body.print-thermal .company-info h1 {
                font-size: 14px;
                margin: 0;
                line-height: 1.2;
            }
            body.print-thermal .company-info .address {
                font-size: 10px;
                opacity: 1;
                margin: 0;
                line-height: 1.2;
            }
            body.print-thermal .invoice-badge {
                text-align: center;
                margin-top: 2px;
            }
            body.print-thermal .invoice-badge .label,
            body.print-thermal .invoice-badge .number {
                font-size: 12px;
                display: inline;
                margin: 0;
            }
            body.print-thermal .invoice-body {
                padding: 2mm 2mm 0;
            }
            body.print-thermal .meta-row {
                flex-direction: column;
                gap: 0;
                border-bottom: 1px dashed #000;
                padding-bottom: 1mm;
                margin-bottom: 2mm;
            }
            body.print-thermal .meta-box {
                flex: none;
                min-width: auto;
                display: flex;
                justify-content: space-between;
                font-size: 10px;
                margin: 0;
                line-height: 1.3;
            }
            body.print-thermal .meta-box h3 {
                font-size: 10px;
                border: none;
                margin: 0;
                padding: 0;
                display: inline;
                font-weight: bold;
            }
            body.print-thermal .meta-box p {
                font-size: 10px;
                display: inline;
                margin: 0;
                line-height: 1.3;
            }
            body.print-thermal .items-table {
                font-size: 10px;
                width: 100%;
            }
            body.print-thermal .items-table th,
            body.print-thermal .items-table td {
                padding: 2mm 1mm;
                border-color: #000;
            }
            body.print-thermal .footer {
                display: none;
            }
            @page {
                size: 80mm auto;
                margin: 0;
            }
        }
    </style>
</head>
<body>
<div class="invoice">
    <div class="invoice-header">
           <div class="company-info">
            <h1><?php echo htmlspecialchars($tx); ?></h1>
            <div class="address">
                Shop No 82<br>
                📞 03212121294 | 03362121292
            </div>
        </div>
        <div class="invoice-badge">
            <div class="label">Gate Pass</div>
            <div class="number">#<?php echo $orderId; ?></div>
        </div>
    </div>

    <div class="invoice-body">
        <?php if ($orderId && !empty($orderItems)): ?>
        <!-- Meta Information -->
        <div class="meta-row">
            <div class="meta-box">
                <h3>Date & Time</h3>
                <p><?php echo htmlspecialchars($dateTime); ?></p>
            </div>
            <div class="meta-box">
                <h3>Customer</h3>
                <p>
                    <?php if($customerName): ?>
                        <?php echo htmlspecialchars($customerName); ?><br>
                        <small><?php echo htmlspecialchars($customerPhone); ?></small>
                    <?php else: ?>
                        Walk‑in Customer
                    <?php endif; ?>
                </p>
            </div>
            <div class="meta-box">
                <h3>City & Transport</h3>
                <p><?php echo htmlspecialchars($city) . ' - ' . htmlspecialchars($transport); ?></p>
            </div>
        </div>

        <!-- Items Table -->
        <div class="items-section">
            <h3>Items</h3>
            <table class="items-table">
                <thead>
                    <tr>
                        <th>#</th>
                 <th>Package</th>
                        <th>Qty</th>
                        
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($orderItems as $index => $item): ?>
                    <tr>
                        <td><?php echo $index + 1; ?></td>
                        <td><?php echo htmlspecialchars($item['package']); ?></td>
                        <!--<td><?php echo htmlspecialchars($item['item_name']); ?></td>-->
                        <td><?php echo htmlspecialchars($item['qty']); ?></td>
                        
                    </tr>
                    <?php endforeach; ?>
                    <tr class="total-row">
                        <td colspan="2"><strong>Total Qty</strong></td>
                        <td colspan="2"><strong><?php echo htmlspecialchars($totalQty); ?></strong></td>
                    </tr>
                </tbody>
            </table>
        </div>

        <div class="footer">
            Generated by: <?php echo htmlspecialchars($userName); ?><br>
            Authorized Gate Pass – No Financial Value<br>
            This is a computer‑generated document.
        </div>
        <?php else: ?>
        <p style="text-align:center; color:#ef4444;">No gate pass found for Order ID: <?php echo htmlspecialchars($orderId); ?></p>
        <?php endif; ?>
    </div>

    <!-- Action Buttons (hidden when printing) -->
    <div class="button-group no-print">
        <button class="btn btn-primary" id="printTrigger">
            🖨️ Print Gate Pass
        </button>
        <button class="btn btn-secondary" onclick="window.location.href='view_gatepass.php'">
            ✖️ Exit
        </button>
    </div>
</div>

<!-- Print options modal -->
<div id="printModal" class="modal no-print">
    <div class="modal-dialog">
        <h2>Select Print Type</h2>
        <p>Choose your printer format:</p>
        <div class="modal-buttons">
            <button class="modal-btn thermal" onclick="selectPrint('thermal')">🖨️ Thermal Receipt (80mm)</button>
            <button class="modal-btn a4" onclick="selectPrint('a4')">📄 A4 / A5 Sheet</button>
        </div>
        <button class="modal-close" onclick="closeModal()">Cancel</button>
    </div>
</div>

<script>
    document.getElementById('printTrigger').addEventListener('click', function(e) {
        e.preventDefault();
        document.getElementById('printModal').classList.add('active');
    });

    function closeModal() {
        document.getElementById('printModal').classList.remove('active');
    }

    function selectPrint(type) {
        if (type === 'thermal') {
            document.body.classList.add('print-thermal');
        } else {
            document.body.classList.remove('print-thermal');
        }
        document.getElementById('printModal').classList.remove('active');
        window.print();
    }

    window.addEventListener('afterprint', function() {
        document.body.classList.remove('print-thermal');
    });

    document.addEventListener('keydown', function(e) {
        if ((e.ctrlKey || e.metaKey) && e.key === 'p') {
            e.preventDefault();
            document.getElementById('printModal').classList.add('active');
        }
    });
</script>

    <link rel="stylesheet" href="../css/responsive-mobile.css">
    <script src="../js/responsive-mobile.js"></script>
</body>
</html>