@extends('layouts.app')

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

    <div class="card-body">
        <form id="editForm">
            @csrf
            @method('PUT')

            <div class="mb-3">
                <label class="form-label">Branch <span class="text-danger">*</span></label>
                <select name="branch_id" class="form-control">
                    @foreach($branches as $branch)
                        <option value="{{ $branch->id }}"
                            {{ $grade->branch_id == $branch->id ? 'selected' : '' }}>
                            {{ $branch->name }}
                        </option>
                    @endforeach
                </select>
            </div>

            <div class="mb-3">
                <label class="form-label">Grade Code <span class="text-danger">*</span></label>
                <input type="text" name="name"
                       value="{{ $grade->name }}"
                       class="form-control" maxlength="50" required>
            </div>

            <div class="mb-3">
                <label class="form-label">Min Salary</label>
                <input type="number" name="min_salary" step="0.01"
                       value="{{ $grade->min_salary }}"
                       class="form-control">
            </div>

            <div class="mb-3">
                <label class="form-label">Max Salary</label>
                <input type="number" name="max_salary" step="0.01"
                       value="{{ $grade->max_salary }}"
                       class="form-control">
            </div>

            <div class="mb-3">
                <label class="form-label">Currency</label>
                <select name="currency_id" class="form-control">
                    <option value="">-- Select --</option>
                    @foreach($currencies as $currency)
                        <option value="{{ $currency->id }}"
                            {{ $grade->currency_id == $currency->id ? 'selected' : '' }}>
                            {{ $currency->iso_code }}
                        </option>
                    @endforeach
                </select>
            </div>

            <div class="form-check mb-3">
                <input type="checkbox" name="status"
                       class="form-check-input"
                       {{ $grade->status ? 'checked' : '' }}>
                <label class="form-check-label">Active</label>
            </div>

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

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

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

    $.ajax({
        url: "{{ route('core.grades.update', $grade->id) }}",
        type: "POST",
        data: $(this).serialize(),
        success: function (res) {
            Swal.fire('Success', res.message, 'success')
                .then(() => window.location.href = "{{ route('core.grades.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>
@endsection
