{% extends "core/base.jinja" %}
{% from 'core/macros.jinja' import user_profile_link %}

{% block title %}
  {% if object %}
    {% trans %}Edit news{% endtrans %}
  {% else %}
    {% trans %}Create news{% endtrans %}
  {% endif %}
{% endblock %}

{% block content %}
  {% if object %}
    <h2>{% trans %}Edit news{% endtrans %}</h2>
  {% else %}
    <h2>{% trans %}Create news{% endtrans %}</h2>
  {% endif %}
  <form action="" method="post">
    {% csrf_token %}
    {{ form.non_field_errors() }}
    <fieldset>
      {{ form.title.errors }}
      {{ form.title.label_tag() }}
      {{ form.title }}
    </fieldset>
    <fieldset>
      {{ form.club.errors }}
      {{ form.club.label_tag() }}
      <span class="helptext">{{ form.club.help_text }}</span>
      {{ form.club }}
    </fieldset>
    {{ form.date_form.non_field_errors() }}
    <div
      class="row gap-2x"
      x-data="{startDate: '{{ form.date_form.start_date.value() }}'}"
    >
        {# startDate is used to dynamically ensure end_date >= start_date,
        whatever the value of start_date #}
      <fieldset>
        {{ form.date_form.start_date.errors }}
        {{ form.date_form.start_date.label_tag() }}
        <span class="helptext">{{ form.date_form.start_date.help_text }}</span>
        {{ form.date_form.start_date|add_attr("x-model=startDate") }}
      </fieldset>
      <fieldset>
        {{ form.date_form.end_date.errors }}
        {{ form.date_form.end_date.label_tag() }}
        <span class="helptext">{{ form.date_form.end_date.help_text }}</span>
        {{ form.date_form.end_date|add_attr(":min=startDate") }}
      </fieldset>
    </div>
    {# lower to convert True and False to true and false #}
    <div x-data="{isWeekly: {{ form.date_form.is_weekly.value()|lower }}}">
      <fieldset>
        <div class="row gap">
          {{ form.date_form.is_weekly|add_attr("x-model=isWeekly") }}
          <div>
            {{ form.date_form.is_weekly.label_tag() }}
            <span class="helptext">{{ form.date_form.is_weekly.help_text }}</span>
          </div>
        </div>
      </fieldset>
      <fieldset x-show="isWeekly" x-transition x-cloak>
        {{ form.date_form.occurrences.label_tag() }}
        <span class="helptext">{{ form.date_form.occurrences.help_text }}</span>
        {{ form.date_form.occurrences }}
      </fieldset>
    </div>
    <fieldset>
      {{ form.summary.errors }}
      {{ form.summary.label_tag() }}
      <span class="helptext">{{ form.summary.help_text }}</span>
      {{ form.summary }}
    </fieldset>
    <fieldset>
      {{ form.content.errors }}
      {{ form.content.label_tag() }}
      <span class="helptext">{{ form.content.help_text }}</span>
      {{ form.content }}
    </fieldset>
    {% if user.is_root or user.is_com_admin %}
      <fieldset>
        {{ form.auto_publish.errors }}
        {{ form.auto_publish }}
        {{ form.auto_publish.label_tag() }}
      </fieldset>
    {% endif %}
    <p><input type="submit" value="{% trans %}Save{% endtrans %}" class="btn btn-blue"/></p>
  </form>
{% endblock %}
