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

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

@section('body')
<main class="app-content">
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h4>Poetries</h4>
            <a href="{{ route('poetries.create') }}" class="btn btn-primary btn-sm">
                <i class="fa fa-plus"></i> Add New
            </a>
        </div>
        <div class="card-body">

            {{-- Filter Form --}}
            <form method="GET" action="{{ route('poetries.index') }}" class="row g-3 mb-4">
                <div class="col-md-3">
                    <input type="text" name="title" class="form-control" placeholder="Title" value="{{ request('title') }}">
                </div>
                <div class="col-md-2">
                    <select name="poet_id" class="form-control">
                        <option value="">All Poets</option>
                        @foreach($poets as $poet)
                            <option value="{{ $poet->id }}" {{ request('poet_id') == $poet->id ? 'selected' : '' }}>
                                {{ $poet->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <select name="cat_id" class="form-control">
                        <option value="">All Categories</option>
                        @foreach($categories as $cat)
                            <option value="{{ $cat->id }}" {{ request('cat_id') == $cat->id ? 'selected' : '' }}>
                                {{ $cat->title_ps }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <select name="alphabit" class="form-control">
                        <option value="">All Alphabets</option>
                        @foreach($alphabets as $alpha)
                            <option value="{{ $alpha->name }}" {{ request('alphabit') == $alpha->name ? 'selected' : '' }}>
                                {{ $alpha->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3 d-flex">
                    <button type="submit" class="btn btn-info me-2">Filter</button>
                    <a href="{{ route('poetries.index') }}" class="btn btn-secondary">Reset</a>
                </div>
            </form>

            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Title</th>
                        <th>Poet</th>
                        <th>Category</th>
                        <th>Alphabet</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($data as $row)
                    <tr>
                        <td>{{ $loop->iteration }}</td>
                        <td>{{ $row->title }}</td>
                        <td>{{ $row->poet->name ?? '-' }}</td>
                        <td>{{ $row->category->title_ps ?? '-' }}</td>
                        <td>{{ $row->alphabit }}</td>
                        <td>
                            <a href="{{ route('poetries.show', $row->id) }}" class="btn btn-sm btn-info" title="View">
                                <i class="fa fa-eye"></i>
                            </a>
                            <a href="{{ route('poetries.edit', $row->id) }}" class="btn btn-sm btn-warning" title="Edit">
                                <i class="fa fa-edit"></i>
                            </a>
                            <form action="{{ route('poetries.delete', $row->id) }}" method="POST" class="d-inline" onsubmit="return confirm('Delete this poetry?')">
                                @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="6" class="text-center">No poetries found.</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>

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

@section('pageJs')
<script>
    $(".app-menu").removeClass("active");
    $("#nav_poetriesi").addClass("active");
    $("#poetriesi").addClass("is-expanded");
    $("#poetries_history").addClass("active");
</script>
@endsection
