{% extends 'base.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 href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"/>
{% 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/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}}').attr('checked', true);
                        if ($('.form-non_rig').is(':hidden')) {
                            $('.form-is_rig').show();
                        } else {
                            $('.form-is_rig').slideDown();
                        }
                    } else {
                        $('#{{form.is_rig.auto_id}}').attr('checked', false);
                        $('.form-is_rig').slideUp();
                    }
                })
            {% endif %}
        })

        $(document).ready(function () {
            setupItemTable($("#{{ form.items_json.id_for_label }}").val());
        });
    </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 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">
						<button type="button" class="btn btn-primary col-xs-12" data-is_rig="1">Rig</button>
					</span>
					<span class="col-sm-6">
						<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">
                    <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">
                            <input type="hidden" id="{{ form.person.id_for_label }}" name="{{ form.person.name }}"
                                   value="{{ form.person.value|default_if_none:"" }}"/>

                            <div class="col-sm-9 col-md-7 col-lg-8">
                                <input type="text" id="{{ form.person.id_for_label }}-input"
                                       class="form-control autocomplete-json autocomplete-update"
                                       value="{{ person|default_if_none:"" }}"
                                       data-sourceurl="{% url 'api_secure' model='person' %}"
                                       data-target="{{ form.person.id_for_label }}"/>
                            </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">
                                        <span class="glyphicon glyphicon-pencil"></span>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <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">
                            <input type="hidden" id="{{ form.organisation.id_for_label }}"
                                   name="{{ form.organisation.name }}"
                                   value="{{ form.organisation.value|default_if_none:"" }}"/>

                            <div class="col-sm-9 col-md-7 col-lg-8">
                                <input type="text" id="{{ form.organisation.id_for_label }}-input"
                                       class="form-control autocomplete-json autocomplete-update"
                                       value="{{ organisation|default_if_none:"" }}"
                                       data-sourceurl="{% url 'api_secure' model='organisation' %}"
                                       data-target="{{ form.organisation.id_for_label }}"/>
                            </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">
                                        <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">
                    <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">
                        <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">
                        <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">
                                <input type="hidden" id="{{ form.venue.id_for_label }}" name="{{ form.venue.name }}"
                                       value="{{ form.venue.value|default_if_none:"" }}"/>

                                <div class="col-sm-9 col-md-7 col-lg-8">
                                    <input type="text" id="{{ form.venue.id_for_label }}-input"
                                           class="form-control autocomplete-json autocomplete-update"
                                           value="{{ venue|default_if_none:"" }}"
                                           data-sourceurl="{% url 'api_secure' model='venue' %}"
                                           data-target="{{ form.venue.id_for_label }}"/>
                                </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">
                                            <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">
                                    {% render_field form.start_date type="date" class+="form-control" %}
                                </div>
                                <div class="col-sm-12 col-md-5">
                                    {% render_field form.start_time type="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">
                                    {% render_field form.end_date type="date" class+="form-control" %}
                                </div>
                                <div class="col-sm-12 col-md-5">
                                    {% render_field form.end_time type="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">
                            <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 type="datetime-local" class+="form-control" %}
                            </div>
                        </div>
                        <div class="form-group">
                            <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 type="datetime-local" class+="form-control" %}
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-4 col-sm-8">
                                <div class="checkbox">
                                    <label>
                                        {% 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">
                        <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">
                            <label for="{{ form.mic.id_for_label }}"
                                   class="col-sm-4 control-label">{{ form.mic.label }}</label>

                            <div class="col-sm-8">
                                <input type="hidden" id="{{ form.mic.id_for_label }}" name="{{ form.mic.name }}"
                                       value="{{ form.mic.value|default_if_none:"" }}"/>

                                <input type="text" id="{{ form.mic.id_for_label }}-input"
                                       class="form-control autocomplete-json"
                                       data-sourceurl="{% url 'api_secure' model='profile' %}?fields=first_name,last_name,initials"
                                       data-target="{{ form.mic.id_for_label }}"
                                       value="{{ mic.name|default_if_none:"" }}"/>
                            </div>
                        </div>

                        {% if object.dry_hire %}
                            <div class="form-group">
                                <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">
                                    <input type="hidden" id="{{ form.checked_in_by.id_for_label }}"
                                           name="{{ form.checked_in_by.name }}"
                                           value="{{ form.checked_in_by.value|default_if_none:"" }}"/>

                                    <input type="text" id="{{ form.checked_in_by.id_for_label }}-input"
                                           class="form-control autocomplete-json"
                                           data-sourceurl="{% url 'api_secure' model='profile' %}?fields=first_name,last_name,initials"
                                           data-target="{{ form.checked_in_by.id_for_label }}"
                                           value="{{ checked_in_by.name|default_if_none:"" }}"/>
                                </div>
                            </div>
                        {% endif %}

                        <div class="form-group">
                            <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>
                </div>
            </div>
        </div>
        <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>
    </div>
    <!-- /.col-sm-12 .col-md-6 -->

    {# 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">
                        <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>
    </form>

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