<?php
// Start session
session_start();

// Include database connection
require_once "../config/connection.php";

// Check if user is logged in and is IT user
if (!is_logged_in() || !is_user_type('it')) {
    header("Location: ../login.php");
    exit();
}

// Check if item_id is provided
if (!isset($_GET['item_id']) || empty($_GET['item_id'])) {
    echo "Item ID is required";
    exit();
}

$item_id = (int)$_GET['item_id'];

try {
    // Get complete item data
    $query = "
        SELECT 
            csi.id,
            csi.kode_barang,
            csi.stock_number,
            csi.status,
            csi.lokasi,
            csi.rack_no,
            csi.qr_code,
            csi.tanggal_pembuatan,
            csi.tanggal_kedaluwarsa,
            csi.keterangan,
            cc.component_name,
            mcp.code_color,
            mcp.deskripsi as color_description,
            mcp.lemari,
            b.nama_buyer,
            b.kode_buyer
        FROM color_stock_items csi
        JOIN color_components cc ON csi.component_id = cc.id
        JOIN master_color_panel mcp ON csi.color_id = mcp.id
        JOIN buyers b ON mcp.buyer_id = b.id
        WHERE csi.id = ?
    ";
    
    $stmt = $conn->prepare($query);
    $stmt->bind_param("i", $item_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows === 0) {
        echo "Item not found";
        exit();
    }
    
    $item = $result->fetch_assoc();
    $stmt->close();
    
    // Check if QR code exists
    if (empty($item['qr_code'])) {
        echo "QR code not found for this item";
        exit();
    }
    
    // Format dates
    $date_range = '';
    if ($item['tanggal_pembuatan'] && $item['tanggal_kedaluwarsa']) {
        $date_range = date('Y-m-d', strtotime($item['tanggal_pembuatan'])) . ' - ' . date('Y-m-d', strtotime($item['tanggal_kedaluwarsa']));
    }
    
} catch (Exception $e) {
    echo "Database error: " . $e->getMessage();
    exit();
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Print QR Code - <?php echo htmlspecialchars($item['kode_barang']); ?></title>
    <style>
        @page {
            size: A4 portrait;
            margin: 10mm;
        }
        
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background-color: #f5f5f5;
        }
        
        .print-container {
            background: white;
            border: none;
            border-radius: 8px;
            padding: 40px;
            width: 100%;
            max-width: 700px;
            text-align: center;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            height: fit-content;
        }
        
        .header {
            margin-bottom: 30px;
        }
        
        .buyer-title {
            font-size: 28px;
            font-weight: bold;
            margin-bottom: 15px;
            color: #333;
        }
        
        .component-title {
            font-size: 22px;
            color: #666;
            margin-bottom: 20px;
        }
        
        .qr-section {
            margin: 30px 0;
        }
        
        .qr-code {
            margin: 15px auto;
            display: block;
        }
        
        .details-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
            margin: 20px 0;
            text-align: left;
            font-size: 18px;
        }
        
        .detail-item {
            margin-bottom: 6px;
            border-bottom: none;
        }
        
        .detail-label {
            font-weight: bold;
            color: #333;
            font-size: 18px;
            border-bottom: none;
        }
        
        .detail-value {
            color: #666;
            margin-top: 4px;
            font-size: 16px;
            border-bottom: none;
        }
        
        .signatures {
            margin-top: 20px;
            border-top: none;
            padding-top: 15px;
        }
        
        .signature-row {
            display: grid;
            grid-template-columns: 1fr 1fr 1fr;
            gap: 40px;
            margin-bottom: 35px;
            font-size: 16px;
        }
        
        .signature-row:last-child {
            margin-bottom: 25px;
        }
        
        .signature-item {
            text-align: center;
            padding: 8px 0;
        }
        
        .signature-line {
            border-bottom: 1px solid #333;
            margin: 40px 0 20px 0;
            height: 2px;
        }
        
        .signature-label {
            font-size: 16px;
            color: #666;
            font-weight: bold;
            margin-bottom: 45px;
        }
        
        .signature-name {
            font-size: 14px;
            color: #888;
            margin-top: 12px;
        }
        
        @media print {
            body {
                background-color: white;
                padding: 0;
                margin: 0;
            }
            
            .print-container {
                box-shadow: none;
                border: none;
                margin: 0;
                max-width: none;
                width: auto;
                height: auto;
                page-break-inside: avoid;
            }
            
            .no-print {
                display: none;
            }
        }
        
        .print-button {
            background-color: #4CAF50;
            color: white;
            padding: 15px 30px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            margin-bottom: 30px;
        }
        
        .print-button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <div>
        <button class="print-button no-print" onclick="window.print()">
            🖨️ Print QR Code
        </button>
        
        <div class="print-container">
            <!-- Header -->
            <div class="header">
                <div class="buyer-title">
                    <?php echo htmlspecialchars($item['nama_buyer']); ?> - <?php echo htmlspecialchars($item['code_color']); ?>
                </div>
                <div class="component-title">
                    <?php echo htmlspecialchars($item['component_name']); ?> (<?php echo htmlspecialchars($item['kode_barang']); ?>)
                </div>
            </div>
            
            <!-- QR Code Section -->
            <div class="qr-section">
                <img src="<?php echo $item['qr_code']; ?>" alt="QR Code" class="qr-code" style="width: 250px; height: 250px;">
            </div>
            
            <!-- Details Grid -->
            <div class="details-grid">
                <div class="detail-item">
                    <div class="detail-label">Buyer:</div>
                    <div class="detail-value"><?php echo htmlspecialchars($item['nama_buyer']); ?></div>
                </div>
                
                <div class="detail-item">
                    <div class="detail-label">Lokasi:</div>
                    <div class="detail-value"><?php echo htmlspecialchars($item['lokasi']); ?></div>
                </div>
                
                <div class="detail-item">
                    <div class="detail-label">Color Code:</div>
                    <div class="detail-value"><?php echo htmlspecialchars($item['code_color']); ?></div>
                </div>
                
                <div class="detail-item">
                    <div class="detail-label">Rack No:</div>
                    <div class="detail-value"><?php echo htmlspecialchars($item['rack_no']); ?></div>
                </div>
                
                <div class="detail-item">
                    <div class="detail-label">Component:</div>
                    <div class="detail-value"><?php echo htmlspecialchars($item['component_name']); ?></div>
                </div>
                
                <div class="detail-item">
                    <div class="detail-label">Status:</div>
                    <div class="detail-value"><?php echo htmlspecialchars($item['status']); ?></div>
                </div>
                
                <div class="detail-item">
                    <div class="detail-label">Code Barang:</div>
                    <div class="detail-value"><?php echo htmlspecialchars($item['kode_barang']); ?></div>
                </div>
                
                <div class="detail-item">
                    <div class="detail-label">Tanggal:</div>
                    <div class="detail-value"><?php echo $date_range; ?></div>
                </div>
            </div>
            
            <!-- Signatures Section -->
            <div class="signatures">
                <!-- First Row -->
                <div class="signature-row">
                    <div class="signature-item">
                        <div class="signature-label">K A Unit</div>
                        <div class="signature-line"></div>
                        <div class="signature-name">Y Andi S</div>
                    </div>
                    
                    <div class="signature-item">
                        <div class="signature-label">Color Room</div>
                        <div class="signature-line"></div>
                        <div class="signature-name">Alifi A.U</div>
                    </div>
                    
                    <div class="signature-item">
                        <div class="signature-label">Akzonobel</div>
                        <div class="signature-line"></div>
                        <div class="signature-name">Asmari</div>
                    </div>
                </div>
                
                <!-- Second Row -->
                <div class="signature-row">
                    <div class="signature-item">
                        <div class="signature-label">Approved by</div>
                        <div class="signature-line"></div>
                        <div class="signature-name">Eris</div>
                    </div>
                    
                    <div class="signature-item">
                        <div class="signature-label">Approved by QC</div>
                        <div class="signature-line"></div>
                        <div class="signature-name">Suhari yanto</div>
                    </div>
                    
                    <div class="signature-item">
                        <div class="signature-label">Approved by</div>
                        <div class="signature-line"></div>
                        <div class="signature-name">QC Buyer</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        // Auto print when page loads (optional)
        // window.onload = function() {
        //     window.print();
        // }
        
        // Close window after printing
        window.onafterprint = function() {
            // window.close();
        }
    </script>
</body>
</html>

<?php $conn->close(); ?>