

<?php $__env->startSection('content'); ?>
    <div class="container">
        <h1>Create Task</h1>

        <?php if($errors->any()): ?>
            <div class="alert alert-danger">
                <ul>
                    <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <li><?php echo e($error); ?></li>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </ul>
            </div>
        <?php endif; ?>

        <form action="<?php echo e(route('tasks.store')); ?>" method="POST" id="taskForm">
            <?php echo csrf_field(); ?>
            <div class="mb-3">
                <label for="title" class="form-label">Title</label>
                <input type="text" name="title" class="form-control" id="title" value="<?php echo e(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><?php echo e(old('description')); ?></textarea>
            </div>

            <div class="mb-3">
                <label for="farm_id" class="form-label">Farm</label>
                <select name="farm" class="form-control" id="farm_id" onchange="fetchSections()" required>
                    <option value="">Select Farm</option>
                    <?php $__currentLoopData = $farms; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $farm): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($farm->id); ?>" <?php echo e(old('farm') == $farm->id ? 'selected' : ''); ?>>
                            <?php echo e($farm->registration_no); ?> - <?php echo e($farm->address); ?>

                        </option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>

            <div class="mb-3">
                <label for="section_id" class="form-label">Section</label>
                <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 name="assigned_to" class="form-control" id="assigned_to" required>
                    <option value="">Select Farmer</option>
                    <?php $__currentLoopData = $farmers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $farmer): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($farmer->id); ?>" <?php echo e(old('assigned_to') == $farmer->id ? 'selected' : ''); ?>>
                            <?php echo e($farmer->fullname); ?>

                        </option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>
            
            

            <button type="submit" class="btn btn-success">Create Task</button>
        </form>
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
    <script>
        function fetchSections() {
            var farmId = document.getElementById('farm_id').value;

            if (farmId) {
                $.ajax({
                    url: '<?php echo e(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>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\laragon\www\VPS_changes\resources\views/tasks/tasks-create.blade.php ENDPATH**/ ?>