
<?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>Add Faculty</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">
                        Add Office Head
                    </li>
                </ol>
            </nav>
        </div>
        <div class="col-md-6 col-sm-12 text-right">
            <a href="<?php echo e(route('admin.office_department')); ?>" class="btn btn-primary">View all Office Head</a>
        </div>
    </div>
</div>

<form action="<?php echo e(route('admin.create_office_department')); ?>" method="POST" autocomplete="off" enctype="multipart/form-data" id="addOfficeDepartmentForm">
    <?php echo csrf_field(); ?>
    <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="office">Select Office Department:</label>
                        <select name="office_id" id="office" class="form-control">
                            <option value="">-- Select an Office --</option>
                            <?php $__currentLoopData = $offices; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $office): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($office->id); ?>"><?php echo e($office->description); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="office_department_name"><b>Office Department Head Name</b>: </label>
                        <input type="text" name="office_department_name" placeholder="Enter Office Department name" class="form-control">
                        <span class="text-danger error-text office_department_name_error"></span>
                    </div>
                    <div class="form-group">
                        <label for="office_department_position"><b>Office Department Position</b>: </label>
                        <input type="text" name="office_department_position" placeholder="Enter Office Department position" class="form-control">
                        <span class="text-danger error-text office_department_position_error"></span>
                    </div>
                    <div class="form-group">
                        <label for="office_department_email"><b>Office Department Email</b>: </label>
                        <input type="email" name="office_department_email" placeholder="Enter Office Department email" class="form-control">
                        <span class="text-danger error-text office_department_email_error"></span>
                    </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="office_head_image"><b>Office Head Image</b>:</label>
                        <input type="file" name="office_head_image" class="form-control-file form-control" height="auto">
                        <span class="text-danger error-text office_head_image_error"></span>
                    </div>
                    <div class="d-block mb-3" style="max-width:200px">
                        <img src="" alt="" class="img-thumbnail" id="office_head_image_preview">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="mb-3">
        <button type="submit" class="btn btn-primary">Add Office Department Head</button>
    </div>
</form>

<?php $__env->stopSection(); ?>
<?php $__env->startPush('scripts'); ?>
<script>
    document.addEventListener("DOMContentLoaded", function() {
        const inputFile = document.querySelector('input[name="office_head_image"]');
        const previewImage = document.getElementById("office_head_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() {
        refreshOfficeDropdown(); // Load available offices on page load

        $('#addOfficeDepartmentForm').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) {
                    try {
                        if (data.status === 1) {
                            $(form)[0].reset();
                            $('#office_head_image_preview').attr('src', '');
                            toastr.success(data.message);

                            // ✅ Refresh dropdown
                            refreshOfficeDropdown();
                        } else {
                            toastr.error(data.message);
                        }
                    } catch (e) {
                        console.error('Parsing error:', e);
                        toastr.error('Unexpected response format.');
                    }
                },
                error: function(data) {
                    $.each(data.responseJSON.errors, function(prefix, val) {
                        $(form).find('span.' + prefix + '_error').text(val[0]);
                    });
                }
            });
        });

        // 🔄 Fetch offices not already assigned
        function refreshOfficeDropdown() {
            $.ajax({
                url: '<?php echo e(route("admin.get_available_offices")); ?>',
                method: 'GET',
                success: function(data) {
                    const $dropdown = $('select[name="office_id"]');
                    $dropdown.empty();
                    $dropdown.append('<option value="">-- Select an Office --</option>');

                    if (data.length > 0) {
                        data.forEach(function(office) {
                            $dropdown.append(`<option value="${office.id}">${office.description}</option>`);
                        });
                    } else {
                        $dropdown.append('<option value="">No offices available</option>');
                    }
                },
                error: function() {
                    toastr.error("Failed to refresh office list.");
                }
            });
        }
    });
</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\offices\add_office_department.blade.php ENDPATH**/ ?>