<?php
session_start();
require_once 'config/database.php';
require_once 'includes/auth.php';
require_once 'includes/functions.php';

requireAdmin();

$id = intval($_GET['id'] ?? 0);

if ($id <= 0) {
    die('ID booking tidak valid');
}

// Get booking data
$sql = "SELECT b.*, u.nama as admin_nama 
        FROM booking b 
        JOIN users u ON b.id_user = u.id 
        WHERE b.id = ?";
$result = query($sql, [$id], 'i');

if ($result->num_rows === 0) {
    die('Booking tidak ditemukan');
}

$booking = $result->fetch_assoc();

// Get booking details
$sql_detail = "SELECT * FROM booking_detail WHERE id_booking = ?";
$result_detail = query($sql_detail, [$id], 'i');

$details = [];
while ($row = $result_detail->fetch_assoc()) {
    $details[] = $row;
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bukti Booking #<?php echo $id; ?> - Umbul Tirtomarto V2</title>
    <script src="includes/thermal-printer.js"></script>
    <script src="includes/printer-manager.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Courier New', monospace;
            font-size: 12px;
            line-height: 1.4;
            padding: 10px;
        }
        
        .struk {
            max-width: 300px;
            margin: 0 auto;
        }
        
        .header {
            text-align: center;
            margin-bottom: 10px;
            border-bottom: 2px dashed #000;
            padding-bottom: 10px;
        }
        
        .header h1 {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 3px;
        }
        
        .header .subtitle {
            font-size: 14px;
            font-weight: bold;
            margin: 5px 0;
        }
        
        .header p {
            font-size: 11px;
            margin: 2px 0;
        }
        
        .info {
            margin: 10px 0;
            font-size: 11px;
        }
        
        .info-row {
            display: flex;
            justify-content: space-between;
            margin: 3px 0;
        }
        
        .section-title {
            font-weight: bold;
            margin-top: 10px;
            margin-bottom: 5px;
            font-size: 11px;
            text-transform: uppercase;
        }
        
        .items {
            margin: 10px 0;
            border-top: 2px dashed #000;
            border-bottom: 2px dashed #000;
            padding: 10px 0;
        }
        
        .item {
            margin: 5px 0;
        }
        
        .item-name {
            font-weight: bold;
        }
        
        .item-detail {
            display: flex;
            justify-content: space-between;
            font-size: 11px;
            margin-top: 2px;
        }
        
        .payment-section {
            margin: 10px 0;
            font-size: 11px;
        }
        
        .payment-row {
            display: flex;
            justify-content: space-between;
            margin: 3px 0;
        }
        
        .payment-row.highlight {
            font-weight: bold;
            font-size: 12px;
        }
        
        .payment-row.total {
            font-size: 14px;
            font-weight: bold;
            border-top: 2px solid #000;
            padding-top: 5px;
            margin-top: 5px;
        }
        
        .payment-row.sisa {
            font-size: 14px;
            font-weight: bold;
            border-top: 1px solid #000;
            padding-top: 5px;
            margin-top: 5px;
        }
        
        .footer {
            text-align: center;
            margin-top: 15px;
            font-size: 10px;
            border-top: 2px dashed #000;
            padding-top: 10px;
        }
        
        .footer p {
            margin: 3px 0;
        }
        
        .no-print {
            text-align: center;
            margin: 20px 0;
        }
        
        .btn {
            padding: 10px 20px;
            margin: 5px;
            font-size: 14px;
            cursor: pointer;
            border: none;
            border-radius: 5px;
        }
        
        .btn-primary {
            background-color: #0ea5e9;
            color: white;
        }
        
        .btn-secondary {
            background-color: #6b7280;
            color: white;
        }
        
        .btn-bluetooth {
            background-color: #3b82f6;
            color: white;
        }
        
        .btn-bluetooth:disabled {
            background-color: #9ca3af;
            cursor: not-allowed;
        }
        
        @media print {
            body {
                padding: 0;
                margin: 0;
                font-size: 13px;
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }
            
            .no-print {
                display: none !important;
            }
            
            .struk {
                max-width: 100%;
                margin: 0;
            }
            
            /* Ukuran kertas thermal 80mm dengan margin */
            @page {
                size: 80mm auto;
                margin: 8mm 5mm;
            }
            
            /* Font lebih jelas saat print */
            .header h1 {
                font-size: 20px;
                font-weight: 900;
                color: #000 !important;
            }
            
            .header p {
                font-size: 12px;
                font-weight: 600;
            }
            
            .item-name {
                font-weight: 900;
                font-size: 13px;
            }
            
            .grand-total {
                font-size: 16px;
                font-weight: 900;
                border-top: 3px solid #000;
            }
            
            /* Border lebih tebal */
            .header,
            .items,
            .footer {
                border-color: #000 !important;
                border-width: 2px;
            }
            
            /* Booking specific */
            .booking-info {
                font-weight: 600;
            }
            
            .payment-info {
                font-weight: 900;
                font-size: 14px;
            }
        }
    </style>
</head>
<body>
    <div class="struk" id="printArea">
        <!-- Header -->
        <div class="header">
            <h1>UMBUL TIRTOMARTO</h1>
            <p class="subtitle">BUKTI BOOKING</p>
            <p>Sehat, Alami, Berbudaya</p>
            <p>Pengging, Boyolali, Jawa Tengah</p>
        </div>
        
        <!-- Info Booking -->
        <div class="info">
            <div class="info-row">
                <span>No. Booking</span>
                <span><strong>#<?php echo str_pad($booking['id'], 6, '0', STR_PAD_LEFT); ?></strong></span>
            </div>
            <div class="info-row">
                <span>Tanggal Booking</span>
                <span><?php echo date('d/m/Y H:i', strtotime($booking['created_at'])); ?></span>
            </div>
        </div>
        
        <!-- Info Organisasi -->
        <div class="section-title">Informasi Pemesan</div>
        <div class="info">
            <div class="info-row">
                <span>Organisasi</span>
            </div>
            <div style="padding-left: 10px; margin-bottom: 3px;">
                <strong><?php echo htmlspecialchars($booking['nama_organisasi']); ?></strong>
            </div>
            <div class="info-row">
                <span>PIC</span>
                <span><?php echo htmlspecialchars($booking['nama_pic']); ?></span>
            </div>
            <div class="info-row">
                <span>No Telp/WA</span>
                <span><?php echo htmlspecialchars($booking['no_telepon']); ?></span>
            </div>
            <div class="info-row">
                <span>Tgl Kedatangan</span>
                <span><strong><?php echo date('d/m/Y', strtotime($booking['tanggal_kedatangan'])); ?></strong></span>
            </div>
        </div>
        
        <!-- Items -->
        <div class="section-title">Detail Layanan</div>
        <div class="items">
            <?php foreach ($details as $item): ?>
            <div class="item">
                <div class="item-name"><?php echo htmlspecialchars($item['nama_layanan']); ?></div>
                <div class="item-detail">
                    <span><?php echo $item['quantity']; ?> x <?php echo formatRupiah($item['harga']); ?></span>
                    <span><?php echo formatRupiah($item['subtotal']); ?></span>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        
        <!-- Payment -->
        <div class="section-title">Pembayaran</div>
        <div class="payment-section">
            <div class="payment-row">
                <span>Subtotal</span>
                <span><?php echo formatRupiah($booking['subtotal']); ?></span>
            </div>
            
            <?php if ($booking['diskon'] > 0): ?>
            <div class="payment-row">
                <span>Diskon</span>
                <span>(<?php echo formatRupiah($booking['diskon']); ?>)</span>
            </div>
            <?php endif; ?>
            
            <div class="payment-row total">
                <span>TOTAL</span>
                <span><?php echo formatRupiah($booking['total']); ?></span>
            </div>
            
            <?php if ($booking['dp'] > 0): ?>
            <div class="payment-row highlight" style="margin-top: 8px;">
                <span>DP Dibayar</span>
                <span><?php echo formatRupiah($booking['dp']); ?></span>
            </div>
            <?php endif; ?>
            
            <div class="payment-row sisa">
                <span>SISA BAYAR</span>
                <span><?php echo formatRupiah($booking['sisa']); ?></span>
            </div>
        </div>
        
        <!-- Footer -->
        <div class="footer">
            <p><strong>CATATAN PENTING:</strong></p>
            <p>Mohon bawa bukti booking ini saat kedatangan</p>
            <p>Sisa pembayaran dibayar saat kedatangan</p>
            <p>=================================</p>
            <p style="margin-top: 8px;">Terima kasih atas kepercayaan Anda</p>
            <p style="margin-top: 5px; font-size: 9px;">Wahana Kreasi Nusanta</p>
        </div>
    </div>
    
    <!-- Actions -->
    <div class="no-print">
        <button onclick="printerManager.showPrintDialog(bookingData)" class="btn btn-primary">
            🖨️ Print Thermal (Smart)
        </button>
        <button onclick="window.print()" class="btn btn-secondary">
            📄 Print Biasa
        </button>
        <button onclick="window.location.href='riwayat-booking.php'" class="btn btn-secondary">Kembali</button>
    </div>
    
    <script>
        // Data booking untuk thermal printer
        const bookingData = {
            id: <?php echo $id; ?>,
            tanggal: '<?php echo formatTanggal(date('Y-m-d')); ?>',
            kasir: '<?php echo $booking['admin_nama']; ?>',
            total: <?php echo $booking['total']; ?>,
            items: <?php echo json_encode(array_map(function($item) {
                return [
                    'nama' => $item['nama_layanan'],
                    'quantity' => $item['quantity'],
                    'harga' => $item['harga'],
                    'subtotal' => $item['subtotal']
                ];
            }, $details)); ?>,
            // Additional booking info
            subtotal: <?php echo $booking['subtotal']; ?>,
            diskon: <?php echo $booking['diskon']; ?>,
            dp: <?php echo $booking['dp']; ?>,
            sisa: <?php echo $booking['sisa']; ?>,
            organisasi: '<?php echo addslashes($booking['nama_organisasi']); ?>',
            pic: '<?php echo addslashes($booking['nama_pic']); ?>',
            no_hp: '<?php echo $booking['no_telepon']; ?>',
            tanggal_kedatangan: '<?php echo formatTanggal($booking['tanggal_kedatangan']); ?>'
        };
        
        console.log('[Print Booking] Data loaded:', bookingData);
        console.log('[Print Booking] Print method detected:', window.printerManager ? printerManager.detectedMethod : 'unknown');
    </script>
</body>
</html>
