{% extends "base.html" %}

{% block title %}
Feedme
{% endblock title %}

{% block styles %}
{{ block.super }}
{% endblock styles %}

{% block submenu %}
{% include "feedme/nav.html" %}
{% endblock submenu %}

{% block content %}

<section id="feedme">
  <div class="container">
    <div class="page-header clearfix">
      <div class="row">
        <div class="col-md-12">
        {% if order %}
          <h2>
              {{ order }}
              (<a href="{{ order.restaurant.menu_url }}" target="_blank">Menu</a>) 
              {% if user.is_staff %}
                <span class="glyphicon glyphicon-phone-alt"></span> {{ order.restaurant.phone_number }}
              {% endif %}
          </h2>
        </div>
      </div>
    </div>
    <div id="feedme-order">
      <div class="row">
        <div class="col-md-12">
          <div role="tabpanel">
            <ul class="nav nav-tabs" role="tablist">
              <li role="presentation" class="active">
                <a href="#order" aria-controls="order" role="tab" data-toggle="tab">Overview</a>
              </li>
              <li role="presentation" {% if not user.is_staff %}class="disabled"{% endif %}>
                <a href="#receipt" aria-controls="receipt" role="tab" data-toggle="tab">Receipt</a>
              </li>
            </ul>
            <div class="tab-content">
              <div role="tabpanel" class="tab-pane active" id="order">
                  <table class="table table-striped table-bordered">
                    <tr>
                      <th>Menu item</th>
                      <th>Soda</th>
                      <th>Creator</th>
                      <th>Extras/comment</th>
                      <th>Buddies</th>
                      <th>Administration</th>
                    </tr>
                    {% for order_line in order.orderline_set.all %}
                      <tr>
                    <td>{{ order_line.menu_item }} <span class="pull-right">{{ order_line.price }} kr</span></td>
                    <td>{{ order_line.soda }}</td>
                    <td>{{ order_line.creator }}</td>
                    <td>{{ order_line.extras }}</td>
                    <td>
                      {% if order_line.users %}
                        {% for user in order_line.users.all %}
                          {% if not user == order_line.creator %}
                            {{ user }}
                          {% endif %}
                        {% endfor %}
                      {% endif %}
                    </td>
                    <td>
                      {% if user.is_authenticated %}
                      {% if user == order_line.creator or user in order_line.users.all %}
                        <a href="{% url "feedme:edit_orderline" group order_line.id %}"><button class="btn btn-primary">edit</button></a>
                      {% endif %}
                      {% if user not in order_line.users.all and order.restaurant.buddy_system and can_join %}
                        <a href={% url "feedme:join_orderline" group order_line.id %}><button class="btn btn-primary">join</button></a>
                        {% elif user in order_line.users.all and user != order_line.creator and order.restaurant.buddy_system %}
                        <a href={% url "feedme:leave_orderline" group order_line.id %}><button class="btn btn-danger">leave</button></a>
                      {% endif %}
                        {% if user == order_line.creator %}
                        <a href="{% url "feedme:delete_orderline" group order_line.id %}"><button class="btn btn-danger">delete</button></a>
                        {% endif %}
                    {% endif %}
                    </td>
                  </tr>
                {% endfor %}
              </table>
              {% if can_join and user.is_authenticated %}
                  <a href="{% url "feedme:create_orderline" group %}"><button class="btn btn-primary">New order line</button></a>
              {% endif %}
            </div>
            <div role="tabpanel" class="tab-pane" id="receipt">
                <h3>Receipt</h3>
                <h4>Menu items</h4>
                <ul>
                    {% for orderline in order.orderline_set.all %}
                        {% if orderline.menu_item %}
                        <li>{{ orderline.menu_item }}</li>
                        {% endif %}
                    {% empty %}
                        No items from this category ordered.
                    {% endfor %}
                </ul>    
                <h4>Soda</h4>
                    <ul>
                    {% for orderline in order.orderline_set.all %}
                        {% if orderline.soda %}
                        <li>{{ orderline.soda }}</li>
                        {% endif %}
                    {% empty %}
                        No items from this category ordered.
                    {% endfor %}
                </ul>
                <h4>Comments</h4>
                    <ul>
                    {% for orderline in order.orderline_set.all %}
                        {% if orderline.extras %}
                        <li>{{ orderline.extras }}</li>
                        {% endif %}
                    {% empty %}
                        No items from this category ordered.
                    {% endfor %}
                </ul>
            </div>
          </div>
        {% else %}
          {% if poll %}
            {% include 'feedme/poll.html' %}
          {% else %}
            <div class="col-md-12 text-center">
              <img class="text-center" src="{{ STATIC_URL }}img/feedme_nyan.gif" alt="No content available">
            </div>
          {% endif %}
        {% endif %}
      </div>
    </div>
  </div>
</section>
{% endblock content %}
