{% extends "layout.html" %}
{% from "_macros.html" import render_co_organizer_select2, render_event_date_defintion_code, render_date_definition_container, render_manage_form_styles, render_manage_form_scripts, render_cropper_header, render_end_container_handling, render_jquery_steps_header, render_cropper_code, render_crop_image_form_section, render_radio_buttons, render_field_with_errors, render_field %}

{%- block title -%}
{{ _('Update event') }}
{%- endblock -%}

{% block styles %}
{{ render_manage_form_styles() }}
{% endblock %}

{% block header_before_site_js %}
{{ render_manage_form_scripts() }}
{{ render_jquery_steps_header() }}
{{ render_cropper_header() }}

  <script>
    $( function() {

      {{ render_cropper_code() }}

      var form = $("#main-form");
      form.validate({
            rules: {
              event_place_id: "required",
              organizer_id: "required"
            }
        });

      {{ render_event_date_defintion_code() }}

      // Organizer
      var organizer_select =$('#organizer_id');
      var input_group = organizer_select.parent();
      input_group.append('<button type="button" id="organizer-edit-btn" class="btn btn-outline-secondary my-1"><i class="fa fa-edit"></i></button>');
      input_group.append('<button type="button" id="organizer-add-btn" class="btn btn-outline-secondary m-1"><i class="fa fa-plus"></i></button>');
      $('#organizer-edit-btn').click(function () {
        window.open('/organizer/' + organizer_select.val() + '/update');
        return false;
      });
      $('#organizer-add-btn').click(function () {
        window.open('{{ url_for("manage_admin_unit_organizer_create", id=event.admin_unit_id) }}');
        return false;
      });

      // Place
      var place_select =$('#event_place_id');
      var input_group = place_select.parent();
      input_group.append('<button type="button" id="place-edit-btn" class="btn btn-outline-secondary my-1"><i class="fa fa-edit"></i></button>');
      input_group.append('<button type="button" id="place-add-btn" class="btn btn-outline-secondary m-1"><i class="fa fa-plus"></i></button>');
      $('#place-edit-btn').click(function () {
        window.open('/event_place/' + place_select.val() + '/update');
        return false;
      });
      $('#place-add-btn').click(function () {
        window.open('{{ url_for("manage_admin_unit_places_create", id=event.admin_unit_id) }}');
        return false;
      });

      $('#event_place_id').select2({
        width: '100%',
        theme: 'bootstrap4',
        ajax: {
          url: "{{ url_for('js_autocomplete_place') }}",
          dataType: 'json',
          delay: 250,
          cache: true,
          data: function (params) {
            return {
              keyword: params.term,
              admin_unit_id: '{{ event.admin_unit_id }}',
              exclude_gmaps: 1,
            };
          }
        },
        placeholder: "{{ _('Enter place or address') }}"
      });

      $('#organizer_id').select2({
        width: '100%',
        theme: 'bootstrap4',
        ajax: {
          url: "{{ url_for('api_v1_organization_organizer_list', id=event.admin_unit.id) }}",
          dataType: 'json',
          delay: 250,
          cache: true,
          data: function (params) {
            return {
              name: params.term,
              per_page: 5
            };
          },
          processResults: function (data) {
            return {
              results: data.items.map(p => ({"id": p.id, "text": p.name}))
            };
          }
        },
        placeholder: "{{ _('Enter organizer') }}"
      });

      $('#organizer_id,#event_place_id').on('select2:select', function (e) {
          $(this).valid();
      });

      {{ render_co_organizer_select2(event.admin_unit.id) }}

    });
  </script>
{% endblock %}
{% block content %}

<h1>{{ _('Update event') }}</h1>

{{ render_date_definition_container(form.date_definition_template, "date-definition-template d-none") }}

<form id="main-form" action="{{ url_for('event_update', event_id=event.id) }}" method="POST" enctype="multipart/form-data">
  {{ form.hidden_tag() }}

  <div class="card mb-4">
    <div class="card-header">
      {{ _('Event') }}
    </div>
    <div class="card-body">
      {{ render_field_with_errors(form.name) }}
      {{ render_field_with_errors(form.description) }}
    </div>
  </div>

  <div class="card mb-4">
    <div class="card-header">
      {{ _('Event dates') }}
    </div>
    <div class="card-body pb-3">
      {% for date_definition in form.date_definitions %}
        {{ render_date_definition_container(date_definition) }}
      {% endfor %}

      <button type="button" class="btn btn-outline-secondary btn-small" id="add-date-defintion-btn"><i class="fa fa-calendar-plus"></i> {{ _('Add event date') }}</button>
    </div>
  </div>

  <div class="card mb-4">
    <div class="card-header">
      {{ _('Status') }}
    </div>
    <div class="card-body">
      {{ render_field_with_errors(form.public_status, class="autocomplete w-100") }}
      {{ render_field_with_errors(form.status, class="autocomplete w-100") }}
      {{ render_field_with_errors(form.previous_start_date) }}
    </div>
  </div>

  <div class="card mb-4">
    <div class="card-header">
      {{ _('Organizer') }}
    </div>
    <div class="card-body pb-0">
      {{ render_field_with_errors(form.organizer_id, class="w-100", label_hidden=True) }}
      {{ render_field_with_errors(form.co_organizer_ids, class="w-100") }}
    </div>
  </div>

  <div class="card mb-4">
    <div class="card-header">
      {{ _('Place') }}
    </div>
    <div class="card-body pb-0">
      {{ render_field_with_errors(form.event_place_id, class="w-100", label_hidden=True) }}
    </div>
  </div>

  {{ render_crop_image_form_section(form.photo) }}

  <div class="card mb-4">
    <div class="card-header">
      {{ _('Access') }}
    </div>
    <div class="card-body">
      {{ render_field_with_errors(form.accessible_for_free, ri="switch") }}
      {{ render_field_with_errors(form.registration_required, ri="switch") }}
      {{ render_field_with_errors(form.price_info) }}
      {{ render_field_with_errors(form.ticket_link) }}
      {{ render_field_with_errors(form.booked_up, ri="switch") }}
      {{ render_field_with_errors(form.attendance_mode, class="autocomplete w-100") }}
    </div>
  </div>

  <div class="card mb-4">
    <div class="card-header">
      {{ _('Target group') }}
    </div>
    <div class="card-body">
      {{ render_field_with_errors(form.target_group_origin, class="autocomplete w-100") }}
      {{ render_field_with_errors(form.kid_friendly, ri="switch") }}
      {{ render_field_with_errors(form.age_from) }}
      {{ render_field_with_errors(form.age_to) }}
      {{ render_field_with_errors(form.expected_participants) }}
    </div>
  </div>

  <div class="card mb-4">
    <div class="card-header">
      {{ _('Additional information') }}
    </div>
    <div class="card-body">
      {{ render_field_with_errors(form.category_ids, class="autocomplete-multi w-100") }}
      {{ render_field_with_errors(form.tags) }}
      {{ render_field_with_errors(form.external_link) }}
    </div>
  </div>

  {% if form.rating.choices|length > 1 %}
  <div class="card mb-4">
    <div class="card-header">
      {{ _('Rating') }}
    </div>
    <div class="card-body">
      {{ render_field_with_errors(form.rating) }}
    </div>
  </div>
  {% endif %}

  {{ render_field(form.submit) }}

</form>

{% endblock %}
