{% extends "base.html" %}
{% block title %}{{ 'Modifier' if part is defined and part else 'Nouvelle' }} pièce — GMAO{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1><i class="bi bi-plus-circle"></i> {{ 'Modifier' if part is defined and part else 'Nouvelle' }} pièce</h1>
        <a href="{{ url_for('parts.index') }}" class="btn btn-secondary">
            <i class="bi bi-arrow-left"></i> Retour
        </a>
    </div>

    <div class="card">
        <div class="card-body">
            <form method="POST">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Nom</label>
                        <input type="text" name="name" class="form-control" value="{{ part.name if part is defined and part else '' }}" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Référence</label>
                        <input type="text" name="reference" class="form-control" value="{{ part.reference if part is defined and part and part.reference else '' }}">
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Quantité</label>
                        <input type="number" name="quantity" class="form-control" value="{{ part.quantity if part is defined and part else 0 }}" min="0">
                        {% if part is defined and part %}<input name="stock_reason" class="form-control mt-2" placeholder="Motif si la quantité change">{% endif %}
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Unité</label>
                        <select name="unit" class="form-select">
                            {% for value, label in [('unité','Unité'),('mètre','Mètre'),('litre','Litre'),('kg','Kilogramme')] %}<option value="{{ value }}" {% if part is defined and part and part.unit == value %}selected{% endif %}>{{ label }}</option>{% endfor %}
                        </select>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Prix unitaire</label>
                        <input type="number" name="unit_price" class="form-control" step="0.01" min="0" value="{{ part.unit_price if part is defined and part and part.unit_price is not none else '' }}">
                    </div>
                </div>
                
                <div class="mb-3">
                    <label class="form-label">Fournisseur</label>
                    <input type="text" name="supplier" class="form-control" value="{{ part.supplier if part is defined and part and part.supplier else '' }}">
                </div>
                
                <button type="submit" class="btn btn-primary">
                    <i class="bi bi-check"></i> Enregistrer
                </button>
            </form>
        </div>
    </div>
</div>
{% endblock %}
