{% extends "oneevent/base.html" %}

{% block navbar_header_text %}Manage Event{% endblock %}

{% block heading_action %}Manage{% endblock %}
{% block heading_title %}{{ event.title }}
    <small>{{ event.start|date:"D, d N Y H:i" }}</small>
{% endblock %}

{% block content %}

<div class="row form-inline bottom-separation">
<div class='col-sm-4'>
    <a href="{% url 'event_update' event_id=event.id %}"
       class="btn btn-warning">Edit the event</a>
</div>
<div class='col-sm-8 pull-right' align="right">
    <div class="form-group">
        <label for="regristrationAddress">URL to register to this event:</label>
        <div class="input-group">
            <input type="text" class="form-control" id="regristrationAddress" size="40"
                   value="{{ registration_url }}"></input>
            <div class="input-group-addon" onClick="copyToClipboard(document.getElementById('regristrationAddress').value)">
                <span class="glyphicon glyphicon-copy" aria-hidden="true"></span>
            </div>
        </div>
    </div>

</div>
</div>

<div class="row">

{% if event.choices.exists %}
<div class="panel panel-info collapsible-caret">
    <div class="panel-heading collapsible-toggle" data-toggle="collapse" data-target="#collapseChoices">
        <strong>Choices summary</strong>
        <a href="{% url 'event_download_options_summary' event_id=event.id %}"
            onclick="avoid_collapse_toggle(event)"
            class="btn btn-info">
            <span class="glyphicon glyphicon-download-alt"></span> Download list
        </a>
    </div>
    <div id="collapseChoices" class="panel-collapse collapse in table-responsive" >
        <table class="panel-body table table-bordered table-striped">
            <thead>
                <tr>
                    <th>Choice</th>
                    <th colspan="0">Options</th>
                </tr>
            </thead>
            <tbody>
                {% for choice, options in event.get_options_counts.iteritems %}
                <tr>
                    <th>{{ choice.title }}</th>
                    {% for option, total in options.items %}
                    <td>
                        {{ option.title }}
                        <span class="badge pull-right">{{ total }}</span>
                    </td>
                    {% endfor %}
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>
{% endif %}

{% if event.sessions.exists %}
<div class="panel panel-info collapsible-caret">
    <div class="panel-heading collapsible-toggle" data-toggle="collapse" data-target="#collapseSessions">
        <strong>Sessions summary</strong>
    </div>
    <div id="collapseSessions" class="panel-collapse collapse in table-responsive" >
        <table class="panel-body table table-bordered table-striped">
            <thead>
                <tr>
                    <th>Session</th>
                    <th>Participants</th>
                </tr>
            </thead>
            <tbody>
                {% for session in event.sessions.all %}
                <tr>
                    <td>{{ session.title }}</td>
                    <td><span class="badge">{{ session.get_active_bookings.count }}</span></td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>
{% endif %}

<div class="panel panel-info collapsible-caret">
    <div class="panel-heading collapsible-toggle collapsed" data-toggle="collapse" data-target="#collapseParticipants">
        <strong>Participants</strong>
        <a href="{% url 'event_download_participants_list' event_id=event.id %}"
            onclick="avoid_collapse_toggle(event)"
            class="btn btn-info" data-toggle="" data-target="">
            <span class="glyphicon glyphicon-download-alt"></span> Download list
        </a>
        {% if not event.is_fully_booked %}
        <a href="{% url 'booking_create_on_behalf' event_id=event.id %}"
            onclick="avoid_collapse_toggle(event)"
            class="btn btn-success" data-toggle="" data-target="">
            <span class="glyphicon glyphicon-plus"></span> Add participant
        </a>
        {% else %}
        <button type="button" class="btn btn-success disabled">
            <span class="glyphicon glyphicon-plus"></span> Event Full
        </button>
        {% endif %}
        <div class="checkbox pull-right">
            <input type="checkbox" onclick="hide_cancelled_toggle(event)">Show Cancelled
        </div>
    </div>
    <div id="collapseParticipants" class="panel-collapse collapse table-responsive">
        <table class="panel-body table table-bordered">
            <thead>
                <tr>
                    <th rowspan=2>Name</th>
                    <th rowspan=2>Category</th>
                    <th rowspan=2>Actions</th>
                    <th rowspan=2>Booking Status</th>
                    {% if event.sessions.exists %}<th rowspan=2>Session</th>{% endif %}
                    {% if event.choices.exists %}<th colspan=0>Choices</th>{% endif %}
                </tr>
                <tr>
                    {% for choice in event.choices.all %}
                    <th>{{ choice.title }}</th>
                    {% endfor %}
                </tr>
            </thead>
            <tbody>
                {% for booking in event.bookings.all %}
                <tr class="{{ booking.get_payment_status_class }}
                           {% if booking.cancelledBy %}cancelled hidden{% endif %}">
                    <td><!-- Name -->
                        <p>{{ booking.person.get_full_name }}</p>
                        {% if booking.person.email %}
                        <p><a href="mailto:{{booking.person.email}}?Subject={{booking.event.title|urlencode}}"
                              title="Email participant" target="_blank">
                            <span class="glyphicon glyphicon-envelope"></span>
                         </a>
                         <a onclick="send_invite(
                                 '{% url 'booking_send_invite' booking_id=booking.id %}',
                                 '{{ booking.person.get_full_name }}');"
                            title="Send invite">
                             <span class="glyphicon glyphicon-calendar"></span>
                         </a></p>
                         {% endif %}
                    </td>

                    <td><!-- Category -->
                        {{ booking.get_category_name }}
                    </td>

                    <td><!--  Actions -->
                        {% if booking.cancelledBy and booking.paidTo%}
                        <div><a href="{% url 'booking_payment_cancel' booking_id=booking.id %}"
                            class="btn btn-danger btn-sm">
                            <span class="glyphicon glyphicon-gbp"></span>
                            Refund
                        </a></div>
                        {% endif %}
                        {% if not booking.cancelledBy and not booking.paidTo and booking.must_pay > 0 %}
                        <div><a href="{% url 'booking_payment_confirm' booking_id=booking.id %}"
                            class='btn btn-success btn-sm'>
                            <span class="glyphicon glyphicon-ok"></span>
                            Confirm Payment
                        </a></div>
                        <div><a href="{% url 'booking_payment_exempt' booking_id=booking.id %}"
                            class='btn btn-default btn-sm bg-success'>
                            <span class="glyphicon glyphicon-thumbs-up"></span>
                            Exempt of Payment
                        </a></div>
                        {% endif %}
                        {% if not booking.cancelledBy and booking.exempt_of_payment %}
                            <div><a href="{% url 'booking_payment_unexempt' booking_id=booking.id %}"
                                class='btn btn-default btn-sm bg-danger'>
                                <span class="glyphicon glyphicon-thumbs-down"></span>
                                Cancel Exemption
                            </a></div>
                        {% endif %}
                        <div>
                            <a href="{% url 'booking_update' booking_id=booking.id %}"
                                class="btn btn-warning btn-xs">
                                <span class="glyphicon glyphicon-pencil"></span>
                                Edit
                            </a>
                            {% if not booking.cancelledBy %}
                            <a href="{% url 'booking_cancel' booking_id=booking.id %}"
                                class="btn btn-danger btn-xs">
                                <span class="glyphicon glyphicon-ban-circle"></span>
                                Cancel
                            </a>
                            {% endif %}

                        </div>
                    </td>

                    <td><!-- Booking status -->
                        {% if booking.cancelledBy %}
                            <div><span class="label label-default">Cancelled</span>
                            by {{booking.cancelledBy.get_full_name}} on {{booking.cancelledOn|date:"d N Y H:i"}}</div>
                        {% endif %}

                        <div><span class='label label-{{ booking.get_payment_status_class }}'>
                        {% if booking.exempt_of_payment %}
                            Exempted of payment
                        {% elif booking.paidTo %}
                            Payment received
                        {% elif booking.must_pay > 0 %}
                            No payment received
                        {% else %}
                            No payment needed
                        {% endif %}
                        </span></div>
                        <div>
                        {% if booking.exempt_of_payment or booking.paidTo %}
                            by {{ booking.paidTo.get_full_name }} on {{ booking.datePaid|date:"d N Y H:i" }}
                        {% endif %}
                        </div>
                    </td>

                    <!-- Session -->
                    {% if event.sessions.exists %}
                    <td>{% if booking.session %}
                        {{ booking.session.title }}
                    {% else %}
                        <span class="glyphicon glyphicon-warning-sign text-danger" aria-hidden="true"></span>
                    {% endif %}
                    </td>
                    {% endif %}

                    <!-- Options -->
                    {% for option in booking.options.all %}
                    <td>
                        {{ option.option.title }}
                    </td>
                    {% endfor %}
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    <div id="panel-footer">
        <div class="row"><div class="col-xs-10 col-xs-offset-1">
            <table>
                <tr>
                    <td class='text-right'><strong>Confirmed bookings: </strong></td>
                    <td> {{event.get_active_bookings.count}}</td>
                </tr>
                <tr>
                    <td class='text-right'><strong>Cancelled bookings: </strong></td>
                    <td> {{event.get_cancelled_bookings.count}}</td>
                </tr>
                <tr>
                    <td class='text-right'><strong>Total: </strong></td>
                    <td> {{event.bookings.count}}</td>
                </tr>
            </table>
        </div></div>
    </div>
</div>

{% if event.categories.exists %}
<div class="panel panel-info collapsible-caret">
    <div class="panel-heading collapsible-toggle" data-toggle="collapse" data-target="#collapseMoney">
        <strong>Collected money</strong>
    </div>
    <div id="collapseMoney" class="panel-collapse collapse in table-responsive">
        <table class="panel-body table table-striped table-hover">
        {% with collected_sums=event.get_collected_money_sums %}
            <thead>
                <tr>
                    <th>Organiser</th>
                    {% for category in collected_sums.categories %}
                    <th>{{ category }}</th>
                    {% endfor %}
                    <th>Total</th>
                </tr>
            </thead>
            <tbody>
            {% for row in collected_sums.table_rows %}
                {% if forloop.last %}
                <tr class="info">
                {% else %}
                <tr>
                {% endif %}
                {% for cell in row %}
                    {% if forloop.first %}
                    <th>{{ cell }}</th>
                    {% else %}
                    <td>
                        {{ cell|default_if_none:-1|floatformat:2 }}
                        {% if event.price_currency %}({{event.price_currency}}){% endif %}
                    </td>
                    {% endif %}
                {% endfor %}
                </tr>
            {% endfor %}
            </tbody>
        {% endwith %}
        </table>
    </div>
</div>
{% endif %}

</div>
{% endblock %}
