@extends('layout.app')

@section('content')

<div class="pagetitle">
    <h1>Normalisasi Alternatif</h1>
    <nav>
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ route('admindash') }}">Home</a></li>
            <li class="breadcrumb-item active"><a href="{{ route('alternatif.pilih') }}">Pemilihan Alternatif</a></li>
            <li class="breadcrumb-item active"><a href="{{ route('alternatif.perbandingan') }}">Perbandingan Alternatif</a></li>
            <li class="breadcrumb-item active"><a href="{{ route('alternatif.normalisasi') }}">Normalisasi Alternatif</a></li>
        </ol>
    </nav>
</div><!-- End Page Title -->

<section class="section">
    <div class="row">
        <div class="col-lg-12">

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

            @foreach ($kriterias as $kriteria)
                <div class="card mb-4">
                    <div class="card-body">
                        <h5 class="card-title">{{ $kriteria->nama }}</h5>

                        <div class="table-responsive">
                            <table class="table table-bordered text-center">
                                <thead>
                                    <tr>
                                        <th>Alternatif</th>
                                        @foreach ($alternatifs as $alt)
                                            <th>{{ $alt->nama }}</th>
                                        @endforeach
                                        <th>Jumlah</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @php
                                        $jumlahKolom = [];
                                        foreach ($alternatifs as $colAlt) {
                                            $jumlahKolom[$colAlt->id] = 0;
                                        }

                                        // hitung jumlah kolom
                                        foreach ($alternatifs as $rowAlt) {
                                            foreach ($alternatifs as $colAlt) {
                                                $jumlahKolom[$colAlt->id] += $matriks[$kriteria->id][$rowAlt->id][$colAlt->id];
                                            }
                                        }
                                    @endphp

                                    @foreach ($alternatifs as $rowAlt)
                                        <tr>
                                            <th>{{ $rowAlt->nama }}</th>
                                            @php $jumlahBaris = 0; @endphp
                                            @foreach ($alternatifs as $colAlt)
                                                @php
                                                    $val = $matriks[$kriteria->id][$rowAlt->id][$colAlt->id];
                                                    $normalized = $val / $jumlahKolom[$colAlt->id];
                                                    $jumlahBaris += $normalized;
                                                @endphp
                                                <td>{{ number_format($normalized, 4) }}</td>
                                            @endforeach
                                            <td><strong>{{ number_format($jumlahBaris, 4) }}</strong></td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>

                    </div>
                </div>
            @endforeach

            <!-- Form untuk menyimpan hasil normalisasi -->
            <form action="{{ route('alternatif.simpanNormalisasi') }}" method="POST">
                @csrf
                <div class="text-center mb-5">
                    <button type="submit" class="btn btn-success">Simpan Normalisasi</button>
                </div>
            </form>

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

@endsection
