{% extends "base.html" %}
{% block title %}Contrats - GMAO{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h2 class="h4"><i class="bi bi-file-earmark-text"></i> Contrats d'entreprise</h2>
        <a href="{{ url_for('contracts.create') }}" class="btn btn-primary btn-sm">
            <i class="bi bi-plus-circle"></i> Nouveau contrat
        </a>
    </div>

    <!-- Stats -->
    <div class="row g-2 mb-3">
        <div class="col-6 col-md-3">
            <div class="card text-center border-success">
                <div class="card-body py-2">
                    <div class="display-6 text-success">{{ active_count }}</div>
                    <small class="text-muted">Actifs</small>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card text-center border-warning">
                <div class="card-body py-2">
                    <div class="display-6 text-warning">{{ expiring_count }}</div>
                    <small class="text-muted">Expirent bientot</small>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card text-center border-warning">
                <div class="card-body py-2">
                    <div class="display-6 text-warning">{{ notice_count }}</div>
                    <small class="text-muted">Preavis a envoyer</small>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card text-center border-danger">
                <div class="card-body py-2">
                    <div class="display-6 text-danger">{{ expired_count }}</div>
                    <small class="text-muted">Expires</small>
                </div>
            </div>
        </div>
    </div>

    <!-- Liste -->
    {% if contracts %}
    <div class="card border-0 shadow-sm">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        <th>Nom</th>
                        <th>Entreprise</th>
                        <th>Lot / Equipement</th>
                        <th>Signe le</th>
                        <th>Fin</th>
                        <th>Preavis</th>
                        <th>Tacite</th>
                        <th>Statut</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    {% for c in contracts %}
                    <tr>
                        <td>
                            <a href="{{ url_for('contracts.detail', id=c.id) }}">
                                <strong>{{ c.name }}</strong>
                                {% if c.contract_number %}<br><small class="text-muted">{{ c.contract_number }}</small>{% endif %}
                            </a>
                        </td>
                        <td>
                            {% if c.company %}
                            <span class="badge bg-success">{{ c.company.name }}</span>
                            {% else %}—{% endif %}
                        </td>
                        <td>
                            {% if c.lot %}<span class="badge bg-info">Lot: {{ c.lot.name }}</span><br>{% endif %}
                            {% if c.equipment %}<span class="badge bg-secondary">{{ c.equipment.name }}</span>{% endif %}
                            {% if not c.lot and not c.equipment %}—{% endif %}
                        </td>
                        <td>{{ c.signed_date.strftime('%d/%m/%Y') if c.signed_date else '—' }}</td>
                        <td>
                            {% if c.end_date %}
                            {{ c.end_date.strftime('%d/%m/%Y') }}
                            {% if c.days_until_expiry is not none %}
                                {% if c.days_until_expiry < 0 %}
                                <span class="badge bg-danger">expire {{ c.days_until_expiry }}j</span>
                                {% elif c.days_until_expiry <= 90 %}
                                <span class="badge bg-warning text-dark">{{ c.days_until_expiry }}j</span>
                                {% else %}
                                <small class="text-muted">({{ c.days_until_expiry }}j)</small>
                                {% endif %}
                            {% endif %}
                            {% else %}
                            <span class="text-muted">indetermine</span>
                            {% endif %}
                        </td>
                        <td>
                            {% if c.notice_period_days %}
                            {{ c.notice_period_days }}j
                            {% if c.notice_deadline_calculated %}
                                <br><small class="text-muted">limite: {{ c.notice_deadline_calculated.strftime('%d/%m/%Y') }}</small>
                                {% if c.must_send_notice_now %}
                                <span class="badge bg-warning text-dark">URGENT</span>
                                {% endif %}
                            {% endif %}
                            {% else %}—{% endif %}
                        </td>
                        <td>
                            {% if c.tacit_renewal %}
                            <span class="badge bg-primary">Oui ({{ c.renewal_period_days }}j)</span>
                            {% else %}
                            <span class="badge bg-secondary">Non</span>
                            {% endif %}
                        </td>
                        <td><span class="badge bg-{{ c.status_color }}">{{ c.status }}</span></td>
                        <td>
                            <a href="{{ url_for('contracts.edit', id=c.id) }}" class="btn btn-sm btn-outline-primary">
                                <i class="bi bi-pencil"></i>
                            </a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    {% else %}
    <div class="text-center py-5 text-muted">
        <i class="bi bi-file-earmark-text" style="font-size:3rem;"></i>
        <p class="mt-2">Aucun contrat enregistre</p>
        <a href="{{ url_for('contracts.create') }}" class="btn btn-primary">
            <i class="bi bi-plus-circle"></i> Creer le premier contrat
        </a>
    </div>
    {% endif %}
</div>
{% endblock %}