@extends('guru.layouts.app')

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

@push('styles')
<style>
.page-title {
    font-size: 22px;
    font-weight: 800;
    margin-bottom: 6px;
    margin-top: -20px;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.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: 20px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.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;
    white-space: nowrap;
}

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

.deadline-badge {
    font-size: 12px;
    font-weight: 600;
    padding: 3px 10px;
    border-radius: 99px;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    white-space: nowrap;
}

.deadline-lewat  { background: #fee2e2; color: #ef4444; }
.deadline-aktif  { background: #dcfce7; color: #16a34a; }

.submit-count {
    font-size: 13px;
    font-weight: 700;
    color: #f97316;
}

.btn-detail {
    background: #f97316;
    color: white;
    border: none;
    border-radius: 8px;
    padding: 5px 14px;
    font-size: 13px;
    font-weight: 600;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    transition: background 0.2s;
    white-space: nowrap;
}

.btn-detail:hover { background: #ea6c0a; color: white; }

.btn-edit-tbl {
    background: #fef9c3;
    color: #854d0e;
    border: none;
    border-radius: 8px;
    padding: 5px 12px;
    font-size: 13px;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    white-space: nowrap;
    cursor: pointer;
    transition: background 0.2s;
}
.btn-edit-tbl:hover { background: #fde047; color: #713f12; }

.inline-icon { width: 16px; height: 16px; vertical-align: middle; display: inline-block; }

.btn-green {
    background: #16a34a; color: white; border: none; border-radius: 10px;
    padding: 8px 18px; font-size: 13px; font-weight: 700;
    cursor: pointer; font-family: 'Poppins', sans-serif;
    display: inline-flex; align-items: center; gap: 6px; transition: background 0.2s;
}

.alert-success-custom {
    background: #dcfce7;
    color: #166534;
    border-radius: 10px;
    padding: 12px 16px;
    margin-bottom: 16px;
    font-weight: 500;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.empty-state {
    text-align: center;
    padding: 50px 20px;
    color: #94a3b8;
}

.table { min-width: 650px; }

/* ===== MODAL STYLE (selaras dengan daftar-siswa) ===== */
.modal-content {
    border-radius: 20px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
    border: none;
}

.modal-header-pastel-yellow {
    background: #FFD97D !important;
    color: black !important;
    border-bottom: none;
    padding: 15px 20px;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
}

.modal-header-pastel-yellow .modal-title {
    font-weight: 700;
    font-size: 16px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.modal-body .form-label-custom {
    font-size: 13px;
    font-weight: 700;
    color: #475569;
    margin-bottom: 5px;
    display: block;
}

.modal-body .form-control {
    border-radius: 10px;
    border: 2px solid #e2e8f0;
    font-size: 14px;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.modal-body .form-control:focus {
    border-color: #2b8ef3;
    box-shadow: 0 0 0 3px rgba(43,142,243,0.12);
}

@media (max-width: 576px) {
    .page-title { font-size: 18px; margin-top: 0; }
    .custom-card { padding: 12px; border-radius: 14px; }
}
</style>
@endpush

@section('content')

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

<h3 class="page-title">
    <img src="{{ asset('images/icon/gurud/history.png') }}" class="icon-inline" alt="Ikon history">
    HISTORY TUGAS
</h3>

@if(session('success'))
    <div class="alert-success-custom">
        <img src="{{ asset('images/icon/gurud/v.png') }}" class="icon-inline" alt="Berhasil">
        {{ session('success') }}
    </div>
@endif

<div class="custom-card">
    @if($tugasList->isEmpty())
        <div class="empty-state">
            <div style="margin-bottom:12px">
                <img src="{{ asset('images/icon/gurud/mailbox.png') }}" class="icon-lg" alt="Belum ada tugas">
            </div>
            <p>Belum ada tugas yang dibuat.</p>
        </div>
    @else
        <table class="table align-middle">
            <thead class="table-header text-center">
                <tr>
                    <th>No</th>
                    <th>Judul Tugas</th>
                    <th>Mata Pelajaran</th>
                    <th>Kelas</th>
                    <th>Deadline</th>
                    <th>Pengumpulan</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                @foreach($tugasList as $i => $tugas)
                @php
                    $isLewat      = \Carbon\Carbon::parse($tugas->deadline)->isPast();
                    $jumlahKumpul = $tugas->pengumpulanTugas->count();
                    // Format deadline untuk input datetime-local (Y-m-d\TH:i)
                    $deadlineInput = \Carbon\Carbon::parse($tugas->deadline)->format('Y-m-d\TH:i');
                @endphp
                <tr>
                    <td class="text-center">{{ $tugasList->firstItem() + $i }}</td>
                    <td>
                        <div style="font-weight:600;color:#1e293b;word-break:break-word;max-width:180px;">{{ $tugas->judul_tugas }}</div>
                        @if($tugas->keterangan)
                            <div style="font-size:12px;color:#94a3b8;margin-top:2px">
                                {{ Str::limit($tugas->keterangan, 60) }}
                            </div>
                        @endif
                    </td>
                    <td class="text-center">
                        <span class="mapel-badge">
                            {{ optional(optional($tugas->mengajar)->mapel)->nama_mapel ?? '-' }}
                        </span>
                    </td>
                    <td class="text-center">
                        <span class="kelas-badge">
                            {{ optional(optional($tugas->mengajar)->kelas)->tingkat }}
                            {{ optional(optional($tugas->mengajar)->kelas)->nama_kelas ?? '-' }}
                        </span>
                    </td>
                    <td class="text-center">
                        @if($isLewat)
                            <span class="deadline-badge deadline-lewat">Lewat
                            </span>
                        @else
                            <span class="deadline-badge deadline-aktif">Aktif
                            </span>
                        @endif
                        <div style="font-size:12px;color:#64748b;margin-top:4px;white-space:nowrap;">
                            {{ \Carbon\Carbon::parse($tugas->deadline)->format('d M Y, H:i') }}
                        </div>
                    </td>
                    <td class="text-center">
                        <span class="submit-count">{{ $jumlahKumpul }}</span>
                        <span style="font-size:12px;color:#64748b"> pengumpulan</span>
                    </td>
                    <td class="text-center">
                        <div style="display:flex;gap:6px;justify-content:center;flex-wrap:wrap;">
                            {{-- Tombol Edit --}}
                            <button
                                type="button"
                                class="btn-edit-tbl"
                                onclick="openEditTugas(
                                    {{ $tugas->id_tugas }},
                                    '{{ addslashes($tugas->judul_tugas) }}',
                                    '{{ addslashes($tugas->keterangan ?? '') }}',
                                    '{{ $deadlineInput }}'
                                )">
                                <img src="{{ asset('images/icon/main/edit.png') }}" class="icon-inline" alt="Edit">
                                Edit
                            </button>

                            {{-- Tombol Detail --}}
                            <a href="{{ route('guru.tugas.detail', $tugas->id_tugas) }}" class="btn-detail">Detail</a>
                        </div>
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>

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


{{-- ===== MODAL EDIT TUGAS ===== --}}
<div class="modal fade" id="modalEditTugas" tabindex="-1" aria-labelledby="modalEditTugasLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered modal-dialog-scrollable">
        <div class="modal-content">

            <div class="modal-header modal-header-pastel-yellow">
                <h5 class="modal-title" id="modalEditTugasLabel">
                    <img src="{{ asset('images/icon/main/edit.png') }}" width="18" alt="Edit">
                    Edit Tugas
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Tutup"></button>
            </div>

            <form id="formEditTugas" method="POST">
                @csrf
                @method('PUT')

                <div class="modal-body">

                    <div class="mb-3">
                        <label class="form-label-custom">
                            Judul Tugas <span class="text-danger">*</span>
                        </label>
                        <input type="text"
                               id="editTugasJudul"
                               name="judul_tugas"
                               class="form-control"
                               required
                               placeholder="Masukkan judul tugas">
                    </div>

                    <div class="mb-3">
                        <label class="form-label-custom">Keterangan / Instruksi</label>
                        <textarea id="editTugasKeterangan"
                                  name="keterangan"
                                  class="form-control"
                                  rows="4"
                                  style="resize:vertical;"
                                  placeholder="Tuliskan instruksi atau keterangan tugas (opsional)"></textarea>
                    </div>

                    <div class="mb-3">
                        <label class="form-label-custom">
                            Deadline <span class="text-danger">*</span>
                        </label>
                        <input type="datetime-local"
                               id="editTugasDeadline"
                               name="deadline"
                               class="form-control"
                               required>
                        <small class="text-muted" style="font-size:11px;">
                            Ubah deadline jika diperlukan. Siswa yang sudah mengumpulkan tidak terpengaruh.
                        </small>
                    </div>

                </div>

                <div class="modal-footer" style="border-top: 2px solid #f1f5f9;">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                <button type="submit" class="btn-green" id="tambah-btn-submit">
                    <img src="{{ asset('images/icon/gurud/save.png') }}" class="inline-icon" alt=""> Simpan
                </button>
                </div>
            </form>

        </div>
    </div>
</div>


@push('scripts')
<script>
function openEditTugas(id, judul, keterangan, deadline) {
    // Set action form ke route update tugas
    document.getElementById('formEditTugas').action = "{{ url('guru/tugas') }}/" + id;

    // Isi field
    document.getElementById('editTugasJudul').value      = judul;
    document.getElementById('editTugasKeterangan').value = keterangan;
    document.getElementById('editTugasDeadline').value   = deadline;

    // Buka modal Bootstrap
    new bootstrap.Modal(document.getElementById('modalEditTugas')).show();
}
</script>

{{-- Re-open modal edit jika validasi gagal --}}
@if($errors->any() && session('error_from') == 'edit_tugas' && session('edit_tugas'))
<script>
document.addEventListener('DOMContentLoaded', function () {
    @php $et = session('edit_tugas'); @endphp
    document.getElementById('formEditTugas').action          = "{{ url('guru/tugas') }}/{{ $et['id_tugas'] }}";
    document.getElementById('editTugasJudul').value          = '{{ addslashes($et['judul_tugas']) }}';
    document.getElementById('editTugasKeterangan').value     = '{{ addslashes($et['keterangan'] ?? '') }}';
    document.getElementById('editTugasDeadline').value       = '{{ $et['deadline'] }}';
    new bootstrap.Modal(document.getElementById('modalEditTugas')).show();
});
</script>
@endif
@endpush

@endsection