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

@section('content')
<div class="page-header">
    <div class="d-flex align-items-center justify-content-between flex-wrap gap-2">
        <div class="d-flex align-items-center gap-2">
            <a href="{{ route('training.index') }}" class="btn btn-sm btn-outline-secondary"><i class="bi bi-arrow-left"></i></a>
            <div>
                <h1>{{ $training->title }}</h1>
                <nav aria-label="breadcrumb"><ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ route('training.index') }}">Training</a></li>
                    <li class="breadcrumb-item active">{{ Str::limit($training->title, 40) }}</li>
                </ol></nav>
            </div>
        </div>
        <div class="d-flex gap-2 align-items-center">
            <span class="badge bg-{{ $training->status==='ongoing'?'success':($training->status==='scheduled'?'primary':($training->status==='completed'?'secondary':'danger')) }} fs-6">
                {{ ucfirst($training->status) }}
            </span>
            @if(auth()->user()->isEmployee())
            @php $myEmp = auth()->user()->employee; $isEnrolled = $myEmp && $training->participants->contains('id',$myEmp->id); @endphp
            @if($isEnrolled)
            <span class="badge bg-success fs-6"><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-primary btn-sm"><i class="bi bi-person-plus me-1"></i>Enroll Me</button>
            </form>
            @endif
            @endif
        </div>
    </div>
</div>

<div class="row g-3">
    <div class="col-12 col-lg-4">
        <div class="card mb-3">
            <div class="card-header" style="font-size:.83rem;"><i class="bi bi-info-circle me-2" style="color:var(--ul-primary);"></i>Program Info</div>
            <div class="card-body">
                @php
                $info = [
                    ['Type',        ucfirst($training->type)],
                    ['Start Date',  \Carbon\Carbon::parse($training->start_date)->format('M j, Y')],
                    ['End Date',    \Carbon\Carbon::parse($training->end_date)->format('M j, Y')],
                    ['Location',    $training->location],
                    ['Facilitator', $training->facilitator],
                    ['Provider',    $training->provider],
                    ['Max Slots',   $training->max_participants ? $training->max_participants.' participants' : 'No limit'],
                    ['Cost',        $training->cost ? '$'.number_format($training->cost,2) : 'Free'],
                    ['Created By',  $training->creator->name ?? '—'],
                ];
                @endphp
                @foreach($info as [$label, $value])
                @if($value)
                <div class="d-flex justify-content-between py-2" style="border-bottom:1px solid var(--card-border);font-size:.83rem;">
                    <span style="color:var(--text-2);">{{ $label }}</span>
                    <span class="fw-semibold">{{ $value }}</span>
                </div>
                @endif
                @endforeach
                @if($training->description)
                <div class="mt-3">
                    <div style="font-size:.7rem;color:var(--text-2);font-weight:700;text-transform:uppercase;letter-spacing:.06em;">Description</div>
                    <div style="font-size:.84rem;margin-top:.35rem;line-height:1.65;color:var(--text-1);">{{ $training->description }}</div>
                </div>
                @endif
            </div>
        </div>
    </div>

    <div class="col-12 col-lg-8">
        {{-- Enrolled participants --}}
        <div class="card mb-3">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span><i class="bi bi-people me-2" style="color:var(--ul-primary);"></i>Participants ({{ $training->participants->count() }})</span>
                @if(auth()->user()->isHrOrAdmin() && $available->count())
                <button class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#enrollModal">
                    <i class="bi bi-person-plus me-1"></i>Enroll Employees
                </button>
                @endif
            </div>
            @if($training->participants->count())
            <div class="table-responsive">
                <table class="table mb-0">
                    <thead><tr>
                        <th>Employee</th>
                        <th class="d-none d-md-table-cell">Department</th>
                        <th>Status</th>
                        <th class="d-none d-lg-table-cell">Score</th>
                        @if(auth()->user()->isHrOrAdmin() && $training->status === 'completed')<th class="text-end">Action</th>@endif
                    </tr></thead>
                    <tbody>
                    @foreach($training->participants as $p)
                    <tr>
                        <td>
                            <div class="d-flex align-items-center gap-2">
                                <div class="avatar avatar-sm" style="background:linear-gradient(135deg,var(--ul-primary),var(--ul-accent));color:#fff;flex-shrink:0;">
                                    {{ strtoupper(substr($p->first_name??'?',0,1)) }}
                                </div>
                                <div class="fw-semibold" style="font-size:.83rem;">{{ $p->full_name }}</div>
                            </div>
                        </td>
                        <td class="d-none d-md-table-cell" style="font-size:.82rem;color:var(--text-2);">{{ $p->department->name ?? '—' }}</td>
                        <td>
                            <span class="badge bg-{{ $p->pivot->status==='completed'?'success':($p->pivot->status==='enrolled'?'primary':'secondary') }}">
                                {{ ucfirst($p->pivot->status) }}
                            </span>
                            @if($p->pivot->certificate_issued)
                            <span class="badge bg-success ms-1" style="font-size:.65rem;"><i class="bi bi-award me-1"></i>Certificate</span>
                            @endif
                        </td>
                        <td class="d-none d-lg-table-cell">{{ $p->pivot->score ? $p->pivot->score.'%' : '—' }}</td>
                        @if(auth()->user()->isHrOrAdmin() && $training->status === 'completed')
                        <td class="text-end">
                            @if($p->pivot->status !== 'completed')
                            <form method="POST" action="{{ route('training.complete', [$training, $p]) }}">
                                @csrf
                                <div class="d-flex gap-1 justify-content-end">
                                    <input type="number" name="score" class="form-control form-control-sm" style="width:70px;" placeholder="%" min="0" max="100">
                                    <label class="d-flex align-items-center gap-1" style="font-size:.75rem;white-space:nowrap;">
                                        <input type="checkbox" name="certificate_issued" value="1"> Cert
                                    </label>
                                    <button class="btn btn-sm btn-success" style="white-space:nowrap;">✓ Done</button>
                                </div>
                            </form>
                            @else
                            <span style="font-size:.78rem;color:var(--text-2);">Completed</span>
                            @endif
                        </td>
                        @endif
                    </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
            @else
            <div class="card-body text-center py-4" style="color:var(--text-2);">
                <i class="bi bi-people d-block" style="font-size:2rem;margin-bottom:.5rem;"></i>
                No participants enrolled yet.
            </div>
            @endif
        </div>
    </div>
</div>

{{-- Enroll Modal --}}
@if(auth()->user()->isHrOrAdmin() && $available->count())
<div class="modal fade" id="enrollModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content" style="background:var(--card-bg);border:1px solid var(--card-border);">
            <div class="modal-header" style="border-color:var(--card-border);">
                <h5 class="modal-title" style="color:var(--text-1);">Enroll Employees</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="{{ route('training.enroll', $training) }}">
            @csrf
            <div class="modal-body">
                <div style="max-height:360px;overflow-y:auto;">
                    @foreach($available as $emp)
                    <label class="d-flex align-items-center gap-2 p-2 rounded-2" style="cursor:pointer;font-size:.84rem;color:var(--text-1);">
                        <input type="checkbox" name="employee_ids[]" value="{{ $emp->id }}" style="accent-color:var(--ul-primary);">
                        <div class="avatar avatar-sm" style="background:linear-gradient(135deg,var(--ul-primary),var(--ul-accent));color:#fff;flex-shrink:0;">{{ strtoupper(substr($emp->first_name??'?',0,1)) }}</div>
                        <div>
                            <div class="fw-semibold">{{ $emp->full_name }}</div>
                            <div style="font-size:.72rem;color:var(--text-2);">{{ $emp->department->name ?? '' }}</div>
                        </div>
                    </label>
                    @endforeach
                </div>
            </div>
            <div class="modal-footer" style="border-color:var(--card-border);">
                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="submit" class="btn btn-primary"><i class="bi bi-person-plus me-1"></i>Enroll Selected</button>
            </div>
            </form>
        </div>
    </div>
</div>
@endif
@endsection
