@extends('layouts.app')
@section('title', 'Teacher Attendance')
@section('page_title', 'Teacher / Staff Attendance')
@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Home</a></li>
    <li class="breadcrumb-item active">Teacher Attendance</li>
@endsection
@section('page_actions')
    <a href="{{ route('attendance.teachers.report') }}" class="btn btn-outline-primary"><i class="bi bi-bar-chart me-1"></i>Monthly Report</a>
@endsection

@section('content')
    <div class="card mb-3"><div class="card-body">
        <form method="GET" class="row g-2 align-items-end">
            <div class="col-md-3"><label class="form-label small">Date</label>
                <input type="date" name="date" value="{{ $date }}" class="form-control form-control-sm"></div>
            <div class="col-md-3"><button class="btn btn-sm btn-primary"><i class="bi bi-search me-1"></i>Load</button></div>
        </form>
    </div></div>

    @if($staff->isNotEmpty())
        <form method="POST" action="{{ route('attendance.teachers.store') }}">
            @csrf
            <input type="hidden" name="date" value="{{ $date }}">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <span><i class="bi bi-person-workspace me-1"></i>{{ $staff->count() }} staff &mdash; {{ \Illuminate\Support\Carbon::parse($date)->format('d M Y') }}</span>
                    <button type="button" class="btn btn-sm btn-light border" id="markAllPresent"><i class="bi bi-check2-all me-1"></i>Mark all present</button>
                </div>
                <div class="card-body">
                    <table class="table table-hover align-middle">
                        <thead><tr><th>Staff</th><th>Role</th><th>Status</th><th>Remarks</th></tr></thead>
                        <tbody>
                            @foreach($staff as $member)
                                @php $rec = $existing->get($member->id); $cur = $rec?->status ?? 'present'; @endphp
                                <tr>
                                    <td><div class="d-flex align-items-center gap-2"><span class="sms-avatar sm">{{ $member->initials }}</span><div><div class="fw-semibold">{{ $member->name }}</div><div class="small text-muted">{{ $member->employee_code ?? $member->email }}</div></div></div></td>
                                    <td><span class="badge bg-soft-info">{{ $member->role_label }}</span></td>
                                    <td style="min-width:210px">
                                        <div class="btn-group btn-group-sm" role="group">
                                            @foreach(App\Models\TeacherAttendance::STATUSES as $st)
                                                <input type="radio" class="btn-check" name="status[{{ $member->id }}]" id="t{{ $member->id }}_{{ $st }}" value="{{ $st }}" @checked($cur===$st)>
                                                <label class="btn btn-outline-{{ App\Models\TeacherAttendance::statusColor($st) }}" for="t{{ $member->id }}_{{ $st }}">{{ ucfirst($st) }}</label>
                                            @endforeach
                                        </div>
                                    </td>
                                    <td><input type="text" name="remarks[{{ $member->id }}]" value="{{ $rec?->remarks }}" class="form-control form-control-sm" placeholder="—"></td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
                <div class="card-footer text-end"><button class="btn btn-primary"><i class="bi bi-save me-1"></i>Save Attendance</button></div>
            </div>
        </form>
        @push('scripts')
        <script>document.getElementById('markAllPresent').addEventListener('click',()=>document.querySelectorAll('input[id$="_present"]').forEach(r=>r.checked=true));</script>
        @endpush
    @else
        <div class="card"><div class="card-body text-center text-muted py-4">No staff found for this branch.</div></div>
    @endif
@endsection
