@extends('layouts.app')

@section('title', 'Browse Jobs | Infra24')

@section('content')
<div class="row">
    <div class="col-12">
        <div class="page-title-box d-sm-flex align-items-center justify-content-between">
            <h4 class="mb-sm-0">Browse Jobs</h4>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-lg-3">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title mb-0"><i class="ri-filter-line align-bottom me-1"></i> Filters</h5>
            </div>
            <div class="card-body">
                <form action="{{ route('jobs.index') }}" method="GET">
                    <div class="mb-3">
                        <label class="form-label">Search Keyword</label>
                        <input type="text" name="search" class="form-control" placeholder="Search jobs..." value="{{ request('search') }}">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Job Type</label>
                        <select class="form-select" name="job_type">
                            <option value="">All Types</option>
                            <option value="fixed" {{ request('job_type') == 'fixed' ? 'selected' : '' }}>Fixed Price</option>
                            <option value="hourly" {{ request('job_type') == 'hourly' ? 'selected' : '' }}>Hourly Rate</option>
                        </select>
                    </div>
                    <div class="text-end mt-4">
                        <button type="submit" class="btn btn-primary w-100">Apply Filters</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-9">
        @if($jobs->count() > 0)
            <div class="row g-4">
                @foreach($jobs as $job)
                <div class="col-md-6">
                    <div class="card border list-view-card h-100 mb-0 card-hover-gradient">
                        <div class="card-body">
                            <div class="d-flex align-items-center mb-3">
                                <div class="flex-shrink-0 avatar-sm">
                                    @if($job->company->logo)
                                        <img src="{{ Storage::url($job->company->logo) }}" alt="company" class="avatar-sm rounded img-thumbnail">
                                    @else
                                        <div class="avatar-sm rounded bg-light text-primary d-flex justify-content-center align-items-center">
                                            <i class="ri-building-4-line fs-20"></i>
                                        </div>
                                    @endif
                                </div>
                                <div class="flex-grow-1 ms-3">
                                    <h5 class="fs-16 mb-1"><a href="{{ route('jobs.show', $job->id) }}" class="text-dark">{{ $job->title }}</a></h5>
                                    <p class="text-muted mb-0">{{ $job->company->name ?? 'Unknown Company' }}</p>
                                </div>
                            </div>
                            <p class="text-muted text-truncate-two-lines">{{ Str::limit($job->description, 100) }}</p>
                            
                            <div class="d-flex gap-2 mb-3">
                                <span class="badge badge-soft-info">{{ ucfirst($job->job_type) }}</span>
                                @if($job->budget_min || $job->budget_max)
                                    <span class="badge badge-soft-success">
                                        {{ $job->budget_min ? '$'.$job->budget_min : '' }} 
                                        {{ $job->budget_min && $job->budget_max ? '-' : '' }} 
                                        {{ $job->budget_max ? '$'.$job->budget_max : '' }}
                                    </span>
                                @endif
                            </div>

                            <div class="d-flex gap-1 flex-wrap">
                                @foreach($job->skills->take(3) as $skill)
                                    <span class="badge bg-light text-dark">{{ $skill->name }}</span>
                                @endforeach
                                @if($job->skills->count() > 3)
                                    <span class="badge bg-light text-dark">+{{ $job->skills->count() - 3 }}</span>
                                @endif
                            </div>
                        </div>
                        <div class="card-footer border-top-dashed">
                            <div class="d-flex justify-content-between align-items-center">
                                <div class="text-muted small">
                                    <i class="ri-time-line align-middle me-1"></i> {{ $job->created_at->diffForHumans() }}
                                </div>
                                <a href="{{ route('jobs.show', $job->id) }}" class="btn btn-sm btn-primary">View Details <i class="ri-arrow-right-s-line align-middle ms-1"></i></a>
                            </div>
                        </div>
                    </div>
                </div>
                @endforeach
            </div>
            
            <div class="mt-4">
                {{ $jobs->withQueryString()->links() }}
            </div>
        @else
            <div class="text-center mt-5">
                <lord-icon src="https://cdn.lordicon.com/msoeawqm.json" trigger="loop" colors="primary:#405189,secondary:#0ab39c" style="width:72px;height:72px"></lord-icon>
                <h5 class="mt-4">Sorry! No Jobs Found</h5>
                <p class="text-muted">We couldn't find any jobs matching your search criteria. Try different keywords.</p>
            </div>
        @endif
    </div>
</div>
@endsection
