@extends('layouts.app')

@section('title', $pagename . ' page ' . $page . '  Latest News - Sports Portal')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-12">
            <h1 class="mb-4">{{ strtoupper($pagename) }}</h1>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-8">
            <div class="blog-posts index-post-wrap grid-items">
                @if(isset($sportsData) && is_array($sportsData))
                    @php
                        $articles = $sportsData['items'] ?? null;
                        if ($articles === null) {
                            $articles = array_values(array_filter($sportsData, function($item) {
                                return is_array($item) && isset($item['sports']) && $item['sports'] != null;
                            }));
                        } else {
                            $articles = array_values(array_filter($articles, function($item) {
                                return is_array($item) && isset($item['sports']) && $item['sports'] != null;
                            }));
                        }
                    @endphp

                    @foreach(($articles ?? []) as $key => $item)
                        <article class="post hentry item-0 mb-4">
                            <div class="card">
                                <div class="row g-0">
                                    @if(!empty($item['poster']))
                                    <div class="col-md-4">
                                        <img class="img-fluid rounded-start" src="{{ $item['poster'] }}" alt="{{ $item['title'] }}">
                                    </div>
                                    @endif
                                    <div class="col-md-8">
                                        <div class="card-body">
                                            <span class="badge bg-primary mb-2">{{ strtoupper($item['sports']) }}</span>
                                            <h5 class="card-title">
                                                @php
                                                    $hasParams = isset($item['sports'], $item['type'], $item['idsport'], $item['id'], $item['news']) && $item['sports'] && $item['type'] && $item['idsport'] && $item['id'] && $item['news'];
                                                    $fallbackLink = isset($item['link']) && $item['link'] ? $item['link'] : '#';
                                                    $link =  $fallbackLink ;
                                                    // echo var_dump($item);
                                                    switch ($item['type']) {
                                                        case 'news':
                                                             $link =route('sports.article', [
                                                            'sport' => $item['sports'],
                                                            'type' => $item['type'],
                                                            'idsport' => $item['idsport'],
                                                            'id' => $item['id'],
                                                            'term' => $item['news']??'xxxe'
                                                            ]);
                                                            break;
                                                        case 'report':
                                                             $link =route('sports.article', [
                                                            'sport' => $item['sports'],
                                                            'type' => $item['type'],
                                                            'idsport' => $id,
                                                            'id' => $item['id'],
                                                            'term' => $item['news']??'xxxe'
                                                            ]);
                                                            break;
                                                        case 'video':
                                                             $link =route('sports.article', [
                                                            'sport' => $item['sports'],
                                                            'type' => $item['type'],
                                                            'idsport' => $item['idsport'],
                                                            'id' => $item['id'],
                                                            'term' => $item['news']??'xxx'
                                                            ]);
                                                        case 'live-blog':
                                                             $link =route('sports.article', [
                                                            'sport' => $item['sports'],
                                                            'type' => $item['type'],
                                                            'idsport' => $item['idsport'],
                                                            'id' => $item['id'],
                                                            'term' => $item['news']??'xxxx'
                                                            ]);
                                                            break;
                                                        
                                                        default:
                                                             $link =route('sports.article', [
                                                            'sport' => $item['sports'],
                                                            'type' => $item['type'],
                                                            'idsport' => $item['idsport'],
                                                            'id' => $item['id'],
                                                            'term' => $item['news']??'xxxe'
                                                            ]);
                                                            break;
                                                    }
                                            // echo json_encode($item);
                                              @endphp
                                                <a href="{{ $link }}" class="text-decoration-none">
                                                    {{ $item['title'] }}
                                                </a>
                                            </h5>
                                            <p class="card-text">
                                                <small class="text-muted">
                                                    <i class="fas fa-tag"></i> {{ $item['tag'] ?? 'Sports' }}
                                                </small>
                                            </p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </article>
                        @if($key == 19) @break @endif
                    @endforeach
                @else
                    <div class="alert alert-info">
                        <h4>No Sports Data Available</h4>
                        <p>Unable to fetch sports data at the moment. Please try again later.</p>
                    </div>
                @endif
            </div>
  
            <!-- Pagination -->
            @php
                $currentPage = $page ?? 1;
                $hasNext = $sportsData['next'] ?? false;
            @endphp
            <nav aria-label="Page navigation">
                <ul class="pagination justify-content-center">
                    @if($currentPage > 1)
                        <li class="page-item">
                            <a class="page-link" href="{{ route('sports.category', ['page' => $currentPage - 1, 'category' => $term, 'id' => $id]) }}">Previous</a>
                        </li>
                    @endif
                    @if($hasNext)
                        <li class="page-item">
                            <a class="page-link" href="{{ route('sports.category', ['page' => $currentPage + 1, 'category' => $term, 'id' => $id]) }}">Next</a>
                        </li>
                    @endif
                </ul>
            </nav>
        </div>
        
        <!-- Sidebar -->
        <div class="col-md-4">
            @include('partials.sidebar')
        </div>
    </div>
</div>
@endsection
