<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Harvests</title>
</head>
<body>
    <h1>Harvests</h1>

    <a href="{{ route('harvests.create') }}">Create New Harvest</a>

    <table>
        <thead>
            <tr>
                <th>Date</th>
                <th>Vineyard Row</th>
                <th>Weight (kg)</th>
                <th>Sugar Content</th>
                <th>Quality</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            @forelse($harvests as $harvest)
                <tr>
                    <td>
                        @if($harvest->plannedTask && $harvest->plannedTask->execution_date)
                            {{ $harvest->plannedTask->execution_date->format('Y-m-d') }}
                        @else
                            {{ $harvest->date->format('Y-m-d') }}
                        @endif
                    </td>
                    <td>{{ $harvest->vineyardRow->varietyVariation->grapeVariety->variety_name ?? 'Row #' . ($harvest->vineyardRow->id ?? 'N/A') }}</td>
                    <td>{{ $harvest->weight }}</td>
                    <td>{{ $harvest->sugar_content }}</td>
                    <td>{{ $harvest->quality }}</td>
                    <td>
                        <a href="{{ route('harvests.show', $harvest) }}">View</a>
                        <a href="{{ route('harvests.edit', $harvest) }}">Edit</a>
                        <form action="{{ route('harvests.destroy', $harvest) }}" method="POST" style="display: inline;">
                            @csrf
                            @method('DELETE')
                            <button type="submit">Delete</button>
                        </form>
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="6">No harvests found.</td>
                </tr>
            @endforelse
        </tbody>
    </table>

    {{ $harvests->links() }}
</body>
</html>
