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

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

<div class="row">
    <!-- Informations générales -->
    <div class="col-md-6">
        <div class="card mb-3">
            <div class="card-header"><i class="bi bi-info-circle"></i> Informations</div>
            <div class="card-body">
                <table class="table table-sm mb-0">
                    {% if company.trade_name %}<tr><th>Nom commercial</th><td>{{ company.trade_name }}</td></tr>{% endif %}
                    {% if company.siret %}<tr><th>SIRET</th><td>{{ company.siret }}</td></tr>{% endif %}
                    <tr><th>Spécialité</th><td><span class="badge bg-info">{{ company.specialty }}</span></td></tr>
                    <tr><th>Contact</th><td>{{ company.contact_name or '—' }}</td></tr>
                    <tr><th>Email</th><td>{{ company.contact_email or '—' }}</td></tr>
                    <tr><th>Téléphone</th><td>{{ company.contact_phone or '—' }}</td></tr>
                    {% if company.rating %}<tr><th>Note</th><td>{% for i in range(company.rating) %}⭐{% endfor %}</td></tr>{% endif %}
                    <tr><th>Statut</th><td>{% if company.is_active %}<span class="badge bg-success">Active</span>{% else %}<span class="badge bg-secondary">Inactive</span>{% endif %}</td></tr>
                </table>
            </div>
        </div>
    </div>
    
    <!-- Contacts techniciens -->
    <div class="col-md-6">
        <div class="card mb-3">
            <div class="card-header"><i class="bi bi-person-gear"></i> Contacts techniciens</div>
            <div class="card-body">
                {% if company.tech1_name or company.tech2_name or company.tech3_name %}
                <table class="table table-sm mb-0">
                    {% if company.tech1_name %}
                    <tr>
                        <th>Tech 1: {{ company.tech1_name }}</th>
                        <td>{{ company.tech1_phone or '' }} {% if company.tech1_email %}<br><small>{{ company.tech1_email }}</small>{% endif %}</td>
                    </tr>
                    {% endif %}
                    {% if company.tech2_name %}
                    <tr>
                        <th>Tech 2: {{ company.tech2_name }}</th>
                        <td>{{ company.tech2_phone or '' }} {% if company.tech2_email %}<br><small>{{ company.tech2_email }}</small>{% endif %}</td>
                    </tr>
                    {% endif %}
                    {% if company.tech3_name %}
                    <tr>
                        <th>Tech 3: {{ company.tech3_name }}</th>
                        <td>{{ company.tech3_phone or '' }} {% if company.tech3_email %}<br><small>{{ company.tech3_email }}</small>{% endif %}</td>
                    </tr>
                    {% endif %}
                </table>
                {% else %}
                <p class="text-muted mb-0">Aucun contact technicien renseigné.</p>
                {% endif %}
            </div>
        </div>
    </div>
</div>

{% if company.address %}
<div class="card mb-3">
    <div class="card-header"><i class="bi bi-geo-alt"></i> Adresse</div>
    <div class="card-body">{{ company.address }}</div>
</div>
{% endif %}

{% if company.notes %}
<div class="card mb-3">
    <div class="card-header"><i class="bi bi-file-text"></i> Notes</div>
    <div class="card-body">{{ company.notes }}</div>
</div>
{% endif %}

<!-- Lots concernés par cette entreprise -->
<div class="card mb-3">
    <div class="card-header"><i class="bi bi-boxes"></i> Lots concernés ({{ company.lots|length }})</div>
    <div class="card-body">
        {% if company.lots %}
        <div class="table-responsive">
            <table class="table table-sm table-hover mb-0">
                <thead><tr><th>Lot</th><th>Catégories</th><th>Interventions</th></tr></thead>
                <tbody>
                {% for lot in company.lots %}
                <tr>
                    <td><a href="{{ url_for('lots.detail', id=lot.id) }}">{{ lot.name }}</a></td>
                    <td><span class="badge bg-info">{{ lot.categories|length }}</span></td>
                    <td><span class="badge bg-secondary">{{ lot.interventions|length }}</span></td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <p class="text-muted">Aucun lot assigné à cette entreprise.</p>
        {% endif %}
    </div>
</div>

<!-- Interventions de cette entreprise -->
<div class="card">
    <div class="card-header"><i class="bi bi-wrench"></i> Interventions ({{ interventions|length }})</div>
    <div class="card-body">
        {% if interventions %}
        <div class="table-responsive">
            <table class="table table-sm table-hover mb-0">
                <thead><tr><th>#</th><th>Titre</th><th>Type</th><th>Statut</th><th>Date</th></tr></thead>
                <tbody>
                {% for interv in interventions %}
                <tr>
                    <td><a href="{{ url_for('interventions.detail', id=interv.id) }}">#{{ interv.id }}</a></td>
                    <td>{{ interv.title }}</td>
                    <td>{% if interv.type == 'curatif' %}<span class="badge bg-danger">Curatif</span>{% else %}<span class="badge bg-info">Préventif</span>{% endif %}</td>
                    <td><span class="badge {{ interv.status|status_badge }}">{{ interv.status|format_status }}</span></td>
                    <td>{{ interv.requested_at|date_fmt }}</td>
                </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <p class="text-muted">Aucune intervention pour cette entreprise.</p>
        {% endif %}
    </div>
</div>
{% endblock %}