{% extends "base.html" %}
{% block title %}Dashboard{% endblock %}
{% block breadcrumb %}Beranda{% endblock %}
{% block page_title %}Dashboard{% endblock %}

{% block content %}
<div class="stat-grid">
    <div class="stat-card">
        <div class="stat-icon">🏗️</div>
        <div class="stat-label">Proyek Aktif</div>
        <div class="stat-value text-accent">{{ stats.total_projects }}</div>
        <div class="stat-sub">proyek berjalan</div>
    </div>
    <div class="stat-card">
        <div class="stat-icon">📋</div>
        <div class="stat-label">PR Menunggu</div>
        <div class="stat-value text-yellow">{{ stats.total_pr }}</div>
        <div class="stat-sub">perlu persetujuan</div>
    </div>
    <div class="stat-card">
        <div class="stat-icon">📦</div>
        <div class="stat-label">PO Aktif</div>
        <div class="stat-value text-green">{{ stats.total_po }}</div>
        <div class="stat-sub">sedang diproses</div>
    </div>
    <div class="stat-card">
        <div class="stat-icon">📁</div>
        <div class="stat-label">Total Dokumen</div>
        <div class="stat-value">{{ stats.total_docs }}</div>
        <div class="stat-sub">dokumen tersimpan</div>
    </div>
</div>

<div class="grid-2">
    <div class="card">
        <div class="card-header">
            <div class="card-title">Proyek Aktif</div>
            <a href="{{ url_for('projects.create') }}" class="btn btn-primary btn-sm">+ Proyek Baru</a>
        </div>
        {% if projects %}
        <div class="table-wrap">
            <table>
                <thead>
                    <tr><th>Kode</th><th>Nama Proyek</th><th>Nilai Kontrak</th><th>Progress</th></tr>
                </thead>
                <tbody>
                {% for p in projects %}
                <tr>
                    <td><a href="{{ url_for('projects.detail', id=p.id) }}" class="text-accent font-mono fs-13">{{ p.project_code }}</a></td>
                    <td>
                        <div class="fw-600">{{ p.project_name[:35] }}{% if p.project_name|length > 35 %}…{% endif %}</div>
                        <div class="text-muted fs-13">{{ p.client_name }}</div>
                    </td>
                    <td class="font-mono fs-13">Rp {{ "{:,.0f}".format(p.contract_value) }}</td>
                    <td style="min-width:100px">
                        <div style="display:flex;align-items:center;gap:8px">
                            <div class="progress-bar" style="flex:1">
                                <div class="progress-fill {% if p.completion_percentage > 90 %}danger{% elif p.completion_percentage > 70 %}warn{% endif %}" style="width:{{ p.completion_percentage }}%"></div>
                            </div>
                            <span class="fs-13 text-muted">{{ p.completion_percentage }}%</span>
                        </div>
                    </td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <div style="text-align:center;padding:32px;color:var(--text-muted)">
            <div style="font-size:40px;margin-bottom:12px">🏗️</div>
            <div>Belum ada proyek aktif</div>
            <a href="{{ url_for('projects.create') }}" class="btn btn-primary btn-sm" style="margin-top:12px">Buat Proyek Pertama</a>
        </div>
        {% endif %}
    </div>

    <div class="card">
        <div class="card-header">
            <div class="card-title">Purchase Request Terbaru</div>
            <a href="{{ url_for('procurement.pr_create') }}" class="btn btn-primary btn-sm">+ Buat PR</a>
        </div>
        {% if recent_pr %}
        <div class="table-wrap">
            <table>
                <thead><tr><th>No. PR</th><th>Proyek</th><th>Status</th><th>Total</th></tr></thead>
                <tbody>
                {% for pr in recent_pr %}
                <tr>
                    <td><a href="{{ url_for('procurement.pr_detail', id=pr.id) }}" class="text-accent font-mono fs-13">{{ pr.pr_number }}</a></td>
                    <td class="fs-13">{{ pr.project.project_code }}</td>
                    <td>
                        {% if pr.status == 'draft' %}<span class="badge badge-secondary">Draft</span>
                        {% elif pr.status == 'submitted' %}<span class="badge badge-warning">Diajukan</span>
                        {% elif pr.status == 'approved' %}<span class="badge badge-success">Disetujui</span>
                        {% elif pr.status == 'rejected' %}<span class="badge badge-danger">Ditolak</span>
                        {% elif pr.status == 'ordered' %}<span class="badge badge-info">Dipesan</span>
                        {% endif %}
                    </td>
                    <td class="font-mono fs-13">Rp {{ "{:,.0f}".format(pr.total_amount) }}</td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <div style="text-align:center;padding:32px;color:var(--text-muted)">
            <div style="font-size:40px;margin-bottom:12px">📋</div>
            <div>Belum ada Purchase Request</div>
        </div>
        {% endif %}
    </div>
</div>

<div class="card mt-4">
    <div class="card-header">
        <div class="card-title">📌 Informasi PSAK 72 — Pengakuan Pendapatan</div>
    </div>
    <div style="display:grid;grid-template-columns:repeat(3,1fr);gap:16px">
        <div style="padding:16px;background:rgba(79,110,247,.06);border:1px solid rgba(79,110,247,.15);border-radius:8px">
            <div style="font-weight:600;margin-bottom:6px;color:var(--accent)">5 Langkah PSAK 72</div>
            <div class="fs-13 text-muted" style="line-height:1.7">
                1. Identifikasi kontrak<br>
                2. Identifikasi kewajiban pelaksanaan<br>
                3. Tentukan harga transaksi<br>
                4. Alokasi harga transaksi<br>
                5. Akui pendapatan saat kewajiban terpenuhi
            </div>
        </div>
        <div style="padding:16px;background:rgba(34,197,94,.06);border:1px solid rgba(34,197,94,.15);border-radius:8px">
            <div style="font-weight:600;margin-bottom:6px;color:var(--green)">Metode Pengakuan</div>
            <div class="fs-13 text-muted" style="line-height:1.7">
                <strong>Overtime:</strong> Pendapatan diakui sepanjang waktu berdasarkan % penyelesaian (input/output method)<br><br>
                <strong>Point in Time:</strong> Diakui saat kendali beralih ke pelanggan
            </div>
        </div>
        <div style="padding:16px;background:rgba(245,158,11,.06);border:1px solid rgba(245,158,11,.15);border-radius:8px">
            <div style="font-weight:600;margin-bottom:6px;color:var(--yellow)">Dokumen Wajib</div>
            <div class="fs-13 text-muted" style="line-height:1.7">
                • Kontrak/SPK<br>
                • BAST (Berita Acara Serah Terima)<br>
                • Invoice & Faktur Pajak<br>
                • SPM & SP2D<br>
                • Laporan Kemajuan Pekerjaan
            </div>
        </div>
    </div>
</div>
{% endblock %}
