{% load custom_tags_and_filters %}
{% for ticket in ostickets %}
    {% include 'NEMO_osticket/osticket_details.html' with ticket=ticket %}
{% empty %}
    {% if not osticket_api_available %}<div>There are no open tickets for this tool.</div>{% endif %}
{% endfor %}
{% if osticket_api_available %}
    <p>Use this form to create a ticket in the help desk ticketing system.</p>
    <form class="col-lg-9 well"
          style="float: none"
          action="{% url 'osticket_create_ticket' tool_id %}"
          method="post">
        {% csrf_token %}
        {% for field_name, field in form.fields.items %}
            <div class="form-group">
                {% if field.widget|class_name == "Select" %}
                    <label class="control-label" for="osticket-{{ field_name }}">{{ field.label }}</label>
                    <select name="{{ field_name }}"
                            id="osticket-{{ field_name }}"
                            class="form-control"
                            {% if field.required %}required{% endif %}>
                        <option value="" selected {% if field.required %}disabled{% endif %}>Select an option</option>
                        {% if field_name == "reservation" %}
                            {% for option in reservations %}
                                <option value="{{ option.id }}">
                                    {{ option.start|date:"SHORT_DATETIME_FORMAT" }} to {{ option.end|date:"SHORT_DATETIME_FORMAT" }} by {{ option.user }}
                                </option>
                            {% endfor %}
                        {% else %}
                            {% for option in field.choices %}<option value="{{ option.0 }}">{{ option.1 }}</option>{% endfor %}
                        {% endif %}
                    </select>
                {% elif field.widget|class_name == "Textarea" %}
                    <label class="control-label" for="osticket-{{ field_name }}">{{ field.label }}</label>
                    <textarea name="{{ field_name }}"
                              id="osticket-{{ field_name }}"
                              class="form-control"
                              rows="6"
                              {% if field.required %}required{% endif %}></textarea>
                {% elif field.widget|class_name == "TextInput" %}
                    <label class="control-label" for="osticket-{{ field_name }}">{{ field.label }}</label>
                    <input type="text"
                           name="{{ field_name }}"
                           id="osticket-{{ field_name }}"
                           class="form-control"
                           {% if field.required %}required{% endif %} />
                {% endif %}
            </div>
        {% endfor %}
        <div class="form-group">{% button type="save" value="Create ticket" icon="glyphicon-send" %}</div>
    </form>
{% endif %}
<div style="height:150px">{# Spacer #}</div>
