{% extends "base.html" %}
{% block title %}Manajemen Dokumen{% endblock %}
{% block breadcrumb %}Dokumen{% endblock %}
{% block page_title %}Manajemen Dokumen{% endblock %}
{% block topbar_actions %}<a href="{{ url_for('documents.upload') }}" class="btn btn-primary">+ Upload Dokumen</a>{% endblock %}
{% block content %}
<div class="flex gap-2 mb-4" style="flex-wrap:wrap">
    <a href="{{ url_for('documents.index') }}" class="btn {% if not current_type %}btn-primary{% else %}btn-secondary{% endif %} btn-sm">Semua</a>
    {% for dt in doc_types %}
    <a href="?doc_type={{ dt }}" class="btn {% if current_type == dt %}btn-primary{% else %}btn-secondary{% endif %} btn-sm">{{ dt }}</a>
    {% endfor %}
</div>
<div class="card">
    <div class="table-wrap">
        <table>
            <thead><tr><th>No. Dokumen</th><th>Nama Dokumen</th><th>Tipe</th><th>Proyek</th><th>Ukuran</th><th>Diunggah</th><th>Aksi</th></tr></thead>
            <tbody>
            {% for doc in documents %}
            <tr>
                <td class="font-mono fs-13">{{ doc.doc_number or '—' }}</td>
                <td>
                    <div class="fw-600">{{ doc.doc_name }}</div>
                    {% if doc.description %}<div class="fs-13 text-muted">{{ doc.description[:60] }}</div>{% endif %}
                    {% if doc.tags %}<div style="margin-top:4px">{% for tag in doc.tags.split(',') %}<span class="badge badge-secondary" style="margin-right:3px;font-size:10px">{{ tag.strip() }}</span>{% endfor %}</div>{% endif %}
                </td>
                <td><span class="badge badge-info">{{ doc.doc_type }}</span></td>
                <td class="fs-13">{% if doc.project %}{{ doc.project.project_code }}{% else %}—{% endif %}</td>
                <td class="fs-13 text-muted">
                    {% if doc.file_size %}
                    {{ "%.1f"|format(doc.file_size/1024) }} KB
                    {% endif %}
                    <br><span class="badge badge-secondary" style="font-size:10px">{{ doc.file_type|upper }}</span>
                </td>
                <td class="fs-13 text-muted">{{ doc.created_at.strftime('%d %b %Y') }}</td>
                <td style="display:flex;gap:6px">
                    <a href="{{ url_for('documents.download', id=doc.id) }}" class="btn btn-secondary btn-sm">⬇ Unduh</a>
                    <form method="POST" action="{{ url_for('documents.delete', id=doc.id) }}" onsubmit="return confirm('Hapus dokumen ini?')">
                        <button class="btn btn-danger btn-sm">✗</button>
                    </form>
                </td>
            </tr>
            {% else %}
            <tr><td colspan="7" style="text-align:center;padding:40px;color:var(--text-muted)">Belum ada dokumen. <a href="{{ url_for('documents.upload') }}" style="color:var(--accent)">Upload dokumen</a>.</td></tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
</div>
{% endblock %}
