{% extends "base.html" %}
{% block title %}{{ part.name }} — GMAO Collège{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h1><i class="bi bi-box-seam"></i> {{ part.name }}</h1>
    <a href="{{ url_for('parts.edit', id=part.id) }}" class="btn btn-outline-primary"><i class="bi bi-pencil"></i> Modifier</a>
</div>

{% if part.is_low_stock %}
<div class="alert alert-danger">
    <i class="bi bi-exclamation-triangle"></i> <strong>Stock bas !</strong>
    Il reste {{ part.quantity }} {{ part.unit }} (minimum : {{ part.min_quantity }}).
</div>
{% endif %}

<div class="card mb-3">
    <div class="card-body">
        <div class="row">
            <div class="col-md-6">
                <table class="table table-sm mb-0">
                    <tr><th>Référence</th><td>{{ part.reference or '—' }}</td></tr>
                    <tr><th>Catégorie</th><td><span class="badge bg-secondary">{{ part.category }}</span></td></tr>
                    <tr><th>Unité</th><td>{{ part.unit }}</td></tr>
                    <tr><th>Fournisseur</th><td>{{ part.supplier or '—' }}</td></tr>
                </table>
            </div>
            <div class="col-md-6">
                <table class="table table-sm mb-0">
                    <tr><th>Quantité en stock</th><td class="{% if part.is_low_stock %}text-danger fw-bold{% endif %}">{{ part.quantity }} {{ part.unit }}</td></tr>
                    <tr><th>Quantité minimale</th><td>{{ part.min_quantity }} {{ part.unit }}</td></tr>
                    <tr><th>Prix unitaire</th><td>{{ "%.2f"|format(part.unit_price) if part.unit_price else '—' }} €</td></tr>
                    <tr><th>Emplacement</th><td>{{ part.location or '—' }}</td></tr>
                </table>
            </div>
        </div>
    </div>
</div>

{% if part.description %}
<div class="card">
    <div class="card-header"><i class="bi bi-file-text"></i> Description</div>
    <div class="card-body">{{ part.description }}</div>
</div>
{% endif %}
<div class="card mt-3">
    <div class="card-header"><i class="bi bi-arrow-left-right"></i> Mouvements de stock</div>
    <div class="card-body"><form method="post" action="{{ url_for('parts.movement', id=part.id) }}" class="row g-2">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        <div class="col-md-3"><select name="movement_type" class="form-select" required><option value="entree">Entrée</option><option value="sortie">Sortie</option><option value="inventaire">Valeur d’inventaire</option></select></div>
        <div class="col-md-2"><input type="number" min="0" name="quantity" class="form-control" placeholder="Quantité" required></div>
        <div class="col-md-5"><input name="reason" class="form-control" placeholder="Motif / bon / intervention" required></div>
        <div class="col-md-2"><button class="btn btn-primary w-100">Enregistrer</button></div>
    </form></div>
    <div class="table-responsive"><table class="table table-sm mb-0"><thead><tr><th>Date</th><th>Type</th><th>Variation</th><th>Stock</th><th>Motif</th><th>Par</th></tr></thead><tbody>
    {% for movement in movements %}<tr><td>{{ movement.created_at|datetime_fmt }}</td><td>{{ movement.movement_type }}</td><td class="{{ 'text-success' if movement.quantity_delta > 0 else 'text-danger' }}">{{ '%+d'|format(movement.quantity_delta) }}</td><td>{{ movement.quantity_before }} → {{ movement.quantity_after }}</td><td>{{ movement.reason }}</td><td>{{ movement.user.full_name if movement.user else 'Système' }}</td></tr>
    {% else %}<tr><td colspan="6" class="text-center text-muted">Aucun mouvement enregistré</td></tr>{% endfor %}
    </tbody></table></div>
</div>
{% endblock %}
