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

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

@section('body')
<main class="app-content">
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h4>Posts</h4>
            <div>
                <a href="{{ route('posts.report') }}" class="btn btn-info btn-sm me-2">
                    <i class="fa fa-bar-chart"></i> Report
                </a>
                <a href="{{ route('posts.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('posts.index') }}" class="row g-3 mb-4">
                <div class="col-md-2">
                    <input type="text" name="title" class="form-control" placeholder="Title" value="{{ request('title') }}">
                </div>
                <div class="col-md-2">
                    <select name="category_id" class="form-select">
                        <option value="">All Categories</option>
                        @foreach($categories as $cat)
                            <option value="{{ $cat->id }}" {{ request('category_id') == $cat->id ? 'selected' : '' }}>
                                {{ $cat->title_en }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <select name="language_id" class="form-select">
                        <option value="">All Languages</option>
                        @foreach($languages as $lang)
                            <option value="{{ $lang->id }}" {{ request('language_id') == $lang->id ? 'selected' : '' }}>
                                {{ $lang->title_en }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <select name="status" class="form-select">
                        <option value="">All Status</option>
                        <option value="1" {{ request('status') == '1' ? 'selected' : '' }}>Published</option>
                        <option value="0" {{ request('status') == '0' ? 'selected' : '' }}>Draft</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <input type="date" name="date_from" class="form-control" placeholder="From" value="{{ request('date_from') }}">
                </div>
                <div class="col-md-2">
                    <input type="date" name="date_to" class="form-control" placeholder="To" value="{{ request('date_to') }}">
                </div>
                <div class="col-md-1">
                    <button type="submit" class="btn btn-info w-100">Filter</button>
                </div>
                <div class="col-md-1">
                    <a href="{{ route('posts.index') }}" class="btn btn-secondary w-100">Reset</a>
                </div>
            </form>

            {{-- Posts Table --}}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Image</th>
                            <th>Title</th>
                            <th>Category</th>
                            <th>Language</th>
                            <th>Date</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($posts as $row)
                        <tr>
                            <td>{{ $loop->iteration }}</td>
                            <td>
                                @if($row->image && $row->image != 'image/noimage.jpg')
                                    <img src="{{ asset('storage/'.$row->image) }}" alt="thumb" style="width: 50px; height: 40px; object-fit: cover;">
                                @else
                                    <span>No image</span>
                                @endif
                            </td>
                            <td>{{ Str::limit($row->title, 30) }}</td>
                            <td>{{ $row->category->title_en ?? '-' }}</td>
                            <td>{{ $row->language->title_en ?? '-' }}</td>
                            <td>{{ $row->date }}</td>
                            <td>
                                @if($row->status == 1)
                                    <span class="badge bg-success">Published</span>
                                @else
                                    <span class="badge bg-warning">Draft</span>
                                @endif
                            </td>
                            <td>
                                <a href="{{ route('posts.show', $row->id) }}" class="btn btn-sm btn-info" title="View">
                                    <i class="fa fa-eye"></i>
                                </a>
                                <a href="{{ route('posts.edit', $row->id) }}" class="btn btn-sm btn-warning" title="Edit">
                                    <i class="fa fa-edit"></i>
                                </a>
                                <a href="{{ route('posts.status', $row->id) }}" class="btn btn-sm {{ $row->status ? 'btn-secondary' : 'btn-success' }}" title="{{ $row->status ? 'Unpublish' : 'Publish' }}">
                                    <i class="fa {{ $row->status ? 'fa-eye-slash' : 'fa-eye' }}"></i>
                                </a>
                                <form action="{{ route('posts.destroy', $row->id) }}" method="POST" class="d-inline" onsubmit="return confirm('Are you sure?')">
                                    @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="8" class="text-center">No posts found.</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

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

@section('pageJs')
<script>
    // Highlight active menu
    $(".app-menu").removeClass("active");
    $("#nav_stocki").addClass("active");
    $("#stocki").addClass("is-expanded");
    $("#posts").addClass("active");
</script>
@endsection
