{% extends "base.html" %}
{% block title %}Purchase Request{% endblock %}
{% block breadcrumb %}Pengadaan / Purchase Request{% endblock %}
{% block page_title %}Purchase Request{% endblock %}
{% block topbar_actions %}<a href="{{ url_for('procurement.pr_create') }}" class="btn btn-primary">+ Buat PR</a>{% endblock %}
{% block content %}
<div class="flex gap-2 mb-4">
    {% for s, label in [('all','Semua'),('draft','Draft'),('submitted','Diajukan'),('approved','Disetujui'),('rejected','Ditolak'),('ordered','Dipesan')] %}
    <a href="?status={{ s }}" class="btn {% if current_status == s %}btn-primary{% else %}btn-secondary{% endif %} btn-sm">{{ label }}</a>
    {% endfor %}
</div>
<div class="card">
    <div class="table-wrap">
        <table>
            <thead><tr><th>No. PR</th><th>Proyek</th><th>Tgl Request</th><th>Prioritas</th><th class="text-right">Total Est.</th><th>Status</th><th>Aksi</th></tr></thead>
            <tbody>
            {% for pr in prs %}
            <tr>
                <td><a href="{{ url_for('procurement.pr_detail', id=pr.id) }}" class="text-accent font-mono fw-600">{{ pr.pr_number }}</a></td>
                <td class="fs-13">{{ pr.project.project_code }}<br><span class="text-muted">{{ pr.project.project_name[:40] }}</span></td>
                <td class="fs-13">{{ pr.request_date.strftime('%d %b %Y') }}</td>
                <td>
                    {% if pr.priority == 'urgent' %}<span class="badge badge-danger">Urgent</span>
                    {% elif pr.priority == 'high' %}<span class="badge badge-warning">Tinggi</span>
                    {% elif pr.priority == 'low' %}<span class="badge badge-secondary">Rendah</span>
                    {% else %}<span class="badge badge-info">Normal</span>{% endif %}
                </td>
                <td class="font-mono text-right">Rp {{ "{:,.0f}".format(pr.total_amount) }}</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-purple">Dipesan</span>{% endif %}
                </td>
                <td>
                    <a href="{{ url_for('procurement.pr_detail', id=pr.id) }}" class="btn btn-secondary btn-sm">Detail</a>
                    {% if pr.status == 'approved' %}
                    <a href="{{ url_for('procurement.po_create', pr_id=pr.id) }}" class="btn btn-primary btn-sm">Buat PO</a>
                    {% endif %}
                </td>
            </tr>
            {% else %}
            <tr><td colspan="7" style="text-align:center;padding:40px;color:var(--text-muted)">Belum ada Purchase Request</td></tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
</div>
{% endblock %}
