<?php $__env->startPush('head-script'); ?>
    <link rel="stylesheet" href="<?php echo e(asset('assets/plugins/datepicker/datepicker3.css')); ?>">
<?php $__env->stopPush(); ?>

<?php $__env->startSection('content'); ?>
    <?php
        $gender = [
            'male' => __('modules.front.male'),
            'female' => __('modules.front.female'),
            'others' => __('modules.front.others')
        ];
    ?>
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title mb-4"><?php echo app('translator')->get('app.edit'); ?></h4>

                    <form class="ajax-form" method="POST" id="editForm">
                        <?php echo csrf_field(); ?>
                        <?php echo method_field('PUT'); ?>
                        <div class="row">
                            <div class="col-md-4 pl-4 pr-4">
                                <h5><?php echo app('translator')->get('modules.front.personalInformation'); ?></h5>
                            </div>

                            <div class="col-md-8">

                                <div class="form-group">
                                    <input type="hidden" value="" name="job_id" id="job_id">
                                    <input type="hidden" value="" name="location_id" id="location_id">
                                    <label class="control-label"><?php echo app('translator')->get('menu.jobs'); ?></label>
                                    
                                    <select name="job_job_location_id" id="job_job_location_id" onchange="getQuestions(this.value)"
                                        class="select2 form-control">
                                        <?php $__currentLoopData = $locations; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $location): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <?php
                                                $isSelected = ($application->location_id == $location->location_id && $application->job_id == $location->job_id);
                                                $title = ucwords($location->job->title);
                                                $locationName = $location->location->location;
                                            ?>
                                            <option value= <?php echo e($location->id); ?> <?php echo e($isSelected ? 'selected' : ''); ?>>
                                                <?php echo e($title); ?>(<?php echo e($locationName); ?>)
                                            </option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>

                                <div class="form-group">
                                    <label class="control-label"><?php echo app('translator')->get('app.name'); ?></label>
                                    <input class="form-control" type="text" value="<?php echo e($application->full_name); ?>" name="full_name" placeholder="<?php echo app('translator')->get('app.name'); ?>">
                                </div>

                                <div class="form-group">
                                    <label class="control-label"><?php echo app('translator')->get('app.email'); ?></label>
                                    <input class="form-control" type="email" name="email" value="<?php echo e($application->email); ?>"
                                           placeholder="<?php echo app('translator')->get('app.email'); ?>">
                                </div>

                                <div class="form-group">
                                    <label class="control-label"><?php echo app('translator')->get('app.phone'); ?></label>
                                    <input class="form-control" type="tel" name="phone" value="<?php echo e($application->phone); ?>"
                                           placeholder="<?php echo app('translator')->get('app.phone'); ?>">
                                </div>

                                <div class="form-group">
                                    <label class="control-label"><?php echo app('translator')->get('app.address'); ?></label>
                                    <textarea class="form-control" name="address" rows="4" cols="50"
                                           placeholder="<?php echo app('translator')->get('app.address'); ?>"><?php echo e($application->address); ?></textarea>
                                </div>

                                <div id="show-columns">
                                    <?php echo $__env->make('admin.job-applications.required-columns', ['job' => $application->job, 'application' => $application, 'gender' => $gender], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
                                </div>
                            </div>
                        </div>
                        <div id="show-sections">
                            <?php echo $__env->make('admin.job-applications.required-sections', ['section_visibility' => $jobs[0]->section_visibility, 'application' => $application], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
                        </div>
                        <div class="row">
                            <?php if(count($jobQuestion) > 0): ?>
                                <div class="col-md-4 pl-4 pr-4 pt-4 b-b" id="questionBoxTitle">
                                    <h5><?php echo app('translator')->get('modules.front.additionalDetails'); ?></h5>
                                </div>


                                <div class="col-md-8 pt-4 b-b" id="questionBox">

                                </div>
                            <?php endif; ?>
                            <div class="col-md-4 pl-4 pr-4 pt-4">
                                <h5><?php echo app('translator')->get('app.status'); ?></h5>
                            </div>

                            <div class="col-md-8 pt-4">

                                <div class="form-group">
                                    <select name="status_id" id="status_id" class="select2 form-control">
                                        <?php $__currentLoopData = $statuses; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $status): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option
                                                    <?php if($application->status_id == $status->id): ?> selected <?php endif; ?>
                                                    value="<?php echo e($status->id); ?>"><?php echo e(ucwords($status->status)); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <button type="button" id="save-form" class="btn btn-success">
                            <i class="fa fa-check"></i> 
                            <?php echo app('translator')->get('app.save'); ?>
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('footer-script'); ?>
    <script src="<?php echo e(asset('assets/plugins/datepicker/bootstrap-datepicker.js')); ?>"></script>
    <script src="<?php echo e(asset('assets/node_modules/select2/dist/js/select2.full.min.js')); ?>"></script>
    <script>
        const fetchCountryState = "<?php echo e(route('jobs.fetchCountryState')); ?>";
        const csrfToken = "<?php echo e(csrf_token()); ?>";
        const selectCountry = "<?php echo app('translator')->get('modules.front.selectCountry'); ?>";
        const selectState = "<?php echo app('translator')->get('modules.front.selectState'); ?>";
        const selectCity = "<?php echo app('translator')->get('modules.front.selectCity'); ?>";
        const pleaseWait = "<?php echo app('translator')->get('modules.front.pleaseWait'); ?>";

        let country = "<?php echo e($application->country); ?>";
        let state = "<?php echo e($application->state); ?>";
    </script>
    <script src="<?php echo e(asset('front/assets/js/location.js')); ?>"></script>
    <script>
        var datepicker = $('.dob').datepicker({
            autoclose: true,
            format: 'yyyy-mm-dd',
            endDate: (new Date()).toDateString(),
        });

        <?php if($application->dob): ?>
            datepicker.datepicker('setDate', new Date('<?php echo e($application->dob); ?>'))
        <?php endif; ?>
        
        $('.select2').select2({
            width: '100%'
        });

        $('#save-form').click(function () {

            $.easyAjax({
                url: '<?php echo e(route('admin.job-applications.update', $application->id)); ?>',
                container: '#editForm',
                type: "POST",
                redirect: true,
                file:true,
                error: function (response) {
                    handleFails(response);
                }
            })
        });

        var val = $('#job_job_location_id').val(); // get Current Selected Job
        if (val != '' && typeof val !== 'undefined') {
            getQuestions(val); // get Questions by question on page load
        }

        // get Questions on change Job
        function getQuestions(id) {
            var url = "<?php echo e(route('admin.job-applications.question', [':id', $application->id])); ?>";
            url = url.replace(':id', id);

            $.easyAjax({
                type: 'GET',
                url: url,
                container: '#editForm',
                success: function (response) {
                    $('#job_id').val(response.jobJobLocation.job_id)
                    $('#location_id').val(response.jobJobLocation.location_id)
                    if (response.status == "success") {
                        if (response.count > 0) { // Question Found for selected job
                            $('#questionBox').show();
                            $('#questionBoxTitle').show();
                            $('#questionBox').html(response.view);
                        } else { // Question Not Found for selected job
                            $('#questionBox').hide();
                            $('#questionBoxTitle').hide();
                        }
                        $('#show-columns').html(response.requiredColumnsView);
                        $('#show-sections').html(response.requiredSectionsView);
                        if(response.requiredColumnsView !== '') {
                            var datepicker = $('.dob').datepicker({
                                autoclose: true,
                                format: 'yyyy-mm-dd',
                                endDate: (new Date()).toDateString(),
                            });
                            if (response.application.dob !== null) {
                                $('.dob').datepicker('setDate', new Date(response.application.dob));
                            }

                            $('.select2').select2({
                                width: '100%'
                            });

                            country = response.application.country;
                            state = response.application.state;

                            var loc = new locationInfo()
                            loc.getCountries()
                        }
                    }
                }
            });
        }

        function handleFails(response) {

            if (typeof response.responseJSON.errors != "undefined") {
                var keys = Object.keys(response.responseJSON.errors);
                $('#editForm').find(".has-error").find(".help-block").remove();
                $('#editForm').find(".has-error").removeClass("has-error");

                for (var i = 0; i < keys.length; i++) {
                    // Escape dot that comes with error in array fields
                    var key = keys[i].replace(".", '\\.');
                    var formarray = keys[i];

                    // If the response has form array
                    if(formarray.indexOf('.') >0){
                        var array = formarray.split('.');
                        response.responseJSON.errors[keys[i]] = response.responseJSON.errors[keys[i]];
                        key = array[0]+'['+array[1]+']';
                    }

                    var ele = $('#editForm').find("[name='" + key + "']");

                    var grp = ele.closest(".form-group");
                    $(grp).find(".help-block").remove();

                    //check if wysihtml5 editor exist
                    var wys = $(grp).find(".wysihtml5-toolbar").length;

                    if(wys > 0){
                        var helpBlockContainer = $(grp);
                    }
                    else{
                        var helpBlockContainer = $(grp).find("div:first");
                    }
                    if($(ele).is(':radio')){
                        helpBlockContainer = $(grp);
                    }

                    if (helpBlockContainer.length == 0) {
                        helpBlockContainer = $(grp);
                    }

                    helpBlockContainer.append('<div class="help-block">' + response.responseJSON.errors[keys[i]] + '</div>');
                    $(grp).addClass("has-error");
                }

                if (keys.length > 0) {
                    var element = $("[name='" + keys[0] + "']");
                    if (element.length > 0) {
                        $("html, body").animate({scrollTop: element.offset().top - 150}, 200);
                    }
                }
            }
        }
    </script>
<?php $__env->stopPush(); ?>
<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/hoseadevimcon/hr.hosea.dev-imcon.com/resources/views/admin/job-applications/edit.blade.php ENDPATH**/ ?>