@extends('layouts.app')

@section('styles')
<link rel="stylesheet" href="{{ asset('css/penyuluh.css') }}">
@endsection

@section('content')

<div class="page-wrapper">

    {{-- HEADER --}}
    <div class="breadcrumb">
        <a href="/lapor/{{ $lapor->id }}">← Detail Laporan</a>
        <span>›</span>
        <span>Analisis TOPSIS</span>
    </div>

    <div class="page-header">
        <div>
            <h2 class="page-title">📊 Analisis AHP–TOPSIS</h2>
            <p class="page-sub">{{ $lapor->nama_petani }} · {{ $lapor->jenis_hama }}</p>
        </div>
        <div class="result-pill">
            Ranking <strong>#{{ $lapor->ranking }}</strong>
        </div>
    </div>

    {{-- TOP GRID: Data + Bobot + Hasil --}}
    <div class="analisis-top-grid">

        {{-- DATA LAPORAN --}}
        <div class="info-card">
            <div class="info-card-title">📌 Data Laporan</div>
            <div class="info-rows">
                <div class="ir"><span>Petani</span><strong>{{ $lapor->nama_petani }}</strong></div>
                <div class="ir"><span>Jenis Hama</span><strong>{{ $lapor->jenis_hama }}</strong></div>
                <div class="ir">
                    <span>Tingkat Serangan</span>
                    <strong>
                        <span class="tingkat-badge
                            @if($lapor->tingkat_serangan=='Berat') t-berat
                            @elseif($lapor->tingkat_serangan=='Sedang') t-sedang
                            @else t-ringan @endif">
                            {{ $lapor->tingkat_serangan }}
                        </span>
                    </strong>
                </div>
                <div class="ir"><span>Luas Lahan</span><strong>{{ $lapor->luas_lahan }} Ha</strong></div>
                <div class="ir"><span>Lama Kejadian</span><strong>{{ $lapor->lama_kejadian }} hari</strong></div>
            </div>
        </div>

        {{-- BOBOT AHP --}}
        <div class="info-card">
            <div class="info-card-title">⚖️ Bobot Kriteria AHP</div>
            @php
                $kriteriaLabel = ['Tingkat Serangan','Luas Lahan','Lama Kejadian','Jenis Hama'];
                $bobotMax = max($bobot);
            @endphp
            <div class="bobot-list">
                @foreach($bobot as $i => $b)
                <div class="bobot-row">
                    <span class="bobot-label">{{ $kriteriaLabel[$i] ?? 'C'.($i+1) }}</span>
                    <div class="bobot-bar-wrap">
                        <div class="bobot-bar" style="width:{{ round(($b/$bobotMax)*100) }}%"></div>
                    </div>
                    <span class="bobot-val">{{ number_format($b*100,1) }}%</span>
                </div>
                @endforeach
            </div>
        </div>

        {{-- HASIL AKHIR --}}
        <div class="result-card">
            <div class="info-card-title">🏆 Hasil Akhir</div>
            <div class="result-big">
                <div class="result-num">{{ number_format($lapor->nilai_preferensi,4) }}</div>
                <div class="result-lbl">Nilai Preferensi TOPSIS</div>
            </div>
            <div class="result-rank">
                Ranking <span>#{{ $lapor->ranking }}</span>
                dari {{ count($matrix) }} laporan
            </div>
            <div class="result-note">
                Semakin mendekati 1, semakin prioritas penanganan.
            </div>
        </div>

    </div>

    {{-- TABEL PERBANDINGAN --}}
    <div class="table-card">
        <div class="table-topbar">
            <span class="table-label">📊 Perbandingan Semua Laporan</span>
        </div>
        <div class="table-scroll">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>Rank</th>
                        <th>Petani</th>
                        <th>Serangan</th>
                        <th>Luas (Ha)</th>
                        <th>Lama (hr)</th>
                        <th>Hama</th>
                        <th>Preferensi</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($matrix as $row)
                    <tr class="data-row {{ $row['id'] == $lapor->id ? 'row-highlight' : '' }}">
                        <td>
                            <span class="rank-badge
                                @if($row['ranking']==1) rank-gold
                                @elseif($row['ranking']==2) rank-silver
                                @elseif($row['ranking']==3) rank-bronze
                                @else rank-normal @endif">
                                #{{ $row['ranking'] }}
                            </span>
                        </td>
                        <td>
                            <div class="petani-name {{ $row['id']==$lapor->id ? 'highlight-name' : '' }}">
                                {{ $row['petani'] }}
                                @if($row['id']==$lapor->id)
                                    <span class="current-tag">◀ Ini</span>
                                @endif
                            </div>
                        </td>
                        <td>
                            <span class="tingkat-badge
                                @if($row['c1']=='Berat') t-berat
                                @elseif($row['c1']=='Sedang') t-sedang
                                @else t-ringan @endif">
                                {{ $row['c1'] }}
                            </span>
                        </td>
                        <td class="num-cell">{{ $row['c2'] }}</td>
                        <td class="num-cell">{{ $row['c3'] }}</td>
                        <td><span class="hama-tag">{{ $row['c4'] }}</span></td>
                        <td class="nilai-cell {{ $row['id']==$lapor->id ? 'highlight-val' : '' }}">
                            {{ number_format($row['preferensi'],4) }}
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>

</div>

@endsection