@extends('layouts.winemaker')

@section('title', 'Create Blended Wine - Winemaker')

@section('content')
<style>
    .page-header {
        margin-bottom: 2rem;
        text-align: center;
    }

    .page-title {
        font-size: 2rem;
        font-weight: 700;
        color: #2d3748;
        margin-bottom: 0.5rem;
    }

    .breadcrumb {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        color: #718096;
        font-size: 0.875rem;
    }

    .breadcrumb a {
        color: #4a5568;
        text-decoration: none;
        transition: color 0.2s;
    }

    .breadcrumb a:hover {
        color: #2d3748;
    }

    .content-card {
        background: white;
        border-radius: 12px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
        padding: 2rem;
        margin-bottom: 2rem;
    }

    .card-title {
        font-size: 1.25rem;
        font-weight: 600;
        color: #2d3748;
        margin-bottom: 1.5rem;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .harvests-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
        gap: 1.5rem;
        margin-bottom: 2rem;
    }

    .harvest-card {
        border: 2px solid #e2e8f0;
        border-radius: 12px;
        padding: 1.5rem;
        transition: all 0.2s;
        cursor: pointer;
        position: relative;
    }

    .harvest-card:hover {
        border-color: #89b4d9;
        box-shadow: 0 4px 12px rgba(137, 180, 217, 0.2);
    }

    .harvest-card.selected {
        border-color: #89b4d9;
        background: linear-gradient(135deg, #f7fafc 0%, #edf2f7 100%);
    }

    .harvest-checkbox {
        position: absolute;
        top: 1rem;
        right: 1rem;
        width: 24px;
        height: 24px;
        cursor: pointer;
    }

    .harvest-header {
        margin-bottom: 1rem;
        padding-right: 2rem;
    }

    .harvest-id {
        font-size: 1.125rem;
        font-weight: 600;
        color: #2d3748;
        margin-bottom: 0.5rem;
    }

    .harvest-date {
        font-size: 0.875rem;
        color: #718096;
    }

    .harvest-details {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
    }

    .harvest-detail-row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 0.875rem;
    }

    .detail-label {
        color: #718096;
    }

    .detail-value {
        font-weight: 600;
        color: #2d3748;
    }

    .variety-badge {
        display: inline-flex;
        align-items: center;
        gap: 0.375rem;
        padding: 0.375rem 0.75rem;
        background: linear-gradient(135deg, #ddd6fe 0%, #c4b5fd 100%);
        color: #5b21b6;
        border-radius: 6px;
        font-size: 0.875rem;
        font-weight: 500;
    }

    .weight-input-section {
        display: none;
        margin-top: 1rem;
        padding-top: 1rem;
        border-top: 2px dashed #e2e8f0;
    }

    .weight-input-section.visible {
        display: block;
    }

    .form-label {
        display: block;
        font-weight: 500;
        color: #4a5568;
        margin-bottom: 0.5rem;
        font-size: 0.875rem;
    }

    .form-input {
        width: 100%;
        padding: 0.75rem 1rem;
        border: 2px solid #e2e8f0;
        border-radius: 8px;
        font-size: 0.875rem;
        transition: all 0.2s;
    }

    .form-input:focus {
        outline: none;
        border-color: #89b4d9;
        box-shadow: 0 0 0 3px rgba(137, 180, 217, 0.1);
    }

    .form-hint {
        font-size: 0.75rem;
        color: #718096;
        margin-top: 0.25rem;
    }

    .blend-summary {
        position: sticky;
        top: 80px;
        background: white;
        border-radius: 12px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
        padding: 2rem;
    }

    .summary-row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.75rem 0;
        border-bottom: 1px solid #e2e8f0;
    }

    .summary-row:last-child {
        border-bottom: none;
        padding-top: 1rem;
        font-weight: 700;
        font-size: 1.125rem;
    }

    .summary-label {
        color: #4a5568;
    }

    .summary-value {
        font-weight: 600;
        color: #2d3748;
    }

    .percentage-bar {
        margin-top: 0.5rem;
        height: 8px;
        background: #e2e8f0;
        border-radius: 4px;
        overflow: hidden;
        position: relative;
    }

    .percentage-fill {
        height: 100%;
        background: linear-gradient(135deg, #89b4d9 0%, #a8cce5 100%);
        transition: width 0.3s;
    }

    .btn-primary {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
        width: 100%;
        padding: 0.875rem 1.5rem;
        background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
        color: white;
        border: none;
        border-radius: 8px;
        font-weight: 600;
        font-size: 1rem;
        cursor: pointer;
        transition: all 0.2s;
        margin-top: 1.5rem;
    }

    .btn-primary:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(72, 187, 120, 0.4);
    }

    .btn-primary:disabled {
        opacity: 0.5;
        cursor: not-allowed;
        transform: none;
    }

    .btn-secondary {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
        width: 100%;
        padding: 0.875rem 1.5rem;
        background: white;
        color: #4a5568;
        border: 2px solid #e2e8f0;
        border-radius: 8px;
        font-weight: 600;
        font-size: 1rem;
        cursor: pointer;
        transition: all 0.2s;
        text-decoration: none;
        margin-top: 1rem;
    }

    .btn-secondary:hover {
        background: #f7fafc;
        border-color: #cbd5e0;
    }

    .wine-form-section {
        display: none;
    }

    .wine-form-section.visible {
        display: block;
    }

    .form-group {
        margin-bottom: 1.5rem;
    }

    .form-select,
    .form-textarea {
        width: 100%;
        padding: 0.75rem 1rem;
        border: 2px solid #e2e8f0;
        border-radius: 8px;
        font-size: 0.875rem;
        transition: all 0.2s;
        font-family: inherit;
    }

    .form-select:focus,
    .form-textarea:focus {
        outline: none;
        border-color: #89b4d9;
        box-shadow: 0 0 0 3px rgba(137, 180, 217, 0.1);
    }

    .form-textarea {
        resize: vertical;
        min-height: 100px;
    }

    .form-row {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 1rem;
    }

    .error-message {
        color: #dc3545;
        font-size: 0.875rem;
        margin-top: 0.25rem;
    }

    .icon {
        width: 20px;
        height: 20px;
    }

    .empty-state {
        text-align: center;
        padding: 4rem 2rem;
    }

    .empty-icon {
        width: 80px;
        height: 80px;
        margin: 0 auto 1.5rem;
        opacity: 0.5;
    }

    .empty-title {
        font-size: 1.5rem;
        font-weight: 600;
        color: #2d3748;
        margin-bottom: 0.5rem;
    }

    .empty-description {
        color: #718096;
        margin-bottom: 2rem;
    }

    @media (max-width: 968px) {
        .harvests-grid {
            grid-template-columns: 1fr;
        }

        .form-row {
            grid-template-columns: 1fr;
        }
    }
</style>

<div class="page-header">
    <h1 class="page-title">Create Blended Wine</h1>
    <p class="page-subtitle" style="color: #718096; font-size: 1rem; display: flex; align-items: center; gap: 0.5rem; text-align: left;">
        <a href="{{ route('winemaker.harvests.index') }}" style="color: #89b4d9; text-decoration: none; display: inline-flex; align-items: center; gap: 0.5rem;">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width: 24px; height: 24px; fill: currentColor;">
                <path d="M20 11H7.83l5.59-5.59L12 4l-8 8 8 8 1.41-1.41L7.83 13H20v-2z"/>
            </svg>
            <span style="color: #718096; font-weight: 600;">Back to Harvests</span>
        </a>
    </p>
</div>

@if($availableHarvests->count() > 0)
<form action="{{ route('winemaker.cellar.blend.store') }}" method="POST" id="blendForm" enctype="multipart/form-data">
    @csrf

    <div class="content-card">
        <h2 class="card-title">
            <svg class="icon" fill="currentColor" viewBox="0 0 24 24">
                <path d="M12,2C11.5,2 11,2.19 10.59,2.59L2.59,10.59C1.8,11.37 1.8,12.63 2.59,13.41L10.59,21.41C11.37,22.2 12.63,22.2 13.41,21.41L21.41,13.41C22.2,12.63 22.2,11.37 21.41,10.59L13.41,2.59C13,2.19 12.5,2 12,2M12,4L20,12L12,20L4,12L12,4M12,7C9.79,7 8,8.79 8,11C8,12.5 9,13.77 10.5,14.37L12,16L13.5,14.37C15,13.77 16,12.5 16,11C16,8.79 14.21,7 12,7M12,9A2,2 0 0,1 14,11A2,2 0 0,1 12,13A2,2 0 0,1 10,11A2,2 0 0,1 12,9Z"/>
            </svg>
            Step 1: Select Harvests & Specify Weights
        </h2>

        <div class="harvests-grid">
            @foreach($availableHarvests as $harvest)
            <div class="harvest-card" data-harvest-id="{{ $harvest->id }}" onclick="toggleHarvest(this)">
                <input type="checkbox"
                    name="harvests[{{ $harvest->id }}][selected]"
                    class="harvest-checkbox"
                    id="harvest_{{ $harvest->id }}"
                    onclick="handleCheckboxClick(event, this)"
                    onchange="updateHarvestSelection(this)">

                <div class="harvest-header">
                    <div class="harvest-id">Harvest #{{ $harvest->id }}</div>
                    <div class="harvest-date">
                        @if($harvest->plannedTask && $harvest->plannedTask->execution_date)
                            {{ \Carbon\Carbon::parse($harvest->plannedTask->execution_date)->format('d M Y') }}
                        @else
                            {{ \Carbon\Carbon::parse($harvest->date)->format('d M Y') }}
                        @endif
                    </div>
                </div>

                <div class="harvest-details">
                    <div class="harvest-detail-row">
                        <span class="detail-label">Variety:</span>
                        <span class="variety-badge">
                            {{ $harvest->varietyVariation->grapeVariety->variety_name ?? 'Unknown' }}
                        </span>
                    </div>
                    <div class="harvest-detail-row">
                        <span class="detail-label">Available Weight:</span>
                        <span class="detail-value" style="color: #065f46;">{{ number_format($harvest->remaining_weight, 2) }} kg</span>
                    </div>
                    <div class="harvest-detail-row">
                        <span class="detail-label">Sugar Content:</span>
                        <span class="detail-value">{{ number_format($harvest->sugar_content, 1) }}°NM</span>
                    </div>
                    <div class="harvest-detail-row">
                        <span class="detail-label">Quality:</span>
                        <span class="detail-value">{{ ucfirst($harvest->quality ?? 'good') }}</span>
                    </div>
                </div>

                <div class="weight-input-section" id="weight_section_{{ $harvest->id }}">
                    <label for="weight_{{ $harvest->id }}" class="form-label">Weight to Use (kg) *</label>
                    <input type="number"
                        name="harvests[{{ $harvest->id }}][weight]"
                        id="weight_{{ $harvest->id }}"
                        class="form-input weight-input"
                        min="0.01"
                        max="{{ $harvest->remaining_weight }}"
                        step="0.01"
                        data-max="{{ $harvest->remaining_weight }}"
                        oninput="updateBlendSummary()"
                        onclick="event.stopPropagation()">
                    <div class="form-hint">Maximum: {{ number_format($harvest->remaining_weight, 2) }} kg</div>
                </div>
            </div>
            @endforeach
        </div>
    </div>

    <div class="content-card wine-form-section" id="wineFormSection">
        <h2 class="card-title">
            <svg class="icon" fill="currentColor" viewBox="0 0 24 24">
                <path d="M6,2V10C6,11.11 6.9,12 8,12H10V22H14V12H16C17.11,12 18,11.11 18,10V2H6M8,4H10V6H8V4M12,4H14V6H12V4M16,4H18V6H16V4M8,8H10V10H8V8M12,8H14V10H12V8M16,8H18V10H16V8Z"/>
            </svg>
            Step 2: Wine Details
        </h2>

        <div class="form-group">
            <label for="wine_name" class="form-label">Wine Name *</label>
            <input type="text" id="wine_name" name="wine_name" class="form-input" value="{{ old('wine_name') }}" required>
            @error('wine_name')
                <div class="error-message">{{ $message }}</div>
            @enderror
        </div>

        <div class="form-row">
            <div class="form-group">
                <label for="vintage" class="form-label">Vintage Year *</label>
                <input type="number" id="vintage" name="vintage" class="form-input" value="{{ old('vintage', date('Y')) }}" min="1900" max="{{ date('Y') + 1 }}" required>
                @error('vintage')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>

            <div class="form-group">
                <label for="wine_type" class="form-label">Wine Type *</label>
                <select id="wine_type" name="wine_type" class="form-select" required>
                    <option value="">Select type</option>
                    <option value="red" {{ old('wine_type') == 'red' ? 'selected' : '' }}>Red</option>
                    <option value="white" {{ old('wine_type') == 'white' ? 'selected' : '' }}>White</option>
                    <option value="rose" {{ old('wine_type') == 'rose' ? 'selected' : '' }}>Rosé</option>
                </select>
                @error('wine_type')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>
        </div>

        <div class="form-row">
            <div class="form-group">
                <label for="sweetness" class="form-label">Sweetness *</label>
                <select id="sweetness" name="sweetness" class="form-select" required>
                    <option value="">Select sweetness</option>
                    <option value="dry" {{ old('sweetness') == 'dry' ? 'selected' : '' }}>Dry</option>
                    <option value="semi_dry" {{ old('sweetness') == 'semi_dry' ? 'selected' : '' }}>Semi-Dry</option>
                    <option value="semi_sweet" {{ old('sweetness') == 'semi_sweet' ? 'selected' : '' }}>Semi-Sweet</option>
                    <option value="sweet" {{ old('sweetness') == 'sweet' ? 'selected' : '' }}>Sweet</option>
                </select>
                @error('sweetness')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>

            <div class="form-group">
                <label for="alcohol_percentage" class="form-label">Alcohol % *</label>
                <input type="number" id="alcohol_percentage" name="alcohol_percentage" class="form-input" value="{{ old('alcohol_percentage') }}" min="0" max="20" step="0.1" required>
                @error('alcohol_percentage')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>
        </div>

        <div class="form-row">
            <div class="form-group">
                <label for="bottles_produced" class="form-label">Bottles Produced *</label>
                <input type="number" id="bottles_produced" name="bottles_produced" class="form-input" value="{{ old('bottles_produced') }}" min="1" required>
                <div class="form-hint">Number of bottles produced</div>
                @error('bottles_produced')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>

            <div class="form-group">
                <label for="bottle_volume" class="form-label">Bottle Volume (L)</label>
                <input type="number" id="bottle_volume" name="bottle_volume" class="form-input" value="{{ old('bottle_volume', '0.75') }}" min="0.1" max="10" step="0.01">
                <div class="form-hint">Default: 0.75L</div>
                @error('bottle_volume')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>
        </div>

        <div class="form-row">
            <div class="form-group">
                <label for="production_date" class="form-label">Production Date *</label>
                <input type="date" id="production_date" name="production_date" class="form-input" value="{{ old('production_date', date('Y-m-d')) }}" required>
                @error('production_date')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>

            <div class="form-group">
                <label for="bottling_date" class="form-label">Bottling Date</label>
                <input type="date" id="bottling_date" name="bottling_date" class="form-input" value="{{ old('bottling_date') }}">
                <div class="form-hint">Leave empty if not yet bottled</div>
                @error('bottling_date')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>
        </div>

        <div class="form-group">
            <label for="status" class="form-label">Wine Status *</label>
            <select id="status" name="status" class="form-select" required>
                <option value="in_production" {{ old('status', 'in_production') == 'in_production' ? 'selected' : '' }}>In Production</option>
                <option value="aging" {{ old('status') == 'aging' ? 'selected' : '' }}>Aging</option>
                <option value="ready" {{ old('status') == 'ready' ? 'selected' : '' }}>Ready</option>
            </select>
            <div class="form-hint">Set to "Ready" if the wine is prepared for sale</div>
            @error('status')
                <div class="error-message">{{ $message }}</div>
            @enderror
        </div>

        <div class="form-group">
            <label for="price_per_bottle" class="form-label">Price per Bottle (€)</label>
            <input type="number" id="price_per_bottle" name="price_per_bottle" class="form-input" value="{{ old('price_per_bottle') }}" min="0" step="0.01">
            <div class="form-hint">Set price now or later</div>
            @error('price_per_bottle')
                <div class="error-message">{{ $message }}</div>
            @enderror
        </div>

        <div class="form-group">
            <label for="description" class="form-label">Description</label>
            <textarea id="description" name="description" class="form-textarea" rows="4">{{ old('description') }}</textarea>
            <div class="form-hint">Tasting notes, characteristics, pairing suggestions</div>
            @error('description')
                <div class="error-message">{{ $message }}</div>
            @enderror
        </div>

        <div class="form-group">
            <label for="image" class="form-label">Wine Image</label>
            <input type="file" id="image" name="image" class="form-input" accept="image/*">
            <div class="form-hint">Upload an image of the wine bottle (JPG, PNG, max 2MB)</div>
            @error('image')
                <div class="error-message">{{ $message }}</div>
            @enderror
        </div>
    </div>

    <div class="blend-summary">
        <h3 class="card-title">
            <svg class="icon" fill="currentColor" viewBox="0 0 20 20">
                <path d="M9 2a1 1 0 000 2h2a1 1 0 100-2H9z"/>
                <path fill-rule="evenodd" d="M4 5a2 2 0 012-2 3 3 0 003 3h2a3 3 0 003-3 2 2 0 012 2v11a2 2 0 01-2 2H6a2 2 0 01-2-2V5zm3 4a1 1 0 000 2h.01a1 1 0 100-2H7zm3 0a1 1 0 000 2h3a1 1 0 100-2h-3zm-3 4a1 1 0 100 2h.01a1 1 0 100-2H7zm3 0a1 1 0 100 2h3a1 1 0 100-2h-3z" clip-rule="evenodd"/>
            </svg>
            Blend Summary
        </h3>

        <div id="blendDetails">
            <div style="text-align: center; padding: 2rem; color: #718096;">
                <svg style="width: 48px; height: 48px; margin: 0 auto 1rem; opacity: 0.5;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"/>
                </svg>
                <p>Select harvests above to see blend composition</p>
            </div>
        </div>

        <button type="submit" class="btn-primary" id="submitBtn" disabled>
            <svg class="icon" fill="currentColor" viewBox="0 0 20 20">
                <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"/>
            </svg>
            Create Blended Wine
        </button>

        <a href="{{ route('winemaker.cellar.index') }}" class="btn-secondary">
            <svg class="icon" fill="currentColor" viewBox="0 0 20 20">
                <path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"/>
            </svg>
            Cancel
        </a>
    </div>
</form>

@else
<div class="content-card">
    <div class="empty-state">
        <svg class="empty-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"/>
        </svg>
        <h2 class="empty-title">No Harvests Available</h2>
        <p class="empty-description">You need at least one harvest with available weight to create a blend.</p>
        <a href="{{ route('winemaker.harvests.index') }}" class="btn-primary" style="width: auto; display: inline-flex;">
            <svg class="icon" fill="currentColor" viewBox="0 0 24 24">
                <path d="M12,2C11.5,2 11,2.19 10.59,2.59L2.59,10.59C1.8,11.37 1.8,12.63 2.59,13.41L10.59,21.41C11.37,22.2 12.63,22.2 13.41,21.41L21.41,13.41C22.2,12.63 22.2,11.37 21.41,10.59L13.41,2.59C13,2.19 12.5,2 12,2M12,4L20,12L12,20L4,12L12,4M12,7C9.79,7 8,8.79 8,11C8,12.5 9,13.77 10.5,14.37L12,16L13.5,14.37C15,13.77 16,12.5 16,11C16,8.79 14.21,7 12,7M12,9A2,2 0 0,1 14,11A2,2 0 0,1 12,13A2,2 0 0,1 10,11A2,2 0 0,1 12,9Z"/>
            </svg>
            View Harvests
        </a>
    </div>
</div>
@endif

@push('scripts')
<script>
function handleCheckboxClick(event, checkbox) {
    // Stop propagation so the card's onclick doesn't fire
    event.stopPropagation();
}

function updateHarvestSelection(checkbox) {
    const card = checkbox.closest('.harvest-card');
    const weightSection = card.querySelector('.weight-input-section');
    const weightInput = weightSection.querySelector('.weight-input');

    if (checkbox.checked) {
        card.classList.add('selected');
        weightSection.classList.add('visible');

        // Enable the weight input and set default weight if not set
        weightInput.disabled = false;
        if (!weightInput.value) {
            weightInput.value = weightInput.getAttribute('max');
        }
    } else {
        card.classList.remove('selected');
        weightSection.classList.remove('visible');

        // Disable the weight input so it won't be submitted
        weightInput.disabled = true;
    }

    updateBlendSummary();
}

function toggleHarvest(card) {
    const checkbox = card.querySelector('.harvest-checkbox');
    const weightSection = card.querySelector('.weight-input-section');
    const weightInput = weightSection.querySelector('.weight-input');

    checkbox.checked = !checkbox.checked;

    if (checkbox.checked) {
        card.classList.add('selected');
        weightSection.classList.add('visible');

        // Enable the weight input and set default weight if not set
        weightInput.disabled = false;
        if (!weightInput.value) {
            weightInput.value = weightInput.getAttribute('max');
        }
    } else {
        card.classList.remove('selected');
        weightSection.classList.remove('visible');

        // Disable the weight input so it won't be submitted
        weightInput.disabled = true;
    }

    updateBlendSummary();
}

function updateBlendSummary() {
    const checkboxes = document.querySelectorAll('.harvest-checkbox:checked');
    const blendDetails = document.getElementById('blendDetails');
    const submitBtn = document.getElementById('submitBtn');
    const wineFormSection = document.getElementById('wineFormSection');

    let totalWeight = 0;
    let blendData = [];

    checkboxes.forEach(checkbox => {
        const harvestId = checkbox.closest('.harvest-card').dataset.harvestId;
        const weightInput = document.getElementById(`weight_${harvestId}`);
        const weight = parseFloat(weightInput.value) || 0;

        if (weight > 0) {
            totalWeight += weight;
            const card = checkbox.closest('.harvest-card');
            const variety = card.querySelector('.variety-badge').textContent.trim();
            blendData.push({ harvestId, variety, weight });
        }
    });

    if (blendData.length === 0) {
        blendDetails.innerHTML = `
            <div style="text-align: center; padding: 2rem; color: #718096;">
                <svg style="width: 48px; height: 48px; margin: 0 auto 1rem; opacity: 0.5;" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"/>
                </svg>
                <p>Select harvests above to see blend composition</p>
            </div>
        `;
        submitBtn.disabled = true;
        wineFormSection.classList.remove('visible');
        return;
    }

    let html = '';
    blendData.forEach(item => {
        const percentage = (item.weight / totalWeight * 100).toFixed(1);
        html += `
            <div class="summary-row">
                <span class="summary-label">Harvest #${item.harvestId} (${item.variety})</span>
                <span class="summary-value">${item.weight.toFixed(2)} kg (${percentage}%)</span>
            </div>
            <div class="percentage-bar">
                <div class="percentage-fill" style="width: ${percentage}%"></div>
            </div>
        `;
    });

    html += `
        <div class="summary-row">
            <span class="summary-label">Total Weight</span>
            <span class="summary-value">${totalWeight.toFixed(2)} kg</span>
        </div>
    `;

    blendDetails.innerHTML = html;
    submitBtn.disabled = totalWeight === 0;
    wineFormSection.classList.add('visible');
}

// Initialize: Disable all weight inputs on page load (they get enabled when harvest is selected)
document.addEventListener('DOMContentLoaded', function() {
    // Disable all weight inputs initially
    document.querySelectorAll('.weight-input').forEach(input => {
        input.disabled = true;
    });
});
</script>
@endpush
@endsection
