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

    <div class="card-body">

        <form method="POST" id="createForm">
        <?php echo csrf_field(); ?>

        <div class="row mb-3">
            <div class="col-md-4">
                <label>Employee</label>
                <select name="employee_id" id="employeeSelect"
                        class="form-control" required>
                    <option value="">Select</option>
                    <?php $__currentLoopData = $employees; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $emp): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($emp->id); ?>"
                            data-working="<?php echo e($emp->currently_working ? 1 : 0); ?>"
                            data-attendance="<?php echo e($emp->attendance ? 1 : 0); ?>"
                            data-payroll="<?php echo e($emp->payroll ? 1 : 0); ?>">
                            <?php echo e($emp->employee_code); ?> - <?php echo e($emp->first_name); ?> <?php echo e($emp->last_name); ?>

                        </option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>

            <div class="col-md-4">
                <label>Exit Date</label>
                <input type="date" name="exit_date" class="form-control" required>
            </div>
        </div>

        <div class="row mb-3">
            
            <div class="col-md-6">
                <label class="fw-bold">Current Status</label>
                <div class="border rounded p-2">
                    <div>
                        <input type="checkbox" id="curWorking" disabled>
                        <label>Currently Working</label>
                    </div>
                    <div>
                        <input type="checkbox" id="curAttendance" disabled>
                        <label>Attendance</label>
                    </div>
                    <div>
                        <input type="checkbox" id="curPayroll" disabled>
                        <label>Payroll</label>
                    </div>
                </div>
            </div>

            
            <div class="col-md-6">
                <label class="fw-bold">
                    Status after adding this exited record (To be edited)
                </label>
                <div class="border rounded p-2">
                    <div>
                        <input type="hidden" name="currently_working" value="0">
                        <input type="checkbox" id="afterWorking" name="currently_working" value="1">
                        <label>Currently Working</label>
                    </div>
                    <div>
                        <input type="hidden" name="attendance" value="0">
                        <input type="checkbox" name="attendance"
                               id="afterAttendance" value="1">
                        <label>Attendance</label>
                    </div>
                    <div>
                        <input type="hidden" name="payroll" value="0">
                        <input type="checkbox" name="payroll" value="1"
                               id="afterPayroll">
                        <label>Payroll</label>
                    </div>
                </div>
            </div>
        </div>

        <div class="mb-3">
            <label>Remarks</label>
            <textarea name="remarks" class="form-control"></textarea>
        </div>

        <button class="btn btn-primary">Save</button>
        <a href="<?php echo e(route('core.employee-exits.index')); ?>"
           class="btn btn-secondary">Cancel</a>

        </form>

    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
$('#employeeSelect').on('change', function () {

    let opt = $(this).find(':selected');

    if (!opt.val()) {
        return;
    }

    let working   = opt.data('working') == 1;
    let attendance= opt.data('attendance') == 1;
    let payroll   = opt.data('payroll') == 1;

    // CURRENT STATUS (READ ONLY)
    $('#curWorking').prop('checked', working);
    $('#curAttendance').prop('checked', attendance);
    $('#curPayroll').prop('checked', payroll);

    // STATUS AFTER EXIT
    $('#afterWorking').prop('checked', false); // exited
    $('#afterAttendance').prop('checked', attendance);
    $('#afterPayroll').prop('checked', payroll);
});

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

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

    $.ajax({
        url: "<?php echo e(route('core.employee-exits.store')); ?>",
        type: "POST",
        data: $(this).serialize(),
        success: function (res) {
            Swal.fire('Success', res.message, 'success')
                .then(() => window.location.href =
                    "<?php echo e(route('core.employee-exits.index')); ?>");
        },
        error: function (xhr) {
            btn.prop('disabled', false);
            if (xhr.status === 422) {
                let error = Object.values(xhr.responseJSON.errors)[0][0];
                Swal.fire('Validation Error', error, '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/employee_exits/create.blade.php ENDPATH**/ ?>