{% extends "base.html" %}

{% block title %}MeinAntrag{% endblock %}

{% block content %}
<div class="text-center">
    <h1 class="title display-4">MeinAntrag</h1>
    <p class="description">
        Erstelle einfach Vorlagen für Anfragen oder Anträge an die Karlsruher Stadtverwaltung
        zu deinem persönlichen Thema und schicke diese direkt an eine Stadtratsfraktion!
    </p>
    
    <form id="meinantragForm" class="text-start">
        <div class="mb-4">
            <label for="party" class="form-label fw-bold">Fraktion</label>
            <select class="form-select" id="party" name="party_id" required>
                <option value="">Fraktion auswählen...</option>
            </select>
        </div>
        
        <div class="mb-4">
            <label for="body" class="form-label fw-bold">Mein Anliegen:</label>
            <textarea class="form-control" id="body" name="body" rows="5" 
                      placeholder="Beschreibe hier, welche Anfrage oder Antrag du an die Stadtverwaltung stellen möchtest ..." required></textarea>
        </div>
        
        <div class="text-center">
            <button type="submit" class="btn btn-primary btn-lg">
                <span class="btn-text">Antrag generieren</span>
                <span class="loading spinner-border spinner-border-sm ms-2" role="status"></span>
            </button>
        </div>
    </form>
    
    <div id="result" class="mt-4" style="display: none;">
        <h5 class="text-success mb-3">Link erfolgreich generiert!</h5>
        <div class="input-group">
            <input type="text" class="form-control" id="generatedLinkInput" readonly>
            <button class="btn btn-outline-primary" type="button" id="copyBtn">In Zwischenablage</button>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    $(document).ready(function() {
        // Initialize Select2 for public bodies
        $('#publicbody').select2({
            theme: 'bootstrap-5',
            placeholder: 'Behörde auswählen...',
            allowClear: true,
            width: '100%',
            ajax: {
                url: '/api/publicbodies',
                dataType: 'json',
                delay: 250,
                data: function(params) {
                    return {
                        search: params.term,
                        page: params.page || 1
                    };
                },
                processResults: function(data, params) {
                    params.page = params.page || 1;
                    return {
                        results: data.objects.map(function(item) {
                            return {
                                id: item.id,
                                text: item.name + ' (' + item.jurisdiction.name + ')'
                            };
                        }),
                        pagination: {
                            more: data.meta.next !== null
                        }
                    };
                },
                cache: true
            }
        });
        
        // Handle form submission (client-side)
        $('#meinantragForm').on('submit', function(e) {
            e.preventDefault();

            const publicbodyId = $('#publicbody').val() || '';
            const subject = $('#subject').val() || '';
            const body = $('#body').val() || '';

            let link = 'https://fragdenstaat.de/anfrage-stellen/';
            if (publicbodyId) {
                link += `an/${publicbodyId}/`;
            }

            const params = new URLSearchParams();
            if (subject) params.set('subject', subject);
            if (body) params.set('body', body);

            if ([...params].length > 0) {
                link += `?${params.toString()}`;
            }

            $('#generatedLinkInput').val(link);
            $('#result').show();
        });

        // Copy to clipboard
        $(document).on('click', '#copyBtn', function() {
            const link = $('#generatedLinkInput').val();
            navigator.clipboard.writeText(link).then(function() {
                const btn = $('#copyBtn');
                const originalText = btn.text();
                btn.text('Kopiert!');
                setTimeout(() => btn.text(originalText), 1500);
            });
        });
    });
</script>
{% endblock %} 