<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Grafik Penilaian IKK</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        .header-section {
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
            color: white;
            padding: 2rem 0;
        }
        .card {
            border: none;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            border-radius: 10px;
        }
        .chart-container {
            position: relative;
            height: 400px;
        }
    </style>
</head>
<body>
    <div class="header-section">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <h1 class="mb-2">
                        <i class="fas fa-chart-bar me-3"></i>
                        Grafik Penilaian IKK
                    </h1>
                    <p class="mb-0 fs-5">Visualisasi Capaian Indikator Kinerja Kunci</p>
                </div>
                <div class="col-md-4 text-end">
                    <a href="{{ route('ikk.lembar-jawab.index') }}" class="btn btn-light">
                        <i class="fas fa-arrow-left me-2"></i>Kembali
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="container my-5">
        <!-- Filter -->
        <div class="card mb-4">
            <div class="card-body">
                <form method="GET">
                    <div class="row align-items-end">
                        <div class="col-md-4">
                            <label class="form-label fw-semibold">OPD</label>
                            <select name="opd" class="form-select">
                                <option value="">Semua OPD</option>
                                @foreach($opdList as $opdItem)
                                    <option value="{{ $opdItem }}" {{ $opd == $opdItem ? 'selected' : '' }}>{{ $opdItem }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label fw-semibold">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-2">
                            <button type="submit" class="btn btn-primary w-100">
                                <i class="fas fa-search me-2"></i>Filter
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <!-- Charts -->
        <div class="row">
            <!-- Capaian per OPD -->
            <div class="col-lg-6 mb-4">
                <div class="card">
                    <div class="card-header bg-primary text-white">
                        <h6 class="mb-0"><i class="fas fa-building me-2"></i>Capaian per OPD</h6>
                    </div>
                    <div class="card-body">
                        <div class="chart-container">
                            <canvas id="capaianPerOPDChart"></canvas>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Trend Capaian -->
            <div class="col-lg-6 mb-4">
                <div class="card">
                    <div class="card-header bg-success text-white">
                        <h6 class="mb-0"><i class="fas fa-chart-line me-2"></i>Trend Capaian</h6>
                    </div>
                    <div class="card-body">
                        <div class="chart-container">
                            <canvas id="trendCapaianChart"></canvas>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Status Distribusi -->
            <div class="col-lg-6 mb-4">
                <div class="card">
                    <div class="card-header bg-warning text-dark">
                        <h6 class="mb-0"><i class="fas fa-pie-chart me-2"></i>Distribusi Status</h6>
                    </div>
                    <div class="card-body">
                        <div class="chart-container">
                            <canvas id="statusChart"></canvas>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Summary Stats -->
            <div class="col-lg-6 mb-4">
                <div class="card">
                    <div class="card-header bg-info text-white">
                        <h6 class="mb-0"><i class="fas fa-calculator me-2"></i>Ringkasan Statistik</h6>
                    </div>
                    <div class="card-body">
                        <div class="row text-center">
                            <div class="col-6 mb-3">
                                <h4 class="text-primary">
                                    {{ count($chartData['capaianPerOPD']['data']) > 0 ? number_format(array_sum($chartData['capaianPerOPD']['data']) / count($chartData['capaianPerOPD']['data']), 1) : 0 }}%
                                </h4>
                                <small class="text-muted">Rata-rata Capaian</small>
                            </div>
                            <div class="col-6 mb-3">
                                <h4 class="text-success">
                                    {{ count($chartData['capaianPerOPD']['data']) > 0 ? max($chartData['capaianPerOPD']['data']) : 0 }}%
                                </h4>
                                <small class="text-muted">Capaian Tertinggi</small>
                            </div>
                            <div class="col-6">
                                <h4 class="text-warning">
                                    {{ count($chartData['capaianPerOPD']['data']) > 0 ? min($chartData['capaianPerOPD']['data']) : 0 }}%
                                </h4>
                                <small class="text-muted">Capaian Terendah</small>
                            </div>
                            <div class="col-6">
                                <h4 class="text-info">{{ array_sum($chartData['statusDistribusi']['data']) }}</h4>
                                <small class="text-muted">Total Lembar Jawab</small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        // Capaian per OPD Chart
        const ctxOPD = document.getElementById('capaianPerOPDChart').getContext('2d');
        new Chart(ctxOPD, {
            type: 'bar',
            data: {
                labels: @json($chartData['capaianPerOPD']['labels']),
                datasets: [{
                    label: 'Capaian (%)',
                    data: @json($chartData['capaianPerOPD']['data']),
                    backgroundColor: ['#0d6efd', '#198754', '#ffc107', '#dc3545'],
                    borderColor: ['#0d6efd', '#198754', '#ffc107', '#dc3545'],
                    borderWidth: 1
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                scales: {
                    y: {
                        beginAtZero: true,
                        max: 100
                    }
                }
            }
        });

        // Trend Capaian Chart
        const ctxTrend = document.getElementById('trendCapaianChart').getContext('2d');
        new Chart(ctxTrend, {
            type: 'line',
            data: {
                labels: @json($chartData['trendCapaian']['labels']),
                datasets: [{
                    label: 'Capaian (%)',
                    data: @json($chartData['trendCapaian']['data']),
                    borderColor: '#198754',
                    backgroundColor: 'rgba(25, 135, 84, 0.1)',
                    tension: 0.4,
                    fill: true
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                scales: {
                    y: {
                        beginAtZero: true,
                        max: 100
                    }
                }
            }
        });

        // Status Distribution Chart
        const ctxStatus = document.getElementById('statusChart').getContext('2d');
        new Chart(ctxStatus, {
            type: 'doughnut',
            data: {
                labels: @json($chartData['statusDistribusi']['labels']),
                datasets: [{
                    data: @json($chartData['statusDistribusi']['data']),
                    backgroundColor: ['#6c757d', '#0d6efd', '#198754', '#dc3545']
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false
            }
        });
    </script>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>