{% extends 'assignment_desk/base.html' %}


{% load bootstrap3 dict_itemgetter static %}


{% block html_title %}Edit assignment list | {{ block.super }}{% endblock %}


{% block stylesheets %}
{{ block.super }}
{{ form.media }}
{% endblock %}


{% block body_class %}week-edit{% endblock %}


{% block back_nav_link %}{% url 'assignment-desk:week-list' %}{% endblock %}


{% block nav_header %}Edit list: {{ week.role_type.name }} department assignments starting {{ week.beginning_date|date:'N j' }}{% endblock %}


{% block right_nav_buttons %}{% endblock %}


{% block content %}
<form action="" method="POST">
    <div class="list-header">
        <h4>Assignments</h4>
    </div>

    <div class="fullscreen-card table-card assignment-table-card">
        {{ inline_formset.management_form }}

        {% regroup inline_formset by instance.role_id as formset_by_role %}

        <table id="assignment-forms" class="assignments-table">
            <thead>
                <tr data-role="days">
                    <th class="spacer"></th>
                    {% for assignment in formset_by_role.0.list %}
                        {% include "assignment_desk/partials/assignments_table/header_cell.html" with day=assignment.instance.day %}
                    {% endfor %}
                </tr>
            </thead>
            <tbody>

            {% for role_id, role_forms in formset_by_role %}
                <tr data-role="{{ role_crosswalk|get_item:role_id|get_item:'slug' }}">
                    <td class="role-name">{{ role_crosswalk|get_item:role_id|get_item:'name' }}</td>

                    {% for assignment_form in role_forms %}
                        {% include "assignment_desk/partials/assignments_table/body_cell.html" with day=assignment_form.instance.day staffer=assignment_form.staffer.value notes=assignment_form.notes.value as_form=True %}
                    {% endfor %}
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

    <div class="fullscreen-card week-primary-card">
        {% bootstrap_form_errors form %} {% if form.non_field_errors %}
        <div class="non-field-error-padding">
            <ul class="non-field-errors knockout">
                {% for error_message in form.non_field_errors %}
                <li class="form-error">{{ error_message }}</li>
                {% endfor %}
            </ul>
        </div>
        {% endif %} {% bootstrap_formset_errors inline_formset %}

        <div class="form-group-container form-input--full" id="form__extra-information">
            {% bootstrap_field form.extra_information layout='immaterial' %}
        </div>

        {% csrf_token %}
    </div>

    <div class="edit-actions">
        <div class="wrap">
            <div class="right-buttons">
                {% bootstrap_button 'Save' extra_classes='btn-primary' button_type='submit' name="submit" value="_save" %}
                {% bootstrap_button 'Save & continue editing' extra_classes='btn-primary-flat' button_type='submit' name="submit" value="_continue" %}
            </div>
        </div>
    </div>
</form>
{% endblock %}

{% block scripts %}
<script type="text/javascript">
    window.stafferList = {{ staffers|safe }};
</script>

<script src="{% static 'assignment_desk/js/week.edit.js' %}"></script>
{% endblock %}
