<?php $__env->startSection('content'); ?>
<div class="card">
    <div class="card-header">
        <h5>Edit Attendance Settings: <?php echo e($setting->name); ?></h5>
        
    </div>

    <div class="card-body">
        <form id="settingsForm">
            <?php echo csrf_field(); ?>
            <?php echo method_field('PUT'); ?>
            <?php echo $__env->make('attendance::settings/partials/form', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

            <div class="mt-4">
                <button type="submit" class="btn btn-success">Update Settings</button>
                <a href="<?php echo e(route('attendance.settings.index')); ?>" class="btn btn-secondary">Cancel</a>
            </div>
        </form>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<?php echo $__env->make('attendance::settings/partials/scripts', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
<script>
// Pre-populate field mappings
<?php if(isset($setting) && $setting->field_mapping): ?>
let existingMappings = <?php echo json_encode($setting->field_mapping, 15, 512) ?>;
Object.keys(existingMappings).forEach(function(field) {
    let mapping = existingMappings[field];
    if (mapping.csv_column !== null && mapping.csv_column !== undefined) {
        $('input[name="field_mapping[' + field + '][csv_column]"]').val(mapping.csv_column);
    }
    if (mapping.api_path) {
        $('input[name="field_mapping[' + field + '][api_path]"]').val(mapping.api_path);
    }
});
<?php endif; ?>

// Pre-populate IN/OUT mappings
<?php if(isset($setting) && $setting->in_out_mapping): ?>
let inOutMapping = <?php echo json_encode($setting->in_out_mapping, 15, 512) ?>;

// Set structure
if (inOutMapping.structure) {
    $('input[name="in_out_structure"][value="' + inOutMapping.structure + '"]').prop('checked', true);
    toggleInOutStructure();
}

// Set IN/OUT values
if (inOutMapping.in_values && Array.isArray(inOutMapping.in_values)) {
    $('#in_values').val(inOutMapping.in_values.join(','));
}
if (inOutMapping.out_values && Array.isArray(inOutMapping.out_values)) {
    $('#out_values').val(inOutMapping.out_values.join(','));
}
if (inOutMapping.case_sensitive) {
    $('#case_sensitive').prop('checked', true);
}
<?php endif; ?>

$('#settingsForm').on('submit', function (e) {
    e.preventDefault();

    // Collect mappings before submit
    collectFieldMappings();
    collectInOutMappings();

    $.ajax({
        url: "<?php echo e(route('attendance.settings.update', $setting->id)); ?>",
        method: 'POST',
        data: $(this).serialize(),
        success: function(res) {
            Swal.fire({
                icon: 'success',
                title: 'Success',
                text: res.message
            }).then(() => {
                window.location.href = "<?php echo e(route('attendance.settings.index')); ?>";
            });
        },
        error: function(xhr) {
            if (xhr.status === 422) {
                let errors = xhr.responseJSON.errors;
                let errorMsg = Object.values(errors).flat().join('\n');
                Swal.fire({
                    icon: 'error',
                    title: 'Validation Error',
                    text: errorMsg
                });
            } else {
                Swal.fire({
                    icon: 'error',
                    title: 'Error',
                    text: 'Something went wrong'
                });
            }
        }
    });
});
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/codeproh/va.laraoffice.com/app/Modules/HR/Attendance/Providers/../Views/settings/edit.blade.php ENDPATH**/ ?>