{% extends 'base_admin.html' %}
{% block title %}KPI Agency - CWN{% endblock %}

{% block content %}
<div class="container-fluid mt-4">
    <h2 class="text-center text-white p-3 rounded mb-4 mt-2 custom-bg">KPI Agency</h2>

    <div class="card shadow mb-4">
        <div class="card-body">
            <form method="GET" action="{% url 'kpi' %}">
                <div class="form-row align-items-center">
                    <!-- Bulan -->
                    <div class="form-group col-md-4">
                        <label for="bulan">Bulan</label>
                        <select id="bulan" name="bulan" class="form-control">
                            <option value="" {% if not bulan %}selected{% endif %}>-- Pilih Bulan --</option>
                            <option value="01" {% if bulan == '01' %}selected{% endif %}>Januari</option>
                            <option value="02" {% if bulan == '02' %}selected{% endif %}>Februari</option>
                            <option value="03" {% if bulan == '03' %}selected{% endif %}>Maret</option>
                            <option value="04" {% if bulan == '04' %}selected{% endif %}>April</option>
                            <option value="05" {% if bulan == '05' %}selected{% endif %}>Mei</option>
                            <option value="06" {% if bulan == '06' %}selected{% endif %}>Juni</option>
                            <option value="07" {% if bulan == '07' %}selected{% endif %}>Juli</option>
                            <option value="08" {% if bulan == '08' %}selected{% endif %}>Agustus</option>
                            <option value="09" {% if bulan == '09' %}selected{% endif %}>September</option>
                            <option value="10" {% if bulan == '10' %}selected{% endif %}>Oktober</option>
                            <option value="11" {% if bulan == '11' %}selected{% endif %}>November</option>
                            <option value="12" {% if bulan == '12' %}selected{% endif %}>Desember</option>
                        </select>
                    </div>

                    <!-- Tahun -->
                    <div class="form-group col-md-4">
                        <label for="tahun">Tahun</label>
                        <select id="tahun" name="tahun" class="form-control">
                            <option value="" {% if not tahun %}selected{% endif %}>-- Pilih Tahun --</option>
                            <option value="2024" {% if tahun == '2024' %}selected{% endif %}>2024</option>
                            <option value="2025" {% if tahun == '2025' %}selected{% endif %}>2025</option>
                        </select>
                    </div>

                    <div class="form-group col-md-4 mt-2">
                        <button type="submit" class="btn btn-primary mt-4" style="width: 100%;">Filter</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="card shadow mb-5">
        <div class="card-header custom-bg text-white">
            <h4 class="m-0 font-weight-bold">KPI INDIHOME CWN</h4>
            <small>Periode: {{ bulan }}/{{ tahun }}</small>
        </div>
        <div class="card-body">
            {% if kpi_data %}
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead class="thead-dark">
                        <tr>
                            <th>Indikator</th>
                            <th>Target</th>
                            <th>Real</th>
                            <th>Ach %</th>
                            <th>Bobot</th>
                            <th>Nilai</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for item in kpi_data %}
                            <tr>
                                <td>{{ item.kategori }}</td>
                                <td>{{ item.target|floatformat:0 }}</td>
                                <td>{{ item.real|floatformat:0 }}</td>
                                <td>{{ item.ach|floatformat:0 }}%</td>
                                <td>{{ item.bobot|floatformat:0 }}%</td>
                                <td>{{ item.nilai|floatformat:0 }}%</td>
                            </tr>
                        {% endfor %}
                        <tr class="table-success font-weight-bold">
                            <td colspan="4" class="text-center">SUMMARY KPI</td>
                            <td>{{ total_bobot|floatformat:0 }}%</td>
                            <td>{{ total_nilai|floatformat:0 }}%</td>
                        </tr>
                    </tbody>
                </table>
            </div>
            {% else %}
                <div class="alert alert-warning shadow mt-4">
                    Tidak ada data yang ditemukan untuk bulan dan tahun yang dipilih.
                </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
