{% import 'macros/form.html' as f with context %}

{% if request.endpoint.endswith('new') %}
  {% set endpoint = 'admin.coupons_new' %}
  {% set form_kwargs = {} %}
  {% set legend = 'Add a new coupon' %}
  {% set button = 'Add' %}
{% else %}
  {% set endpoint = 'admin.coupons_edit' %}
  {% set form_kwargs = {'id': coupon.id} %}
  {% set legend = 'Update this coupon' %}
  {% set button = 'Save' %}
{% endif %}

<div class="row">
  <div class="col-md-4 col-md-offset-4 well">
    {% call f.form_tag(endpoint, **form_kwargs) %}
      <legend>{{ legend }}</legend>
      <p class="small text-muted">
        You may pick either a percent <strong>or</strong> amount off.
      </p>

      <div class="row margin-bottom">
        <div class="col-md-6">
          {% call f.form_group(form.percent_off) %}
          {% endcall %}
        </div>
        <div class="col-md-6">
          {% call f.form_group(form.amount_off) %}
          {% endcall %}
        </div>
      </div>

      {% call f.form_group(form.code, css_class='sm-margin-bottom') %}
      {% endcall %}

      {% call f.form_group(form.currency, css_class='sm-margin-bottom') %}
      {% endcall %}

      {% call f.form_group(form.duration, css_class='sm-margin-bottom') %}
      {% endcall %}

      <div id="duration-in-months">
        {% call f.form_group(form.duration_in_months, css_class='sm-margin-bottom') %}
        {% endcall %}
      </div>

      {% call f.form_group(form.max_redemptions, css_class='sm-margin-bottom') %}
      {% endcall %}

      <div class="dt relative">
        {% call f.form_group(form.redeem_by) %}
        {% endcall %}
      </div>

      <hr/>
      <div class="row">
        <div class="col-md-6">
          <button type="submit" class="btn btn-primary btn-block">
            <img src="{{ url_for('static', filename='images/spinner.gif') }}"
                class="spinner"
                width="16" height="11" alt="Spinner"/>
            {{ button }}
          </button>
        </div>
        <div class="col-md-6">
          <div class="visible-xs visible-sm sm-margin-top"></div>
          <a href="{{ url_for('admin.coupons') }}"
              class="btn btn-default btn-block">
            Cancel
          </a>
        </div>
      </div>
    {% endcall %}
  </div>
</div>
