{% extends 'base.html' %}
{% load filter_tags %}

{% block bodydata %}
data-project-id="{{ project.id }}"
data-project-locked="{{ project.islocked }}"
data-subset-id="{{ subset.id }}"
{% endblock %}

{% block title %} | Project: {{ project.name }} - Subset: {{ subset.name }} - Data{% endblock %}

{% block main %}
<div class="page-header">
    <div class="container">
        <h1 class="smaller">
            {% if project.islocked %}<span class="glyphicon glyphicon-lock" aria-hidden="true"></span>{% endif %}
            <a href="{% url 'admin:project_overview' project.id %}">{{ project.name }}</a>
        </h1>

        {% include 'projects/navigation.html' %}
    </div>
</div>

<div class="container">
    {% include 'snippets/messages.html' %}

    <div class="row">
        <div class="col-sm-4">
            {% include 'subsets/subset_navigation.html' %}
        </div>

        <div class="col-sm-8">
            <h3 class="header">
                {% if project.islocked %}<span class="glyphicon glyphicon-lock" aria-hidden="true"></span>{% endif %}
                Subset data
            </h3>

            {% if project.islocked %}
                <div class="alert alert-info">
                    <p>The project is locked, therefore subset data cannot be edited. If you wish to edit it, you have to unlock the project in the <a href="{% url 'admin:project_settings' project.id %}"><strong>project settings</strong></a> first.</p>
                </div>
            {% elif not project.categories.all %}
                <div class="alert alert-info">
                    <p>The project has no categories, therefore subset data cannot be edited. If you wish to edit it, you have to <a href="{% url 'admin:category_create' project.id %}"><strong>create a new category</strong></a> first.</p>
                </div>
            {% endif %}

            {% if project.categories.all %}
                <form id="data-access" method="POST" action="{% url 'admin:subset_data' project.id subset.id %}" novalidate>
                    {% csrf_token %}

                    <input type="hidden" name="filters" value="-1">

                    <fieldset id="filter">
                        {% if not project.islocked %}<p>Select categories that will be accessible through the subset:</p>{% endif %}

                        {% for category in project.categories.all %}
                            <div class="category checkbox">
                                <label>
                                    <input type="checkbox" class="category" name="category" value="{{ category.id }}" {% if subset.filters|is_in:category.id %}checked="checked"{% endif %} {% if project.islocked %}disabled{% endif %} />
                                    {{ category.name }}
                                    {% if not project.islocked %}{% show_restrict subset.filters category %}{% endif %}
                                </label>

                                {% if subset.filters|is_in:category.id %}
                                    {% show_fields subset.filters category %}
                                {% endif%}
                            </div>
                        {% endfor %}
                    </fieldset>

                    {% if not project.islocked %}
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">Save</button>
                        </div>
                    {% endif %}
                </form>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}

{% block libraries %}
<script src="/static/lib/moment/moment.min.js"></script>

<script src="/static/lib/handlebars/handlebars.js"></script>
<script src="/static/js/templates.js"></script>

<link rel="stylesheet" href="/static/lib/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css">
<script src="/static/lib/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js"></script>

<script src="/static/js/admin.ui.filters.data.js"></script>
<script src="/static/js/admin.ui.forms.validate.js"></script>
<script src="/static/js/admin.control.ajax.js"></script>
{% endblock %}
