{% extends "kegadmin/base.html" %}
{% load kegweblib %}
{% load crispy_forms_tags %}

{% block pagetitle %}Kegbot Admin: Taps{% endblock %}

{% block kegadmin-main %}
{% for tapinfo in all_taps %}
{% with tapinfo.tap as tap %}
{% with tap.current_keg as keg %}

<!-- Tap {{ tap }} -->
<div class="row">
  <div class="span11">
    <h2>{{ tap.name }}</h2>
    <div class="well">
      <ul id="tab" class="nav nav-tabs">
        <li class="active"><a href="#tap-{{tap.id}}-keg" data-toggle="tab">Keg</a></li>
        <li><a href="#tap-{{tap.id}}-settings" data-toggle="tab">Settings</a></li>
        <li><a href="#tap-{{tap.id}}-last" data-toggle="tab">Last Pour</a></li>
      </ul>

      <div class="tab-content">
        <div class="tab-pane fade active in" id="tap-{{tap.id}}-keg">
            {% if keg and keg.is_active %}
            <div class="row">
            <div class="span3">
              {% comment %}
              <p>
                <!-- TODO: get rid of form -->
                <form method="GET" action="{% url kegadmin-edit-keg kbsite.url keg.seqn %}">
                <button type="submit" class="btn small fill-parent-width">
                  Edit Keg
                </button>
                </form>
              </p>
              {% endcomment %}
              <p>
                <form action="{% url kegadmin-do-end-keg kbsite.url %}" method="POST">
                  {% csrf_token %}
                  {{ tapinfo.end_form|crispy }}
                  <button type="submit" class="btn btn-danger small fill-parent-width">End Keg</button>
                </form>
              </p>
            </div>
            <div class="span7">
              <h4>{{ keg.type}}</h4>
              <small>
                {% if keg.is_empty %}
                  Empty,
                {% else %}
                  {% volume keg.remaining_volume %} remaining
                  ({{ keg.percent_full|floatformat:1 }}% full),
                {% endif %}
                tapped {% timeago keg.start_time %}
              </small>
            </div> <!-- /span11 -->
          </div> <!-- /row -->
          {% else %}
          <h4>
            Empty tap, activate a new keg.
            <small>Time for a beer run?</small>
          </h4>

          <form action="{% url kegadmin-edit-tap kbsite.url tap.seqn %}" method="POST" class="form-stacked">
              {% csrf_token %}
              {{ tapinfo.keg_form|crispy }}
              <div class="actions">
                <input type="submit" class="btn btn-success"
                    name="{{ tapinfo.prefix }}-submit"
                    value="Activate Keg"/>
              </div>
          </form>
          </p>
          {% endif %}
        </div> <!-- /keg -->

        <div class="tab-pane fade" id="tap-{{tap.id}}-settings">
          <form action="" method="POST" class="form-stacked">
              {% csrf_token %}
              {{ tapinfo.edit_form|crispy }}
              <div class="actions">
                <button type="submit" class="btn btn-primary"
                    name="{{ tapinfo.prefix }}-submit">
                  <i class="icon-ok icon-white"></i> Update Tap</button>
                </button>
                <button type="submit" class="btn btn-danger"
                    name="{{ tapinfo.prefix }}-delete">
                  <i class="icon-trash icon-white"></i> Delete Tap</button>
                </button>
              </div>
          </form>
        </div>

        <div class="tab-pane fade" id="tap-{{tap.id}}-last">
          <table class="condensed-table zebra-striped">
            <tbody>
              {% with keg.drinks.latest as drink %}
              {% if drink %}
              <tr>
                <th>
                  <div class="span3">
                    <p>
                      <button type="submit" class="btn small fill-parent-width">Adjust Drink</button>
                    </p>
                    <p>
                      <button type="submit" class="btn btn-danger small fill-parent-width">Cancel Drink</button>
                    </p>
                  </div>
                </th>
                <td>
                  <h4>Last Pour</h4>
                  <a href="{% url kb-drink kbsite.url drink.seqn %}"
                    target="_new">Drink {{ drink.seqn }}</a> by {{ drink.user }}
                </td>
              </tr>
              {% endif %}
              {% endwith %}
            </tbody>
          </table>
        </div>

      </div> <!-- tab-content -->

    </div> <!-- well -->
  </div>
</div>

{% endwith %}
{% endwith %}
{% endfor %}

<div class="row">
  <div class="span11">
    <h2>Add a New Tap</h2>
    <div class="well">
      <form action="" method="POST" class="form-stacked">
        {% csrf_token %}
        {{ create_tap_form|crispy }}
        <div class="actions">
          <button type="submit" class="btn btn-success"
              name="new-tap-submit">
                <i class="icon-ok icon-white"></i> Create Tap</button>
        </div>
      </form>
    </div>
  </div>
</div>

{% endblock %}
