@extends('guru.layouts.app')

@section('title', 'History Materi')

@push('styles')
<style>
.page-title {
    font-size: 28px;
    font-weight: 800;
    margin-bottom: 6px;
    margin-top: -20px;
}

.back-link {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: #2b8ef3;
    font-weight: 600;
    font-size: 14px;
    text-decoration: none;
    margin-bottom: 20px;
}

.back-link:hover { text-decoration: underline; }

.custom-card {
    background: white;
    border-radius: 20px;
    border: 2px solid #e5e5e5;
    padding: 25px;
}

.table-header { background: #a5e6ba; }

.mapel-badge {
    display: inline-block;
    background: #e6f0ff;
    color: #1d4ed8;
    font-size: 12px;
    font-weight: 600;
    padding: 3px 10px;
    border-radius: 99px;
}

.kelas-badge {
    display: inline-block;
    background: #f0fdf4;
    color: #166534;
    font-size: 12px;
    font-weight: 600;
    padding: 3px 10px;
    border-radius: 99px;
}

.file-icon { font-size: 20px; }

.btn-hapus {
    background: #fee2e2;
    color: #ef4444;
    border: none;
    border-radius: 8px;
    padding: 5px 12px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}

.btn-hapus:hover { background: #fca5a5; }

.btn-unduh {
    background: #e6f0ff;
    color: #2b8ef3;
    border: none;
    border-radius: 8px;
    padding: 5px 12px;
    font-size: 13px;
    font-weight: 600;
    text-decoration: none;
    transition: background 0.2s;
    display: inline-block;
}

.btn-unduh:hover { background: #bfdbfe; color: #1d4ed8; }

.alert-success-custom {
    background: #dcfce7;
    color: #166534;
    border-radius: 10px;
    padding: 12px 16px;
    margin-bottom: 16px;
    font-weight: 500;
    font-size: 14px;
}

.empty-state {
    text-align: center;
    padding: 50px 20px;
    color: #94a3b8;
}
</style>
@endpush

@section('content')

<a href="{{ route('guru.mapel.index') }}" class="back-link">← Kembali ke Mata Pelajaran</a>

<h3 class="page-title">🗂️ History Materi</h3>
<p style="color:#64748b;font-size:14px;margin-bottom:20px">
    Semua materi yang pernah Anda upload.
</p>

@if(session('success'))
    <div class="alert-success-custom">✅ {{ session('success') }}</div>
@endif

<div class="custom-card">
    @if($materiList->isEmpty())
        <div class="empty-state">
            <div style="font-size:48px;margin-bottom:12px">📭</div>
            <p>Belum ada materi yang diupload.</p>
        </div>
    @else
        <table class="table align-middle">
            <thead class="table-header text-center">
                <tr>
                    <th>No</th>
                    <th>Judul Materi</th>
                    <th>Mata Pelajaran</th>
                    <th>Kelas</th>
                    <th>Tanggal Upload</th>
                    <th>File</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                @foreach($materiList as $i => $materi)
                <tr>
                    <td class="text-center">{{ $materiList->firstItem() + $i }}</td>
                    <td>
                        <div style="font-weight:600;color:#1e293b">{{ $materi->judul_materi }}</div>
                        @if($materi->deskripsi)
                            <div style="font-size:12px;color:#94a3b8;margin-top:2px">
                                {{ Str::limit($materi->deskripsi, 60) }}
                            </div>
                        @endif
                    </td>
                    <td class="text-center">
                        <span class="mapel-badge">
                            {{ optional(optional($materi->mengajar)->mapel)->nama_mapel ?? '-' }}
                        </span>
                    </td>
                    <td class="text-center">
                        <span class="kelas-badge">
                            {{ optional(optional($materi->mengajar)->kelas)->tingkat }}
                            {{ optional(optional($materi->mengajar)->kelas)->nama_kelas ?? '-' }}
                        </span>
                    </td>
                    <td class="text-center" style="font-size:13px;color:#64748b">
                        {{ \Carbon\Carbon::parse($materi->tanggal_upload)->format('d M Y, H:i') }}
                    </td>
                    <td class="text-center">
                        @if($materi->lampiran_materi)
                            @php
                                $ext = strtolower(pathinfo($materi->lampiran_materi, PATHINFO_EXTENSION));
                                $icon = match(true) {
                                    in_array($ext, ['pdf'])              => '📄',
                                    in_array($ext, ['doc','docx'])       => '📝',
                                    in_array($ext, ['ppt','pptx'])       => '📊',
                                    in_array($ext, ['jpg','jpeg','png'])  => '🖼️',
                                    default                              => '📎',
                                };
                            @endphp
                            <a href="{{ asset('storage/' . $materi->lampiran_materi) }}"
                               target="_blank" class="btn-unduh">
                                {{ $icon }} Unduh
                            </a>
                        @else
                            <span style="font-size:12px;color:#94a3b8">Tidak ada file</span>
                        @endif
                    </td>
                    <td class="text-center">
                        <form action="{{ route('guru.materi.destroy', $materi->id_materi) }}"
                              method="POST"
                              onsubmit="return confirm('Yakin hapus materi ini?')">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn-hapus">🗑️ Hapus</button>
                        </form>
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>

        <div class="d-flex justify-content-end mt-3">
            {{ $materiList->links() }}
        </div>
    @endif
</div>

@endsection