@extends('layouts.app')

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

    <div class="card-body">
        <form id="createForm">
            @csrf

            <div class="mb-3">
                <label class="form-label">Country <span class="text-danger">*</span></label>
                <select name="country_id" id="country_id" class="form-control" required>
                    <option value="">-- Select --</option>
                    @foreach($countries as $country)
                        <option value="{{ $country->id }}">
                            {{ $country->name }}
                        </option>
                    @endforeach
                </select>
            </div>

            <div class="mb-3">
                <label class="form-label">State <span class="text-danger">*</span></label>
                <select name="state_id" id="state_id" class="form-control" required>
                    <option value="">-- Select --</option>
                </select>
            </div>

            <div class="mb-3">
                <label class="form-label">City Name <span class="text-danger">*</span></label>
                <input type="text" name="name" class="form-control" maxlength="150" required>
            </div>

            <div class="form-check mb-3">
                <input type="checkbox" name="status" class="form-check-input" checked>
                <label class="form-check-label">Active</label>
            </div>

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

@section('scripts')
<script>
$('#country_id').on('change', function () {
    let countryId = $(this).val();
    $('#state_id').html('<option value="">Loading...</option>');

    if (countryId) {
        $.get("{{ url('settings/states/by-country') }}/" + countryId, function (data) {
            let options = '<option value="">-- Select --</option>';
            data.forEach(function (state) {
                options += `<option value="${state.id}">${state.name}</option>`;
            });
            $('#state_id').html(options);
        });
    } else {
        $('#state_id').html('<option value="">-- Select --</option>');
    }
});

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

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

    $.ajax({
        url: "{{ route('settings.cities.store') }}",
        type: "POST",
        data: $(this).serialize(),
        success: function (res) {
            Swal.fire('Success', res.message, 'success')
                .then(() => window.location.href = "{{ route('settings.cities.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
