{% extends "base.html" %}

{% block title %}
FeedMe
{% endblock title %}

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

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

{% block content %}

<section id="feedMe">
  <div class="container">
    <div class="row">
      <div class="col-md-12">
        {% if order %}
          <div class="page-header">
            <h1>{{ order.date }}: {{ order.restaurant.restaurant_name }} (<a href="{{ order.restaurant.menu_url }}" target="_blank">Menu</a>)</h1>
          </div>
          <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 }}</span></td>
                <td>{{ order_line.soda }}</td>
                <td>{{ order_line.creator.username }}</td>
                <td>{{ order_line.extras }}</td>
                <td>
                  {% if order_line.users %}
                    {% for user in order_line.users.all %}
                      {% if user.nickname %}
                        {% if not user.nickname == orderline.creator %}
                            {{ user.nickname }}
                        {% endif %}
                      {% else %}
                        {% if not user == order_line.creator %}
                            {{ user.username }}
                        {% endif %}
                      {% endif %}
                    {% endfor %}
                  {% endif %}
                </td>
                <td>
                {% if user.is_authenticated %}
                  {% if user == order_line.creator %}
                    <a href="{% url "edit_orderline" order_line.id %}"><button class="btn btn-primary">edit</button></a>
                    <a href="{% url "delete_orderline" order_line.id %}"><button class="btn btn-danger">delete</button></a>
                  {% elif user not in order_line.users.all and order.restaurant.buddy_system and can_join %}
                    <a href={% url "join_orderline" order_line.id %}><button class="btn btn-primary">join</button></a>
                  {% elif user in order_line.users.all and order.restaurant.buddy_system %}
                    <a href={% url "leave_orderline" order_line.id %}><button class="btn btn-danger">leave</button></a>
                  {% endif %}
                {% endif %}
                </td>
              </tr>
            {% endfor %}
          </table>
          {% if order.order_set.all %}
            <table class="table table-striped table-bordered">
              <tr>
                <th>User</th>
                <th>Description</th>
                <th></th>
              </tr>
              {% for order_line in order.order_set.all %}
                <tr>
                  <td>{{ order_line.user }}</td>
                  <td>{{ order_line.content }}</td>
                  <td>
                  {% if user == order.user %}
                    <a href="{% url "edit_orderline" order.id %}"><button class="btn btn-primary">edit</button></a>
                    <a href="{% url "delete_orderline" order.id %}"><button class="btn btn-danger">delete</button></a></td>
                  {% endif %}
                  </td>
                </tr>
              {% endfor %}
            </table>
          {% endif %}
          {% if can_join and user.is_authenticated %}
              <a href="{% url "new_orderline" %}"><button class="btn btn-primary">New order line</button></a>
          {% endif %}
        {% else %}
          <div class="page-header"><h1>No orders available.</h1></div>
          {% if poll %}
            {% include 'poll.html' %}
          {% endif %}
        {% endif %}
        {% if not user.is_authenticated %}
        <h3 style="color: deeppink;">You are <span style="color: red; font-size: 100px;">not</span> logged in. Head over <a href="/admin/">here</a> to log in.</h3>
        {% endif %}
      </div>
    </div>
  </div>
</section>
{% endblock content %}
