@extends('layouts.winemaker')

@section('title', 'Edit Wine - Winemaker')

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

    .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;
        max-width: 900px;
        margin: 0 auto;
    }

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

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

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

    .form-input,
    .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-input:focus,
    .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: 120px;
    }

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

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

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

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

    .btn-group {
        display: flex;
        gap: 1rem;
        margin-top: 2rem;
    }

    .btn-submit {
        flex: 1;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
        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;
    }

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

    .btn-cancel {
        flex: 1;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
        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;
    }

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

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

    .btn-delete:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(245, 101, 101, 0.4);
    }

    .danger-zone {
        margin-top: 3rem;
        padding-top: 2rem;
        border-top: 2px solid #e2e8f0;
    }

    .danger-zone-title {
        font-size: 1.1rem;
        font-weight: 600;
        color: #e53e3e;
        margin-bottom: 0.5rem;
    }

    .danger-zone-text {
        color: #718096;
        font-size: 0.875rem;
        margin-bottom: 1rem;
    }

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

    @media (max-width: 768px) {
        .form-row,
        .form-row-3 {
            grid-template-columns: 1fr;
        }

        .btn-group {
            flex-direction: column-reverse;
        }
    }
</style>

<div class="page-header">
    <h1 class="page-title">Edit Wine</h1>
    <div class="breadcrumb">
        <a href="{{ route('winemaker.cellar.index') }}">Cellar</a>
        <span>/</span>
        <span>Edit {{ $wine->wine_name }}</span>
    </div>
</div>

<div class="content-card">
    <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>
        Wine Details
    </h2>

    <form action="{{ route('wines.update', $wine) }}" method="POST" enctype="multipart/form-data">
        @csrf
        @method('PUT')

        <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', $wine->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 *</label>
                <input type="number" id="vintage" name="vintage" class="form-input"
                    value="{{ old('vintage', $wine->vintage) }}"
                    min="1900" max="{{ date('Y') + 1 }}" required>
                @error('vintage')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>

            <div class="form-group">
                <label for="grape_variety_id" class="form-label">Grape Variety *</label>
                <select id="grape_variety_id" name="grape_variety_id" class="form-select" required>
                    <option value="">Select variety...</option>
                    @foreach($grapeVarieties as $variety)
                        <option value="{{ $variety->id }}"
                            {{ old('grape_variety_id', $wine->grape_variety_id) == $variety->id ? 'selected' : '' }}>
                            {{ $variety->variety_name }}
                        </option>
                    @endforeach
                </select>
                @error('grape_variety_id')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>
        </div>

        <div class="form-row">
            <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', $wine->wine_type) == 'red' ? 'selected' : '' }}>Red</option>
                    <option value="white" {{ old('wine_type', $wine->wine_type) == 'white' ? 'selected' : '' }}>White</option>
                    <option value="rose" {{ old('wine_type', $wine->wine_type) == 'rose' ? 'selected' : '' }}>Rosé</option>
                </select>
                @error('wine_type')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>

            <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', $wine->sweetness) == 'dry' ? 'selected' : '' }}>Dry</option>
                    <option value="semi_dry" {{ old('sweetness', $wine->sweetness) == 'semi_dry' ? 'selected' : '' }}>Semi-Dry</option>
                    <option value="semi_sweet" {{ old('sweetness', $wine->sweetness) == 'semi_sweet' ? 'selected' : '' }}>Semi-Sweet</option>
                    <option value="sweet" {{ old('sweetness', $wine->sweetness) == 'sweet' ? 'selected' : '' }}>Sweet</option>
                </select>
                @error('sweetness')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>
        </div>

        <div class="form-row-3">
            <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', $wine->alcohol_percentage) }}"
                    min="0" max="20" step="0.1" required>
                @error('alcohol_percentage')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>

            <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', $wine->bottles_produced) }}"
                    min="1" required readonly style="background-color: #f7fafc; cursor: not-allowed;">
                <div class="form-hint">Cannot be changed after creation</div>
                @error('bottles_produced')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>

            <div class="form-group">
                <label for="bottles_in_stock" class="form-label">Bottles in Stock *</label>
                <input type="number" id="bottles_in_stock" name="bottles_in_stock" class="form-input"
                    value="{{ old('bottles_in_stock', $wine->bottles_in_stock) }}"
                    min="0" max="{{ $wine->bottles_produced }}" required>
                <div class="form-hint">Max: {{ $wine->bottles_produced }}</div>
                @error('bottles_in_stock')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>
        </div>

        <div class="form-row">
            <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', $wine->bottle_volume ?? 0.75) }}"
                    min="0.1" max="10" step="0.01" required>
                @error('bottle_volume')
                    <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', $wine->price_per_bottle) }}"
                    min="0" step="0.01">
                @error('price_per_bottle')
                    <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', $wine->production_date ? $wine->production_date->format('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', $wine->bottling_date ? $wine->bottling_date->format('Y-m-d') : '') }}">
                @error('bottling_date')
                    <div class="error-message">{{ $message }}</div>
                @enderror
            </div>
        </div>

        <div class="form-group">
            <label for="status" class="form-label">Status *</label>
            <select id="status" name="status" class="form-select" required>
                <option value="in_production" {{ old('status', $wine->status) == 'in_production' ? 'selected' : '' }}>In Production</option>
                <option value="aging" {{ old('status', $wine->status) == 'aging' ? 'selected' : '' }}>Aging</option>
                <option value="ready" {{ old('status', $wine->status) == 'ready' ? 'selected' : '' }}>Ready (Available for Sale)</option>
                <option value="sold_out" {{ old('status', $wine->status) == 'sold_out' ? 'selected' : '' }}>Sold Out</option>
            </select>
            @error('status')
                <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', $wine->description) }}</textarea>
            @error('description')
                <div class="error-message">{{ $message }}</div>
            @enderror
        </div>

        <div class="form-group">
            <label for="image" class="form-label">Wine Image</label>
            @if($wine->image_url)
                <div style="margin-bottom: 1rem;">
                    <img src="{{ asset('storage/' . $wine->image_url) }}" alt="{{ $wine->wine_name }}" style="max-width: 200px; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);">
                    <div class="form-hint">Current image</div>
                </div>
            @endif
            <input type="file" id="image" name="image" class="form-input" accept="image/*">
            <div class="form-hint">Upload a new image (JPG, PNG, max 2MB) or leave empty to keep current</div>
            @error('image')
                <div class="error-message">{{ $message }}</div>
            @enderror
        </div>

        <div class="btn-group">
            <a href="{{ route('winemaker.cellar.index') }}" class="btn-cancel">
                <svg class="icon" fill="currentColor" viewBox="0 0 20 20">
                    <path fill-rule="evenodd" d="M9.707 16.707a1 1 0 01-1.414 0l-6-6a1 1 0 010-1.414l6-6a1 1 0 011.414 1.414L5.414 9H17a1 1 0 110 2H5.414l4.293 4.293a1 1 0 010 1.414z" clip-rule="evenodd"/>
                </svg>
                Cancel
            </a>
            <button type="submit" class="btn-submit">
                <svg class="icon" fill="currentColor" viewBox="0 0 20 20">
                    <path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"/>
                </svg>
                Update Wine
            </button>
        </div>
    </form>

    <!-- Danger Zone -->
    <div class="danger-zone">
        <div class="danger-zone-title">Danger Zone</div>
        <p class="danger-zone-text">
            Permanently remove this wine from the cellar. This action cannot be undone.
        </p>
        <form action="{{ route('wines.destroy', $wine) }}" method="POST"
              onsubmit="return confirm('Are you sure you want to permanently delete {{ $wine->wine_name }}? This action cannot be undone.');">
            @csrf
            @method('DELETE')
            <button type="submit" class="btn-delete">
                <svg class="icon" fill="currentColor" viewBox="0 0 20 20">
                    <path fill-rule="evenodd" d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z" clip-rule="evenodd"/>
                </svg>
                Delete Wine from Cellar
            </button>
        </form>
    </div>
</div>
@endsection
