{% extends base_template_path %}
{% load powercrud %}
{% load partials %}

{% if use_crispy %}
    {% include framework_template_path|add:"/crispy_partials.html#load_tags" %}
{% endif %}

{% block content %}
    {% partial pcrud_content %}
{% endblock %}

{% partialdef pcrud_content %}
<div class="is-flex is-flex-direction-column m-2 p-2">
    {% if conflict_detected %}
        {% partial conflict_detected %}
    {% else %}
        {% partial normal_content %}
    {% endif %}
</div>
{% endpartialdef pcrud_content %}

{% partialdef conflict_detected %}
<div class="card bg-base-100 shadow-xl">
    <div class="card-body text-center">
        <div class="bg-warning text-warning-content px-4 py-2 rounded-lg mb-4">
            <h3 class="text-lg font-semibold">Edit Conflict</h3>
        </div>
        <p class="text-lg">{{ conflict_message }}</p>
        <h2 class="text-lg opacity-60 mt-4">{{ object }}</h2>

        {% comment %}Only show button when NOT using modals{% endcomment %}
        {% if not use_modal %}
        <div class="card-actions justify-end mt-4">
            {% if use_htmx %}
                <button type="button" class="btn btn-outline"
                    hx-get="{{ list_view_url }}{% if request.GET.page_size %}?page_size={{ request.GET.page_size }}{% endif %}{% if filter_params %}&{{ filter_params }}{% endif %}"
                    hx-target="{{ original_target }}" hx-push-url="true">
                    Return to List</button>
            {% else %}
                <a href="{{ list_view_url }}" class="btn btn-outline">
                    Return to List
                </a>            
            {% endif %}
        </div>
        {% endif %}
    </div>
</div>
{% endpartialdef conflict_detected %}

{% partialdef normal_content %}
<h1 class="flex-1 font-bold text-xl bg-primary text-primary-content rounded p-2 mb-4">
    {% if object %}Edit {{object_verbose_name}}{% else %}Create {{object_verbose_name}}{% endif %}
</h1>

<div class="box">
    <form method="POST" 
        {% if form.is_multipart %}enctype="multipart/form-data" {% endif %}
        action="{% if object %}{{ update_view_url }}{% else %}{{ create_view_url }}{% endif %}"
        data-powercrud-form="object"
        {% if use_htmx and original_target %}
            hx-post="{% if object %}{{ update_view_url }}{% else %}{{ create_view_url }}{% endif %}"
            hx-target="{{ original_target }}"
            {% if not use_modal %}hx-push-url="true"{% else %}hx-push-url="false"{% endif %}
            hx-headers='{"X-Redisplay-Object-List": "true"}'
        {% endif %}>
        {% csrf_token %}

        {# Add hidden fields for all current GET params except csrf, page, and form fields #}
        {% for key, values in request.GET.lists %}
            {% if key != 'csrfmiddlewaretoken' and key != 'page' %}
                {% for value in values %}
                    <input type="hidden" name="_powercrud_filter_{{ key }}" value="{{ value }}">
                {% endfor %}
            {% endif %}
        {% endfor %}
        
        {% if use_crispy %}
            {% include framework_template_path|add:"/crispy_partials.html#crispy_form" %}
        {% else %}
            {{ form }}
        {% endif %}

        <button type="submit" class="btn btn-primary mt-4" data-form-save>Save</button>
    </form>
</div>
{% endpartialdef normal_content %}

{% block extra_js %}
    {{ block.super }}
{% endblock %}
