{% extends 'base_rigs.html' %}

{% load widget_tweaks %}
{% load static %}
{% load multiply from filters %}
{% load button from filters %}

{% block css %}
    {{ block.super }}
    <link rel="stylesheet" type="text/css" href="{% static 'css/selects.css' %}"/>
    <link rel="stylesheet" type="text/css" href="{% static 'css/easymde.min.css' %}">
{% endblock %}

{% block preload_js %}
    {{ block.super }}
    <script src="{% static 'js/selects.js' %}"></script>
    <script src="{% static 'js/easymde.min.js' %}"></script>
{% endblock %}

{% block js %}
    {{ block.super }}
    <script src="{% static 'js/autocompleter.js' %}"></script>
    <script src="{% static 'js/interaction.js' %}"></script>
    <script src="{% static 'js/tooltip.js' %}"></script>

    <script>
        const matches = window.matchMedia("(prefers-reduced-motion: reduce)").matches || window.matchMedia("(update: slow)").matches;
        $(document).ready(function () {
            dur = matches ? 0 : 500;
            {% if object.pk %}
            // Editing
                {% if not object.is_rig %}
                    $('.form-is_rig').hide();
                {% endif %}
            //Creation
            {% else %}
                // If there were errors, apply the previous Rig/not-Rig selection
                {% if form.errors %}
                    $('.form-hws').show();
                    if ($('#{{form.is_rig.auto_id}}').attr('checked') !== 'checked') {
                        $('.form-is_rig').hide();
                    }
                {% else %}
                    //Initial hide
                    $('.form-hws').slideUp(dur);
                {% endif %}
                //Button handling
                $('#is_rig-selector button').on('click', function () {
                    $('.form-non_rig').slideDown(dur); //Non rig stuff also needed for rig, so always slide down
                    if ($(this).data('is_rig') === 1) {
                        $('#{{form.is_rig.auto_id}}').prop('checked', true);
                        if ($('.form-non_rig').is(':hidden')) {
                            $('.form-is_rig').show();
                        } else {
                            $('.form-is_rig').slideDown(dur);
                        }
                        $('.form-hws, .form-hws .form-is_rig').css('overflow', 'visible');
                    } else {
                        $('#{{form.is_rig.auto_id}}').prop('checked', false);
                        $('.form-is_rig').slideUp(dur);
                    }
                });
            {% endif %}
        });
        $(document).ready(function () {
            setupMDE('#id_description');
            setupMDE('#id_notes');
            setupMDE('#item_description');

            $('#itemModal').on('shown.bs.modal', function (e) {
                $('#item_description').data('mde_editor').value(
                    $('#item_description').val()
                    );
            });

            setupItemTable($("#{{ form.items_json.id_for_label }}").val());
        });
        $(function () {
          $('[data-toggle="tooltip"]').tooltip();
        });
    </script>
{% endblock %}

{% block content %}
{% include 'partials/item_modal.html' %}
<form class="itemised_form" role="form" method="POST">
    {% csrf_token %}
    <div class="row">
        <div class="col-12">
            {% include 'form_errors.html' %}
        </div>
        {% render_field form.is_rig style="display: none" %}
        <input type="hidden" name="{{ form.items_json.name }}" id="{{ form.items_json.id_for_label }}"
               value="{{ form.items_json.value }}"/>

        {# New rig buttons #}
        {% if not object.pk %}
        <div class="col-sm-12">
            <div class="card text-center" id="is_rig-selector">
                <div class="card-body">
		            <span data-toggle="tooltip"
                          title="Anything that involves TEC kit, crew, or otherwise us providing a service to anyone.">
			            <button type="button" class="btn btn-primary w-25" data-is_rig="1">Rig</button>
		            </span>
		            <span data-toggle="tooltip"
                          title="Things that aren't service-based, like training, meetings and site visits.">
			            <button type="button" class="btn btn-info w-25" data-is_rig="0">Non-Rig</button>
		            </span>
                </div>
            </div>
        </div>
        {% endif %}

        {# Contact details #}
        <div class="col-md-6 mt-3">
            <div class="card form-hws form-is_rig  {% if object.pk and not object.is_rig %}hidden{% endif %} mb-3" {% if not object.pk and not form.errors %}style="display: none;"{% endif%}>
                <div class="card-header">Contact Details</div>
                <div class="card-body">
                    <div class="form-group" data-toggle="tooltip" title="The main contact for the event, can be left blank if purely an organisation">
                        <label for="{{ form.person.id_for_label }}"
                               class="col-sm-4 col-form-label">{{ form.person.label }}</label>
                        <div class="col-sm-8">
                            <div class="row">
                                <div class="col-sm-9 col-md-7 col-lg-8">
                                    <select id="{{ form.person.id_for_label }}" name="{{ form.person.name }}" class="selectpicker" data-live-search="true" data-sourceurl="{% url 'api_secure' model='person' %}">
                                        {% if person %}
                                            <option value="{{form.person.value}}" selected="selected" data-update_url="{% url 'person_update' form.person.value %}">{{ person }}</option>
                                        {% endif %}
                                    </select>
                                </div>
                                <div class="col-sm-3 col-md-5 col-lg-4 align-right">
                                    <div class="btn-group">
                                        <a href="{% url 'person_create' %}" class="btn btn-success modal-href"
                                           data-target="#{{ form.person.id_for_label }}">
                                            <span class="fas fa-plus"></span>
                                        </a>
                                        <a {% if form.person.value %}href="{% url 'person_update' form.person.value %}"{% endif %} class="btn btn-warning modal-href" id="{{ form.person.id_for_label }}-update" data-target="#{{ form.person.id_for_label }}">
                                            <span class="fas fa-user-edit"></span>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group" data-toggle="tooltip" title="The client organisation, leave blank if client is an individual">
                        <label for="{{ form.organisation.id_for_label }}"
                               class="col-sm-4 col-form-label">{{ form.organisation.label }}</label>

                        <div class="col-sm-8">
                            <div class="row">
                                <div class="col-sm-9 col-md-7 col-lg-8">
                                    <select id="{{ form.organisation.id_for_label }}" name="{{ form.organisation.name }}" class="selectpicker" data-live-search="true" data-sourceurl="{% url 'api_secure' model='organisation' %}" >
                                        {% if organisation %}
                                            <option value="{{form.organisation.value}}" selected="selected" data-update_url="{% url 'organisation_update' form.organisation.value %}">{{ organisation }}</option>
                                        {% endif %}
                                    </select>
                                </div>
                                <div class="col-sm-3 col-md-5 col-lg-4 align-right">
                                    <div class="btn-group">
                                        <a href="{% url 'organisation_create' %}" class="btn btn-success modal-href"
                                           data-target="#{{ form.organisation.id_for_label }}">
                                            <span class="fas fa-plus"></span>
                                        </a>
                                        <a {% if form.organisation.value %}href="{% url 'organisation_update' form.organisation.value %}"{% endif %} class="btn btn-warning modal-href" id="{{ form.organisation.id_for_label }}-update" data-target="#{{ form.organisation.id_for_label }}">
                                            <span class="fas fa-edit"></span>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card form-hws form-non_rig mb-3" {% if not object.pk and not form.errors %}style="display: none;"{% endif%}>
                <div class="card-header">Event Description</div>
                <div class="card-body">
                    <div class="form-group" data-toggle="tooltip" title="A short description of the event, shown on rigboard and on paperwork">
                        <label for="{{ form.description.id_for_label }}"
                               class="col-sm-4 col-form-label">{{ form.description.label }}</label>

                        <div class="col-sm-12">
                            {% render_field form.description class+="form-control" %}
                        </div>
                    </div>
                </div>
            </div>
        </div>

        {# Event details #}
        <div class="col-md-6 my-3">
            <div class="card card-default form-hws form-non_rig" {% if not object.pk and not form.errors %}style="display: none;"{% endif%}>
                <div class="card-header">Event Details</div>
                <div class="card-body">
                    <div id="form-hws">
                        <div class="form-group" data-toggle="tooltip" title="Name of the event, displays on rigboard and on paperwork">
                            <label for="{{ form.name.id_for_label }}"
                                   class="col-sm-4 col-form-label">{{ form.name.label }}</label>

                            <div class="col-sm-8">
                                {% render_field form.name class+="form-control" %}
                            </div>
                        </div>
                        <div class="form-group" data-toggle="tooltip" title="The venue for the rig, leave blank if unknown (e.g. for a dry hire)">
                            <label for="{{ form.venue.id_for_label }}"
                                   class="col-sm-4 col-form-label">{{ form.venue.label }}</label>

                            <div class="col-sm-8">
                                <div class="row">
                                    <div class="col-sm-9 col-md-7 col-lg-8">
                                        <select id="{{ form.venue.id_for_label }}" name="{{ form.venue.name }}" class="selectpicker" data-live-search="true" data-sourceurl="{% url 'api_secure' model='venue' %}">
                                        {% if venue %}
                                            <option value="{{venue.id}}" selected="selected" data-update_url="{% url 'venue_update' venue.id %}">{{ venue }}</option>
                                        {% endif %}
                                        </select>
                                    </div>
                                    <div class="col-sm-3 col-md-5 col-lg-4 align-right">
                                        <div class="btn-group">
                                            <a href="{% url 'venue_create' %}" class="btn btn-success modal-href"
                                               data-target="#{{ form.venue.id_for_label }}">
                                                <span class="fas fa-plus"></span>
                                            </a>
                                            <a href="{% if object.venue %}{% url 'venue_update' object.venue.pk %}{% endif %}" class="btn btn-warning modal-href" id="{{ form.venue.id_for_label }}-update" data-target="#{{ form.venue.id_for_label }}">
                                                <span class="fas fa-edit"></span>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="{{ form.start_date.id_for_label }}"
                                   class="col-sm-4 col-form-label">{{ form.start_date.label }}</label>

                            <div class="col-sm-10">
                                <div class="row">
                                    <div class="col-sm-12 col-md-7" data-toggle="tooltip" title="Start date for event, required">
                                        {% render_field form.start_date class+="form-control" %}
                                    </div>
                                    <div class="col-sm-12 col-md-5" data-toggle="tooltip" title="Start time of event, can be left blank">
                                        {% render_field form.start_time class+="form-control" step="60" %}
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="{{ form.end_date.id_for_label }}"
                                   class="col-sm-4 col-form-label">{{ form.end_date.label }}</label>

                            <div class="col-sm-10">
                                <div class="row">
                                    <div class="col-sm-12 col-md-7" data-toggle="tooltip" title="End date of event, leave blank if unknown or same as start date">
                                        {% render_field form.end_date class+="form-control" %}
                                    </div>
                                    <div class="col-sm-12 col-md-5" data-toggle="tooltip" title="End time of event, leave blank if unknown">
                                        {% render_field form.end_time class+="form-control" step="60" %}
                                    </div>
                                </div>
                            </div>
                        </div>

                        {# Rig only information #}
                        <div class="form-is_rig {% if object.pk and not object.is_rig %}hidden{% endif %}">
                            <div class="form-group" data-toggle="tooltip" title="The date/time at which TEC have access to the venue">
                                <label for="{{ form.access_at.id_for_label }}"
                                       class="col-sm-4 col-form-label">{{ form.access_at.label }}</label>
                                <div class="col-sm-8">
                                    {% render_field form.access_at class+="form-control" step="60" %}
                                </div>
                            </div>
                            <div class="form-group" data-toggle="tooltip" title="The date/time at which crew should meet for this event">
                                <label for="{{ form.meet_at.id_for_label }}"
                                       class="col-sm-4 col-form-label">{{ form.meet_at.label }}</label>

                                <div class="col-sm-8">
                                    {% render_field form.meet_at class+="form-control" step="60" %}
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-offset-4 col-sm-8">
                                        <label data-toggle="tooltip" title="Mark this event as a dry-hire, so it needs to be checked in at the end">
                                        {{ form.dry_hire.label }} {% render_field form.dry_hire %}
                                    </div>
                                </div>
                            </div>
                        </div>

                        {# Status is needed on all events types and it looks good here in the form #}
                        <div class="form-group" data-toggle="tooltip" title="The current status of the event. Only mark as booked once paperwork is received">
                            <label for="{{ form.status.id_for_label }}"
                                   class="col-sm-4 col-form-label">{{ form.status.label }}</label>

                            <div class="col-sm-8">
                                {% render_field form.status class+="form-control" %}
                            </div>
                        </div>

                        <div class="form-is_rig {% if object.pk and not object.is_rig %}hidden{% endif %}">
                            <div class="form-group" data-toggle="tooltip" title="The Member in Charge of this event">
                                <label for="{{ form.mic.id_for_label }}"
                                       class="col-sm-4 col-form-label">{{ form.mic.label }}</label>

                                <div class="col-sm-8">
                                    <select id="{{ form.mic.id_for_label }}" name="{{ form.mic.name }}" class="px-0 selectpicker" data-live-search="true" data-sourceurl="{% url 'api_secure' model='profile' %}?fields=first_name,last_name,initials">
                                        {% if mic %}
                                            <option value="{{form.mic.value}}" selected="selected" >{{ mic.name }}</option>
                                        {% endif %}
                                    </select>
                                </div>
                            </div>

                            {% if object.dry_hire %}
                                <div class="form-group" data-toggle="tooltip" title="The person who checked-in this dry hire">
                                    <label for="{{ form.checked_in_by.id_for_label }}"
                                           class="col-sm-4 col-form-label">{{ form.checked_in_by.label }}</label>

                                    <div class="col-sm-8">
                                         <select id="{{ form.checked_in_by.id_for_label }}" name="{{ form.checked_in_by.name }}" class="px-0 selectpicker" data-live-search="true" data-sourceurl="{% url 'api_secure' model='profile' %}?fields=first_name,last_name,initials">
                                            {% if checked_in_by %}
                                                <option value="{{form.checked_in_by.value}}" selected="selected" >{{ checked_in_by.name }}</option>
                                            {% endif %}
                                        </select>
                                    </div>
                                </div>
                            {% endif %}

                            <div class="form-group" data-toggle="tooltip" title="The student ID of the client who collected the dry-hire">
                                <label for="{{ form.collector.id_for_label }}"
                                       class="col-sm-4 col-form-label">{{ form.collector.label }}</label>

                                <div class="col-sm-8">
                                    {% render_field form.collector class+="form-control" %}
                                </div>
                            </div>

                            <div class="form-group" data-toggle="tooltip" title="The purchase order number (for external clients)">
                                <label for="{{ form.purchase_order.id_for_label }}"
                                       class="col-sm-4 col-form-label">{{ form.purchase_order.label }}</label>

                                <div class="col-sm-8">
                                    {% render_field form.purchase_order class+="form-control" %}
                                </div>
                            </div>

                            <div class="form-group" data-toggle="tooltip" title="The thread for this event on the TEC Forum">
                                <label for="{{ form.forum_url.id_for_label }}"
                                       class="col-sm-4 col-form-label">Forum Thread</label>
                                <div class="col-sm-12">
                                    <p class="small mb-0">Paste URL</p>
                                    {% render_field form.forum_url class+="form-control" %}
                                    {% if object.pk %}
                                    <p class="small mb-0">or</p>
                                    <a href="{% url 'event_thread' object.pk %}" class="btn btn-primary" title="Create Forum Thread" target="_blank">
                                    <span class="fas fa-plus"></span> <span class="hidden-xs">Create Forum Thread</span></a>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        {# Notes and item shit #}
        <div class="col-sm-12">
            <div class="card card-default form-hws form-is_rig  {% if object.pk and not object.is_rig %}hidden{% endif %}" {% if not object.pk and not form.errors %}style="display: none;"{% endif%}>
                <div class="card-body">
                    <div class="col-sm-12">
                        <div class="form-group" data-toggle="tooltip" title="Notes on the event. This is only visible to keyholders, and is not displayed on the paperwork">
                            <label for="{{ form.notes.id_for_label }}">{{ form.notes.label }}</label>
                            {% render_field form.notes class+="form-control md-enabled" %}
                        </div>
                    </div>
                    {% include 'partials/item_table.html' %}
                </div>
            </div>
        </div>
        <div class="col-sm-12 text-right form-hws form-non_rig my-3" {% if not object.pk and not form.errors %}style="display: none;"{% endif%}>
            {% button 'submit' %}
        </div>
    </div>
</form>
{% endblock %}
