@extends('layouts.app')

@section('title', ($siteName ?? 'Bursa Kerja') . ' - Lowongan Pekerjaan Terbaru')

@section('content')
{{-- Hero Section --}}
<div class="bg-indigo-700 text-white py-10 sm:py-16">
    <div class="max-w-7xl mx-auto px-4 text-center">
        <h1 class="text-2xl sm:text-4xl font-bold mb-4 animate-fade-in-up">Temukan Pekerjaan Impianmu</h1>
        <p class="text-base sm:text-lg text-indigo-100 mb-8 animate-fade-in-up" style="animation-delay: 0.1s">Ribuan lowongan pekerjaan menunggu lamaranmu</p>

        {{-- Search --}}
        <form action="{{ route('home') }}" method="GET" class="max-w-2xl mx-auto animate-fade-in-up" style="animation-delay: 0.2s">
            <div class="flex flex-col sm:flex-row bg-white rounded-xl overflow-hidden">
                <input type="text" name="search" value="{{ request('search') }}"
                    placeholder="Cari posisi, perusahaan, atau lokasi..."
                    class="flex-1 px-6 py-4 text-gray-700 focus:outline-none">
                <button type="submit" class="bg-indigo-800 text-white px-8 py-4 hover:outline hover:outline-2 hover:outline-indigo-900 font-medium">
                    Cari
                </button>
            </div>
            {{-- Filter --}}
            <div class="flex flex-wrap justify-center mt-4 gap-2">
                <a href="{{ route('home') }}"
                    class="filter-pill px-4 py-2 rounded-full text-sm font-medium {{ !request('type') ? 'bg-white text-indigo-800' : 'bg-indigo-500 text-white hover:outline hover:outline-2 hover:outline-indigo-300' }}">
                    Semua
                </a>
                @foreach(['Full-time', 'Part-time', 'Freelance', 'Internship'] as $type)
                <a href="{{ route('home', ['type' => $type, 'search' => request('search')]) }}"
                    class="filter-pill px-4 py-2 rounded-full text-sm font-medium {{ request('type') === $type ? 'bg-white text-indigo-800' : 'bg-indigo-500 text-white hover:outline hover:outline-2 hover:outline-indigo-300' }}">
                    {{ $type }}
                </a>
                @endforeach
            </div>
        </form>
    </div>
</div>

<div class="max-w-7xl mx-auto px-4 py-8">
    {{-- Banner Ad Top (Desktop) --}}
    @if($bannerTop)
    <div class="ad-container ad-desktop">
        <div class="ad-label">Iklan</div>
        <div class="ad-content">{!! $bannerTop->code !!}</div>
    </div>
    @endif

    {{-- Mobile Banner Top --}}
    @if($mobileBanner)
    <div class="ad-container ad-mobile">
        <div class="ad-label">Iklan</div>
        <div class="ad-content">{!! $mobileBanner->code !!}</div>
    </div>
    @endif

    {{-- Job Listings --}}
    @if($jobs->count() > 0)
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 mt-6">
        @foreach($jobs as $index => $job)
        <div class="bg-indigo-600 rounded-xl border border-indigo-500 p-6 card-hover animate-stagger">
            <div class="flex items-start justify-between mb-3">
                <span class="inline-block px-3 py-1 text-xs font-medium rounded-full bg-white/20 text-white">
                    {{ $job->type }}
                </span>
            </div>

            <h3 class="text-lg font-semibold text-white mb-1">{{ $job->title }}</h3>
            <p class="text-white/80 font-medium text-sm mb-2">{{ $job->company }}</p>
            <p class="text-white/70 text-sm mb-3 flex items-center">
                <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"/>
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"/>
                </svg>
                {{ $job->location }}
            </p>

            <div class="text-white font-semibold text-sm mb-4 flex items-center">
                <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M12 6v12m-3-2.818l.879.659c1.171.879 3.07.879 4.242 0 1.172-.879 1.172-2.303 0-3.182C13.536 12.219 12.768 12 12 12c-.725 0-1.45-.22-2.003-.659-1.106-.879-1.106-2.303 0-3.182s2.9-.879 4.006 0l.415.33M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
                {{ $job->formatted_salary }}
            </div>

            <p class="text-white/70 text-sm mb-4 line-clamp-2">{{ Str::limit(strip_tags($job->description), 100) }}</p>

            <a href="{{ route('job.show', $job) }}"
                class="block text-center bg-indigo-800 text-white py-2.5 rounded-lg font-medium hover:outline hover:outline-2 hover:outline-white/50">
                Lihat Detail
            </a>

            @if($appliedJobIds->contains($job->id))
            <p class="mt-3 text-xs text-center text-white/90 bg-white/10 border border-white/20 rounded-lg px-3 py-2">
                Anda sudah melamar posisi ini
            </p>
            @endif
        </div>

        {{-- Native Ad after every 2nd item --}}
        @if(($index + 1) % 2 === 0 && $nativeAd)
        <div class="bg-white rounded-xl border border-gray-100 p-6 ad-container flex items-center justify-center">
            <div>
                <div class="ad-label">IKLAN</div>
                <div class="ad-content">{!! $nativeAd->code !!}</div>
            </div>
        </div>
        @endif
        @endforeach
    </div>

    {{-- Pagination --}}
    <div class="mt-8">
        {{ $jobs->appends(request()->query())->links('pagination') }}
    </div>
    @else
    <div class="text-center py-16 animate-fade-in">
        <svg class="w-16 h-16 mx-auto text-gray-300 mb-4" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-5.197-5.197m0 0A7.5 7.5 0 105.196 5.196a7.5 7.5 0 0010.607 10.607z"/></svg>
        <h3 class="text-xl font-semibold text-gray-700 mb-2">Tidak ada lowongan ditemukan</h3>
        <p class="text-gray-500">Coba ubah kata kunci pencarian Anda</p>
    </div>
    @endif

    {{-- Banner Ad Bottom (Desktop) --}}
    @if($bannerBottom)
    <div class="ad-container mt-8 ad-desktop">
        <div class="ad-label">Iklan</div>
        <div class="ad-content">{!! $bannerBottom->code !!}</div>
    </div>
    @endif

    {{-- Mobile Banner Bottom --}}
    @if($mobileBanner)
    <div class="ad-container mt-8 ad-mobile">
        <div class="ad-label">Iklan</div>
        <div class="ad-content">{!! $mobileBanner->code !!}</div>
    </div>
    @endif
</div>
@endsection
