
<?php $__env->startSection('pageTitle', isset($pageTitle) ? $pageTitle : 'Page Title Here'); ?>
<?php $__env->startSection('content'); ?>

<div class="page-header">
    <div class="row">
        <div class="col-md-6 col-sm-12">
            <div class="title">
                <h4>Edit Facility</h4>
            </div>
            <nav aria-label="breadcrumb" role="navigation">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item">
                        <a href="<?php echo e(route('admin.dashboard')); ?>">Home</a>
                    </li>
                    <li class="breadcrumb-item active" aria-current="page">Edit Facility</li>
                </ol>
            </nav>
        </div>
        <div class="col-md-6 col-sm-12 text-right">
            <a href="<?php echo e(route('admin.facilities')); ?>" class="btn btn-primary">View All Facility</a>
        </div>
    </div>
</div>

<form action="<?php echo e(route('admin.update_facility')); ?>" method="POST" enctype="multipart/form-data" id="editFacilityForm" autocomplete="off">
    <?php echo csrf_field(); ?>
    <?php echo method_field('POST'); ?>
    <input type="hidden" name="id" value="<?php echo e($facilities->id); ?>">
    <div class="row">
        <div class="col-md-9">
            <div class="card card-box mb-2">
                <div class="card-body">
                    <div class="form-group">
                        <label for="facility_name"><b>Facility Name</b>:</label>
                        <input type="text" name="facility_name" class="form-control" placeholder="Enter facility name" value="<?php echo e($facilities->name); ?>">
                        <span class="text-danger error-text facility_name_error"></span>
                    </div>
                    <div class="form-group">
                        <label for="description"><b>Description</b>:</label>
                        <textarea name="description" class="form-control" rows="3" placeholder="Write a short description..."><?php echo e($facilities->description); ?></textarea>
                        <span class="text-danger error-text description_error"></span>
                    </div>
                    <div class="form-check mb-3">
                        <input type="checkbox" name="status" class="form-check-input" id="status" <?php echo e($facilities->status ? 'checked' : ''); ?>>
                        <label class="form-check-label" for="is_published">Publish this facility</label>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card card-box mb-2">
                <div class="card-body">
                    <div class="form-group">
                        <label for="cover_photo"><b>Cover Photo</b>:</label>
                        <input type="file" name="cover_photo" class="form-control-file form-control" accept="image/*">
                        <span class="text-danger error-text cover_photo_error"></span>
                    </div>
                    <div class="d-block mb-3" style="max-width:200px">
                        <img src="<?php echo e($facilities->cover_photo ? asset('images/facilities/'.$facilities->cover_photo) : ''); ?>" alt="" class="img-thumbnail" id="facility_image_preview">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="mb-3">
        <button type="submit" class="btn btn-primary">Edit Facility</button>
    </div>
</form>

<?php $__env->stopSection(); ?>
<?php $__env->startPush('scripts'); ?>
<script>
    document.addEventListener("DOMContentLoaded", function() {
        const inputFile = document.querySelector('input[type="file"][name="cover_photo"]');
        const previewImage = document.getElementById("facility_image_preview");

        inputFile.addEventListener("change", function(event) {
            const file = event.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    previewImage.src = e.target.result;
                };
                reader.readAsDataURL(file);
            }
        });
    });

    $(document).ready(function() {
        $('#editFacilityForm').submit(function(e) {
            e.preventDefault();

            var form = this;
            var formData = new FormData(form);

            $.ajax({
                url: $(form).attr('action'),
                method: $(form).attr('method'),
                data: formData,
                processData: false,
                contentType: false,
                dataType: 'json',
                beforeSend: function() {
                    $(form).find('span.error-text').text('');
                },
                success: function(data) {
                    if (data.status === 1) {
                        $(form)[0].reset();
                        $('#facility_image_preview').attr('src', '');
                        toastr.success(data.message);
                    } else {
                        toastr.error(data.message);
                    }
                },
                error: function(data) {
                    $.each(data.responseJSON.errors, function(prefix, val) {
                        $(form).find('span.' + prefix + '_error').text(val[0]);
                    });
                }
            });
        });
    });
</script>
<?php $__env->stopPush(); ?>
<?php echo $__env->make('back.layout.page-layout', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\larablog\resources\views\back\pages\facility\edit_facility.blade.php ENDPATH**/ ?>