@extends('layouts.winemaker')

@section('title', 'Edit Planned Task - 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: 700px;
        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: 100px;
    }

    .form-row {
        display: grid;
        grid-template-columns: 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;
    }

    .info-badge {
        display: inline-block;
        padding: 0.5rem 1rem;
        background: #f7fafc;
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        font-size: 0.875rem;
        color: #4a5568;
    }

    .task-type-badge {
        display: inline-block;
        padding: 0.375rem 0.75rem;
        border-radius: 6px;
        font-size: 0.875rem;
        font-weight: 600;
        text-transform: capitalize;
    }

    .task-type-badge.watering {
        background: linear-gradient(135deg, #4299e1 0%, #3182ce 100%);
        color: white;
    }

    .task-type-badge.fertilization {
        background: linear-gradient(135deg, #f6ad55 0%, #ed8936 100%);
        color: white;
    }

    .task-type-badge.spraying {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
    }

    .task-type-badge.pruning {
        background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
        color: white;
    }

    .task-type-badge.harvest {
        background: linear-gradient(135deg, #ed64a6 0%, #d53f8c 100%);
        color: white;
    }

    .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: 1rem;
    }

    .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;
    }

    .row-selector {
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
        margin-top: 0.5rem;
    }

    .row-selector-item {
        position: relative;
    }

    .row-selector-item input[type="checkbox"] {
        position: absolute;
        opacity: 0;
        width: 0;
        height: 0;
    }

    .row-selector-item label {
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        padding: 0.5rem 0.75rem;
        background: #f7fafc;
        border: 2px solid #e2e8f0;
        border-radius: 8px;
        cursor: pointer;
        font-size: 0.875rem;
        font-weight: 500;
        color: #4a5568;
        transition: all 0.2s;
    }

    .row-selector-item label:hover {
        border-color: #89b4d9;
        background: #f0f7ff;
    }

    .row-selector-item input[type="checkbox"]:checked + label {
        background: linear-gradient(135deg, #89b4d9 0%, #a8cce5 100%);
        border-color: #89b4d9;
        color: white;
    }

    .row-selector-item .checkmark {
        width: 16px;
        height: 16px;
        border: 2px solid #cbd5e0;
        border-radius: 4px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: white;
        transition: all 0.2s;
    }

    .row-selector-item input[type="checkbox"]:checked + label .checkmark {
        background: white;
        border-color: white;
    }

    .row-selector-item input[type="checkbox"]:checked + label .checkmark svg {
        display: block;
    }

    .row-selector-item .checkmark svg {
        display: none;
        width: 10px;
        height: 10px;
        fill: #89b4d9;
    }

    .row-selector-hint {
        font-size: 0.75rem;
        color: #718096;
        margin-top: 0.5rem;
    }

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

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

<div class="page-header">
    <h1 class="page-title">Edit Planned Task</h1>
    <div class="breadcrumb">
        <a href="{{ route('winemaker.planned-tasks') }}">Planned Tasks</a>
        <span>/</span>
        <span>Edit {{ $task->type }}</span>
    </div>
</div>

<div class="content-card">
    <h2 class="card-title">
        <svg class="icon" fill="currentColor" viewBox="0 0 24 24">
            <path d="M19 3h-4.18C14.4 1.84 13.3 1 12 1c-1.3 0-2.4.84-2.82 2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-7 0c.55 0 1 .45 1 1s-.45 1-1 1-1-.45-1-1 .45-1 1-1zm2 14H7v-2h7v2zm3-4H7v-2h10v2zm0-4H7V7h10v2z"/>
        </svg>
        Task Details
    </h2>

    <form action="{{ route('planned-tasks.update', $task) }}" method="POST">
        @csrf
        @method('PUT')

        <div class="form-group">
            <label class="form-label">Task Type</label>
            <span class="task-type-badge {{ strtolower($task->type) }}">{{ $task->type }}</span>
        </div>

        @if(isset($taskRows) && $taskRows->count() > 0)
        <div class="form-group">
            <label class="form-label">Vineyard Rows (select which to update) *</label>
            <div class="row-selector">
                @foreach($taskRows as $taskRow)
                    <div class="row-selector-item">
                        <input type="checkbox"
                               id="task_{{ $taskRow['task_id'] }}"
                               name="task_ids[]"
                               value="{{ $taskRow['task_id'] }}"
                               {{ $taskRow['task_id'] == $task->planned_task_id ? 'checked' : '' }}>
                        <label for="task_{{ $taskRow['task_id'] }}">
                            <span class="checkmark">
                                <svg viewBox="0 0 12 12">
                                    <path d="M10.28 2.28L3.989 8.575 1.695 6.28A1 1 0 00.28 7.695l3 3a1 1 0 001.414 0l7-7A1 1 0 0010.28 2.28z"/>
                                </svg>
                            </span>
                            Row {{ $taskRow['row_id'] }}
                            @if($taskRow['row']->varietyVariation && $taskRow['row']->varietyVariation->grapeVariety)
                                <small style="opacity: 0.8;">{{ $taskRow['row']->varietyVariation->grapeVariety->variety_name }}</small>
                            @endif
                        </label>
                    </div>
                @endforeach
            </div>
            <div class="row-selector-hint">Select the rows you want to apply the changes to</div>
            @error('task_ids')
                <div class="error-message">{{ $message }}</div>
            @enderror
        </div>
        @elseif($vineyardRow)
        <div class="form-group">
            <label class="form-label">Vineyard Row</label>
            <div class="info-badge">
                Row #{{ $vineyardRow->id }}
                @if($vineyardRow->varietyVariation && $vineyardRow->varietyVariation->grapeVariety)
                    — {{ $vineyardRow->varietyVariation->grapeVariety->variety_name }}
                @endif
            </div>
            <input type="hidden" name="task_ids[]" value="{{ $task->planned_task_id }}">
        </div>
        @endif

        <div class="form-group">
            <label for="planned_date" class="form-label">Planned Date @if(!in_array(strtolower($task->type), ['spraying', 'harvest']))*@endif</label>
            @if(in_array(strtolower($task->type), ['spraying', 'harvest']))
                <input type="date" id="planned_date" name="planned_date" class="form-input"
                    value="{{ $task->planned_date->format('Y-m-d') }}" readonly
                    style="background-color: #f7fafc; cursor: not-allowed;">
                <div class="form-hint" style="color: #718096;">
                    Date cannot be changed for {{ strtolower($task->type) }} tasks. To change the date, delete this task and create a new one.
                </div>
            @else
                <input type="date" id="planned_date" name="planned_date" class="form-input"
                    value="{{ old('planned_date', $task->planned_date->format('Y-m-d')) }}" required
                    min="{{ date('Y-m-d') }}">
            @endif
            @error('planned_date')
                <div class="error-message">{{ $message }}</div>
            @enderror
        </div>

        @php
            $taskableType = $task->taskable_type;
            $taskable = $task->taskable;
        @endphp

        @if(str_contains($taskableType, 'Watering'))
            <div class="form-row">
                <div class="form-group">
                    <label for="time_interval" class="form-label">Time Interval (minutes)</label>
                    <input type="number" id="time_interval" name="time_interval" class="form-input"
                        value="{{ old('time_interval', $taskable->time_interval ?? '') }}"
                        min="1">
                    @error('time_interval')
                        <div class="error-message">{{ $message }}</div>
                    @enderror
                </div>

                <div class="form-group">
                    <label for="amount" class="form-label">Amount (litres) *</label>
                    <input type="number" id="amount" name="amount" class="form-input"
                        value="{{ old('amount', $taskable->amount ?? '') }}"
                        min="0" step="0.01" required>
                    @error('amount')
                        <div class="error-message">{{ $message }}</div>
                    @enderror
                </div>
            </div>
        @elseif(str_contains($taskableType, 'Fertilization'))
            <div class="form-row">
                <div class="form-group">
                    <label for="substance" class="form-label">Substance *</label>
                    <input type="text" id="substance" name="substance" class="form-input"
                        value="{{ old('substance', $taskable->substance ?? '') }}" required>
                    @error('substance')
                        <div class="error-message">{{ $message }}</div>
                    @enderror
                </div>

                <div class="form-group">
                    <label for="concentration" class="form-label">Concentration</label>
                    <input type="number" id="concentration" name="concentration" class="form-input"
                        value="{{ old('concentration', $taskable->concentration ?? '') }}"
                        min="0" step="0.01">
                    @error('concentration')
                        <div class="error-message">{{ $message }}</div>
                    @enderror
                </div>
            </div>
        @elseif(str_contains($taskableType, 'Spraying'))
            <div class="form-row">
                <div class="form-group">
                    <label for="pesticide" class="form-label">Pesticide *</label>
                    <input type="text" id="pesticide" name="pesticide" class="form-input"
                        value="{{ old('pesticide', $taskable->pesticide ?? '') }}" required>
                    @error('pesticide')
                        <div class="error-message">{{ $message }}</div>
                    @enderror
                </div>

                <div class="form-group">
                    <label for="concentration" class="form-label">Concentration</label>
                    <input type="number" id="concentration" name="concentration" class="form-input"
                        value="{{ old('concentration', $taskable->concentration ?? '') }}"
                        min="0" step="0.01">
                    @error('concentration')
                        <div class="error-message">{{ $message }}</div>
                    @enderror
                </div>
            </div>
        @elseif(str_contains($taskableType, 'Pruning'))
            <div class="form-row">
                <div class="form-group">
                    <label for="method" class="form-label">Method *</label>
                    <input type="text" id="method" name="method" class="form-input"
                        value="{{ old('method', $taskable->method ?? '') }}" required>
                    @error('method')
                        <div class="error-message">{{ $message }}</div>
                    @enderror
                </div>

                <div class="form-group">
                    <label for="percentage_removed" class="form-label">Percentage Removed</label>
                    <input type="number" id="percentage_removed" name="percentage_removed" class="form-input"
                        value="{{ old('percentage_removed', $taskable->percentage_removed ?? '') }}"
                        min="0" max="100">
                    <div class="form-hint">0-100%</div>
                    @error('percentage_removed')
                        <div class="error-message">{{ $message }}</div>
                    @enderror
                </div>
            </div>
        @elseif(str_contains($taskableType, 'Harvest'))
            <div class="form-group">
                <div class="form-hint" style="padding: 1rem; background: #f7fafc; border-radius: 8px; border: 1px solid #e2e8f0;">
                    Harvest details (weight, sugar content, etc.) are recorded when the task is completed by an employee.
                </div>
            </div>
        @endif

        <div class="form-group">
            <label for="note" class="form-label">Notes</label>
            <textarea id="note" name="note" class="form-textarea"
                rows="3" placeholder="Optional notes">{{ old('note', $task->note) }}</textarea>
            @error('note')
                <div class="error-message">{{ $message }}</div>
            @enderror
        </div>

        <div class="btn-group">
            <a href="{{ route('winemaker.planned-tasks') }}" 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 Task
            </button>
        </div>
    </form>

    <!-- Danger Zone -->
    <div class="danger-zone">
        <div class="danger-zone-title">Danger Zone</div>
        <p class="danger-zone-text">
            Permanently delete this planned task. This action cannot be undone.
        </p>
        <form action="{{ route('planned-tasks.destroy', $task) }}" method="POST"
              onsubmit="return confirm('Are you sure you want to delete this planned {{ strtolower($task->type) }} task? 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 Planned Task
            </button>
        </form>
    </div>
</div>
@endsection
