{% extends "base.html" %}
{% block content %}
<div class="level">
    <div class="level-left"><h1 class="title is-3">Neues Aufmaß-Projekt</h1></div>
    <div class="level-right"><a class="button is-light" href="{{ url_for('aufmass.index') }}">← Zurück</a></div>
</div>

<div class="columns is-centered">
    <div class="column is-half">
        <form method="POST" class="box">
            <div class="field">
                <label class="label">Projektname / Bezeichnung *</label>
                <div class="control"><input class="input" name="bezeichnung" required></div>
            </div>
            <div class="columns">
                <div class="column is-6">
                    <div class="field">
                        <label class="label">Vertrag</label>
                        <div class="select is-fullwidth">
                            <select name="contract_id" id="contract-select" onchange="loadLVNames()">
                                <option value="">– Kein Vertrag –</option>
                                {% for c in contracts %}
                                <option value="{{ c.id }}">{{ c.name }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                </div>
                <div class="column is-6">
                    <div class="field">
                        <label class="label">LV-Name</label>
                        <div class="select is-fullwidth">
                            <select name="lv_name" id="lv-select">
                                <option value="">– LV wählen –</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="field mt-4">
                <button class="button is-primary" type="submit">Projekt anlegen</button>
                <a class="button is-light" href="{{ url_for('aufmass.index') }}">Abbrechen</a>
            </div>
        </form>
    </div>
</div>

{% endblock %}

{% block scripts %}
<script>
function loadLVNames() {
    const cid = document.getElementById('contract-select').value;
    const url = cid ? '/contracts/api/lv-names?contract_id=' + cid : '/contracts/api/lv-names';
    fetch(url)
        .then(function(r) { return r.json(); })
        .then(function(names) {
            const sel = document.getElementById('lv-select');
            sel.innerHTML = '<option value="">– LV wählen –</option>';
            names.forEach(function(n) {
                sel.innerHTML += '<option value="' + n.replace(/&/g,'&amp;').replace(/"/g,'&quot;') + '">' + n + '</option>';
            });
        });
}
</script>
{% endblock %}
