{% extends 'base_rigs.html' %}
{% load widget_tweaks %}
{% load static %}
{% load multiply from filters %}
{% block title %}{% if object.pk %}Event {% if object.is_rig %}N{{ object.pk|stringformat:"05d" }}{% else %}
    {{ object.pk }}{% endif %}{% else %}New Event{% endif %}{% endblock %}

{% block css %}
    <link rel="stylesheet" href="{% static "css/bootstrap-select.min.css" %}"/>
    <link rel="stylesheet" href="{% static "css/ajax-bootstrap-select.css" %}"/>
{% endblock %}

{% block preload_js %}
    <script src="{% static "js/bootstrap-select.js" %}"></script>
    <script src="{% static "js/ajax-bootstrap-select.js" %}"></script>
{% endblock %}

{% block js %}
    <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
    <script src="{% static "js/interaction.js" %}"></script>
    <script src="{% static "js/modal.js" %}"></script>
    <script src="{% static "js/tooltip.js" %}"></script>

    <script src="{% static "js/autocompleter.js" %}"></script>

    <script>
        function setTime23Hours() {
            $('#{{ form.end_time.id_for_label }}').val('23:00');
        }

        function setTime02Hours() {
            var id_start = "{{ form.start_date.id_for_label }}";
            var id_end_date = "{{ form.end_date.id_for_label }}";
            var id_end_time = "{{ form.end_time.id_for_label }}";
            if ($('#'+id_start).val() == $('#'+id_end_date).val()) {
                var end_date = new Date($('#'+id_end_date).val());
                end_date.setDate(end_date.getDate() + 1);
                $('#'+id_end_date).val(end_date.getISOString());
            }
            $('#'+id_end_time).val('02:00');
        }

        $(document).ready(function () {
            {% if not object.pk and not form.errors %}

                $('.form-hws').slideUp(function () {
                    $('.form-is_rig').slideUp();
                });

            {% elif not object.pk and form.errors %}
                if ($('#{{form.is_rig.auto_id}}').attr('checked') != 'checked') {
                    $('.form-is_rig').hide();
                }
            {% endif %}

            {% if not object.pk %}
                $('#is_rig-selector button').on('click', function () {
                    $('.form-non_rig').slideDown();
                    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();
                        }
                        $('.form-hws, .form-hws .form-is_rig').css('overflow', 'visible');
                    } else {
                        $('#{{form.is_rig.auto_id}}').prop('checked', false);
                        $('.form-is_rig').slideUp();
                    }
                });
            {% endif %}

            function supportsDate() {
                //return false; //for development
                var input = document.createElement('input');
                input.setAttribute('type','date');
                var notADateValue = 'not-a-date';
                input.setAttribute('value', notADateValue); 
                return !(input.value === notADateValue);
            }
            if(supportsDate()){
                //Good, we'll use the browser implementation
            }else{
                //Rubbish browser - do JQuery backup
                $('<link>')
                  .appendTo('head')
                  .attr({type : 'text/css', rel : 'stylesheet'})
                  .attr('href', '{% static "css/bootstrap-datetimepicker.min.css" %}');
                $.when(
                    $.getScript( "{% static "js/moment.min.js" %}" ),
                    $.getScript( "{% static "js/bootstrap-datetimepicker.min.js" %}" ),
                    $.Deferred(function( deferred ){
                        $( deferred.resolve );
                    })
                ).done(function(){
                    $('input[type=date]').attr('type','text').datetimepicker({
                        format: 'YYYY-MM-DD',
                    });
                    $('input[type=time]').attr('type','text').datetimepicker({
                        format: 'HH:mm',
                    });
                    $('input[type=datetime-local]').attr('type','text').datetimepicker({
                        format: 'YYYY-MM-DD[T]HH:mm',
                        sideBySide: true,
                    });
                });

            }
        });

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

{% block content %}
    <form class="form-horizontal itemised_form" role="form" method="POST">{% csrf_token %}
    <div class="row">
    <div class="col-sm-12">
            <div class="col-sm-8">
                <h2>
                    {% if duplicate %}
                        Duplicate of Event N{{ object.pk|stringformat:"05d" }}
                    {% elif object.pk %}
                        Event N{{ object.pk|stringformat:"05d" }}
                    {% else %}
                        New Event
                    {% endif %}
                </h2>
            </div>
            <div class="col-sm-4 text-right">
                <div class="btn-group btn-page">
                    <button type="submit" class="btn btn-default" title="Save"><span
                            class="glyphicon glyphicon-floppy-disk"></span></button>
                </div>
            </div>
        </div>
    </div>
    {% include 'form_errors.html' %}
    {% 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-md-12 well">
            <div class="form-group" id="is_rig-selector">
                <div class="col-sm-12">
					<span class="col-sm-6" 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 col-xs-12" data-is_rig="1">Rig</button>
					</span>
					<span class="col-sm-6" data-toggle="tooltip"
                          title="Things that aren't service-based, like training, meetings and site visits.">
						<button type="button" class="btn btn-info col-xs-12" data-is_rig="0">Non-Rig</button>
					</span>
                </div>
            </div>
        </div>
    {% endif %}

    {# Contact details #}
    <div class="col-sm-12 col-md-6">
        <div class="panel panel-default form-hws form-is_rig  {% if object.pk and not object.is_rig %}hidden{% endif %}">
            <div class="panel-heading">Contact Details</div>
            <div class="panel-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 control-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="form-control 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-default modal-href"
                                       data-target="#{{ form.person.id_for_label }}">
                                        <span class="glyphicon glyphicon-plus"></span>
                                    </a>
                                    <a href="{% if form.person.value %}{% url 'person_update' form.person.value %}{% endif %}" class="btn btn-default modal-href" id="{{ form.person.id_for_label }}-update" data-target="#{{ form.person.id_for_label }}">
                                        <span class="glyphicon glyphicon-pencil"></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 control-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="form-control 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-default modal-href"
                                       data-target="#{{ form.organisation.id_for_label }}">
                                        <span class="glyphicon glyphicon-plus"></span>
                                    </a>
                                    <a href="{% if form.organisation.value %}{% url 'organisation_update' form.organisation.value %}{% endif %}" class="btn btn-default modal-href" id="{{ form.organisation.id_for_label }}-update" data-target="#{{ form.organisation.id_for_label }}">
                                        <span class="glyphicon glyphicon-pencil"></span>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="panel panel-default form-hws form-non_rig">
            <div class="panel-heading">Event Description</div>
            <div class="panel-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 control-label">{{ form.description.label }}</label>

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

    {# Event details #}
    <div class="col-sm-12 col-md-6">
        <div class="panel panel-default form-hws form-non_rig">
            <div class="panel-heading">Event Details</div>
            <div class="panel-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 control-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 control-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="form-control selectpicker" data-live-search="true" data-sourceurl="{% url 'api_secure' model='venue' %}">
                                    {% if venue %}
                                        <option value="{{form.venue.value}}" selected="selected" data-update_url="{% url 'venue_update' form.venue.value %}">{{ 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-default modal-href"
                                           data-target="#{{ form.venue.id_for_label }}">
                                            <span class="glyphicon glyphicon-plus"></span>
                                        </a>
                                        <a href="{% if object.venue %}{% url 'venue_update' object.venue.pk %}{% endif %}" class="btn btn-default modal-href" id="{{ form.venue.id_for_label }}-update" data-target="#{{ form.venue.id_for_label }}">
                                            <span class="glyphicon glyphicon-pencil"></span>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="{{ form.start_date.id_for_label }}"
                               class="col-sm-4 control-label">{{ form.start_date.label }}</label>

                        <div class="col-sm-8">
                            <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" %}
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="{{ form.end_date.id_for_label }}"
                               class="col-sm-4 control-label">{{ form.end_date.label }}</label>

                        <div class="col-sm-8">
                            <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" %}
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-sm-12 col-md-offset-7 col-md-5">
                                    <div class="btn-group btn-group-justified">
                                        <btn class="btn btn-default btn-xs" onclick="setTime23Hours()">23:00</btn>
                                        <btn class="btn btn-default btn-xs" onclick="setTime02Hours()">02:00</btn>
                                    </div>
                                </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 control-label">{{ form.access_at.label }}</label>

                            <div class="col-sm-8">
                                {% render_field form.access_at class+="form-control" %}
                            </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 control-label">{{ form.meet_at.label }}</label>

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

                            <div class="col-sm-8">
                                <select id="{{ form.mic.id_for_label }}" name="{{ form.mic.name }}" class="form-control 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 control-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="form-control 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 control-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 control-label">{{ form.purchase_order.label }}</label>

                            <div class="col-sm-8">
                                {% render_field form.purchase_order class+="form-control" %}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- /.col-sm-12 .col-md-6 -->
    <div class="col-sm-12 text-right">
        <div class="btn-group btn-page">
            <button type="submit" class="btn btn-default" title="Save"><span
                    class="glyphicon glyphicon-floppy-disk"></span>
            </button>
        </div>
    </div>


    {# Notes and item shit #}
    <div class="col-sm-12">
        <div class="panel panel-default form-hws form-is_rig  {% if object.pk and not object.is_rig %}hidden{% endif %}">
            <div class="panel-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" %}
                    </div>
                </div>
                {% include "RIGS/item_table.html" %}
            </div>
        </div>
    </div>
    <div class="col-sm-12 text-right form-hws form-is_rig {% if object.pk and not object.is_rig %}hidden{% endif %}">
        <div class="btn-group btn-page">
            <button type="submit" class="btn btn-default" title="Save"><span
                    class="glyphicon glyphicon-floppy-disk"></span>
            </button>
        </div>
    </div>
    </form>

    {% include 'RIGS/item_modal.html' %}
{% endblock %}
