{% extends "base.html" %}
{% block title %}{{ contract.name if contract else 'Nouveau contrat' }} - GMAO{% endblock %}

{% block content %}
<div class="container-fluid">
    <h2 class="h4 mb-3">
        <i class="bi bi-file-earmark-text"></i>
        {{ 'Modifier' if contract else 'Nouveau' }} contrat
    </h2>

    <form method="POST">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        
        <div class="card border-0 shadow-sm mb-3">
            <div class="card-body">
                <h6 class="text-muted"><i class="bi bi-info-circle"></i> Informations generales</h6>
                <div class="row g-3">
                    <div class="col-md-6">
                        <label class="form-label">Nom du contrat *</label>
                        <input type="text" name="name" class="form-control" value="{{ contract.name if contract else '' }}" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Numero de contrat</label>
                        <input type="text" name="contract_number" class="form-control" value="{{ contract.contract_number if contract else '' }}">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Entreprise</label>
                        <select name="company_id" class="form-select">
                            <option value="">—</option>
                            {% for c in companies %}
                            <option value="{{ c.id }}" {{ 'selected' if contract and contract.company_id == c.id }}>{{ c.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Lot</label>
                        <select name="lot_id" class="form-select">
                            <option value="">—</option>
                            {% for l in lots %}
                            <option value="{{ l.id }}" {{ 'selected' if contract and contract.lot_id == l.id }}{{ 'selected' if pre_lot == l.id }}>{{ l.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Equipement</label>
                        <select name="equipment_id" class="form-select">
                            <option value="">—</option>
                            {% for e in equipments %}
                            <option value="{{ e.id }}" {{ 'selected' if contract and contract.equipment_id == e.id }}{{ 'selected' if pre_eq == e.id }}>{{ e.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <div class="card border-0 shadow-sm mb-3"><div class="card-body"><h6 class="text-muted"><i class="bi bi-tools"></i> Visites de maintenance extérieure</h6><div class="row g-3">
          <div class="col-md-4"><label class="form-label">Fréquence (jours)</label><input type="number" min="1" name="visit_interval_days" class="form-control" value="{{ contract.visit_interval_days if contract and contract.visit_interval_days else '' }}" placeholder="30, 60, 180…"></div>
          <div class="col-md-4"><label class="form-label">Dernière visite</label><input type="date" name="last_visit_date" class="form-control" value="{{ contract.last_visit_date.isoformat() if contract and contract.last_visit_date else '' }}"></div>
          <div class="col-md-4"><label class="form-label">Prochaine visite</label><input type="date" name="next_visit_date" class="form-control" value="{{ contract.next_visit_date.isoformat() if contract and contract.next_visit_date else '' }}"></div>
        </div></div></div>

        <div class="card border-0 shadow-sm mb-3">
            <div class="card-body">
                <h6 class="text-muted"><i class="bi bi-calendar3"></i> Dates et duree</h6>
                <div class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label">Date de signature</label>
                        <input type="date" name="signed_date" class="form-control" value="{{ contract.signed_date.strftime('%Y-%m-%d') if contract and contract.signed_date else '' }}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Date de debut *</label>
                        <input type="date" name="start_date" class="form-control" value="{{ contract.start_date.strftime('%Y-%m-%d') if contract and contract.start_date else '' }}" required>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Date de fin</label>
                        <input type="date" name="end_date" class="form-control" value="{{ contract.end_date.strftime('%Y-%m-%d') if contract and contract.end_date else '' }}">
                        <small class="text-muted">Vide = duree indeterminee</small>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Statut</label>
                        <select name="status" class="form-select">
                            {% for s in ['actif', 'expire', 'resilie', 'en_attente'] %}
                            <option value="{{ s }}" {{ 'selected' if contract and contract.status == s }}{{ 'selected' if not contract and s == 'actif' }}>{{ s }}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <div class="card border-0 shadow-sm mb-3">
            <div class="card-body">
                <h6 class="text-muted"><i class="bi bi-arrow-repeat"></i> Reconduction et preavis</h6>
                <div class="row g-3">
                    <div class="col-md-3">
                        <div class="form-check">
                            <input type="checkbox" name="tacit_renewal" value="1" class="form-check-input" id="tacit" {{ 'checked' if contract and contract.tacit_renewal }}{{ 'checked' if not contract }}>
                            <label class="form-check-label" for="tacit">Tacite reconduction</label>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Periode reconduction (jours)</label>
                        <input type="number" name="renewal_period_days" class="form-control" value="{{ contract.renewal_period_days if contract else 365 }}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Delai de preavis (jours)</label>
                        <input type="number" name="notice_period_days" class="form-control" value="{{ contract.notice_period_days if contract and contract.notice_period_days else '' }}">
                        <small class="text-muted">Avant la date de fin</small>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Date limite preavis</label>
                        <input type="date" name="notice_deadline" class="form-control" value="{{ contract.notice_deadline.strftime('%Y-%m-%d') if contract and contract.notice_deadline else '' }}">
                        <small class="text-muted">Vide = calcule auto</small>
                    </div>
                </div>
            </div>
        </div>

        <div class="card border-0 shadow-sm mb-3">
            <div class="card-body">
                <h6 class="text-muted"><i class="bi bi-cash-coin"></i> Montant et notes</h6>
                <div class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label">Montant annuel (EUR)</label>
                        <input type="number" step="0.01" name="annual_amount" class="form-control" value="{{ contract.annual_amount if contract and contract.annual_amount else '' }}">
                    </div>
                    <div class="col-md-9">
                        <label class="form-label">Notes</label>
                        <textarea name="notes" class="form-control" rows="2">{{ contract.notes if contract else '' }}</textarea>
                    </div>
                </div>
            </div>
        </div>

        <div class="d-flex gap-2">
            <button type="submit" class="btn btn-primary">
                <i class="bi bi-check-circle"></i> {{ 'Modifier' if contract else 'Creer' }}
            </button>
            <a href="{{ url_for('contracts.index') }}" class="btn btn-outline-secondary">Annuler</a>
        </div>
    </form>
</div>
{% endblock %}
