{% extends "base.html" %}
{% block title %}{{ pr.pr_number }}{% endblock %}
{% block breadcrumb %}<a href="{{ url_for('procurement.pr_list') }}" style="color:var(--text-muted)">PR</a> / {{ pr.pr_number }}{% endblock %}
{% block page_title %}{{ pr.pr_number }}{% endblock %}

{% block content %}
<div class="grid-2 mb-4">
<div class="card">
    <div style="display:grid;grid-template-columns:1fr 1fr;gap:14px">
        <div><div class="text-muted fs-13">Proyek</div><div class="fw-600">{{ pr.project.project_code }}</div><div class="fs-13 text-muted">{{ pr.project.project_name }}</div></div>
        <div><div class="text-muted fs-13">Status</div>
            {% if pr.status == 'draft' %}<span class="badge badge-secondary">Draft</span>
            {% elif pr.status == 'submitted' %}<span class="badge badge-warning">Menunggu Persetujuan</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-purple">Sudah Dipesan</span>{% endif %}
        </div>
        <div><div class="text-muted fs-13">Tgl Request</div><div>{{ pr.request_date.strftime('%d %B %Y') }}</div></div>
        <div><div class="text-muted fs-13">Prioritas</div>
            {% if pr.priority == 'urgent' %}<span class="badge badge-danger">URGENT</span>
            {% elif pr.priority == 'high' %}<span class="badge badge-warning">Tinggi</span>
            {% else %}<span class="badge badge-info">{{ pr.priority|title }}</span>{% endif %}
        </div>
        {% if pr.notes %}<div style="grid-column:1/-1"><div class="text-muted fs-13">Catatan</div><div class="fs-13">{{ pr.notes }}</div></div>{% endif %}
    </div>
</div>
<div class="card">
    <div style="font-weight:600;margin-bottom:12px">Aksi</div>
    {% if pr.status == 'draft' %}
    <form method="POST" action="{{ url_for('procurement.pr_submit', id=pr.id) }}">
        <button class="btn btn-primary" style="width:100%;margin-bottom:8px">📤 Ajukan untuk Persetujuan</button>
    </form>
    {% endif %}
    {% if pr.status == 'submitted' %}
    <form method="POST" action="{{ url_for('procurement.pr_approve', id=pr.id) }}" style="display:flex;gap:8px">
        <button name="action" value="approve" class="btn btn-success" style="flex:1">✓ Setujui</button>
        <button name="action" value="reject" class="btn btn-danger" style="flex:1">✗ Tolak</button>
    </form>
    {% endif %}
    {% if pr.status == 'approved' %}
    <a href="{{ url_for('procurement.po_create', pr_id=pr.id) }}" class="btn btn-primary" style="display:block;text-align:center">📦 Buat Purchase Order</a>
    {% endif %}
    <div class="divider"></div>
    <div class="text-muted fs-13">Total Estimasi</div>
    <div style="font-size:20px;font-weight:700;font-family:'JetBrains Mono',monospace;color:var(--accent)">Rp {{ "{:,.0f}".format(pr.total_amount) }}</div>
</div>
</div>

<div class="card">
    <div class="card-title mb-4">Daftar Item</div>
    <div class="table-wrap">
        <table>
            <thead><tr><th>#</th><th>Nama Item</th><th>Spesifikasi</th><th>Kategori</th><th class="text-right">Kuantitas</th><th>Satuan</th><th class="text-right">Harga Est.</th><th class="text-right">Total Est.</th></tr></thead>
            <tbody>
            {% for item in items %}
            <tr>
                <td class="text-muted fs-13">{{ loop.index }}</td>
                <td class="fw-600">{{ item.item_name }}</td>
                <td class="fs-13 text-muted">{{ item.specification or '—' }}</td>
                <td><span class="badge badge-secondary">{{ item.cost_category or '—' }}</span></td>
                <td class="text-right font-mono">{{ item.quantity }}</td>
                <td class="fs-13">{{ item.unit }}</td>
                <td class="text-right font-mono fs-13">Rp {{ "{:,.0f}".format(item.estimated_price) }}</td>
                <td class="text-right font-mono fw-600">Rp {{ "{:,.0f}".format(item.total_price) }}</td>
            </tr>
            {% endfor %}
            <tr style="border-top:2px solid var(--border)">
                <td colspan="7" style="text-align:right;font-weight:600">Total Estimasi</td>
                <td class="text-right font-mono fw-600 text-accent">Rp {{ "{:,.0f}".format(pr.total_amount) }}</td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
{% endblock %}
