@extends('layouts.app')

@section('content')

<div class="card">
    <div class="card-header">
        <h5 class="mb-0 fw-semibold">Create Payroll Adjustment</h5>
    </div>
    <div class="card-body">
        <form id="adjustmentForm" action="{{ route('payroll.payroll-adjustments.store') }}" method="POST">
            @csrf
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label for="branch_id" class="form-label">Branch <span class="text-danger">*</span></label>
                    <select class="form-control" id="branch_id" name="branch_id" required>
                        <option value="">-- Select Branch --</option>
                        @foreach($branches as $branch)
                            <option value="{{ $branch->id }}">{{ $branch->name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-6 mb-3">
                    <label for="payroll_group_id" class="form-label">Payroll Group <span class="text-danger">*</span></label>
                    <select class="form-control" id="payroll_group_id" name="payroll_group_id" required>
                        <option value="">-- Select Payroll Group --</option>
                        @foreach($payrollGroups as $group)
                            <option value="{{ $group->id }}">{{ $group->name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label for="date_from" class="form-label">Date From <span class="text-danger">*</span></label>
                    <input type="date" class="form-control" id="date_from" name="date_from" required>
                </div>
                <div class="col-md-6 mb-3">
                    <label for="date_to" class="form-label">Date To <span class="text-danger">*</span></label>
                    <input type="date" class="form-control" id="date_to" name="date_to" required>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12 mb-3">
                    <label for="remarks" class="form-label">Remarks</label>
                    <textarea class="form-control" id="remarks" name="remarks" rows="2"></textarea>
                </div>
            </div>

            <button type="submit" class="btn btn-primary">Save</button>
            <a href="{{ route('payroll.payroll-adjustments.index') }}" class="btn btn-secondary">Back</a>
           
        </form>
    </div>
</div>
@endsection

@section('scripts')
<script>
$('#adjustmentForm').on('submit', function(e){
    e.preventDefault();

    $.post("{{ route('payroll.payroll-adjustments.store') }}", $(this).serialize())
          .done(res => {
             Swal.fire('Success', res.message, 'success')
                .then(() => window.location.href = "{{ route('payroll.payroll-adjustments.index') }}");
        })
        .fail(err => {
            if (err.status === 422) {
                Swal.fire('Validation Error', err.responseJSON.errors, 'error');
            
            } else {
                Swal.fire('Something went wrong','error');
            }
        });
});
</script>
@endsection
