@extends('layouts.app')

@section('title', '{{ $staff->full_name }} — Workload | 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.workload.index') }}">Workload</a></span> /
    <span class="active-page">{{ $staff->full_name }}</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>Teacher Workload</h4>
        <small class="text-muted">Manage class-subject assignments for {{ $staff->full_name }}</small>
    </div>
    <a href="{{ route('schooladmin.staff.workload.index') }}" class="btn btn-outline-secondary rounded-pill px-3">
        <i class="fas fa-arrow-left me-1"></i>Back
    </a>
</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="row g-4">
    {{-- Teacher Info Card --}}
    <div class="col-lg-4">
        <div class="card border-0 shadow-sm rounded-4 text-center p-4">
            <img src="{{ $staff->avatar_url }}" class="rounded-circle mx-auto mb-3 border border-3 border-primary"
                 width="80" height="80">
            <h5 class="fw-bold mb-0">{{ $staff->full_name }}</h5>
            <div class="text-muted small">{{ $staff->designation ?? 'Teacher' }}</div>
            <div class="text-muted small">{{ $staff->department ?? '' }}</div>
            <hr>
            <div class="fs-2 fw-bold text-primary">{{ $assignments->count() }}</div>
            <div class="text-muted small">Total Assignments</div>
        </div>

        {{-- Assign New --}}
        <div class="card border-0 shadow-sm rounded-4 mt-3">
            <div class="card-header bg-white py-3">
                <h6 class="mb-0 fw-semibold"><i class="fas fa-plus-circle me-2 text-success"></i>Assign Class/Subject</h6>
            </div>
            <div class="card-body">
                <form action="{{ route('schooladmin.staff.workload.assign') }}" method="POST">
                    @csrf
                    <input type="hidden" name="staff_id" value="{{ $staff->id }}">
                    <div class="mb-3">
                        <label class="form-label">Class</label>
                        <select name="school_class_id" class="form-select @error('school_class_id') is-invalid @enderror" required>
                            <option value="">— Select Class —</option>
                            @foreach($classes as $c)
                            <option value="{{ $c->id }}">{{ $c->name }}</option>
                            @endforeach
                        </select>
                        @error('school_class_id')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Subject</label>
                        <select name="subject_id" class="form-select @error('subject_id') is-invalid @enderror" required>
                            <option value="">— Select Subject —</option>
                            @foreach($subjects as $s)
                            <option value="{{ $s->id }}">{{ $s->name }}</option>
                            @endforeach
                        </select>
                        @error('subject_id')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <button type="submit" class="btn btn-success w-100 rounded-pill">
                        <i class="fas fa-plus me-1"></i>Assign
                    </button>
                </form>
            </div>
        </div>
    </div>

    {{-- Current Assignments --}}
    <div class="col-lg-8">
        <div class="card border-0 shadow-sm rounded-4">
            <div class="card-header bg-white py-3">
                <h6 class="mb-0 fw-semibold">Current Assignments</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>Class</th><th>Subject</th><th>Remove</th></tr>
                        </thead>
                        <tbody>
                            @forelse($assignments as $a)
                            <tr>
                                <td>{{ $loop->iteration }}</td>
                                <td>
                                    <span class="badge bg-light text-dark border">
                                        <i class="fas fa-chalkboard me-1"></i>{{ $a->class_name }}
                                    </span>
                                </td>
                                <td>
                                    <span class="badge bg-primary">{{ $a->subject_name }}</span>
                                </td>
                                <td>
                                    <form action="{{ route('schooladmin.staff.workload.unassign') }}" method="POST"
                                          onsubmit="return confirm('Remove this assignment?')">
                                        @csrf
                                        <input type="hidden" name="pivot_id" value="{{ $a->pivot_id }}">
                                        <button class="btn btn-sm btn-outline-danger">
                                            <i class="fas fa-times"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                            @empty
                            <tr>
                                <td colspan="4" class="text-center text-muted py-5">
                                    <i class="fas fa-inbox fa-2x mb-2 d-block"></i>
                                    No assignments yet. Use the form to assign classes.
                                </td>
                            </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
