@extends('layouts.app')

@section('title', 'Workload Overview | Institution Axis')

@section('sidebar-menu')
    @include('schooladmin.partials.sidebar')
@endsection

@section('sidebar-footer')
    <p>{{ auth()->user()->school?->name ?? 'Institution Axis' }}</p>
@endsection

@section('breadcrumb')
    <span><a href="{{ route('schooladmin.dashboard') }}">Home</a></span> /
    <span><a href="{{ route('schooladmin.staff.index') }}">Staff</a></span> /
    <span class="active-page">Workload</span>
@endsection

@section('navbar-right')
    <div class="notification-icon"><i class="fas fa-bell"></i><span class="badge-notify">5</span></div>
    <div class="profile-dropdown dropdown">
        <a href="#" class="dropdown-toggle" data-bs-toggle="dropdown">
            <img src="https://ui-avatars.com/api/?background=e54027&color=fff&name=Admin" class="avatar-sm">
            <span>{{ auth()->user()->name }}</span>
            <i class="fas fa-chevron-down"></i>
        </a>
        <ul class="dropdown-menu dropdown-menu-end">
            <li><a class="dropdown-item text-danger" href="{{ route('logout') }}">Logout</a></li>
        </ul>
    </div>
@endsection

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h4 class="fw-bold mb-0"><i class="fas fa-tasks me-2 text-primary"></i>Workload Overview</h4>
        <small class="text-muted">Class-subject assignments per teacher</small>
    </div>
</div>

@foreach(['success','error'] as $msg)
    @if(session($msg))
    <div class="alert alert-{{ $msg === 'error' ? 'danger' : 'success' }} alert-dismissible fade show">
        {{ session($msg) }}<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @endif
@endforeach

<div class="card shadow-sm border-0 rounded-4">
    <div class="card-header bg-white py-3">
        <h6 class="mb-0 fw-semibold">All Active Teachers — Assignment Summary</h6>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0 align-middle">
                <thead class="table-light">
                    <tr>
                        <th>#</th>
                        <th>Teacher</th>
                        <th>Department</th>
                        <th>Designation</th>
                        <th class="text-center">Assignments</th>
                        <th>Load</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($staffList as $s)
                    @php
                        $periods = $s->periods_per_week ?? 0;
                        $loadColor = $periods > 20 ? 'bg-danger' : ($periods > 12 ? 'bg-warning text-dark' : 'bg-success');
                        $maxPeriods = 25;
                    @endphp
                    <tr>
                        <td>{{ $loop->iteration }}</td>
                        <td>
                            <div class="d-flex align-items-center gap-2">
                                <img src="{{ $s->avatar_url }}" class="rounded-circle" width="36" height="36">
                                <div>
                                    <div class="fw-semibold">{{ $s->full_name }}</div>
                                    <small class="text-muted">{{ $s->user->email }}</small>
                                </div>
                            </div>
                        </td>
                        <td>{{ $s->department ?? '—' }}</td>
                        <td>{{ $s->designation ?? '—' }}</td>
                        <td class="text-center">
                            <span class="badge bg-primary fs-6">{{ $periods }}</span>
                        </td>
                        <td style="width:160px">
                            <div class="d-flex align-items-center gap-2">
                                <div class="progress flex-grow-1" style="height:8px">
                                    <div class="progress-bar {{ $loadColor }}"
                                         style="width:{{ min(100, ($periods/$maxPeriods)*100) }}%"></div>
                                </div>
                                @if($periods > 20)
                                    <i class="fas fa-exclamation-triangle text-danger" title="Overloaded"></i>
                                @endif
                            </div>
                        </td>
                        <td>
                            <a href="{{ route('schooladmin.staff.workload.show', $s) }}"
                               class="btn btn-sm btn-outline-primary">
                                <i class="fas fa-eye me-1"></i>Details
                            </a>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" class="text-center text-muted py-5">
                            <i class="fas fa-users-slash fa-2x mb-2 d-block"></i>
                            No active staff found.
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection
