{% extends "base.twig" %}

{% block title %}{{ title }}{% endblock %}

{% block styles %}
<style>
    :root {
        --primary: #667eea;
        --primary-dark: #4f46e5;
        --success: #10b981;
        --warning: #f59e0b;
        --danger: #ef4444;
        --info: #3b82f6;
        --gray-50: #f9fafb;
        --gray-100: #f3f4f6;
        --gray-200: #e5e7eb;
        --gray-500: #6b7280;
        --gray-600: #4b5563;
        --gray-700: #374151;
        --shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1);
        --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
        --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1);
    }

    .mp-container { padding: 24px; background: var(--gray-50); min-height: calc(100vh - 70px); }

    .page-header {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        padding: 28px 32px;
        border-radius: 16px;
        box-shadow: var(--shadow-lg);
        margin-bottom: 24px;
        position: relative;
        overflow: hidden;
    }
    .page-header::before {
        content: '';
        position: absolute;
        top: -50%;
        right: -10%;
        width: 300px;
        height: 300px;
        background: rgba(255,255,255,0.1);
        border-radius: 50%;
    }
    .page-header h1 { font-size: 28px; font-weight: 700; margin-bottom: 4px; position: relative; z-index: 1; }
    .page-header p { opacity: 0.9; font-size: 14px; margin: 0; position: relative; z-index: 1; }

    .kpi-grid {
        display: grid;
        grid-template-columns: repeat(5, 1fr);
        gap: 16px;
        margin-bottom: 24px;
    }
    @media (max-width: 992px) { .kpi-grid { grid-template-columns: repeat(2, 1fr); } }

    .kpi-card {
        background: white;
        padding: 18px;
        border-radius: 12px;
        box-shadow: var(--shadow);
        border-left: 4px solid var(--primary);
    }
    .kpi-card.success { border-left-color: var(--success); }
    .kpi-card.warning { border-left-color: var(--warning); }
    .kpi-card.danger  { border-left-color: var(--danger); }
    .kpi-card.info    { border-left-color: var(--info); }

    .kpi-label { font-size: 12px; color: var(--gray-600); text-transform: uppercase; letter-spacing: 0.5px; font-weight: 600; }
    .kpi-value { font-size: 26px; font-weight: 800; color: var(--gray-700); margin-top: 4px; }
    .kpi-sub   { font-size: 12px; color: var(--gray-500); margin-top: 4px; }

    /* Module cards */
    .module-grid {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 16px;
        margin-bottom: 24px;
    }
    @media (max-width: 992px) { .module-grid { grid-template-columns: repeat(2, 1fr); } }

    .module-card {
        background: white;
        border-radius: 14px;
        padding: 20px;
        box-shadow: var(--shadow-md);
        border-top: 5px solid var(--primary);
        position: relative;
    }
    .module-card.m1 { border-top-color: #667eea; }
    .module-card.m2 { border-top-color: #10b981; }
    .module-card.m3 { border-top-color: #f59e0b; }
    .module-card.m4 { border-top-color: #ef4444; }

    .module-code {
        display: inline-block;
        padding: 4px 10px;
        background: var(--gray-100);
        color: var(--gray-700);
        border-radius: 6px;
        font-size: 12px;
        font-weight: 700;
        margin-bottom: 8px;
    }
    .m1 .module-code { background: #ede9fe; color: #5b21b6; }
    .m2 .module-code { background: #d1fae5; color: #065f46; }
    .m3 .module-code { background: #fef3c7; color: #92400e; }
    .m4 .module-code { background: #fee2e2; color: #991b1b; }

    .module-title { font-size: 16px; font-weight: 700; color: var(--gray-700); margin-bottom: 4px; }
    .module-date  { font-size: 12px; color: var(--gray-500); margin-bottom: 12px; }

    .module-stat-row { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px dashed var(--gray-200); font-size: 13px; }
    .module-stat-row:last-child { border-bottom: none; }
    .module-stat-label { color: var(--gray-600); }
    .module-stat-value { font-weight: 700; color: var(--gray-700); }

    .delta-badge {
        display: inline-block;
        padding: 6px 12px;
        border-radius: 8px;
        font-weight: 700;
        font-size: 14px;
        margin-top: 10px;
    }
    .delta-badge.up { background: #d1fae5; color: #065f46; }
    .delta-badge.down { background: #fee2e2; color: #991b1b; }
    .delta-badge.flat { background: var(--gray-100); color: var(--gray-600); }

    /* Section card */
    .section-card {
        background: white;
        border-radius: 14px;
        padding: 24px;
        box-shadow: var(--shadow-md);
        margin-bottom: 24px;
    }
    .section-title {
        font-size: 18px;
        font-weight: 700;
        color: var(--gray-700);
        margin-bottom: 16px;
        display: flex;
        align-items: center;
        gap: 8px;
    }
    .section-sub { font-size: 13px; color: var(--gray-500); margin-bottom: 14px; }

    /* Tables */
    .data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
    .data-table th {
        background: var(--gray-50);
        padding: 10px 12px;
        text-align: left;
        font-weight: 600;
        color: var(--gray-700);
        border-bottom: 2px solid var(--gray-200);
        font-size: 12px;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }
    .data-table td { padding: 10px 12px; border-bottom: 1px solid var(--gray-100); }
    .data-table tr:hover { background: var(--gray-50); }

    /* Module pill badges */
    .mod-pill {
        display: inline-block;
        padding: 3px 8px;
        border-radius: 6px;
        font-size: 11px;
        font-weight: 700;
        margin-right: 4px;
        border: 1.5px solid transparent;
    }
    .mod-pill.attended.m1 { background: #ede9fe; color: #5b21b6; }
    .mod-pill.attended.m2 { background: #d1fae5; color: #065f46; }
    .mod-pill.attended.m3 { background: #fef3c7; color: #92400e; }
    .mod-pill.attended.m4 { background: #fee2e2; color: #991b1b; }
    .mod-pill.absent {
        background: white;
        color: var(--gray-400);
        border-color: var(--gray-200);
        text-decoration: line-through;
        opacity: 0.6;
    }

    .trend-pill { display: inline-flex; align-items: center; gap: 4px; padding: 4px 10px; border-radius: 12px; font-size: 12px; font-weight: 600; }
    .trend-pill.up { background: #d1fae5; color: #065f46; }
    .trend-pill.down { background: #fee2e2; color: #991b1b; }
    .trend-pill.flat { background: var(--gray-100); color: var(--gray-600); }
    .trend-pill.none { background: var(--gray-100); color: var(--gray-500); font-style: italic; }

    /* Individual cards */
    .indiv-card {
        background: white;
        border-radius: 12px;
        margin-bottom: 12px;
        box-shadow: var(--shadow);
        overflow: hidden;
    }
    .indiv-head {
        padding: 14px 18px;
        cursor: pointer;
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 12px;
        flex-wrap: wrap;
        border-left: 4px solid var(--gray-200);
    }
    .indiv-card.up   .indiv-head { border-left-color: var(--success); }
    .indiv-card.down .indiv-head { border-left-color: var(--danger); }
    .indiv-card.none .indiv-head { border-left-color: var(--gray-300); }
    .indiv-head:hover { background: var(--gray-50); }
    .indiv-name { font-weight: 700; color: var(--gray-700); }
    .indiv-business { font-size: 12px; color: var(--gray-500); }
    .indiv-body { display: none; padding: 18px; border-top: 1px solid var(--gray-100); background: var(--gray-50); }
    .indiv-card.expanded .indiv-body { display: block; }

    .chart-mini { height: 180px; max-height: 180px; }
    .chart-wrap { position: relative; height: 320px; }
    .chart-wrap-sm { position: relative; height: 280px; }
    .lk-chart-wrap {
        position: relative;
        height: 300px;
        background: linear-gradient(180deg, #fafbff 0%, #ffffff 100%);
        padding: 10px 14px;
        border-radius: 12px;
        border: 1px solid var(--gray-200);
    }

    /* LK Cards */
    .lk-grid {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 14px;
        margin-top: 16px;
    }
    @media (max-width: 992px) { .lk-grid { grid-template-columns: repeat(2, 1fr); } }
    @media (max-width: 576px) { .lk-grid { grid-template-columns: 1fr; } }

    .lk-card {
        background: linear-gradient(135deg, #f8fafc 0%, #ffffff 100%);
        border: 1px solid var(--gray-200);
        border-radius: 12px;
        padding: 16px;
        transition: all 0.2s;
    }
    .lk-card:hover { box-shadow: var(--shadow-md); transform: translateY(-2px); }

    .lk-head { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
    .lk-num {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        width: 44px;
        height: 44px;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: 800;
        font-size: 14px;
        flex-shrink: 0;
    }
    .lk-title { font-size: 13px; font-weight: 600; color: var(--gray-700); line-height: 1.3; }

    .lk-stat-row { display: flex; gap: 12px; margin-bottom: 12px; }
    .lk-stat {
        flex: 1;
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 8px 10px;
        border-radius: 8px;
    }
    .lk-stat.done { background: #d1fae5; color: #065f46; }
    .lk-stat.undone { background: #fee2e2; color: #991b1b; }
    .lk-stat i { font-size: 20px; flex-shrink: 0; }
    .lk-stat-num { font-size: 18px; font-weight: 800; line-height: 1; }
    .lk-stat-lbl { font-size: 10px; text-transform: uppercase; letter-spacing: 0.5px; font-weight: 600; opacity: 0.8; }

    .lk-progress-wrap {
        position: relative;
        background: var(--gray-100);
        border-radius: 999px;
        height: 24px;
        overflow: hidden;
    }
    .lk-progress-bar {
        background: linear-gradient(90deg, #10b981 0%, #34d399 100%);
        height: 100%;
        border-radius: 999px;
        transition: width 0.6s ease;
    }
    .lk-progress-text {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        font-size: 12px;
        font-weight: 700;
        color: var(--gray-700);
        z-index: 2;
    }

    .filter-bar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
    .filter-bar input, .filter-bar select {
        padding: 8px 12px;
        border: 1px solid var(--gray-200);
        border-radius: 8px;
        font-size: 13px;
        background: white;
    }

    .insight-box {
        background: linear-gradient(135deg, #eef2ff 0%, #f0f9ff 100%);
        border-left: 4px solid var(--primary);
        padding: 14px 18px;
        border-radius: 10px;
        margin-top: 12px;
        font-size: 13px;
        color: var(--gray-700);
        line-height: 1.6;
    }

    .pagination-row { display: flex; justify-content: space-between; align-items: center; margin-top: 16px; gap: 10px; flex-wrap: wrap; }
    .page-btn { padding: 6px 12px; background: white; border: 1px solid var(--gray-200); border-radius: 6px; cursor: pointer; font-size: 13px; }
    .page-btn:hover { background: var(--gray-100); }
    .page-btn.active { background: var(--primary); color: white; border-color: var(--primary); }
    .page-btn:disabled { opacity: 0.5; cursor: not-allowed; }
</style>
{% endblock %}

{% block contents %}
<div class="mp-container">
    <div class="page-header">
        <h1><i class="bi bi-mortarboard-fill"></i> Prestasi Modul Latihan (M1–M4)</h1>
        <p>Analisis korelasi antara penyertaan modul dengan prestasi jualan peserta — Sept 2025 hingga Feb 2026</p>
    </div>

    {# === KPI ROW === #}
    <div class="kpi-grid" style="grid-template-columns: repeat(3, 1fr);">
        <div class="kpi-card info">
            <div class="kpi-label">Jumlah Peserta Aktif</div>
            <div class="kpi-value">{{ totalParticipants }}</div>
            <div class="kpi-sub">Sedang dipantau</div>
        </div>
        <div class="kpi-card success">
            <div class="kpi-label">Hadir Semua 4 Modul</div>
            <div class="kpi-value">{{ allFourCount }}</div>
            <div class="kpi-sub">{{ allFourPct|number_format(1) }}% daripada peserta</div>
        </div>
        <div class="kpi-card success">
            <div class="kpi-label">Kadar Penyiapan LK</div>
            <div class="kpi-value">{{ overallLkPct|number_format(1) }}%</div>
            <div class="kpi-sub">{{ totalLkCompletions }} / {{ totalLkPossible }} penyiapan</div>
        </div>
    </div>

    {# === MODULE CARDS === #}
    <div class="section-card">
        <div class="section-title"><i class="bi bi-grid-3x3-gap-fill"></i> Ringkasan 4 Modul</div>
        <div class="section-sub">Kehadiran setiap modul latihan sepanjang program.</div>

        <div class="module-grid">
            {% for code, m in modules %}
            <div class="module-card {{ code|lower }}">
                <span class="module-code">{{ code }}</span>
                <div class="module-title">{{ m.name }}</div>
                <div class="module-date"><i class="bi bi-calendar-event"></i> {{ m.date }}</div>

                <div class="module-stat-row">
                    <span class="module-stat-label">Kehadiran</span>
                    <span class="module-stat-value">{{ m.attended }} / {{ totalParticipants }}</span>
                </div>
                <div class="module-stat-row">
                    <span class="module-stat-label">Tidak Hadir</span>
                    <span class="module-stat-value text-danger">{{ totalParticipants - m.attended }}</span>
                </div>

                <div class="delta-badge up" style="background:#d1fae5; color:#065f46;">
                    <i class="bi bi-people-fill"></i> {{ m.attendanceRate|number_format(1) }}% kadar kehadiran
                </div>
            </div>
            {% endfor %}
        </div>

        <div class="insight-box">
            <strong><i class="bi bi-lightbulb"></i> Penemuan Utama:</strong>
            {{ allFourCount }} peserta ({{ allFourPct|number_format(1) }}%) menghadiri kesemua 4 modul, menunjukkan tahap penglibatan yang tinggi sepanjang program.
        </div>
    </div>

    {# === WORKSHEET (LK) INFOGRAPHIC === #}
    <div class="section-card">
        <div class="section-title"><i class="bi bi-journal-check"></i> Prestasi Lembaran Kerja (LK1–LK6)</div>
        <div class="section-sub">Bilangan peserta yang telah melengkapkan setiap lembaran kerja vs. yang belum.</div>

        {# Top KPI strip for LK #}
        <div class="kpi-grid" style="grid-template-columns: repeat(4, 1fr); margin-top: 8px;">
            <div class="kpi-card info">
                <div class="kpi-label">Jumlah Peserta</div>
                <div class="kpi-value">{{ totalParticipants }}</div>
                <div class="kpi-sub">Sasaran setiap LK</div>
            </div>
            <div class="kpi-card success">
                <div class="kpi-label">Jumlah Penyiapan</div>
                <div class="kpi-value">{{ totalLkCompletions }}</div>
                <div class="kpi-sub">dari {{ totalLkPossible }} kemungkinan</div>
            </div>
            <div class="kpi-card warning">
                <div class="kpi-label">Belum Disiapkan</div>
                <div class="kpi-value">{{ totalLkPossible - totalLkCompletions }}</div>
                <div class="kpi-sub">Penyertaan tertinggal</div>
            </div>
            <div class="kpi-card success">
                <div class="kpi-label">Kadar Keseluruhan</div>
                <div class="kpi-value">{{ overallLkPct|number_format(1) }}%</div>
                <div class="kpi-sub">Purata penyiapan LK</div>
            </div>
        </div>

        {# LK card grid #}
        <div class="lk-grid">
            {% for lk in worksheets %}
            <div class="lk-card">
                <div class="lk-head">
                    <div class="lk-num">LK{{ lk.no }}</div>
                    <div class="lk-title">{{ lk.title }}</div>
                </div>
                <div class="lk-stat-row">
                    <div class="lk-stat done">
                        <i class="bi bi-check-circle-fill"></i>
                        <div>
                            <div class="lk-stat-num">{{ lk.completed }}</div>
                            <div class="lk-stat-lbl">Telah Siap</div>
                        </div>
                    </div>
                    <div class="lk-stat undone">
                        <i class="bi bi-x-circle-fill"></i>
                        <div>
                            <div class="lk-stat-num">{{ lk.not_completed }}</div>
                            <div class="lk-stat-lbl">Belum Siap</div>
                        </div>
                    </div>
                </div>
                <div class="lk-progress-wrap">
                    <div class="lk-progress-bar" style="width: {{ lk.pct }}%;"></div>
                    <span class="lk-progress-text">{{ lk.pct|number_format(1) }}%</span>
                </div>
            </div>
            {% endfor %}
        </div>

        {# Stacked bar chart #}
        <div class="mt-4">
            <h6 style="font-weight: 700; margin-bottom: 8px;"><i class="bi bi-bar-chart-line"></i> Perbandingan Visual</h6>
            <div class="lk-chart-wrap"><canvas id="lkChart"></canvas></div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2.2.0/dist/chartjs-plugin-datalabels.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
    const lkData = {{ worksheets|json_encode|raw }};
    const totalParticipants = {{ totalParticipants }};

    // Don't apply datalabels globally — only enable it per-chart
    if (typeof ChartDataLabels !== 'undefined') {
        Chart.unregister(ChartDataLabels);
    }

    // ── LK (Worksheet) Bar Chart ──
    const lkLabels = lkData.map(w => 'LK' + w.no);
    const lkCompleted = lkData.map(w => w.completed);
    const lkNotCompleted = lkData.map(w => w.not_completed);
    const lkPct = lkData.map(w => w.pct);

    const lkCtx = document.getElementById('lkChart').getContext('2d');
    new Chart(lkCtx, {
        type: 'bar',
        data: {
            labels: lkLabels,
            datasets: [
                {
                    label: 'Telah Siap',
                    data: lkCompleted,
                    backgroundColor: 'rgba(16,185,129,0.9)',
                    borderColor: '#059669',
                    borderWidth: 0,
                    borderRadius: 4,
                    barThickness: 12
                },
                {
                    label: 'Belum Siap',
                    data: lkNotCompleted,
                    backgroundColor: 'rgba(239,68,68,0.9)',
                    borderColor: '#dc2626',
                    borderWidth: 0,
                    borderRadius: 4,
                    barThickness: 12
                }
            ]
        },
        options: {
            indexAxis: 'y',
            responsive: true,
            maintainAspectRatio: false,
            layout: {
                padding: { top: 4, right: 80, bottom: 4, left: 6 }
            },
            plugins: {
                legend: {
                    position: 'top',
                    align: 'end',
                    labels: { boxWidth: 10, boxHeight: 10, padding: 10, font: { size: 11, weight: '600' } }
                },
                tooltip: {
                    backgroundColor: 'rgba(31,41,55,0.95)',
                    padding: 12,
                    cornerRadius: 8,
                    titleFont: { size: 13, weight: 'bold' },
                    bodyFont: { size: 12 },
                    callbacks: {
                        label: (ctx) => {
                            const val = ctx.parsed.x;
                            const pct = totalParticipants > 0 ? (val / totalParticipants * 100).toFixed(1) : 0;
                            return '  ' + ctx.dataset.label + ': ' + val + ' peserta (' + pct + '%)';
                        },
                        afterBody: (items) => {
                            const idx = items[0].dataIndex;
                            return '\n' + lkData[idx].title;
                        }
                    }
                },
                datalabels: {
                    color: (ctx) => ctx.dataset.label === 'Telah Siap' ? '#065f46' : '#991b1b',
                    font: { weight: 'bold', size: 10 },
                    formatter: (value) => {
                        if (value === 0) return '0';
                        const pct = totalParticipants > 0 ? (value / totalParticipants * 100).toFixed(0) : 0;
                        return value + ' (' + pct + '%)';
                    },
                    anchor: 'end',
                    align: 'right',
                    offset: 4,
                    clamp: true,
                    clip: false
                }
            },
            scales: {
                x: {
                    beginAtZero: true,
                    max: totalParticipants,
                    grid: { color: 'rgba(0,0,0,0.05)', drawBorder: false },
                    ticks: { font: { size: 10 }, color: '#6b7280', stepSize: 100 },
                    title: { display: false }
                },
                y: {
                    grid: { display: false, drawBorder: false },
                    ticks: { font: { size: 12, weight: 'bold' }, color: '#374151', padding: 4 }
                }
            }
        },
        plugins: [ChartDataLabels]
    });
});
</script>
{% endblock %}
