<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Barcode Label - {{ $product->name }} - {{ \App\Models\Setting::get('site_name', 'KiyoKart') }}</title>
    <link rel="stylesheet" href="{{ asset('css/bootstrap-icons.css') }}">
    <style>
        @media print {
            @page {
                size: 50mm 25mm;
                margin: 0;
            }
            body {
                margin: 0;
                padding: 0;
            }
            .no-print {
                display: none !important;
            }
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: Arial, sans-serif;
            width: 50mm;
            height: 25mm;
            padding: 0;
            margin: 0;
            display: flex;
            flex-direction: row;
            background: white;
            overflow: hidden;
        }
        
        .label-container {
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: row;
            align-items: stretch;
        }
        
        .logo-section {
            width: 19mm;
            height: 25mm;
            display: flex;
            align-items: center;
            justify-content: center;
            background: white;
            position: relative;
        }
        
        .logo-wrapper {
            width: 25mm;
            height: 19mm;
            display: flex;
            align-items: center;
            justify-content: center;
            transform: rotate(90deg);
            transform-origin: center;
        }
        
        .logo-image {
            max-width: 19mm;
            max-height: 25mm;
            width: auto;
            height: auto;
            object-fit: contain;
        }
        
        .details-section {
            width: 31mm;
            height: 25mm;
            display: flex;
            flex-direction: column;
            justify-content: flex-start;
            align-items: center;
            padding: 1mm 1mm 1mm 1mm; 
            gap: 0.5mm;
            background: white;
        }
        
        .product-name {
            font-size: 9px;
            font-weight: bold;
            color: #000;
            line-height: 1.1;
            text-align: center;
            width: 100%;
            overflow: clip;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        
        .product-size {
            font-size: 8px;
            font-weight: bold;
            color: #000;
            line-height: 1.1;
            text-align: center;
            width: 100%;
        }
        
        .barcode-section {
            width: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            gap: 0.5mm;
        }
        
        .barcode-image {
            max-width: 25mm;
            width: 100%;
            height: auto;
            max-height: 9mm;
        }
        
        .barcode-number {
            font-size: 9px;
            color: #333;
            font-family: 'Courier New', monospace;
            letter-spacing: 1px;
            font-weight: bold;
            line-height: 1;
            text-align: center;
        }
        
        .prices-section {
            width: 100%;
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            align-items: center;
            font-size: 7px;
            line-height: 1.2;
            font-weight: bold;
            color: #000;
            margin-top: 0.5mm;
        }
        
        .price-item {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 0.5mm;
        }
        
        .price-label {
            font-size: 5px;
            font-weight: bold;
            color: #000;
        }

        .strong{
            font-size: 10px !important;
        }

        .price-value {
            font-size: 9px;
            font-weight: bold;
            color: #000;
        } 
        
        .print-controls {
            position: fixed;
            top: 10px;
            right: 10px;
            z-index: 1000;
            display: flex;
            gap: 10px;
        }
        
        .btn {
            padding: 8px 16px;
            background: #13233c;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            text-decoration: none;
            display: inline-block;
        }
        
        .btn:hover {
            background: #F28C28;
        }
        
        .btn-secondary {
            background: #6c757d;
        }
        
        .btn-secondary:hover {
            background: #5a6268;
        }
    </style>
</head>
<body>
    <div class="print-controls no-print">
        <button onclick="window.print()" class="btn">
            <i class="bi bi-printer"></i> Print
        </button>
        <a href="{{ route('admin.products.index') }}" class="btn btn-secondary">
            <i class="bi bi-arrow-left"></i> Back
        </a>
    </div>
    
    @if($product->product_type === 'variant' && !$variant && $product->variants && $product->variants->isNotEmpty())
        <!-- Variant Selection for Variant Products -->
        <div class="variant-selection no-print" style="max-width: 600px; margin: 20px auto; padding: 20px; background: #f8f9fa; border-radius: 8px;">
            <h3 style="margin-bottom: 15px; color: #13233c;">Select Variant to Print</h3>
            <div class="variants-list" style="display: grid; gap: 10px;">
                @foreach($product->variants->where('is_active', true) as $v)
                    <a href="{{ route('admin.products.barcode.print', ['product' => $product->id, 'variant_id' => $v->id]) }}" 
                       class="variant-item" 
                       style="display: block; padding: 12px; background: white; border: 2px solid #dee2e6; border-radius: 6px; text-decoration: none; color: #333; transition: all 0.2s;">
                        <div style="font-weight: 600; margin-bottom: 5px;">{{ $v->variation_name }}</div>
                        <div style="font-size: 0.85em; color: #666;">
                            <span>Barcode: {{ $v->barcode ?? $product->barcode ?? 'N/A' }}</span> | 
                            <span>Price: ₹{{ number_format($v->price, 2) }}</span>
                        </div>
                    </a>
                @endforeach
            </div>
            <div style="margin-top: 15px; text-align: center;">
                <a href="{{ route('admin.products.index') }}" class="btn btn-secondary">Back to Products</a>
            </div>
        </div>
    @else
        <!-- Barcode Label -->
        <div class="label-container">
            
            <!-- Right Section: Details (31mm) -->
            <div class="details-section">
                <div class="product-name">
                    {{ $product->name }}
                </div>
                
                @if(!empty($variantAttributes))
                    <div class="product-size">
                        @foreach($variantAttributes as $attr)
                            {{ $attr['value'] }}@if(!$loop->last) | @endif
                        @endforeach
                    </div>
                @endif
                
                <div class="barcode-section">
                    <img src="{{ $barcodeImage }}" alt="Barcode {{ $barcodeValue }}" class="barcode-image">
                    <div class="barcode-number">@if($wholesalePrice) S{{ (int) $wholesalePrice }} | @endif {{ $barcodeValue }}</div>
                </div>
                
                <div class="prices-section">
                    @if($mrp)
                        <div class="price-item">
                            <div class="price-label strong">MRP: {{ number_format($mrp, 0) }}</div>
                        </div>
                    @endif
                    @if($salesPrice)
                        <div class="price-item">
                            <div class="price-label strong">W.P: {{ number_format($salesPrice, 0) }}</div>
                        </div>
                    @endif
                </div>
            </div>
        </div>
        <!-- Left Section: White Space (19mm) - Logo pre-printed on label -->
        <div class="logo-section">
            <!-- Logo is pre-printed on the label, keeping white space -->
        </div>
    @endif
    
    <script>
        // Auto-trigger print dialog when page loads (optional)
        // Uncomment the line below if you want auto-print
        // window.onload = function() { window.print(); };
    </script>
</body>
</html>

