@extends('layout.app')
@section('content')

<div class="pagetitle">
    <h1>Matriks Perbandingan</h1>
    <nav>
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ route('admindash') }}">Home</a></li>
            <li class="breadcrumb-item"><a href="{{ route('perbandingan') }}">Perbandingan Kriteria</a></li>
        </ol>
    </nav>
</div><!-- End Page Title -->

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

            <!-- Form input -->
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title mb-4">Input Perbandingan Kriteria</h5>
                    <form method="POST" action="{{ route('sementara') }}">
                        @csrf
                        <div class="table-responsive">
                            <table class="table table-bordered text-center">
                                <thead>
                                    <tr>
                                        <th>Kriteria</th>
                                        @foreach ($kriterias as $k)
                                            <th>{{ $k->nama }}</th>
                                        @endforeach
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($kriterias as $i => $k1)
                                        <tr>
                                            <th>{{ $k1->nama }}</th>
                                            @foreach ($kriterias as $j => $k2)
                                                <td>
                                                    @if ($i == $j)
                                                        <input type="text" class="form-control text-center" value="1" disabled>
                                                    @elseif ($i < $j)
                                                        <select name="nilai[{{ $k1->id }}][{{ $k2->id }}]" class="form-select">
                                                            @for ($n = 1; $n <= 9; $n++)
                                                                <option value="{{ $n }}">{{ $n }}</option>
                                                            @endfor
                                                        </select>
                                                    @else
                                                        <input type="text" class="form-control text-center" value="-" disabled>
                                                    @endif
                                                </td>
                                            @endforeach
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                        <div class="text-end mt-3">
                            <button type="submit" class="btn btn-warning">Proses (Tampilkan)</button>
                        </div>
                    </form>
                </div>
            </div>

            <!-- Tabel hasil sementara -->
            @if(session('matriks'))
            <div class="card mt-4">
                <div class="card-body">
                    <h5 class="card-title">Matriks Perbandingan Sementara</h5>
                    <div class="table-responsive">
                        <table class="table table-bordered text-center">
                            <thead>
                                <tr>
                                    <th>Kriteria</th>
                                    @foreach ($kriterias as $k)
                                        <th>{{ $k->nama }}</th>
                                    @endforeach
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($kriterias as $baris)
                                    <tr>
                                        <th>{{ $baris->nama }}</th>
                                        @foreach ($kriterias as $kolom)
                                            <td>{{ number_format(session('matriks')[$baris->id][$kolom->id], 4) }}</td>
                                        @endforeach
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>

                    <!-- Tombol trigger modal -->
                    <div class="text-end">
                        <button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#konfirmasiModal">
                            Simpan & Normalisasi
                        </button>
                    </div>

                    <!-- Modal konfirmasi -->
                    <div class="modal fade" id="konfirmasiModal" tabindex="-1" aria-labelledby="konfirmasiModalLabel" aria-hidden="true">
                        <div class="modal-dialog modal-dialog-centered">
                            <div class="modal-content">
                                <form method="POST" action="{{ route('simpan.perbandingan') }}">
                                    @csrf
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="konfirmasiModalLabel">Konfirmasi Penyimpanan</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Tutup"></button>
                                    </div>
                                    <div class="modal-body">
                                        Apakah Anda yakin data perbandingan ini sudah benar dan ingin disimpan untuk proses normalisasi?
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                                        <button type="submit" class="btn btn-primary">Ya, Simpan</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    <!-- End Modal -->

                </div>
            </div>
            @endif

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

@endsection
