<?php $__env->startSection('content'); ?>
<div class="card">
    <div class="card-header">
        <h5>Edit Shift Segment</h5>
    </div>

    <div class="card-body">
        <form id="editForm">
            <?php echo csrf_field(); ?>
            <?php echo method_field('PUT'); ?>

            <?php echo $__env->make('core::shift_segment/partials/form', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

            <button type="submit" class="btn btn-primary">Update</button>
            <a href="<?php echo e(route('core.shift-segment.index')); ?>" class="btn btn-secondary">Cancel</a>
        </form>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
    const WEEK_DAYS = [
    'sunday',
    'monday',
    'tuesday',
    'wednesday',
    'thursday',
    'friday',
    'saturday'
];

/**
 * Reorder weekday rows based on selected date
 */
function reorderWeekdaysByDate(dateValue) {
    if (!dateValue) return;

    const date = new Date(dateValue);
    const startIndex = date.getDay(); // 0 = Sunday

    const orderedDays = [
        ...WEEK_DAYS.slice(startIndex),
        ...WEEK_DAYS.slice(0, startIndex)
    ];

    const tbody = $('#weekdayTable tbody');
    const rows = {};

    // cache existing rows
    tbody.find('tr').each(function () {
        const day = $(this).data('weekday');
        rows[day] = $(this);
    });

    tbody.empty();

    // append rows in new order
    orderedDays.forEach(day => {
        if (rows[day]) {
            tbody.append(rows[day]);
        }
    });
}

// on date change
$(document).on('change', '#effectiveFrom', function () {
    reorderWeekdaysByDate(this.value);
});

// initial load (create & edit)
$(document).ready(function () {
    reorderWeekdaysByDate($('#effectiveFrom').val());
});
$('#editForm').on('submit', function (e) {
    e.preventDefault();

    let btn = $(this).find('button[type=submit]');
    btn.prop('disabled', true);

    $.ajax({
        url: "<?php echo e(route('core.shift-segment.update', $shiftSegment->id)); ?>",
        type: "POST",
        data: $(this).serialize(),
        success: function (res) {
            Swal.fire('Success', res.message, 'success')
                .then(() => {
                    window.location.href = "<?php echo e(route('core.shift-segment.index')); ?>";
                });
        },
        error: function (xhr) {
            btn.prop('disabled', false);
            if (xhr.status === 422) {
                let errors = xhr.responseJSON.errors;
                let errorList = '<ul style="text-align: left;">';
                $.each(errors, function(field, messages) {
                    $.each(messages, function(index, message) {
                        errorList += '<li>' + message + '</li>';
                    });
                });
                errorList += '</ul>';
                Swal.fire({
                    title: 'Validation Error',
                    html: errorList,
                    icon: 'error'
                });
            } else {
                Swal.fire('Error', 'An error occurred. Please try again.', 'error');
            }
        }
    });
});
</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/Core/Providers/../Views/shift_segment/edit.blade.php ENDPATH**/ ?>