@extends('layouts.app')

@section('content')
    <!--begin::App Content Header-->
    <div class="app-content-header"> <!--begin::Container-->
        <div class="container-fluid">
            <!--begin::Row-->
            <div class="row">
                <div class="col-sm-6">
                    <h3 class="mb-0">Create Task</h3>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-end">
                        <li class="breadcrumb-item"><a href="{{ route('admin.dashboard') }}">Home</a></li>
                        <li class="breadcrumb-item"><a href="{{ route('tasks.index') }}">Index Project</a></li>
                        <li class="breadcrumb-item active">Create Project</li>
                    </ol>
                </div>
            </div>
            <!--end::Row-->
        </div> <!--end::Container-->
    </div>
    <!--end::App Content Header-->

    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-body">
                    @if ($errors->any())
                        <div class="alert alert-danger">
                            <ul>
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <form action="{{ route('tasks.store') }}" method="POST" id="taskForm">
                        @csrf
                        <div class="mb-3">
                            <label for="title" class="form-label">Title</label>
                            <input type="text" name="title" class="form-control" id="title"
                                value="{{ old('title') }}" required>
                        </div>

                        <div class="mb-3">
                            <label for="description" class="form-label">Description</label>
                            <textarea name="description" class="form-control" id="description" rows="4" required>{{ old('description') }}</textarea>
                        </div>

                        <div class="mb-3">
                            <label for="farm_id" class="form-label">Farm</label>
                            <select class="form-select" name="farm" class="form-control" id="farm_id"
                                onchange="fetchSections()" required>
                                <option value="">Select Farm</option>
                                @foreach ($farms as $farm)
                                    <option value="{{ $farm->id }}" {{ old('farm') == $farm->id ? 'selected' : '' }}>
                                        {{ $farm->registration_no }} - {{ $farm->address }}
                                    </option>
                                @endforeach
                            </select>
                        </div>

                        <div class="mb-3">
                            <label for="section_id" class="form-label">Section</label>
                            <select class="form-select" name="section" class="form-control" id="section_id" required>
                                <option value="">Select Section</option>
                                <!-- Sections will be populated dynamically using AJAX -->
                            </select>
                        </div>

                        <div class="mb-3">
                            <label for="assigned_to" class="form-label">Assigned To</label>
                            <select class="form-select" name="assigned_to" class="form-control" id="assigned_to" required>
                                <option value="">Select Farmer</option>
                                @foreach ($farmers as $farmer)
                                    <option value="{{ $farmer->id }}"
                                        {{ old('assigned_to') == $farmer->id ? 'selected' : '' }}>
                                        {{ $farmer->fullname }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <button type="submit" class="btn btn-success">Create Task</button>
                    </form>
                </div>
            </div>
        @endsection

        @section('scripts')
            <script>
                function fetchSections() {
                    var farmId = document.getElementById('farm_id').value;

                    if (farmId) {
                        $.ajax({
                            url: '{{ route('tasks.get-sections') }}',
                            type: 'GET',
                            data: {
                                farm_id: farmId
                            },
                            success: function(response) {
                                var sectionSelect = document.getElementById('section_id');
                                sectionSelect.innerHTML = '<option value="">Select Section</option>';

                                response.forEach(function(section) {
                                    var option = document.createElement('option');
                                    option.value = section.id;
                                    option.textContent = section.name;
                                    sectionSelect.appendChild(option);
                                });
                            }
                        });
                    } else {
                        document.getElementById('section_id').innerHTML = '<option value="">Select Section</option>';
                    }
                }
            </script>
        </div>
    </div>
@endsection
