@extends('layouts.admin')

@section('content')
    <div class="content-wrapper pb-0 min-vh-100">
        <div class="page-header flex-wrap">
            <h3 class="mb-0">Sistem Klasifikasi Penentuan Kelayakan Pemberian Kredit Menggunakan Metode K-Means</h3>
        </div>

        <div class="card">
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered" id="datatable">
                        <thead>
                            <tr>
                                <th>Nama</th>
                                <th>History Pinjaman</th>
                                <th>Setoran/Bulan</th>
                                <th>Bunga</th>
                                <th>Setoran + Bunga</th>
                                <th>Sisa Pinjaman</th>
                                <th>Kelayakan</th>
                                <th>Cluster</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($nasabah as $n)
                                <tr>
                                    <td>{{ $n->nama }}</td>
                                    <td>Rp. {{ number_format($n->history_pinjaman, 0, ',', '.') }}</td>
                                    <td>Rp. {{ number_format($n->setoran_per_bulan, 0, ',', '.') }}</td>
                                    <td>Rp. {{ number_format($n->bunga, 0, ',', '.') }}</td>
                                    <td>Rp. {{ number_format($n->setoran_dan_bunga, 0, ',', '.') }}</td>
                                    <td>Rp. {{ number_format($n->sisa_pinjaman, 0, ',', '.') }}</td>
                                    <td>
                                        <span class="badge {{ $n->kelayakan == 'Layak' ? 'bg-success' : 'bg-danger' }}">
                                            {{ $n->kelayakan }}
                                        </span>
                                    </td>
                                    <td>
                                        <span class="">
                                            {{ $n->cluster == 0 ? 1 : ($n->cluster == 1 ? 0 : $n->cluster) }}
                                        </span>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <!-- Iterasi K-Means -->
        <div class="card mt-4">
            <div class="card-body">
                <h4 class="card-title">Proses Iterasi K-Means</h4>
                @foreach ($iterations as $iter)
                    <h5 class="mt-5">Iterasi {{ $iter['iteration'] }}</h5>
                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>Nama Nasabah</th>
                                    <th>Jarak ke Cluster 1</th>
                                    <th>Jarak ke Cluster 2</th>
                                    <th>Cluster Terpilih</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($iter['distances'] as $index => $distance)
                                    <tr>
                                        <td>{{ $nasabah[$index]->nama }}</td>
                                        <td>{{ number_format($distance[0], 2) }}</td>
                                        <td>{{ number_format($distance[1], 2) }}</td>
                                        <td><span class="badge bg-primary text-white">Cluster
                                                {{ $iter['clusters'][$index] == 0 ? 1 : ($iter['clusters'][$index] == 1 ? 0 : $iter['clusters'][$index]) }}
                                            </span></td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>

                    <!-- Centroid -->
                    <h5 class="mt-5">Centroid Setelah Iterasi {{ $iter['iteration'] }}</h5>
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>Cluster</th>
                                <th>History Pinjaman</th>
                                <th>Setoran/Bulan</th>
                                <th>Bunga</th>
                                <th>Setoran + Bunga</th>
                                <th>Sisa Pinjaman</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($iter['centroids'] as $index => $centroid)
                                <tr>
                                    <td>Cluster
                                        {{ $index - 1 + 1 == 0 ? 1 : ($index - 1 + 1 == 1 ? 0 : $index - 1 + 1) }}
                                    </td>
                                    @foreach ($centroid as $value)
                                        <td>{{ number_format($value, 2) }}</td>
                                    @endforeach
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                @endforeach
            </div>
        </div>
    </div>
@endsection
