@extends('back-end.master.app')

@section('title', 'Poets Management')

@section('body')
<main class="app-content">
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h4>Poets</h4>
            <div>

                <a href="{{ route('poets.create') }}" class="btn btn-primary btn-sm">
                    <i class="fa fa-plus"></i> Add New
                </a>
            </div>
        </div>
        <div class="card-body">

            {{-- Filter Form --}}
            <form method="GET" action="{{ route('poets.index') }}" class="row g-3 mb-4">
                <div class="col-md-4">
                    <input type="text" name="name" class="form-control" placeholder="Name" value="{{ request('name') }}">
                </div>
                <div class="col-md-3">
                    <select name="centry_id" class="form-select">
                        <option value="">All Centuries</option>
                        @foreach($centries as $cent)
                            <option value="{{ $cent->id }}" {{ request('centry_id') == $cent->id ? 'selected' : '' }}>
                                {{ $cent->title_ps }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3">
                    <input type="text" name="address" class="form-control" placeholder="Address" value="{{ request('address') }}">
                </div>
                <div class="col-md-2 d-flex">
                    <button type="submit" class="btn btn-info w-50 me-1">Filter</button>
                    <a href="{{ route('poets.index') }}" class="btn btn-secondary w-50">Reset</a>
                </div>
            </form>

            {{-- Poets Table --}}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Image</th>
                            <th>Name</th>
                            <th>Century</th>
                            <th>Address</th>
                            <th>Poetries</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($poets as $row)
                        <tr>
                            <td>{{ $loop->iteration }}</td>
                            <td>
                                @if($row->image && $row->image != 'image/noimage.jpg')
                                    <img src="{{ asset('storage/'.$row->image) }}" alt="{{ $row->name }}" style="width: 50px; height: 50px; object-fit: cover; border-radius: 50%;">
                                @else
                                    <span>No image</span>
                                @endif
                            </td>
                            <td>{{ $row->name }}</td>
                            <td>{{ $row->centry->title_ps ?? '-' }}</td>
                            <td>{{ $row->address }}</td>
                            <td>{{ $row->poetries_count ?? $row->poetries()->count() }}</td>
                            <td>
                                <a href="{{ route('poets.show', $row->id) }}" class="btn btn-sm btn-info" title="View">
                                    <i class="fa fa-eye"></i>
                                </a>
                                <a href="{{ route('poets.edit', $row->id) }}" class="btn btn-sm btn-warning" title="Edit">
                                    <i class="fa fa-edit"></i>
                                </a>
                                <form action="{{ route('poets.delete', $row->id) }}" method="POST" class="d-inline" onsubmit="return confirm('Delete this poet? All related poetries will also be deleted.')">
                                    @csrf
                                    @method('DELETE')
                                    <button class="btn btn-sm btn-danger" title="Delete"><i class="fa fa-trash"></i></button>
                                </form>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="7" class="text-center">No poets found.</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            {{ $poets->links('pagination::bootstrap-4') }}
        </div>
    </div>
</main>
@endsection

@section('pageJs')
<script>
    // Active menu highlight
    $(".app-menu").removeClass("active");
    $("#nav_poetsi").addClass("active");
    $("#poetsi").addClass("is-expanded");
    $("#poets_history").addClass("active");
</script>
@endsection
