{% extends "base.html" %}

{% block title %}Kunde bearbeiten{% endblock %}

{% block content %}
<div class="container py-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>Kunde bearbeiten</h2>
        <div>
            <a href="{{ url_for('customers.customers_licenses', show_fake=request.args.get('show_fake')) }}" class="btn btn-secondary">👥 Zurück zur Übersicht</a>
        </div>
    </div>

    <div class="card mb-4">
        <div class="card-body">
            <form method="post" action="{{ url_for('customers.edit_customer', customer_id=customer.id) }}" accept-charset="UTF-8">
                {% if request.args.get('show_fake') == 'true' %}
                    <input type="hidden" name="show_fake" value="true">
                {% endif %}
                <div class="row g-3">
                    <div class="col-md-6">
                        <label for="name" class="form-label">Kundenname</label>
                        <input type="text" class="form-control" id="name" name="name" value="{{ customer.name }}" accept-charset="UTF-8" required>
                    </div>
                    <div class="col-md-6">
                        <label for="email" class="form-label">E-Mail</label>
                        <input type="email" class="form-control" id="email" name="email" value="{{ customer.email or '' }}" accept-charset="UTF-8">
                    </div>
                    <div class="col-12">
                        <label class="form-label text-muted">Erstellt am</label>
                        <p class="form-control-plaintext">{{ customer.created_at.strftime('%d.%m.%Y %H:%M') }}</p>
                    </div>
                </div>

                <div class="form-check mt-3">
                    <input class="form-check-input" type="checkbox" id="isTest" name="is_fake" {% if customer.is_fake %}checked{% endif %}>
                    <label class="form-check-label" for="isTest">
                        <i class="fas fa-flask"></i> Als Fake-Daten markieren
                        <small class="text-muted">(Kunde und seine Lizenzen werden von der Software ignoriert)</small>
                    </label>
                </div>

                <div class="mt-4">
                    <button type="submit" class="btn btn-primary">💾 Änderungen speichern</button>
                    <a href="{{ url_for('customers.customers_licenses', show_fake=request.args.get('show_fake')) }}" class="btn btn-secondary">Abbrechen</a>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-header">
            <h5 class="mb-0">Lizenzen des Kunden</h5>
        </div>
        <div class="card-body">
            {% if licenses %}
            <div class="table-responsive">
                <table class="table table-sm">
                    <thead>
                        <tr>
                            <th>Lizenzschlüssel</th>
                            <th>Typ</th>
                            <th>Gültig von</th>
                            <th>Gültig bis</th>
                            <th>Aktiv</th>
                            <th>Aktionen</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for license in licenses %}
                        <tr>
                            <td><code>{{ license[1] }}</code></td>
                            <td>
                                {% if license[2] == 'full' %}
                                    <span class="badge bg-success">Vollversion</span>
                                {% else %}
                                    <span class="badge bg-warning">Testversion</span>
                                {% endif %}
                            </td>
                            <td>{{ license[3].strftime('%d.%m.%Y') }}</td>
                            <td>{{ license[4].strftime('%d.%m.%Y') }}</td>
                            <td>
                                {% if license[5] %}
                                    <span class="text-success">✓</span>
                                {% else %}
                                    <span class="text-danger">✗</span>
                                {% endif %}
                            </td>
                            <td>
                                <a href="{{ url_for('licenses.edit_license', license_id=license[0]) }}" class="btn btn-outline-primary btn-sm">Bearbeiten</a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <p class="text-muted mb-0">Dieser Kunde hat noch keine Lizenzen.</p>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}