@extends('layouts.app')

@section('content')
<div class="card">
    <div class="card-header">
        <h5>New Exit</h5>
    </div>

    <div class="card-body">

        <form method="POST" id="createForm">
        @csrf

        <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>
                    @foreach($employees as $emp)
                        <option value="{{ $emp->id }}"
                            data-working="{{ $emp->currently_working ? 1 : 0 }}"
                            data-attendance="{{ $emp->attendance ? 1 : 0 }}"
                            data-payroll="{{ $emp->payroll ? 1 : 0 }}">
                            {{ $emp->employee_code }} - {{ $emp->first_name }} {{ $emp->last_name }}
                        </option>
                    @endforeach
                </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">
            {{-- CURRENT STATUS --}}
            <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>

            {{-- STATUS AFTER EXIT --}}
            <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="{{ route('core.employee-exits.index') }}"
           class="btn btn-secondary">Cancel</a>

        </form>

    </div>
</div>
@endsection

@section('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: "{{ route('core.employee-exits.store') }}",
        type: "POST",
        data: $(this).serialize(),
        success: function (res) {
            Swal.fire('Success', res.message, 'success')
                .then(() => window.location.href =
                    "{{ 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>
@endsection
