<?php $__env->startSection('content'); ?>

<div class="card">
    <div class="card-header">
        <?php echo e(trans('global.create')); ?> <?php echo e(trans('cruds.serviceComplaint.title_singular')); ?>

    </div>

    <div class="card-body">
        <form method="POST" action="<?php echo e(route("admin.service-complaints.store")); ?>" enctype="multipart/form-data">
            <?php echo csrf_field(); ?>
            <div class="form-group">
                <label class="required" for="client_id"><?php echo e(trans('cruds.serviceComplaint.fields.client')); ?></label>
                <select class="form-control select2 <?php echo e($errors->has('client') ? 'is-invalid' : ''); ?>" name="client_id" id="client_id" required>
                    <?php $__currentLoopData = $clients; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $id => $entry): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($id); ?>" <?php echo e(old('client_id') == $id ? 'selected' : ''); ?>><?php echo e($entry); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
                <?php if($errors->has('client')): ?>
                    <div class="invalid-feedback">
                        <?php echo e($errors->first('client')); ?>

                    </div>
                <?php endif; ?>
                <span class="help-block"><?php echo e(trans('cruds.serviceComplaint.fields.client_helper')); ?></span>
            </div>

            <div class="form-group">
                <input type="hidden" name="location_id" id="location_id">
                <?php if($errors->has('location')): ?>
                    <div class="invalid-feedback">
                        <?php echo e($errors->first('location')); ?>

                    </div>
                <?php endif; ?>
                <span class="help-block"><?php echo e(trans('cruds.serviceComplaint.fields.location_helper')); ?></span>
            </div>

<!--
            <div class="form-group">
                <label for="asset_id"><?php echo e(trans('cruds.serviceComplaint.fields.asset')); ?></label>
                <select class="form-control select2 <?php echo e($errors->has('asset') ? 'is-invalid' : ''); ?>" name="asset_id" id="asset_id" disabled>

                </select>
                <?php if($errors->has('asset')): ?>
                    <div class="invalid-feedback">
                        <?php echo e($errors->first('asset')); ?>

                    </div>
                <?php endif; ?>
                <span class="help-block"><?php echo e(trans('cruds.serviceComplaint.fields.asset_helper')); ?></span>
            </div>
        -->

<!-- Cursor -->

            <div class="form-group">
                <label class="required" for="asset_id"><?php echo e(trans('cruds.serviceComplaint.fields.asset')); ?></label>
                <div class="checkbox-container">
                    <!-- Asset category sections -->
                    <div id="air-compressor-section" style="display: none;">
                        <h5>Air Compressors</h5>
                        <div class="air-compressor-items"></div>
                    </div>

                    <div id="air-dryer-section" style="display: none;">
                        <h5>Air Dryers</h5>
                        <div class="air-dryer-items"></div>
                    </div>

                    <div id="filter-section" style="display: none;">
                        <h5>Filters</h5>
                        <div class="filter-items"></div>
                    </div>

                    <div id="eco-drain-section" style="display: none;">
                        <h5>Eco Drains</h5>
                        <div class="eco-drain-items"></div>
                    </div>

                    <div id="air-receiver-section" style="display: none;">
                        <h5>Air Receivers</h5>
                        <div class="air-receiver-items"></div>
                    </div>

                    <!-- Uncategorized assets will go here -->
                    <div id="asset_checkbox" class="text-danger">
                        <!-- Initial placeholder message -->
                        <div class="form-check">
                            <label class="form-check-label"><?php echo e(trans('global.pleaseSelect')); ?> a client first</label>
                        </div>
                    </div>
                    <div id="asset_checkbox" class="text-danger"></div>
                </div>
                <div id="asset-error" class="text-danger" style="display: none;">
                    Please select an asset
                </div>
                <?php if($errors->has('asset')): ?>
                    <div class="invalid-feedback">
                        <?php echo e($errors->first('asset')); ?>

                    </div>
                <?php endif; ?>
                <span class="help-block"><?php echo e(trans('cruds.serviceComplaint.fields.asset_helper')); ?></span>
            </div>



<!-- Cursor -->

<!-- Editable -->

<div class="form-group">
    <label class="required" for="service_date"><?php echo e(trans('cruds.serviceComplaint.fields.service_date')); ?></label>
    <input class="form-control date <?php echo e($errors->has('service_date') ? 'is-invalid' : ''); ?>" type="text" name="service_date" id="service_date" value="<?php echo e(old('service_date')); ?>" required>
    <?php if($errors->has('service_date')): ?>
        <div class="invalid-feedback">
            <?php echo e($errors->first('service_date')); ?>

        </div>
    <?php endif; ?>
    <span class="help-block"><?php echo e(trans('cruds.serviceComplaint.fields.service_date_helper')); ?></span>
</div>
<div class="form-group">
    <label class="required" for="complaint"><?php echo e(trans('cruds.serviceComplaint.fields.complaint')); ?></label>
    <input class="form-control <?php echo e($errors->has('complaint') ? 'is-invalid' : ''); ?>" type="text" name="complaint" id="complaint" value="<?php echo e(old('complaint', '')); ?>" required>
    <?php if($errors->has('complaint')): ?>
        <div class="invalid-feedback">
            <?php echo e($errors->first('complaint')); ?>

        </div>
    <?php endif; ?>
    <span class="help-block"><?php echo e(trans('cruds.serviceComplaint.fields.complaint_helper')); ?></span>
</div>

<!--Editable -->

<!--Default unless needs change -->



            <div class="form-group">
                <label class="required" for="service_type_id"><?php echo e(trans('cruds.serviceComplaint.fields.service_type')); ?></label>
                <select class="form-control select2 <?php echo e($errors->has('service_type') ? 'is-invalid' : ''); ?>" name="service_type_id" id="service_type_id" required>
                    <?php $__currentLoopData = $service_types; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $id => $entry): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($id); ?>" <?php echo e(old('service_type_id','8') == $id ? 'selected' : ''); ?>><?php echo e($entry); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
                <?php if($errors->has('service_type')): ?>
                    <div class="invalid-feedback">
                        <?php echo e($errors->first('service_type')); ?>

                    </div>
                <?php endif; ?>
                <span class="help-block"><?php echo e(trans('cruds.serviceComplaint.fields.service_type_helper')); ?></span>
            </div>
            <div class="form-group">
                <label class="required" for="visit_type_id"><?php echo e(trans('cruds.serviceComplaint.fields.visit_type')); ?></label>
                <select class="form-control select2 <?php echo e($errors->has('visit_type') ? 'is-invalid' : ''); ?>" name="visit_type_id" id="visit_type_id" required>
                    <?php $__currentLoopData = $visit_types; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $id => $entry): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($id); ?>" <?php echo e(old('visit_type_id', '10') == $id ? 'selected' : ''); ?>><?php echo e($entry); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
                <?php if($errors->has('visit_type')): ?>
                    <div class="invalid-feedback">
                        <?php echo e($errors->first('visit_type')); ?>

                    </div>
                <?php endif; ?>
                <span class="help-block"><?php echo e(trans('cruds.serviceComplaint.fields.visit_type_helper')); ?></span>
            </div>
            <div class="form-group">
                <label class="required" for="status_id"><?php echo e(trans('cruds.serviceComplaint.fields.status')); ?></label>
                <select class="form-control select2 <?php echo e($errors->has('status') ? 'is-invalid' : ''); ?>" name="status_id" id="status_id" required>
                    <?php $__currentLoopData = $statuses; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $id => $entry): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($id); ?>" <?php echo e(old('status_id','32') == $id ? 'selected' : ''); ?>><?php echo e($entry); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
                <?php if($errors->has('status')): ?>
                    <div class="invalid-feedback">
                        <?php echo e($errors->first('status')); ?>

                    </div>
                <?php endif; ?>
                <span class="help-block"><?php echo e(trans('cruds.serviceComplaint.fields.status_helper')); ?></span>
            </div>
            <div class="form-group">
                <label class="required" for="enginner_assigned_id"><?php echo e(trans('cruds.serviceComplaint.fields.enginner_assigned')); ?></label>
                <select class="form-control select2 <?php echo e($errors->has('enginner_assigned') ? 'is-invalid' : ''); ?>" name="enginner_assigned_id" id="enginner_assigned_id" required>
                    <?php $__currentLoopData = $enginner_assigneds; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $id => $entry): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($id); ?>" <?php echo e(old('enginner_assigned_id', Auth::id()) == $id ? 'selected' : ''); ?>><?php echo e($entry); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
                <?php if($errors->has('enginner_assigned')): ?>
                    <div class="invalid-feedback">
                        <?php echo e($errors->first('enginner_assigned')); ?>

                    </div>
                <?php endif; ?>
                <span class="help-block"><?php echo e(trans('cruds.serviceComplaint.fields.enginner_assigned_helper')); ?></span>
            </div>
            <div class="form-group">
                <label class="required" for="supervisor_assigned_id"><?php echo e(trans('cruds.serviceComplaint.fields.supervisor_assigned')); ?></label>
                <select class="form-control select2 <?php echo e($errors->has('supervisor_assigned') ? 'is-invalid' : ''); ?>" name="supervisor_assigned_id" id="supervisor_assigned_id">
                    <?php $__currentLoopData = $supervisor_assigneds; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $id => $entry): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($id); ?>" <?php echo e(old('supervisor_assigned_id',2) == $id ? 'selected' : ''); ?>><?php echo e($entry); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
                <?php if($errors->has('supervisor_assigned')): ?>
                    <div class="invalid-feedback">
                        <?php echo e($errors->first('supervisor_assigned')); ?>

                    </div>
                <?php endif; ?>
                <span class="help-block"><?php echo e(trans('cruds.serviceComplaint.fields.supervisor_assigned_helper')); ?></span>
            </div>

            <div class="form-group">
                <label for="team_id"><?php echo e(trans('cruds.serviceComplaint.fields.team')); ?></label>
                <select class="form-control select2 <?php echo e($errors->has('team') ? 'is-invalid' : ''); ?>" name="team_id" id="team_id">
                    <?php $__currentLoopData = $teams; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $id => $entry): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($id); ?>" <?php echo e(old('team_id',1) == $id ? 'selected' : ''); ?>><?php echo e($entry); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
                <?php if($errors->has('team')): ?>
                    <div class="invalid-feedback">
                        <?php echo e($errors->first('team')); ?>

                    </div>
                <?php endif; ?>
                <span class="help-block"><?php echo e(trans('cruds.serviceComplaint.fields.team_helper')); ?></span>
            </div>
            <div class="form-group">
                <button class="btn btn-lg btn-block btn-danger" type="submit">
                    <?php echo e(trans('global.save')); ?>

                </button>
            </div>
        </form>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script type="text/javascript">

    $(document).ready(function(){
        $('#client_id').change(function(){
            var id = $(this).val();
            if(id){
                //$('#location_id').attr('disabled', 'disabled');
                $('#location_id').removeAttr('disabled');
                $('#asset_id').removeAttr('disabled');

                $.ajax({
                    type: 'GET',
                    url: "<?php echo e(url('admin/contact-contacts/getLocationName?client_id=')); ?>" + id,
                    success: function(data){
                        var array = data;
                        console.log(array);
                        //var s = '<option value="">Please select</option>';
                        array.forEach(function(value, index){
                            // Select the first location by default (index === 0)
                            //var selected = (index === 0) ? 'selected' : '';
                            //s += '<option value="' + value['id'] + '" ' + selected + '>' + value['location'] + '</option>';

                            // Update the hidden input field with the first location's ID
                            value['id'] = value['id'];
                            if (index === 0) {
                                $('#location_id').val(value['id']);
                            }
                        });

                        $('#location_id').html(s);
                        // Trigger change event to update any dependent fields
                        $('#location_id').trigger('change');

                    }
                })
                /*
                $.ajax({
                    type: 'GET',
                    url: "<?php echo e(url('admin/assets/getAssetName?client_id=')); ?>" + id,
                    success: function(data){
                        //var array = data;
                        var array = Object.keys(data).map((key) => [key, data[key]])
                        //console.log(array);
                        var s = '<option value="">Please select</option>';
                        array.forEach(function(value){
                            console.log('Hey');
                            console.log(value[1]['asset']);
                            //s += '<option value="' + value['id'] + ' <?php echo e(old('id') == $id ? 'selected' : ''); ?>"> ' + value['asset'] + ' </option>'
                            s += '<option value="' + value[1]['id'] + '">' + value[1]['asset'] + ' </option>'
                           //alert(value);<input type="checkbox" class="form-check-input" disabled>

                        })

                        $('#asset_id').html(s);
                    }
                })
                    */

                    //Cursor
                    $.ajax({
                    type: 'GET',
                    url: "<?php echo e(url('admin/assets/getAssetName?client_id=')); ?>" + id,
                    success: function(data){
                        //var array = data;
                        var array = Object.keys(data).map((key) => [key, data[key]])
                        console.log('Assets data:', array);
                        var checkboxes = '';

                        // Clear existing asset sections
                        $('.air-compressor-items, .air-dryer-items, .filter-items, .eco-drain-items, .air-receiver-items').empty();
                        $('#asset_checkbox').empty();

                        // Hide all category sections initially
                        $('#air-compressor-section, #air-dryer-section, #filter-section, #eco-drain-section, #air-receiver-section').hide();

                        if (array.length === 0) {
                            $('#asset_checkbox').html('<div class="form-check"><label class="form-check-label">No assets found for this client</label></div>');
                        } else {
                            array.forEach(function(value){
                                // Determine the asset type for grouping
                                var assetType = '';
                                if (value[1].asset.toLowerCase().includes('compressor')) {
                                    assetType = 'compressor';
                                } else if (value[1].asset.toLowerCase().includes('dryer') && !value[1].asset.toLowerCase().includes('eco')) {
                                    assetType = 'dryer';
                                } else if (value[1].asset.toLowerCase().includes('filter')) {
                                    assetType = 'filter';
                                } else if (value[1].asset.toLowerCase().includes('eco')) {
                                    assetType = 'ecodrain';
                                } else if (value[1].asset.toLowerCase().includes('receiver')) {
                                    assetType = 'receiver';
                                } else {
                                    assetType = 'other';
                                }

                                // Create radio button instead of checkbox for categorized items
                                var assetItem = '<div class="form-check">';
                                if (assetType !== 'other') {
                                    assetItem += '<input type="radio" class="form-check-input" name="asset_id_' + assetType + '" value="' +
                                        assetType + '_' + value[1].asset_id_service_needed + '" id="asset_' +
                                        assetType + '_' + value[1].asset_id_service_needed + '">';
                                } else {
                                    // Keep checkboxes for uncategorized items
                                    assetItem += '<input type="checkbox" class="form-check-input" name="asset_id[]" value="other_' +
                                        value[1].asset_id_service_needed + '" id="asset_other_' + value[1].asset_id_service_needed + '">';
                                }
                                assetItem += '<label class="form-check-label" for="asset_' + assetType + '_' + value[1].asset_id_service_needed + '">' +
                                    (value[1].asset.includes(':') ? value[1].asset.split(':')[1].trim() : value[1].asset) + '</label>';
                                assetItem += '</div>';

                                // Add to the appropriate category section based on asset type
                                if (assetType === 'compressor') {
                                    $('.air-compressor-items').append(assetItem);
                                    $('#air-compressor-section').show();
                                } else if (assetType === 'dryer') {
                                    $('.air-dryer-items').append(assetItem);
                                    $('#air-dryer-section').show();
                                } else if (assetType === 'filter') {
                                    $('.filter-items').append(assetItem);
                                    $('#filter-section').show();
                                } else if (assetType === 'ecodrain') {
                                    $('.eco-drain-items').append(assetItem);
                                    $('#eco-drain-section').show();
                                } else if (assetType === 'receiver') {
                                    $('.air-receiver-items').append(assetItem);
                                    $('#air-receiver-section').show();
                                } else {
                                    // For uncategorized assets
                                    $('#asset_checkbox').append(assetItem);
                                }
                            });
                        }

                        // Add validation event listener for both radio buttons and checkboxes
                        $('input[type="radio"], input[type="checkbox"]').on('change', function() {
                            // Check if any radio button or checkbox is selected
                            if ($('input[type="radio"]:checked, input[type="checkbox"]:checked').length > 0) {
                                $('#asset-error').hide();
                            }

                            // Create hidden input to store all selected values for form submission
                            updateSelectedAssets();
                        });

                        // Function to update hidden input with all selected assets
                        function updateSelectedAssets() {
                            var selectedAssets = [];

                            // Get all checked radio buttons
                            $('input[type="radio"]:checked').each(function() {
                                selectedAssets.push($(this).val());
                            });

                            // Get all checked checkboxes
                            $('input[type="checkbox"]:checked').each(function() {
                                selectedAssets.push($(this).val());
                            });

                            // Remove any existing hidden inputs
                            $('input[name="asset_id[]"]').remove();

                            // Create hidden inputs for each selected asset
                            selectedAssets.forEach(function(value) {
                                $('<input>').attr({
                                    type: 'hidden',
                                    name: 'asset_id[]',
                                    value: value
                                }).appendTo('form');
                            });
                        }
                    },
                    error: function(xhr, status, error) {
                        console.error('Error loading assets:', error);
                        $('#asset_checkbox').html('<div class="form-check text-danger"><label class="form-check-label">Error loading assets. Please try again.</label></div>');
                    }
                })
                    //cursor


            }
        })

        //Assets




    });

//var array = Object.keys(array).map((key) => [key, array[key]]);
//url: "<?php echo e(url('admin/assets/getAssetName?client_id=')); ?>" + id,


</script>
<?php $__env->stopSection(); ?>




<?php echo $__env->make('layouts.admin', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/newrhytha2/public_html/sfm.fusionengg.in/resources/views/admin/serviceComplaints/create.blade.php ENDPATH**/ ?>