{% load sekizai_tags %}
{% load events_programme %}
{% load community_utils %}

<div class="card {{ card_class }} contribution-card mb-0">
  <div class="card-body p-1" id="{{ base_id|default:'' }}-contribution-{{ contribution.pk }}-header">
    <div class="d-flex justify-content-between">
      <button data-bs-toggle="collapse" type="button" class="btn btn-sm w-100" data-bs-target="#{{ base_id|default:'' }}-contribution-{{ contribution.pk }}-collapse" aria-expanded="false" aria-controls="{{ base_id|default:'' }}-contribution-{{ contribution.pk }}-collapse">
        {% if contribution.start and contribution.duration %}
          {% if show_date or show_time %}
            <div class="card-title text-start">
              {% if show_date %}{% slot_date_time contribution %}{% else %}{% slot_time contribution %}{% endif %}
            </div>
          {% endif %}
        {% endif %}
        <h6 class="card-title text-start fw-bold">
          {{ contribution }}
        </h6>
        {% format_authors contribution.contributingauthor_set.all %}
      </button>
      {% if select_for_session %}
        {% csrf_token %}
        <div class="form-check form-switch">
          <input
           type="checkbox" {% if contribution.session == select_for_session %}checked{% endif %}
           class="form-check-input"
           role="switch"
           id="switch-contribution-{{ contribution.pk }}"
           data-rest-uri='{{ contribution.rest_uri }}' onclick="selectContribution(this)">
          <label class="form-check-label" for="switch-contribution-{{ contribution.pk }}"></label>
        </div>
      {% endif %}
    </div>
  </div>
  <div id="{{ base_id|default:'' }}-contribution-{{ contribution.pk }}-collapse" class="collapse" aria-labelledby="{{ base_id|default:'' }}-contribution-{{ contribution.pk }}-header">
    {% include "programme/components/contribution_card_body.html" %}
  </div>
</div>


{% addtoblock "js" %}
  {% if select_for_session %}
    <script>
      function selectContribution(cb) {
        var sessionId = {{ select_for_session.id }};
        var data = {
          session: cb.checked ? sessionId : null,
        };
        if (!cb.checked) {
          data.start = null;
        }
        $.ajax({
          url: cb.dataset.restUri,
          type: 'PATCH',
          data: data,
          headers:{'X-CSRFTOKEN':$('input[name="csrfmiddlewaretoken"]').val()},
          success: function (data) {
          }
        });
      }
    </script>
  {% endif %}
{% endaddtoblock %}
