<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Situație școlară — {{ $elev->nume }} {{ $elev->prenume }}</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: DejaVu Sans, sans-serif; font-size: 11px; color: #333; padding: 20px; }
        .header { text-align: center; margin-bottom: 25px; border-bottom: 2px solid #333; padding-bottom: 15px; }
        .header h1 { font-size: 16px; margin-bottom: 4px; }
        .header h2 { font-size: 13px; font-weight: normal; color: #555; }
        .info { margin-bottom: 20px; }
        .info table { width: 100%; }
        .info td { padding: 3px 8px; }
        .info .label { font-weight: bold; width: 140px; }
        table.data { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
        table.data th, table.data td { border: 1px solid #999; padding: 5px 8px; text-align: center; }
        table.data th { background: #f0f0f0; font-weight: bold; font-size: 10px; }
        table.data td.left { text-align: left; }
        .section-title { font-size: 13px; font-weight: bold; margin: 15px 0 8px; padding: 4px 8px; background: #e8e8e8; }
        .footer { margin-top: 30px; font-size: 9px; color: #777; text-align: center; border-top: 1px solid #ccc; padding-top: 10px; }
        .media-buna { color: #16a34a; font-weight: bold; }
        .media-medie { color: #d97706; font-weight: bold; }
        .media-slaba { color: #dc2626; font-weight: bold; }
        .status-badge { padding: 2px 8px; font-size: 10px; font-weight: bold; }
        .status-promovat { background: #dcfce7; color: #166534; }
        .status-corigent { background: #fef9c3; color: #854d0e; }
        .status-repetent { background: #fecaca; color: #991b1b; }
        .status-amanat { background: #dbeafe; color: #1e40af; }
    </style>
</head>
<body>
    <div class="header">
        <h1>{{ $scoala->nume }}</h1>
        <h2>{{ $scoala->localitate }}, jud. {{ $scoala->judet }}</h2>
    </div>

    <div class="section-title">SITUAȚIE ȘCOLARĂ</div>

    <div class="info">
        <table>
            <tr>
                <td class="label">Elev:</td>
                <td>{{ $elev->nume }} {{ $elev->prenume }}</td>
                <td class="label">Clasa:</td>
                <td>{{ $clasa->nume }}</td>
            </tr>
            <tr>
                <td class="label">An școlar:</td>
                <td>{{ $clasa->an_scolar }}/{{ $clasa->an_scolar + 1 }}</td>
                <td class="label">Data raport:</td>
                <td>{{ now()->format('d.m.Y') }}</td>
            </tr>
        </table>
    </div>

    @foreach($perioade as $perioada)
    @php
        $situatie = $situatii->where('perioada_id', $perioada->id)->first();
        $np = $notePurtare->where('perioada_id', $perioada->id)->first();
    @endphp
    <div class="section-title">{{ $perioada->nume }}</div>
    <table class="data">
        <thead>
            <tr>
                <th style="width: 25%;">Materia</th>
                <th>Note</th>
                <th style="width: 12%;">Media calc.</th>
                <th style="width: 10%;">Media finală</th>
                <th style="width: 10%;">Absențe</th>
                <th style="width: 10%;">Nemotivate</th>
            </tr>
        </thead>
        <tbody>
            @foreach($materii as $materie)
            @php
                $noteMaterie = $note->where('materie_id', $materie->id)->where('perioada_id', $perioada->id);
                $media = $noteMaterie->where('tip', '!=', 'final')->avg('valoare');
                $medie = $medii->where('materie_id', $materie->id)->where('perioada_id', $perioada->id)->first();
                $absenteMaterie = $absente->where('materie_id', $materie->id)->where('perioada_id', $perioada->id);
                $totalAbsente = $absenteMaterie->count();
                $nemotivate = $absenteMaterie->where('motivata', false)->count();
            @endphp
            <tr>
                <td class="left">{{ $materie->nume }}</td>
                <td>{{ $noteMaterie->pluck('valoare')->implode(', ') ?: '—' }}</td>
                <td>{{ $media ? number_format($media, 2) : '—' }}</td>
                <td class="{{ $medie && $medie->incheiat ? ($medie->media_finala >= 5 ? 'media-buna' : 'media-slaba') : '' }}">
                    {{ $medie && $medie->incheiat ? $medie->media_finala : '—' }}
                </td>
                <td>{{ $totalAbsente ?: '—' }}</td>
                <td>{{ $nemotivate ?: '—' }}</td>
            </tr>
            @endforeach
            <tr style="background: #f9f9f9;">
                <td class="left" style="font-weight: bold;">Purtare</td>
                <td colspan="2"></td>
                <td style="font-weight: bold;">{{ $np?->valoare ?? 10 }}</td>
                <td colspan="2"></td>
            </tr>
        </tbody>
    </table>

    @if($situatie)
    <table class="data" style="width: auto; margin-bottom: 20px;">
        <tr>
            <th>Media generală</th>
            <th>Nota purtare</th>
            <th>Total absențe</th>
            <th>Nemotivate</th>
            <th>Status</th>
        </tr>
        <tr>
            <td class="{{ $situatie->media_generala >= 7 ? 'media-buna' : ($situatie->media_generala >= 5 ? 'media-medie' : 'media-slaba') }}">
                {{ $situatie->media_generala ? number_format($situatie->media_generala, 2) : '—' }}
            </td>
            <td>{{ $situatie->nota_purtare }}</td>
            <td>{{ $situatie->total_absente }}</td>
            <td>{{ $situatie->absente_nemotivate }}</td>
            <td>
                <span class="status-badge status-{{ $situatie->status }}">
                    {{ ['promovat' => 'PROMOVAT', 'corigent' => 'CORIGENT', 'repetent' => 'REPETENT', 'amanat' => 'AMÂNAT', 'in_curs' => 'ÎN CURS'][$situatie->status] ?? $situatie->status }}
                </span>
            </td>
        </tr>
    </table>
    @endif

    @php $corig = $corigente->where('perioada_id', $perioada->id); @endphp
    @if($corig->count() > 0)
    <div style="font-size: 11px; font-weight: bold; margin-bottom: 5px;">Corigențe:</div>
    <table class="data" style="width: auto;">
        <tr><th>Materia</th><th>Media</th><th>Data examen</th><th>Nota</th><th>Rezultat</th></tr>
        @foreach($corig as $c)
        <tr>
            <td class="left">{{ $c->materie?->nume }}</td>
            <td class="media-slaba">{{ number_format($c->media_inainte, 2) }}</td>
            <td>{{ $c->data_examen?->format('d.m.Y') ?? 'Neprogramat' }}</td>
            <td>{{ $c->nota_corigenta ?? '—' }}</td>
            <td>{{ ['programat' => 'Programat', 'promovat' => 'Promovat', 'nepromovat' => 'Nepromovat'][$c->status] ?? $c->status }}</td>
        </tr>
        @endforeach
    </table>
    @endif
    @endforeach

    <div style="margin-top: 25px;">
        <table style="width: 100%;">
            <tr>
                <td style="width: 50%; padding: 20px 0;">
                    <div style="font-size: 10px;">Director,</div>
                    <div style="margin-top: 25px; border-top: 1px solid #999; width: 180px; padding-top: 3px; font-size: 10px;">
                        {{ $scoala->director ?? '________________________' }}
                    </div>
                </td>
                <td style="width: 50%; padding: 20px 0;">
                    <div style="font-size: 10px;">Diriginte,</div>
                    <div style="margin-top: 25px; border-top: 1px solid #999; width: 180px; padding-top: 3px; font-size: 10px;">
                        {{ $clasa->diriginte?->name ?? '________________________' }}
                    </div>
                </td>
            </tr>
        </table>
    </div>

    <div class="footer">
        Document generat automat — {{ $scoala->nume }} — {{ now()->format('d.m.Y H:i') }}
    </div>
</body>
</html>
