@extends('layouts.app')

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

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

            <div class="mb-3">
                <label class="form-label">Bank <span class="text-danger">*</span></label>
                <select name="bank_id" class="form-control" required>
                    <option value="">-- Select --</option>
                    @foreach($banks as $bank)
                        <option value="{{ $bank->id }}"
                            {{ $bankBranch->bank_id == $bank->id ? 'selected' : '' }}>
                            {{ $bank->name }}
                        </option>
                    @endforeach
                </select>
            </div>

            <div class="mb-3">
                <label class="form-label">Branch Name <span class="text-danger">*</span></label>
                <input type="text" name="name"
                       value="{{ $bankBranch->name }}"
                       class="form-control" maxlength="150" required>
            </div>

            <div class="mb-3">
                <label class="form-label">Branch Code</label>
                <input type="text" name="branch_code"
                       value="{{ $bankBranch->branch_code }}"
                       class="form-control" maxlength="50">
            </div>

            <div class="mb-3">
                <label class="form-label">City</label>
                <select name="city_id" class="form-control">
                    <option value="">-- Select --</option>
                    @foreach($cities as $city)
                        <option value="{{ $city->id }}"
                            {{ $bankBranch->city_id == $city->id ? 'selected' : '' }}>
                            {{ $city->name }}
                        </option>
                    @endforeach
                </select>
            </div>

            <div class="mb-3">
                <label class="form-label">Routing Code</label>
                <input type="text" name="routing_code"
                       value="{{ $bankBranch->routing_code }}"
                       class="form-control" maxlength="50">
            </div>

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

            <button type="submit" class="btn btn-primary">Update</button>
            <a href="{{ route('settings.bank-branches.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('settings.bank-branches.update', $bankBranch->id) }}",
        type: "POST",
        data: $(this).serialize(),
        success: function (res) {
            Swal.fire('Success', res.message, 'success')
                .then(() => window.location.href = "{{ route('settings.bank-branches.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
