{% extends "base.html" %}
{% block content %}
<h1>Edit App: {{ app.name }}</h1>
<form method="POST" enctype="multipart/form-data">
    {{ form.hidden_tag() }}
    <div class="mb-3">
        {{ form.name.label(class="form-label") }}
        {{ form.name(class="form-control") }}
        {% for error in form.name.errors %}
            <span class="text-danger">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="mb-3">
        {{ form.description.label(class="form-label") }}
        {{ form.description(class="form-control", rows=4) }}
        {% for error in form.description.errors %}
            <span class="text-danger">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="mb-3">
        {{ form.developer.label(class="form-label") }}
        {{ form.developer(class="form-control") }}
        {% for error in form.developer.errors %}
            <span class="text-danger">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="mb-3">
        {{ form.contact_email.label(class="form-label") }}
        {{ form.contact_email(class="form-control") }}
        {% for error in form.contact_email.errors %}
            <span class="text-danger">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="mb-3">
        {{ form.logo_url.label(class="form-label") }}
        {{ form.logo_url(class="form-control") }}
        <small class="form-text text-muted">Enter a URL or upload a new logo below. Leave blank to keep current logo.</small>
        {% for error in form.logo_url.errors %}
            <span class="text-danger">{{ error }}</span>
        {% endfor %}
        {% if app.logo_url %}
            <p class="mt-2"><strong>Current Logo:</strong></p>
            <img src="{{ app.logo_url }}" alt="Current Logo" style="max-height: 100px;">
        {% endif %}
    </div>
    <div class="mb-3">
        {{ form.logo_upload.label(class="form-label") }}
        {{ form.logo_upload(class="form-control") }}
        {% for error in form.logo_upload.errors %}
            <span class="text-danger">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="mb-3">
        {{ form.launch_url.label(class="form-label") }}
        {{ form.launch_url(class="form-control") }}
        {% for error in form.launch_url.errors %}
            <span class="text-danger">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="mb-3">
        {{ form.website.label(class="form-label") }}
        {{ form.website(class="form-control") }}
        {% for error in form.website.errors %}
            <span class="text-danger">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="mb-3">
        {{ form.designed_for.label(class="form-label") }}
        {{ form.designed_for(class="form-control") }}
        {% for error in form.designed_for.errors %}
            <span class="text-danger">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="mb-3">
        {{ form.fhir_compatibility.label(class="form-label") }}
        {{ form.fhir_compatibility(class="form-control") }}
        {% for error in form.fhir_compatibility.errors %}
            <span class="text-danger">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="mb-3">
        {{ form.categories.label(class="form-label") }}
        {{ form.categories(class="form-control") }}
        {% for error in form.categories.errors %}
            <span class="text-danger">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="mb-3">
        {{ form.licensing_pricing.label(class="form-label") }}
        {{ form.licensing_pricing(class="form-control") }}
        {% for error in form.licensing_pricing.errors %}
            <span class="text-danger">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="mb-3">
        {{ form.os_support.label(class="form-label") }}
        {{ form.os_support(class="form-control") }}
        {% for error in form.os_support.errors %}
            <span class="text-danger">{{ error }}</span>
        {% endfor %}
    </div>
    <div class="mb-3">
        {{ form.app_image_urls.label(class="form-label") }}
        {{ form.app_image_urls(class="form-control", rows=3) }}
        {% for error in form.app_image_urls.errors %}
            <span class="text-danger">{{ error }}</span>
        {% endfor %}
        {% if app.app_images %}
            <p class="mt-2"><strong>Current Images:</strong></p>
            {% for image in app.app_images.split(',') %}
                <img src="{{ image }}" alt="App Image" style="max-height: 100px; margin: 5px;">
            {% endfor %}
        {% endif %}
    </div>
    <div class="mb-3">
        {{ form.app_image_uploads.label(class="form-label") }}
        {{ form.app_image_uploads(class="form-control", multiple=True) }}
        <small class="form-text text-muted">Select multiple images to upload.</small>
        {% for error in form.app_image_uploads.errors %}
            <span class="text-danger">{{ error }}</span>
        {% endfor %}
    </div>
    {{ form.submit(class="btn btn-primary") }}
    <a href="{{ url_for('gallery.app_detail', app_id=app.id) }}" class="btn btn-secondary">Cancel</a>
</form>
{% endblock %}