<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">

    <title>J-Classify Tracer Study MIF</title>
    <meta content="" name="description">
    <meta content="" name="keywords">

    <!-- Favicons -->
    <link href="{{ asset('assets/img/favicon.png') }}" rel="icon">
    <link href="{{ asset('assets/img/apple-touch-icon.png') }}" rel="apple-touch-icon">

    <!-- Google Fonts -->
    <link href="https://fonts.gstatic.com" rel="preconnect">
    <link
        href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Nunito:300,300i,400,400i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i"
        rel="stylesheet">

    <!-- Tambahkan ini di bagian <head> atau sebelum penutup </body> -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>


    <!-- Vendor CSS Files -->
    <link href="{{ asset('assets/vendor/bootstrap/css/bootstrap.min.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/vendor/bootstrap-icons/bootstrap-icons.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/vendor/boxicons/css/boxicons.min.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/vendor/quill/quill.snow.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/vendor/quill/quill.bubble.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/vendor/remixicon/remixicon.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/vendor/simple-datatables/style.css') }}" rel="stylesheet">

    <!-- Template Main CSS File -->
    <link href="{{ asset('assets/css/style.css') }}" rel="stylesheet">
</head>

<body>
    <!-- ======= Header ======= -->
    @include('header')
    <!-- ======= Sidebar ======= -->
    @include('sidebar')

    <main id="main" class="main">
        <div class="pagetitle">
            <h1>Hasil Klasifikasi</h1>
            <nav>
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="index.html">Home</a></li>
                    <li class="breadcrumb-item">Hasil</li>
                </ol>
            </nav>
        </div><!-- End Page Title -->

            {{-- @section('content') --}}
        <section class="section">
            <div class="row">
                <div class="col-lg-12">
                    <div class="card">
        
                        <div class="card-body">
                            <h5 class="card-title">Hasil Klasifikasi K-Means</h5>
        
                            <div class="d-flex justify-content-end align-items-center mb-3">
                               
                            {{-- Dropdown Pilih Angkatan --}}
                            <form action="{{ route('hasil_klasifikasi.index') }}" method="GET" id="angkatanForm">
                                <div class="form-group mb-3">
                                    <label for="angkatan">Pilih Tahun Angkatan:</label>
                                    <select name="id_angkatan" id="angkatan" class="form-control"
                                        onchange="document.getElementById('angkatanForm').submit();">
                                        <option value="">-- Pilih Angkatan --</option>
                                        @foreach($angkatan as $item)
                                            <option value="{{ $item->id_angkatan }}"
                                                {{ (isset($id_angkatan) && $id_angkatan == $item->id_angkatan) ? 'selected' : '' }}>
                                                {{ $item->tahun }}
                                            </option>
                                        @endforeach
                                    </select>
                                </div>
                            </form>

                            </div>
                                @isset($data)
                                @if(is_null($id_angkatan))
                                    <p class="mt-4 text-muted">Silakan pilih tahun angkatan untuk melihat hasil klasifikasi.</p>
                                @elseif(count($data) > 0)
                                    {{-- Tabel Hasil Klasifikasi --}}
                                    <div class="table-responsive">
                                       <table class="table table-bordered">
                                        <thead>
                                            <tr>
                                                <th>No</th>
                                                <th>Nama</th>
                                                <th>NIM</th>
                                                <th>Tahun Angkatan</th>
                                                <th>Pekerjaan</th>
                                                <th>Kelompok Jabatan</th>
                                                <th>Kualifikasi Bidang</th>
                                                <th>Cluster</th>
                                                <th>Hasil Klasifikasi</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach($data as $index => $item)
                                                <tr>
                                                    <td>{{ $index + 1 }}</td>
                                                    <td>{{ $item->nama }}</td>
                                                    <td>{{ $item->nim }}</td>
                                                    <td>{{ $item->tahunAngkatan->tahun ?? '-' }}</td>   
                                                    <td>{{ $item->pekerjaan }}</td>
                                                    <td>{{ $jabatanMap[$item->jabatan] ?? '-' }}</td>
                                                    <td>{{ $item->kualifikasiBidang->kualifikasi_bidang ?? '-' }}</td>
                                                    <td>{{ $item->hasilKlasifikasi->cluster ?? '-' }}</td>
                                                    <td>
                                                        @php
                                                            $cluster = $item->hasilKlasifikasi->cluster ?? null;
                                                            $label = '-';
                                                            if ($cluster === 0) $label = 'Cluster 0 - TIDAK SESUAI';
                                                            elseif ($cluster === 1) $label = 'Cluster 1 - KURANG SESUAI';
                                                            elseif ($cluster === 2) $label = 'Cluster 2 - SESUAI';
                                                        @endphp
                                                        {{ $label }}
                                                    </td>
                                                </tr>
                                            @endforeach
                                        </tbody>
                                    </table>
                                    </div>
                                @else
                                    <p class="mt-4">Belum ada hasil klasifikasi untuk angkatan ini. Silahkan lakukan Perhitungan Proses Kmeans pada menu proses lalu simpan data. </p>
                                @endif
                            @endisset
                        </div>
                    </div>
                </div>
            </div>
        </section>        
        {{-- @endsection --}}

        
    </main><!-- End #main -->

    


    <!-- ======= Footer ======= -->
    @include('footer')

    <a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i
            class="bi bi-arrow-up-short"></i></a>

    <!-- Vendor JS Files -->
    <script src="{{ asset('assets/vendor/apexcharts/apexcharts.min.js') }}"></script>
    <script src="{{ asset('assets/vendor/bootstrap/js/bootstrap.bundle.min.js') }}"></script>
    <script src="{{ asset('assets/vendor/chart.js/chart.umd.js') }}"></script>
    <script src="{{ asset('assets/vendor/echarts/echarts.min.js') }}"></script>
    <script src="{{ asset('assets/vendor/quill/quill.js') }}"></script>
    <script src="{{ asset('assets/vendor/simple-datatables/simple-datatables.js') }}"></script>
    <script src="{{ asset('assets/vendor/tinymce/tinymce.min.js') }}"></script>
    <script src="{{ asset('assets/vendor/php-email-form/validate.js') }}"></script>

    <!-- Template Main JS File -->
    <script src="{{ asset('assets/js/main.js') }}"></script>

    <script>
        document.getElementById('searchInput').addEventListener('keyup', function() {
            let filter = this.value.toLowerCase();
            let rows = document.querySelectorAll('tbody tr');

            rows.forEach(row => {
                let text = row.innerText.toLowerCase();
                row.style.display = text.includes(filter) ? '' : 'none';
            });
        });
    </script>


    <!-- Ini buat sweet alert Konfirmasi Hapus-->
    <script>
        function confirmDelete(userId) {
            Swal.fire({
                title: 'Apakah Anda Yakin?',
                text: "Pengguna ini akan dihapus!",
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#d33',
                cancelButtonColor: '#3085d6',
                confirmButtonText: 'Ya, Hapus!',
                cancelButtonText: 'Batal'
            }).then((result) => {
                if (result.isConfirmed) {
                    // Mengirimkan request delete ke server
                    fetch(`/user/delete/${userId}`, {
                            method: 'DELETE',
                            headers: {
                                'X-CSRF-TOKEN': '{{ csrf_token() }}'
                            }
                        }).then(response => response.json())
                        .then(data => {
                            if (data.success) {
                                Swal.fire(
                                    'Dihapus!',
                                    'Pengguna berhasil dihapus.',
                                    'success'
                                ).then(() => {
                                    location.reload(); // Reload halaman setelah konfirmasi berhasil
                                });
                            } else {
                                Swal.fire(
                                    'Gagal!',
                                    'Pengguna gagal dihapus.',
                                    'error'
                                );
                            }
                        })
                        .catch(error => console.error('Terjadi kesalahan:', error));
                }
            })
        }
    </script>

    <!-- Ini buat datatables Custom Rownya dengan Sorting --> <!--Pencarian-->
    <script>
        document.addEventListener("DOMContentLoaded", function() {
            const table = document.getElementById("userTable");
            const tbody = table.querySelector("tbody");
            const rows = Array.from(tbody.querySelectorAll("tr"));
            const headers = table.querySelectorAll("thead th");
            const paginationContainer = document.createElement("div");
            paginationContainer.className = "pagination-container mt-3 d-flex justify-content-end";

            const searchInput = document.getElementById("searchInput");
            const entriesPerPageSelect = document.getElementById("entriesPerPage");

            let currentPage = 1;
            let rowsPerPage = parseInt(entriesPerPageSelect.value) || 5;
            let currentSortColumn = null;
            let currentSortDirection = "asc";

            function filterRows() {
                const filter = searchInput.value.toLowerCase();
                return rows.filter(row => row.innerText.toLowerCase().includes(filter));
            }

            function sortRows(columnIndex, direction) {
                rows.sort((a, b) => {
                    const aText = a.cells[columnIndex].innerText.trim().toLowerCase();
                    const bText = b.cells[columnIndex].innerText.trim().toLowerCase();

                    if (!isNaN(aText) && !isNaN(bText)) {
                        return direction === "asc" ? aText - bText : bText - aText;
                    }

                    return direction === "asc" ? aText.localeCompare(bText) : bText.localeCompare(aText);
                });
            }

            function displayTablePage(page) {
                const filteredRows = filterRows();
                const totalPages = Math.ceil(filteredRows.length / rowsPerPage);

                if (page > totalPages) page = totalPages;
                if (page < 1) page = 1;

                tbody.innerHTML = "";

                if (entriesPerPageSelect.value === "all") {
                    filteredRows.forEach(row => tbody.appendChild(row));
                } else {
                    const startIndex = (page - 1) * rowsPerPage;
                    const endIndex = startIndex + rowsPerPage;

                    const rowsToDisplay = filteredRows.slice(startIndex, endIndex);
                    rowsToDisplay.forEach(row => tbody.appendChild(row));

                    updatePagination(page, totalPages);
                }
            }

            function updatePagination(currentPage, totalPages) {
                paginationContainer.innerHTML = "";

                if (totalPages <= 1) return;

                for (let i = 1; i <= totalPages; i++) {
                    const button = document.createElement("button");
                    button.textContent = i;
                    button.className = "btn btn-sm btn-outline-primary mx-1";
                    if (i === currentPage) button.classList.add("active");

                    button.addEventListener("click", () => {
                        displayTablePage(i);
                    });

                    paginationContainer.appendChild(button);
                }
            }

            headers.forEach((header, index) => {
                header.addEventListener("click", () => {
                    if (currentSortColumn === index) {
                        currentSortDirection = currentSortDirection === "asc" ? "desc" : "asc";
                    } else {
                        currentSortColumn = index;
                        currentSortDirection = "asc";
                    }

                    sortRows(index, currentSortDirection);
                    displayTablePage(1);
                });
            });

            searchInput.addEventListener("keyup", () => displayTablePage(1));
            entriesPerPageSelect.addEventListener("change", function() {
                rowsPerPage = entriesPerPageSelect.value === "all" ? rows.length : parseInt(
                    entriesPerPageSelect.value);
                displayTablePage(1);
            });

            displayTablePage(1);
            table.parentNode.appendChild(paginationContainer);
        });
    </script>



</body>

</html>
