@extends('layouts.app')

@section('content')
    <div class="row g-4">
        <div class="col-lg-4">
            <div class="card shadow-sm">
                <div class="card-header fw-bold">Add Country</div>
                <div class="card-body">
                    <form method="POST" action="{{ route('countries.store') }}" class="row g-2">
                        @csrf
                        <div class="col-12">
                            <label class="form-label">Continent <span class="text-danger">*</span></label>
                            <select name="continent_id" class="form-select" required>
                                <option value="">— Select —</option>
                                @foreach($continents as $c)
                                    <option value="{{ $c->id }}" {{ old('continent_id') == $c->id ? 'selected' : '' }}>{{ $c->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-12">
                            <label class="form-label">Name <span class="text-danger">*</span></label>
                            <input name="name" class="form-control" required maxlength="255" value="{{ old('name') }}">
                        </div>
                        <div class="col-12">
                            <label class="form-label">Status</label>
                            <select name="status" class="form-select">
                                <option value="1" {{ old('status', '1') == '1' ? 'selected' : '' }}>Active</option>
                                <option value="0" {{ old('status') === '0' ? 'selected' : '' }}>Inactive</option>
                            </select>
                        </div>
                        <div class="col-12 d-grid">
                            <button class="btn btn-success">Save</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="col-lg-8">
            <div class="card shadow-sm">
                <div class="card-header fw-bold">Countries</div>
                <div class="card-body p-0">
                    <table class="table table-sm mb-0">
                        <thead>
                        <tr>
                            <th>Name</th>
                            <th>Continent</th>
                            <th>Status</th>
                            <th></th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($countries as $row)
                            <tr>
                                <td>{{ $row->name }}</td>
                                <td>{{ optional($row->parentContinent)->name ?: $row->continent }}</td>
                                <td>{{ $row->status ? 'Active' : 'Inactive' }}</td>
                                <td class="text-end">
                                    <a href="{{ route('countries.edit', $row) }}" class="btn btn-sm btn-outline-secondary">Edit</a>
                                    <form method="POST" action="{{ route('countries.destroy', $row) }}" class="d-inline" onsubmit="return confirm('Delete this country?');">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-sm btn-outline-danger">Delete</button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="text-center text-muted py-3">No countries yet.</td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@endsection
