@extends('layouts.app')
@section('title','Training & Development')
@section('page-title','Training & Development')

@section('content')
<div class="page-header">
    <div class="d-flex align-items-start justify-content-between flex-wrap gap-2">
        <div>
            <h1>Training & Development</h1>
            <nav aria-label="breadcrumb"><ol class="breadcrumb">
                <li class="breadcrumb-item active">
                    {{ auth()->user()->isEmployee() ? 'Browse and enroll in programs' : 'Manage training programs' }}
                </li>
            </ol></nav>
        </div>
        @if(auth()->user()->isHrOrAdmin())
        <a href="{{ route('training.create') }}" class="btn btn-primary">
            <i class="bi bi-plus-lg me-1"></i>New Program
        </a>
        @endif
    </div>
</div>

{{-- Stats --}}
<div class="row g-3 mb-4">
    <div class="col-6 col-md-3">
        <div class="stat-card sc-teal"><div class="stat-icon"><i class="bi bi-mortarboard"></i></div>
            <div class="stat-value">{{ $totals['total'] ?? 0 }}</div><div class="stat-label">Total Programs</div></div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card sc-success"><div class="stat-icon"><i class="bi bi-play-circle"></i></div>
            <div class="stat-value">{{ $totals['ongoing'] ?? 0 }}</div><div class="stat-label">Ongoing</div></div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card sc-primary"><div class="stat-icon"><i class="bi bi-calendar-event"></i></div>
            <div class="stat-value">{{ $totals['scheduled'] ?? 0 }}</div><div class="stat-label">Scheduled</div></div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card sc-gold"><div class="stat-icon"><i class="bi bi-people"></i></div>
            <div class="stat-value">{{ $totals['enrolled'] ?? 0 }}</div>
            <div class="stat-label">{{ auth()->user()->isEmployee() ? 'My Enrolments' : 'Total Enrolled' }}</div></div>
    </div>
</div>

{{-- Filters --}}
<div class="card mb-3">
    <div class="card-body py-3">
        <form method="GET" class="row g-2 align-items-end">
            <div class="col-12 col-sm-4 col-md-3">
                <input type="search" name="search" class="form-control" placeholder="Search training programs…" value="{{ request('search') }}">
            </div>
            <div class="col-6 col-md-2">
                <select name="status" class="form-select">
                    <option value="">All Status</option>
                    <option value="scheduled" {{ request('status')=='scheduled'?'selected':'' }}>Scheduled</option>
                    <option value="ongoing" {{ request('status')=='ongoing'?'selected':'' }}>Ongoing</option>
                    <option value="completed" {{ request('status')=='completed'?'selected':'' }}>Completed</option>
                </select>
            </div>
            <div class="col-6 col-md-2">
                <select name="type" class="form-select">
                    <option value="">All Types</option>
                    <option value="internal">Internal</option>
                    <option value="external">External</option>
                    <option value="workshop">Workshop</option>
                    <option value="conference">Conference</option>
                    <option value="online">Online</option>
                </select>
            </div>
            @if(auth()->user()->isEmployee())
            <div class="col-6 col-md-2">
                <select name="enrolled_only" class="form-select">
                    <option value="">All Programs</option>
                    <option value="1" {{ request('enrolled_only')?'selected':'' }}>My Enrolments</option>
                </select>
            </div>
            @endif
            <div class="col-12 col-md-2 d-flex gap-2">
                <button type="submit" class="btn btn-primary flex-fill"><i class="bi bi-search me-1"></i>Filter</button>
                <a href="{{ route('training.index') }}" class="btn btn-outline-secondary"><i class="bi bi-x-lg"></i></a>
            </div>
        </form>
    </div>
</div>

<div class="row g-3">
    @forelse($trainings as $training)
    @php
        $stBg  = match($training->status) { 'ongoing'=>'bg-success','scheduled'=>'bg-primary','completed'=>'bg-secondary','cancelled'=>'bg-danger', default=>'bg-secondary' };
        $isEnrolled = auth()->user()->employee && $training->participants->contains(auth()->user()->employee->id);
    @endphp
    <div class="col-12 col-sm-6 col-xl-4">
        <div class="card h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-start mb-2">
                    <span class="badge {{ $stBg }}">{{ ucfirst($training->status) }}</span>
                    <span class="badge bg-secondary" style="font-size:.68rem;">{{ ucfirst($training->type) }}</span>
                </div>
                <div class="fw-bold mb-1" style="font-size:.92rem;">{{ $training->title }}</div>
                @if($training->description)
                <div style="font-size:.79rem;color:var(--text-2);margin-bottom:.75rem;line-height:1.55;">{{ Str::limit($training->description,90) }}</div>
                @endif

                <div class="d-flex flex-column gap-1 mb-3" style="font-size:.74rem;color:var(--text-2);">
                    <div><i class="bi bi-calendar3 me-2" style="color:var(--ul-primary);"></i>
                        {{ \Carbon\Carbon::parse($training->start_date)->format('M d') }} — {{ \Carbon\Carbon::parse($training->end_date)->format('M d, Y') }}
                    </div>
                    @if($training->location)
                    <div><i class="bi bi-geo-alt me-2" style="color:var(--ul-primary);"></i>{{ $training->location }}</div>
                    @endif
                    <div>
                        <i class="bi bi-people me-2" style="color:var(--ul-primary);"></i>
                        {{ $training->participants_count ?? 0 }} enrolled
                        @if($training->max_participants) / {{ $training->max_participants }} max @endif
                    </div>
                    @if($training->facilitator)
                    <div><i class="bi bi-person-badge me-2" style="color:var(--ul-primary);"></i>{{ $training->facilitator }}</div>
                    @endif
                </div>

                <div class="d-flex gap-2 mt-auto">
                    <a href="{{ route('training.show',$training) }}" class="btn btn-outline-primary btn-sm flex-fill">View Details</a>

                    @if(auth()->user()->isEmployee())
                        @if($isEnrolled)
                        <span class="btn btn-sm btn-success disabled">
                            <i class="bi bi-check-circle me-1"></i>Enrolled
                        </span>
                        @elseif(in_array($training->status,['scheduled','ongoing']))
                        <form method="POST" action="{{ route('training.self_enroll',$training) }}">
                            @csrf
                            <button class="btn btn-sm btn-primary">
                                <i class="bi bi-person-plus me-1"></i>Enroll
                            </button>
                        </form>
                        @endif
                    @elseif(auth()->user()->isHrOrAdmin())
                        <a href="{{ route('training.show',$training) }}" class="btn btn-sm btn-outline-secondary">
                            <i class="bi bi-people me-1"></i>Manage
                        </a>
                    @endif
                </div>
            </div>
        </div>
    </div>
    @empty
    <div class="col-12">
        <div class="card p-5 text-center">
            <i class="bi bi-mortarboard d-block" style="font-size:3rem;color:var(--text-3);"></i>
            <div class="mt-2 fw-semibold" style="color:var(--text-2);">No training programs found</div>
            @if(auth()->user()->isHrOrAdmin())
            <a href="{{ route('training.create') }}" class="btn btn-primary mt-3">
                <i class="bi bi-plus-lg me-1"></i>Create First Program
            </a>
            @endif
        </div>
    </div>
    @endforelse
</div>

@if($trainings->hasPages())
<div class="mt-3">{{ $trainings->links('pagination::bootstrap-5') }}</div>
@endif
@endsection
