{# templates/widgets/input/button.html.j2 #}
{% macro render_button(button) %}
{% if button.condition %}

{% if button.confirm %}
  <button type="button" class="{{ button.css_class or 'btn btn-primary' }}"
          data-bs-toggle="modal" data-bs-target="#{{ button.confirm_id }}">
    {% if button.icon %}<i class="{{ button.icon }} me-1"></i>{% endif %}{{ button.label if button.label else "Action" }}
  </button>

  <div class="modal modal-blur fade" id="{{ button.confirm_id }}"
       tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-sm modal-dialog-centered" role="document">
      <div class="modal-content">
        <div class="modal-body">
          <div class="modal-title">Are you sure?</div>
          <div class="text-muted mt-1">{{ button.confirm }}</div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-link link-secondary me-auto"
                  data-bs-dismiss="modal">Cancel</button>
          {% if button.method == 'post' %}
            <form method="post" action="{{ button.url }}" style="display:inline">
              {% for key, val in button.params.items() %}
                <input type="hidden" name="{{ key }}" value="{{ val }}">
              {% endfor %}
              <button type="submit" class="{{ button.css_class or 'btn btn-primary' }}">
                {% if button.icon %}<i class="{{ button.icon }} me-1"></i>{% endif %}{{ button.label if button.label else "Confirm" }}
              </button>
            </form>
          {% else %}
            <a href="{{ button.url }}" class="{{ button.css_class or 'btn btn-primary' }}">
              {% if button.icon %}<i class="{{ button.icon }} me-1"></i>{% endif %}{{ button.label if button.label else "Confirm" }}
            </a>
          {% endif %}
        </div>
      </div>
    </div>
  </div>

{% elif button.method == 'post' %}
  <form method="{{ button.method }}" action="{{ button.url }}" style="display:inline;">
    {% if button.params %}
      {% for key, val in button.params.items() %}
        <input type="hidden" name="{{ key }}" value="{{ val }}">
      {% endfor %}
    {% endif %}
    <button type="submit" class="{{ button.css_class or 'btn btn-primary' }}"
            {% if button.onclick %} onclick="{{ button.onclick }}" {% endif %}>
      {% if button.icon %}<i class="{{ button.icon }} me-1"></i>{% endif %}{{ button.label if button.label else "Action" }}
    </button>
  </form>

{% else %}
  <a href="{{ button.url }}"
     class="{{ button.css_class or 'btn btn-primary' }}"
     {% if button.onclick %} onclick="{{ button.onclick }}" {% endif %}>
    {% if button.icon %}<i class="{{ button.icon }} me-1"></i>{% endif %}{{ button.label if button.label else "Action" }}
  </a>
{% endif %}

{% endif %}
{% endmacro %}
