<?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 with master color, panel, and approval information
    $query = "
        SELECT 
            csi.id,
            csi.kode_barang,
            csi.stock_number,
            csi.status,
            csi.lokasi,
            csi.rack_no,
            csi.qr_code,
            csi.item_type,
            csi.tanggal_pembuatan,
            csi.tanggal_kedaluwarsa,
            csi.keterangan,
            cc.component_name,
            csi.component_id,
            csi.color_id,
            mcp.code_color as panel_code,
            mcp.deskripsi as panel_description,
            mcp.lemari,
            mcp.panel_type,
            mc.code_color as master_code,
            mc.name_color as master_name,
            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
        LEFT JOIN master_color mc ON mcp.master_color_id = mc.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();
    }
    
    // Get approval data with signatures from approval_history
    $approval_query = "
        SELECT 
            ca.id as approval_id,
            ca.ka_unit_signature,
            ca.color_room_signature,
            ca.akzonobel_signature,
            ca.approved_by_signature,
            ca.qc_signature,
            au_ka.nama_lengkap as ka_unit_name,
            au_cr.nama_lengkap as color_room_name,
            au_ak.nama_lengkap as akzonobel_name,
            au_ap.nama_lengkap as approved_by_name,
            au_qc.nama_lengkap as qc_name
        FROM color_approvals ca
        LEFT JOIN approval_users au_ka ON ca.ka_unit_approved_by = au_ka.id
        LEFT JOIN approval_users au_cr ON ca.color_room_approved_by = au_cr.id
        LEFT JOIN approval_users au_ak ON ca.akzonobel_approved_by = au_ak.id
        LEFT JOIN approval_users au_ap ON ca.approved_by_approved_by = au_ap.id
        LEFT JOIN approval_users au_qc ON ca.qc_approved_by = au_qc.id
        WHERE ca.color_id = ? AND ca.component_id = ?
        LIMIT 1
    ";
    
    $stmt = $conn->prepare($approval_query);
    $stmt->bind_param("ii", $item['color_id'], $item['component_id']);
    $stmt->execute();
    $approval_result = $stmt->get_result();
    $approval_data = $approval_result->fetch_assoc();
    $stmt->close();
    
} 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;
        }
        
        .item-type-badge {
            display: inline-block;
            padding: 8px 20px;
            border-radius: 20px;
            font-size: 14px;
            font-weight: bold;
            margin-bottom: 15px;
        }
        
        .badge-master {
            background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
            color: white;
        }
        
        .badge-copy {
            background: linear-gradient(135deg, #10b981 0%, #059669 100%);
            color: white;
        }
        
        .color-code-title {
            font-size: 28px;
            font-weight: bold;
            margin-bottom: 10px;
            color: #333;
        }
        
        .master-reference {
            font-size: 16px;
            color: #666;
            margin-bottom: 10px;
        }
        
        .component-title {
            font-size: 22px;
            color: #666;
            margin-bottom: 20px;
        }
        
        .qr-section {
            margin: 30px 0;
        }
        
        .qr-code {
            margin: 15px auto;
            display: block;
            border: 4px solid #e5e7eb;
            border-radius: 8px;
            padding: 10px;
        }
        
        .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-item.empty {
            visibility: hidden;
        }
        
        .signature-label {
            font-size: 16px;
            color: #666;
            font-weight: bold;
            margin-bottom: 10px;
        }
        
        .signature-image {
            max-width: 150px;
            max-height: 60px;
            margin: 5px auto;
            display: block;
        }
        
        .signature-line {
            border-bottom: 1px solid #333;
            margin: 10px 0;
            height: 2px;
        }
        
        .signature-name {
            font-size: 14px;
            color: #888;
            margin-top: 5px;
        }
        
        @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: linear-gradient(135deg, #10b981 0%, #059669 100%);
            color: white;
            padding: 15px 30px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            font-weight: bold;
            margin-bottom: 30px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .print-button:hover {
            background: linear-gradient(135deg, #059669 0%, #047857 100%);
            transform: translateY(-2px);
            box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
        }
    </style>
</head>
<body>
    <div>
        <button class="print-button no-print" onclick="window.print()">
            🖨️ Print QR Code
        </button>
        
        <div class="print-container">
            <!-- Item Type Badge -->
            <div class="header">
                <span class="item-type-badge <?php echo $item['item_type'] === 'master_color' ? 'badge-master' : 'badge-copy'; ?>">
                    <?php echo $item['item_type'] === 'master_color' ? '👑 MASTER COLOR' : '📋 COPY PANEL'; ?>
                </span>
                
                <!-- Panel Code and Buyer -->
                <div class="color-code-title">
                    <?php echo htmlspecialchars($item['panel_code']); ?> - <?php echo htmlspecialchars($item['nama_buyer']); ?>
                </div>
                
                <!-- Master Color Reference (if exists) -->
                <?php if (!empty($item['master_code'])): ?>
                <div class="master-reference">
                    🔗 Master Color: <?php echo htmlspecialchars($item['master_code']); ?> (<?php echo htmlspecialchars($item['master_name']); ?>)
                </div>
                <?php endif; ?>
                
                <!-- Component and Item Code -->
                <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">Panel Code:</div>
                    <div class="detail-value"><?php echo htmlspecialchars($item['panel_code']); ?></div>
                </div>
                
                <div class="detail-item">
                    <div class="detail-label">Buyer:</div>
                    <div class="detail-value"><?php echo htmlspecialchars($item['nama_buyer']); ?></div>
                </div>
                
                <?php if (!empty($item['master_code'])): ?>
                <div class="detail-item">
                    <div class="detail-label">Master Color:</div>
                    <div class="detail-value"><?php echo htmlspecialchars($item['master_code']); ?></div>
                </div>
                <?php endif; ?>
                
                <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">Code Barang:</div>
                    <div class="detail-value"><?php echo htmlspecialchars($item['kode_barang']); ?></div>
                </div>
                
                <div class="detail-item">
                    <div class="detail-label">Item Type:</div>
                    <div class="detail-value"><?php echo $item['item_type'] === 'master_color' ? 'Master Color' : 'Copy Panel'; ?></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">Rack No:</div>
                    <div class="detail-value"><?php echo htmlspecialchars($item['rack_no']); ?></div>
                </div>
                
                <div class="detail-item">
                    <div class="detail-label">Tgl Pembuatan:</div>
                    <div class="detail-value"><?php echo $item['tanggal_pembuatan'] ? date('d-m-Y', strtotime($item['tanggal_pembuatan'])) : '-'; ?></div>
                </div>
                
                <div class="detail-item">
                    <div class="detail-label">Tgl Kedaluwarsa:</div>
                    <div class="detail-value"><?php echo $item['tanggal_kedaluwarsa'] ? date('d-m-Y', strtotime($item['tanggal_kedaluwarsa'])) : '-'; ?></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>
                        <?php if ($approval_data && !empty($approval_data['ka_unit_signature'])): ?>
                            <img src="<?php echo $approval_data['ka_unit_signature']; ?>" alt="Signature" class="signature-image">
                        <?php endif; ?>
                        <div class="signature-line"></div>
                        <div class="signature-name">
                            <?php echo $approval_data && $approval_data['ka_unit_name'] ? htmlspecialchars($approval_data['ka_unit_name']) : 'Y Andi S'; ?>
                        </div>
                    </div>
                    
                    <div class="signature-item">
                        <div class="signature-label">Color Room</div>
                        <?php if ($approval_data && !empty($approval_data['color_room_signature'])): ?>
                            <img src="<?php echo $approval_data['color_room_signature']; ?>" alt="Signature" class="signature-image">
                        <?php endif; ?>
                        <div class="signature-line"></div>
                        <div class="signature-name">
                            <?php echo $approval_data && $approval_data['color_room_name'] ? htmlspecialchars($approval_data['color_room_name']) : 'Dimas Ardi'; ?>
                        </div>
                    </div>
                    
                    <div class="signature-item">
                        <div class="signature-label">Akzonobel</div>
                        <?php if ($approval_data && !empty($approval_data['akzonobel_signature'])): ?>
                            <img src="<?php echo $approval_data['akzonobel_signature']; ?>" alt="Signature" class="signature-image">
                        <?php endif; ?>
                        <div class="signature-line"></div>
                        <div class="signature-name">
                            <?php echo $approval_data && $approval_data['akzonobel_name'] ? htmlspecialchars($approval_data['akzonobel_name']) : 'Asmari'; ?>
                        </div>
                    </div>
                </div>
                
                <!-- Second Row -->
                <div class="signature-row">
                    <div class="signature-item">
                        <div class="signature-label">Approved by</div>
                        <?php if ($approval_data && !empty($approval_data['approved_by_signature'])): ?>
                            <img src="<?php echo $approval_data['approved_by_signature']; ?>" alt="Signature" class="signature-image">
                        <?php endif; ?>
                        <div class="signature-line"></div>
                        <div class="signature-name">
                            <?php echo $approval_data && $approval_data['approved_by_name'] ? htmlspecialchars($approval_data['approved_by_name']) : 'Eris'; ?>
                        </div>
                    </div>
                    
                    <div class="signature-item">
                        <div class="signature-label">Approved by QC</div>
                        <?php if ($approval_data && !empty($approval_data['qc_signature'])): ?>
                            <img src="<?php echo $approval_data['qc_signature']; ?>" alt="Signature" class="signature-image">
                        <?php endif; ?>
                        <div class="signature-line"></div>
                        <div class="signature-name">
                            <?php echo $approval_data && $approval_data['qc_name'] ? htmlspecialchars($approval_data['qc_name']) : 'Suhari yanto'; ?>
                        </div>
                    </div>
                    
                    <!-- Empty placeholder to maintain grid layout -->
                    <div class="signature-item empty">
                        <div class="signature-label">&nbsp;</div>
                        <div class="signature-line"></div>
                        <div class="signature-name">&nbsp;</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        // Close window after printing (optional)
        window.onafterprint = function() {
            // window.close();
        }
    </script>
</body>
</html>

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