@extends('adminlte.layouts.app')

@section('content')
<div class="content-wrapper">

    {{-- HEADER --}}
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-3">
                <div class="col-sm-6">
                    <h1 class="m-0 fw-bold text-gradient-primary">
                        <i class="fas fa-users me-2"></i>Data Siswa
                    </h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item active">Siswa</li>
                    </ol>
                </div>
            </div>
        </div>
    </div>

    {{-- CONTENT --}}
    <section class="content">
        <div class="container-fluid">

            <div class="card shadow-xl rounded-3 border-0">
                <div class="card-header d-flex justify-content-between align-items-center bg-gradient-primary text-white py-3 px-4">
                    <h3 class="card-title m-0 fw-semibold">
                        <i class="fas fa-user-graduate me-1"></i> Daftar Siswa
                    </h3>

                    <button id="btnTambah" class="btn btn-light btn-sm fw-semibold shadow-sm hover-scale">
                        <i class="fas fa-plus-circle me-1"></i> Tambah Siswa
                    </button>
                </div>

                <div class="card-body px-4 py-3">

                    {{-- ALERT --}}
                    @if (session('success'))
                        <div class="alert alert-success shadow-sm">
                            {{ session('success') }}
                        </div>
                    @endif

                    @if (session('error'))
                        <div class="alert alert-danger shadow-sm">
                            {{ session('error') }}
                        </div>
                    @endif

                    {{-- SEARCH --}}
                    <div class="mb-4">
                        <input type="text" id="search"
                               placeholder="🔍 Cari siswa..."
                               class="form-control shadow-sm rounded-pill"
                               style="max-width: 320px; padding: 10px 20px;">
                    </div>

                    {{-- TABLE --}}
                    <div class="table-responsive rounded-3 overflow-auto shadow-sm">
                        <table id="siswaTable" class="table table-hover table-borderless align-middle">
                            <thead class="text-center text-white"
                                  style="background: linear-gradient(135deg, #6a11cb, #2575fc);">
                                <tr>
                                    <th>No</th>
                                    <th>NIS</th>
                                    <th>Nama</th>
                                    <th>Kelas</th>
                                    <th>Status Anggota</th>
                                    <th>Tanggal Daftar</th>
                                    <th width="300">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($siswa as $item)
                                <tr>
                                    <td class="text-center">{{ $loop->iteration }}</td>
                                    <td>{{ $item->nis }}</td>
                                    <td>{{ $item->nama }}</td>
                                    <td class="text-center">{{ $item->kelas }}</td>

                                    {{-- STATUS --}}
                                    <td class="text-center">
                                        @if ($item->anggota)
                                            @if ($item->anggota->status === 'aktif')
                                                <span class="badge badge-success">Aktif</span>
                                            @else
                                                <span class="badge badge-danger">Nonaktif</span>
                                            @endif
                                        @else
                                            <span class="badge badge-secondary">Belum Terdaftar</span>
                                        @endif
                                    </td>

                                    {{-- TANGGAL --}}
                                    <td class="text-center">
                                        {{ $item->anggota?->tanggal_daftar ?? '-' }}
                                    </td>

                                    {{-- AKSI --}}
                                    <td class="text-center">
                                        <div class="d-flex justify-content-center gap-2 flex-wrap">

                                            {{-- DAFTAR --}}
                                            @if (!$item->anggota)
                                            <form action="{{ route('siswa.daftar', $item->id) }}"
                                                  method="POST"
                                                  class="formDaftar">
                                                @csrf
                                                <button type="submit"
                                                    class="btn btn-success btn-sm shadow-sm hover-scale">
                                                    <i class="fas fa-user-check me-1"></i> Daftarkan
                                                </button>
                                            </form>
                                            @endif

                                            {{-- NONAKTIF --}}
                                            @if ($item->anggota && $item->anggota->status === 'aktif')
                                            <form action="{{ route('siswa.nonaktif', $item->id) }}"
                                                  method="POST"
                                                  class="formNonaktif">
                                                @csrf
                                                <button type="submit"
                                                    class="btn btn-secondary btn-sm shadow-sm hover-scale">
                                                    <i class="fas fa-user-slash me-1"></i> Nonaktifkan
                                                </button>
                                            </form>
                                            @endif
                                            {{-- AKTIFKAN ULANG --}}
@if ($item->anggota && $item->anggota->status === 'nonaktif')
<form action="{{ route('siswa.aktifkan', $item->id) }}" method="POST" class="formAktifkan">
    @csrf
    <button type="submit" class="btn btn-success btn-sm shadow-sm hover-scale">
        <i class="fas fa-user-check me-1"></i> Aktifkan
    </button>
</form>
@endif
<a href="{{ route('siswa.cetakKTA', $item->id) }}"
   class="btn btn-sm btn-primary">
   Cetak KTA
</a>


                                            <button class="btn btn-info btn-sm shadow-sm hover-scale btnShow"
                                                data-url="{{ route('siswa.show', $item->id) }}">
                                                <i class="fas fa-eye me-1"></i> Detail
                                            </button>

                                            <button class="btn btn-warning btn-sm shadow-sm hover-scale btnEdit"
                                                data-url="{{ route('siswa.edit', $item->id) }}">
                                                <i class="fas fa-edit me-1"></i> Edit
                                            </button>

                                            <form action="{{ route('siswa.destroy', $item->id) }}"
                                                  method="POST"
                                                  class="formDelete">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit"
                                                    class="btn btn-danger btn-sm shadow-sm hover-scale">
                                                    <i class="fas fa-trash-alt me-1"></i> Hapus
                                                </button>
                                            </form>

                                        </div>
                                    </td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="7" class="text-center text-muted py-4">
                                        Data siswa belum tersedia
                                    </td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>

                </div>
            </div>

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

{{-- SWEETALERT --}}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {

    // SEARCH
    document.getElementById("search").addEventListener("keyup", function () {
        let filter = this.value.toLowerCase();
        document.querySelectorAll("#siswaTable tbody tr").forEach(row => {
            row.style.display = row.textContent.toLowerCase().includes(filter) ? "" : "none";
        });
    });

    // TAMBAH
    document.getElementById("btnTambah").addEventListener("click", function () {
        window.location.href = "{{ route('siswa.create') }}";
    });

    // DAFTAR
    document.querySelectorAll(".formDaftar").forEach(form => {
        form.addEventListener("submit", function (e) {
            e.preventDefault();
            Swal.fire({
                title: 'Daftarkan sebagai anggota?',
                icon: 'question',
                showCancelButton: true,
                confirmButtonText: 'Ya, Daftarkan',
                cancelButtonText: 'Batal'
            }).then((result) => {
                if (result.isConfirmed) form.submit();
            });
        });
    });
// AKTIFKAN ULANG
document.querySelectorAll(".formAktifkan").forEach(form => {
    form.addEventListener("submit", function (e) {
        e.preventDefault();
        Swal.fire({
            title: 'Aktifkan kembali anggota?',
            icon: 'question',
            showCancelButton: true,
            confirmButtonText: 'Ya, Aktifkan',
            cancelButtonText: 'Batal'
        }).then((result) => {
            if (result.isConfirmed) form.submit();
        });
    });
});

    // NONAKTIF
    document.querySelectorAll(".formNonaktif").forEach(form => {
        form.addEventListener("submit", function (e) {
            e.preventDefault();
            Swal.fire({
                title: 'Nonaktifkan anggota?',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonText: 'Ya, Nonaktifkan',
                cancelButtonText: 'Batal'
            }).then((result) => {
                if (result.isConfirmed) form.submit();
            });
        });
    });

    // DELETE
    document.querySelectorAll(".formDelete").forEach(form => {
        form.addEventListener("submit", function (e) {
            e.preventDefault();
            Swal.fire({
                title: 'Yakin ingin menghapus?',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#d33',
                confirmButtonText: 'Hapus',
                cancelButtonText: 'Batal'
            }).then((result) => {
                if (result.isConfirmed) form.submit();
            });
        });
    });

});
</script>

<style>
.text-gradient-primary {
    background: linear-gradient(90deg, #6a11cb, #2575fc);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}
.hover-scale {
    transition: 0.2s;
}
.hover-scale:hover {
    transform: scale(1.03);
}
</style>
@endsection
