@extends('layouts.admin')

@section('title', 'Mata Pelajaran')
@section('subtitle', 'Kelola data master mata pelajaran')

@section('content')

<div class="row g-3">
    <div class="col-12 col-xl-4">
        <div class="panel">
            <div class="panel-head">
                <h3>Tambah Mata Pelajaran</h3>
            </div>
            <div class="panel-body">
                <form action="{{ route('admin.subjects.store') }}" method="POST">
                    @csrf
                    <div class="mb-3">
                        <label class="form-label">Nama Mapel</label>
                        <input type="text" name="name" class="form-control" placeholder="Contoh: Matematika" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Kode</label>
                        <input type="text" name="code" class="form-control" placeholder="Contoh: MTK" required>
                    </div>
                    <div class="mb-3 form-check">
                        <input type="checkbox" name="is_special" value="1" class="form-check-input" id="is_special_add">
                        <label class="form-check-label" for="is_special_add">
                            Mapel guru khusus (mis. B. Inggris, Agama, PJOK) &mdash; tidak otomatis diampu wali kelas
                        </label>
                    </div>
                    <button type="submit" class="btn btn-brand w-100">
                        <i class="fa-solid fa-plus me-1"></i> Simpan Mapel
                    </button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-12 col-xl-8">
        <div class="panel">
            <div class="panel-head">
                <h3>Daftar Mata Pelajaran</h3>
                <span class="text-muted" style="font-size:.8rem;">{{ $subjects->count() }} mapel</span>
            </div>
            <div class="table-responsive">
                <table class="table-modern">
                    <thead>
                        <tr>
                            <th>Kode</th>
                            <th>Nama Mapel</th>
                            <th>Tipe</th>
                            <th>Jml Guru</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($subjects as $subject)
                            <tr>
                                <td>{{ $subject->code }}</td>
                                <td class="fw-semibold">{{ $subject->name }}</td>
                                <td>
                                    @if ($subject->is_special)
                                        <span class="badge text-bg-warning">Guru Khusus</span>
                                    @else
                                        <span class="badge text-bg-secondary">Default Wali Kelas</span>
                                    @endif
                                </td>
                                <td>{{ $subject->teachers_count }}</td>
                                <td class="d-flex gap-1">
                                    <button type="button" class="btn-soft-secondary" title="Edit"
                                        data-bs-toggle="modal" data-bs-target="#editSubject{{ $subject->id }}">
                                        <i class="fa-solid fa-pen"></i>
                                    </button>
                                    <form action="{{ route('admin.subjects.destroy', $subject->id) }}" method="POST" onsubmit="return confirm('Hapus mapel ini?')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn-soft-danger" title="Hapus">
                                            <i class="fa-solid fa-trash"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>

                            <div class="modal fade" id="editSubject{{ $subject->id }}" tabindex="-1">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <form action="{{ route('admin.subjects.update', $subject->id) }}" method="POST">
                                            @csrf
                                            @method('PUT')
                                            <div class="modal-header">
                                                <h5 class="modal-title">Edit Mata Pelajaran</h5>
                                                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                            </div>
                                            <div class="modal-body">
                                                <div class="mb-3">
                                                    <label class="form-label">Nama Mapel</label>
                                                    <input type="text" name="name" class="form-control" value="{{ $subject->name }}" required>
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label">Kode</label>
                                                    <input type="text" name="code" class="form-control" value="{{ $subject->code }}" required>
                                                </div>
                                                <div class="mb-3 form-check">
                                                    <input type="checkbox" name="is_special" value="1" class="form-check-input"
                                                        id="is_special_edit{{ $subject->id }}" {{ $subject->is_special ? 'checked' : '' }}>
                                                    <label class="form-check-label" for="is_special_edit{{ $subject->id }}">
                                                        Mapel guru khusus
                                                    </label>
                                                </div>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                                                <button type="submit" class="btn btn-brand">Simpan Perubahan</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        @empty
                            <tr>
                                <td colspan="5" class="text-center text-muted py-4">Belum ada data mata pelajaran.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

@endsection
