@extends('layouts.app')

@section('content')
<div class="container-fluid px-4 py-6">
    <div class="max-w-7xl mx-auto">
        <!-- Header -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h2 class="mb-1">Training Management</h2>
                <p class="text-muted mb-0">Manage and track all training programs</p>
            </div>
            <a href="{{ route('trainings.create') }}" class="btn btn-primary">
                <i class="bi bi-plus-lg"></i> Create Training
            </a>
        </div>

        <!-- Success Message -->
        @if(session('success'))
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                {{ session('success') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        @endif

        <!-- Trainings List -->
        @forelse($trainings as $training)
            <div class="card shadow-sm mb-4">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start mb-3">
                        <div>
                            <h4 class="mb-2">{{ $training->name }}</h4>
                            <p class="text-muted mb-0">{{ $training->summary }}</p>
                        </div>
                        <span class="badge bg-success">Active</span>
                    </div>

                    <div class="row g-4 mb-3">
                        <div class="col-md-3">
                            <div class="d-flex gap-2">
                                <i class="bi bi-calendar3 text-muted"></i>
                                <div>
                                    <div class="text-muted small">Duration</div>
                                    <div class="fw-semibold">{{ $training->duration }} day{{ $training->duration !== 1 ? 's' : '' }}</div>
                                    <div class="text-muted small">{{ $training->start_date->format('M d, Y') }} - {{ $training->end_date->format('M d, Y') }}</div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-3">
                            <div class="d-flex gap-2">
                                <i class="bi bi-geo-alt text-muted"></i>
                                <div>
                                    <div class="text-muted small">Location</div>
                                    <div class="fw-semibold">{{ $training->location }}</div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-3">
                            <div class="d-flex gap-2">
                                <i class="bi bi-people text-muted"></i>
                                <div>
                                    <div class="text-muted small">Participants</div>
                                    <div class="fw-semibold">{{ count($training->participants ?? []) }} / {{ $training->expected_participants }}</div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-3">
                            <div class="d-flex gap-2">
                                <i class="bi bi-person text-muted"></i>
                                <div>
                                    <div class="text-muted small">Trainer</div>
                                    <div class="fw-semibold">{{ $training->trainer_name }}</div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- View Details Accordion -->
                    <div class="accordion" id="training{{ $training->id }}">
                        <div class="accordion-item border-0">
                            <h2 class="accordion-header">
                                <button class="accordion-button collapsed bg-light" type="button" data-bs-toggle="collapse" data-bs-target="#details{{ $training->id }}">
                                    <i class="bi bi-info-circle me-2"></i> View Details
                                </button>
                            </h2>
                            <div id="details{{ $training->id }}" class="accordion-collapse collapse" data-bs-parent="#training{{ $training->id }}">
                                <div class="accordion-body">
                                    <div class="row">
                                        <div class="col-md-6 mb-3">
                                            <h6 class="fw-semibold mb-2">Course Outline</h6>
                                            <p class="text-muted small" style="white-space: pre-wrap;">{{ $training->course_outline }}</p>
                                        </div>

                                        <div class="col-md-6 mb-3">
                                            <h6 class="fw-semibold mb-2">Training Details</h6>
                                            <div class="mb-2">
                                                <strong>Creation Date:</strong> {{ $training->creation_date->format('M d, Y') }}
                                            </div>
                                            <div class="mb-2">
                                                <strong>Supervisor:</strong> {{ $training->supervisor_name }}
                                            </div>
                                            @if($training->remarks)
                                                <div class="mb-2">
                                                    <strong>Remarks:</strong> {{ $training->remarks }}
                                                </div>
                                            @endif
                                        </div>

                                        @if($training->participant_officers && $training->participant_officers->count() > 0)
                                            <div class="col-12 mb-3">
                                                <h6 class="fw-semibold mb-2">Participants ({{ $training->participant_officers->count() }})</h6>
                                                <div class="d-flex flex-wrap gap-2">
                                                    @foreach($training->participant_officers as $officer)
                                                        <span class="badge bg-primary">{{ $officer->name }}</span>
                                                    @endforeach
                                                </div>
                                            </div>
                                        @endif
                                    </div>

                                    <!-- Action Buttons -->
                                    <div class="d-flex gap-2 pt-3 border-top">
                                        <a href="{{ route('trainings.show', $training) }}" class="btn btn-sm btn-outline-primary">
                                            <i class="bi bi-eye"></i> View Full Details
                                        </a>
                                        <a href="{{ route('trainings.edit', $training) }}" class="btn btn-sm btn-outline-secondary">
                                            <i class="bi bi-pencil"></i> Edit
                                        </a>
                                        <form action="{{ route('trainings.destroy', $training) }}" method="POST" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this training?');">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-sm btn-outline-danger">
                                                <i class="bi bi-trash"></i> Delete
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        @empty
            <div class="card shadow-sm text-center py-5">
                <div class="card-body">
                    <i class="bi bi-journal-text" style="font-size: 4rem; color: #ccc;"></i>
                    <h4 class="mt-3 mb-2">No trainings yet</h4>
                    <p class="text-muted mb-4">Get started by creating your first training program</p>
                    <a href="{{ route('trainings.create') }}" class="btn btn-primary">
                        <i class="bi bi-plus-lg"></i> Create Training
                    </a>
                </div>
            </div>
        @endforelse
    </div>
</div>
@endsection