<?php
include("../db/cn.php");
include("common/tx.php");
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;
$orderDetails = [];
$dateTime = $loginUser = $userRole = $customerName = $customerPhone = $city = $tranport = $desc_note = '';

if ($orderId) {
    $query = "SELECT * FROM log_user_sales WHERE order_id = '$orderId'";
    $result = mysqli_query($connection, $query);

    if ($result && mysqli_num_rows($result) > 0) {
        $orderDetails = mysqli_fetch_all($result, MYSQLI_ASSOC);
        $first = $orderDetails[0];
        $dateTime = $first['date_time'];
        $loginUser = $first['login_user'];
        $userRole = $first['user_role'];
        $customerName = $first['customer_name'];
        $customerPhone = $first['customer_phone'];
        $city = $first['city'];
        $tranport = $first['transport'];
        $desc_note = $first['desc_note'] ?? '';   // get the description note
    }
}

$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;
        }

        /* ---- new style for desc_note section ---- */
        .desc-note-section {
            margin-top: 20px;
            padding: 20px;
            background: #f8fafc;
            border: 1px solid var(--border);
            border-radius: 8px;
        }
        .desc-note-section h3 {
            font-size: 11px;
            text-transform: uppercase;
            letter-spacing: 1px;
            color: var(--text-muted);
            margin-bottom: 10px;
        }
        .desc-note-section p {
            font-size: 14px;
            line-height: 1.6;
            white-space: pre-wrap; /* respects line breaks */
        }

        .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 .desc-note-section {
                border: none;
                padding: 0;
                background: none;
                margin-top: 2mm;
                font-size: 10px;
            }
            body.print-thermal .desc-note-section h3 {
                font-size: 10px;
                margin-bottom: 1mm;
            }
            body.print-thermal .desc-note-section p {
                font-size: 10px;
                line-height: 1.3;
            }
            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">
                Liaqut Chowck, Lahore<br>
                📞 03322138429 | 03362121295
            </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">
        <!-- Meta Information -->
        <div class="meta-row">
            <div class="meta-box">
                <h3>Date & Time</h3>
                <p><?php echo $dateTime; ?></p>
            </div>
           
            <div class="meta-box">
                <h3>Customer</h3>
                <p>
                    <?php if($customerName && $customerName != 'walkin'): ?>
                        <?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($tranport); ?></p>
            </div>
        </div>

        <!-- Description Note (replaces the items table) -->
        <div class="desc-note-section">
            <h3>Description Note</h3>
            <p><?php echo !empty($desc_note) ? nl2br(htmlspecialchars($desc_note)) : '<em>No description provided.</em>'; ?></p>
        </div>

        <!-- Footer -->
        <div class="footer">
            Authorized Gate Pass – No Financial Value<br>
            This is a computer‑generated document.
        </div>
    </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='order_detail_v.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>