{% extends 'xsd_trips/base.html' %}
{% load permissions %}
{% load markdown %}

{% block trips_content %}
    <div class="row">
        <div class="col">
            <h1>
                {{ trip.uid }} |
                {{ trip.name }}
            </h1>
        </div>
        <div class="col-xs-2">
            {% permission request.user can_view_history trip %}
                <a href="{% url 'xsd_trips:TripHistory' trip.pk %}" class="btn btn-secondary"><i
                        class="fa fa-history"></i> History</a>
            {% endpermission %}
        </div>
    </div>

    <div class="row">
        <div class="col-md-12">

            {% if is_trips %}
                <div class="alert alert-info">
                    <i class="fa fa-info-circle"></i> {{ trip.officer_help_text }}
                </div>
            {% elif trip.owner == profile %}
                <div class="alert alert-info">
                    <i class="fa fa-info-circle"></i> {{ trip.owner_help_text }}
                </div>
            {% endif %}

            <div >
                {% permission request.user can_edit trip %}
                    <a href="{% url 'xsd_trips:TripUpdate' trip.pk %}" class="btn btn-outline-primary"><i
                            class="fa fa-pencil"></i> Edit</a>
                {% endpermission %}

                {% permission request.user can_approve trip %}
                    <form method="POST" action="action/approve/">
                        {% csrf_token %}
                        <button type="submit" class="btn btn-success"><i class="fa fa-check"></i> Approve</button>
                    </form>
                {% endpermission %}

                {% permission request.user can_deny trip %}
                    <form method="POST" action="action/deny/">
                        {% csrf_token %}
                        <button type="submit" class="btn btn-danger"><i class="fa fa-times"></i> Deny</button>
                    </form>
                {% endpermission %}

                {% permission request.user can_open trip %}
                    <form method="POST" action="action/open/">
                        {% csrf_token %}
                        <button type="submit" class="btn btn-outline-primary"><i class="fa fa-play"></i> Open</button>
                    </form>
                {% endpermission %}

                {% permission request.user can_close trip %}
                    <form method="POST" action="action/close/">
                        {% csrf_token %}
                        <button type="submit" class="btn btn-outline-primary"><i class="fa fa-stop"></i> Close</button>
                    </form>
                {% endpermission %}

                {% permission request.user can_cancel trip %}
                    <form method="POST" action="action/cancel/">
                        {% csrf_token %}
                        <button type="submit" class="btn btn-outline-danger"><i class="fa fa-ban"></i> Cancel</button>
                    </form>
                {% endpermission %}

                {% permission request.user can_delete trip %}
                    {% csrf_token %}
                    <button class="btn btn-outline-danger" data-toggle="modal" data-target="#deleteModal">
                        <i class="fa fa-trash"></i> Delete
                    </button>
                {% endpermission %}
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <div class="card">
                <dl class="card-block">
                    <dt>Organiser</dt>
                    <dd>
                        <a href="{{ trip.owner.get_absolute_url }}" class="no-underline">
                            <img src="{{ trip.owner.avatar_xs }}" class="avatar avatar-xs" alt="Avatar"/>
                            <span class="underline">{{ trip.owner }}</span>
                        </a>
                    </dd>
                    <dt>State</dt>
                    <dd class="{{ trip.state_class }}-detail">
                        <i class="{{ trip.state_class }}-icon"></i>
                        {{ trip.get_state_display }}
                        {% if is_trips or trip.owner == profile %}
                            {% if trip.is_public %}
                                <em>(public)</em>
                            {% else %}
                                <em>(hidden)</em>
                            {% endif %}
                        {% endif %}
                    </dd>
                    <dt>Departs</dt>
                    <dd>{{ trip.date_start }}</dd>
                    <dt>Returns</dt>
                    <dd>
                        {% if trip.date_end %}
                            {{ trip.date_end }}
                            <em>({{ trip.duration }} day{{ trip.duration|pluralize:"s" }})</em>
                        {% else %}
                            <em>Unknown</em>
                        {% endif %}
                    </dd>
                    <dt>Cost</dt>
                    <dd>
                        {% if trip.cost %}£ {{ trip.cost }}{% else %}<em>Unknown</em>{% endif %}
                    </dd>
                    <dt>Spaces</dt>
                    <dd>
                        {% if trip.spaces %}
                            {% if trip.is_public %}
                                {{ trip.spaces_left }} / {{ trip.spaces }}
                                {% if trip.spaces_over > 0 %}
                                    <strong class="red">({{ trip.spaces_over }} over)</strong>
                                {% endif %}
                            {% else %}
                                {{ trip.spaces }}
                            {% endif %}
                        {% else %}
                            <em>Unknown</em>
                        {% endif %}
                    </dd>
                    <dt>Maximum Depth</dt>
                    <dd>{% if trip.max_depth %}{{ trip.max_depth }} metres{% else %}<em>Unknown</em>{% endif %}</dd>
                    <dt>Minimum Qualification</dt>
                    <dd>{{ trip.min_qual }}</dd>
                </dl>
            </div>
        </div>

        <div class="col-md-6">
            {{ trip.description | markdown }}
        </div>
    </div>
    {% if trip.is_public %}
        <div class="row">
            <div class="col-md-6">
                <h4>Attendees
                    {% if trip.spaces_taken %}
                        <small class="text-muted">({{ trip.spaces_taken }})</small>
                    {% endif %}
                </h4>
                {% permission request.user can_view_attendee_details trip %}
                    <div class="btn-group">
                        {% if trip.spaces_taken > 0 %}
                            <button type="button" class="btn btn-secondary" data-toggle="modal"
                                    data-target="#exportCSVModal">
                                <i class="fa fa-download"></i> Export CSV Roster
                            </button>
                        {% endif %}
                    </div>
                {% endpermission %}
            </div>
            <div class="col-md-6">
                {% permission request.user can_add trip %}
                    {% include 'base/bulk_select_members.html' %}
                {% endpermission %}
            </div>
            <div class="col-md-12">

                {% if trip.members.count > 0 %}

                    {% include 'xsd_trips/member_list.html' with attendees=trip.attendees %}

                {% else %}

                    <div class="alert alert-info">
                        <i class="fa fa-info-circle"></i> No members have been added.
                    </div>

                {% endif %}
            </div>
        </div>
    {% endif %}
{% endblock %}

{% block modals %}
    {% include 'modal/delete.html' %}
    {% include 'xsd_trips/modal_export.html' %}
{% endblock %}
