@extends('layouts.app')

@section('title', 'Laporan Survei - Admin SKM')

@section('content')
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h2 style="color: #1e3a8a; font-weight: 700;">
                        <i class="bi bi-bar-chart-line me-2"></i> Laporan Survei
                    </h2>
                    <p class="text-muted">Statistik dan analisis hasil survei kepuasan masyarakat</p>
                </div>
                <a href="{{ route('admin.survei.index') }}" class="btn btn-secondary">
                    <i class="bi bi-arrow-left me-2"></i> Kembali
                </a>
            </div>
        </div>
    </div>

    <div class="card card-gov shadow-sm mb-4">
        <div class="card-header">
            <h5 class="mb-0">
                <i class="bi bi-funnel me-2"></i> Filter Laporan
            </h5>
        </div>
        <div class="card-body">
            <form method="GET" action="{{ route('admin.survei.laporan') }}">
                <div class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label fw-bold">Tahun</label>
                        <select name="tahun" class="form-select">
                            @foreach($tahunList as $thn)
                                <option value="{{ $thn }}" {{ $tahun == $thn ? 'selected' : '' }}>{{ $thn }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label fw-bold">Bulan</label>
                        <select name="bulan" class="form-select">
                            <option value="">Semua Bulan</option>
                            @for($i = 1; $i <= 12; $i++)
                                <option value="{{ $i }}" {{ $bulan == $i ? 'selected' : '' }}>
                                    {{ \Carbon\Carbon::create()->month($i)->format('F') }}
                                </option>
                            @endfor
                        </select>
                    </div>
                    <div class="col-md-3 d-flex align-items-end">
                        <button type="submit" class="btn btn-gov-primary w-100">
                            <i class="bi bi-search me-1"></i> Tampilkan
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="row mb-4">
        <div class="col-md-4 mb-3">
            <div class="card border-0 shadow-sm" style="border-left: 4px solid #1e3a8a !important;">
                <div class="card-body text-center">
                    <h6 class="text-muted mb-2" style="font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.5px;">
                        Total Responden
                    </h6>
                    <h2 class="mb-0" style="color: #1e3a8a; font-weight: 700;">{{ $totalResponden }}</h2>
                </div>
            </div>
        </div>
        <div class="col-md-4 mb-3">
            <div class="card border-0 shadow-sm" style="border-left: 4px solid #28a745 !important;">
                <div class="card-body text-center">
                    <h6 class="text-muted mb-2" style="font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.5px;">
                        Indeks Kepuasan Masyarakat (IKM)
                    </h6>
                    <h2 class="mb-0" style="color: #28a745; font-weight: 700;">{{ number_format($ikm, 2) }}</h2>
                </div>
            </div>
        </div>
        <div class="col-md-4 mb-3">
            <div class="card border-0 shadow-sm" style="border-left: 4px solid #17a2b8 !important;">
                <div class="card-body text-center">
                    <h6 class="text-muted mb-2" style="font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.5px;">
                        Kategori
                    </h6>
                    @php
                        if ($ikm >= 88.31) {
                            $badgeClass = 'success';
                        } elseif ($ikm >= 76.61) {
                            $badgeClass = 'info';
                        } elseif ($ikm >= 65.00) {
                            $badgeClass = 'warning';
                        } else {
                            $badgeClass = 'danger';
                        }
                    @endphp
                    <h3 class="mb-0">
                        <span class="badge bg-{{ $badgeClass }} fs-5">{{ $kategori }}</span>
                    </h3>
                </div>
            </div>
        </div>
    </div>

    <div class="card card-gov shadow-sm">
        <div class="card-header">
            <h5 class="mb-0">
                <i class="bi bi-table me-2"></i> Statistik Per Unsur Pelayanan
            </h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead class="table-light">
                        <tr>
                            <th style="width: 50px;" class="text-center">No</th>
                            <th>Unsur Pelayanan</th>
                            <th style="width: 120px;" class="text-center">Total Responden</th>
                            <th style="width: 120px;" class="text-center">Total Nilai</th>
                            <th style="width: 120px;" class="text-center">Rata-rata</th>
                            <th style="width: 120px;" class="text-center">Indeks</th>
                            <th style="width: 150px;" class="text-center">Kategori</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($statistikUnsur as $unsurId => $stat)
                        <tr>
                            <td class="text-center fw-bold">{{ $unsurId }}</td>
                            <td>
                                <strong>{{ $unsurLabels[$unsurId] }}</strong>
                            </td>
                            <td class="text-center">
                                <span class="badge bg-secondary">{{ $stat['total'] }}</span>
                            </td>
                            <td class="text-center">{{ $stat['total_nilai'] }}</td>
                            <td class="text-center">
                                <span class="badge bg-primary fs-6">{{ number_format($stat['rata_rata'], 2) }}</span>
                            </td>
                            <td class="text-center">
                                <strong>{{ number_format($stat['indeks'], 2) }}</strong>
                            </td>
                            <td class="text-center">
                                @php
                                    $indeksUnsur = $stat['indeks'];
                                    if ($indeksUnsur >= 88.31) {
                                        $kategoriUnsur = 'Sangat Baik';
                                        $badgeClassUnsur = 'success';
                                    } elseif ($indeksUnsur >= 76.61) {
                                        $kategoriUnsur = 'Baik';
                                        $badgeClassUnsur = 'info';
                                    } elseif ($indeksUnsur >= 65.00) {
                                        $kategoriUnsur = 'Kurang Baik';
                                        $badgeClassUnsur = 'warning';
                                    } else {
                                        $kategoriUnsur = 'Tidak Baik';
                                        $badgeClassUnsur = 'danger';
                                    }
                                @endphp
                                <span class="badge bg-{{ $badgeClassUnsur }}">{{ $kategoriUnsur }}</span>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                    <tfoot class="table-light">
                        <tr>
                            <th colspan="5" class="text-end">Rata-rata IKM:</th>
                            <th class="text-center">{{ number_format($ikm, 2) }}</th>
                            <th class="text-center">
                                <span class="badge bg-{{ $badgeClass }}">{{ $kategori }}</span>
                            </th>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>

    <div class="card card-gov shadow-sm mt-4">
        <div class="card-header">
            <h5 class="mb-0">
                <i class="bi bi-info-circle me-2"></i> Kategori Penilaian
            </h5>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-3">
                    <div class="alert alert-success mb-2">
                        <strong>Sangat Baik:</strong> 88.31 - 100
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="alert alert-info mb-2">
                        <strong>Baik:</strong> 76.61 - 88.30
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="alert alert-warning mb-2">
                        <strong>Kurang Baik:</strong> 65.00 - 76.60
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="alert alert-danger mb-2">
                        <strong>Tidak Baik:</strong> 25.00 - 64.99
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection