{% extends "base.html" %}
{% import '_macros.html' as macros %}

{% block title %}{{ _("Administration") }} | {{ super() }}{% endblock %}

{% block content %}


<h3>{{ _("Graph properties") }}</h3>

<form role="form" method="post" class="form-inline">
    <input type="hidden" name="formname" value="graphprops" />
    {{ graphprops_form.hidden_tag() }}

    {{ macros.render_field_p(graphprops_form.name, class="input-sm") }}
    {{ macros.render_field_p(graphprops_form.center_id, class="form-control input-sm") }}
    {{ macros.render_field_p(graphprops_form.anonymous, class="form-control input-sm") }}
    <p><button type="submit" class="btn btn-primary">{{ _("Change") }}</button></p>

</form>

<hr />

<h3>{{ _("Relationship types") }}</h3>

<form role="form" method="post" class="form-table"
      action="{{ url_for('admin', graph_id=graph.id) }}">
{{ reltypes_form.hidden_tag() }}
<input type="hidden" name="formname" value="reltypes" />
<table class="table table-centered">
    <thead>
        <tr>
            <th>{{ _("Name") }}</th>
            <th>{{ _("Color") }}</th>
            <th>{{ _("Count") }}</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            {{ macros.render_field_table(reltypes_form.name) }}
            {{ macros.render_field_table(reltypes_form.color, class="color") }}
            <td></td>
            <td>
                <button type="submit" name="action" value="add"
                        class="btn btn-sm btn-primary">
                    <span class="glyphicon glyphicon-plus"></span>
                </button>
            </td>
        </tr>
        {% for reltype in rel_types %}
        <tr>
            <td>
                <input type="hidden" name="origname" value="{{ reltype.name }}" />
                <input type="text" name="name" class="form-control input-sm" value="{{ reltype.name }}" />
            </td>
            <td>
                <input type="color" name="color" class="form-control input-sm color" value="{{ reltype.color }}" />
            </td>
            <td class="text-center">
                {{ reltype.rel_count }}
            </td>
            <td>
                <button name="action" value="edit" data-loading-text="..."
                        class="btn btn-sm btn-info" title="{{ _("Update") }}">
                    <span class="glyphicon glyphicon-edit"></span>
                </button>
                <button name="action" value="delete" data-loading-text="..."
                        class="btn btn-sm btn-warning" title="{{ _("Delete") }}"
                        {% if reltype.rel_count != 0 %}
                        data-confirm="{{ _("You are deleting %(count)s relationships.", count=reltype.rel_count) }}"
                        {% endif %}
                        >
                    <span class="glyphicon glyphicon-remove"></span>
                </button>
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>
</form>


<hr />

<h3>{{ _("Authorizations") }}</h3>

<form role="form" method="post" class="form-table"
      action="{{ url_for('admin', graph_id=graph.id) }}">
{{ newaccess_form.hidden_tag() }}
<input type="hidden" name="formname" value="permissions" />
<table class="table table-centered">
    <thead>
        <tr>
            <th>{{ _("Name") }}</th>
            <th>{{ _("Connection email") }}</th>
            <th>{{ _("Access level") }}</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td></td>
            {{ macros.render_field_table(newaccess_form.email) }}
            {{ macros.render_field_table(newaccess_form.level, class="form-control input-sm") }}
            <td>
                <button type="submit" name="action" value="add" title="{{ _("Add") }}"
                        class="btn btn-sm btn-primary">
                    <span class="glyphicon glyphicon-plus"></span>
                </button>
            </td>
        </tr>
        {% for perm in permissions %}
        <tr>
            <td>{% if perm.user.name %}{{ perm.user.name }}{% endif %}</td>
            <td>{{ perm.user_email }}</td>
            <td>
                <select name="level" class="form-control input-sm">
                    {% for level in perm_choices %}
                    <option value="{{ level[0] }}"
                        {% if level[0] == perm.level %} selected {% endif %}
                        >{{ level[1] }}</option>
                    {% endfor %}
                </select>
            </td>
            <td>
                <input type="hidden" name="email" value="{{ perm.user_email }}" />
                <button name="action" value="edit" data-loading-text="..."
                        class="btn btn-sm btn-info" title="{{ _("Update") }}">
                    <span class="glyphicon glyphicon-edit"></span>
                </button>
                <button name="action" value="delete" data-loading-text="..."
                        class="btn btn-sm btn-warning" title="{{ _("Delete") }}">
                    <span class="glyphicon glyphicon-remove"></span>
                </button>
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>
</form>

<hr />

<h3>{{ _("Import / Export") }}</h3>

<div class="col-md-6 text-center">
    <a class="btn btn-primary"
       href="{{ url_for("export_graph", graph_id=graph.id) }}">{{ _("Export graph") }}</a>
</div>
<form role="form" method="post" class="form-inline col-md-6 text-center"
      action="{{ url_for('admin', graph_id=graph.id) }}"
      enctype="multipart/form-data">
    <input type="hidden" name="formname" value="graphimport" />
    <input type="file" class="form-control" name="import"/>
    <button type="submit" class="btn btn-primary">{{ _("Import graph") }}</button>
</form>
<div class="clearfix"></div>

<hr />

<h3>{{ _("Deletion") }}</h3>

<p>
    <form role="form" method="post" class="form-delete"
          action="{{ url_for('admin', graph_id=graph.id) }}">
    <input type="hidden" name="formname" value="graphdel" />
    <button type="submit" class="btn btn-danger"
        data-confirm="{{ _("Undoing this is impossible. Go on?") }}"
        >{{ _("Delete the graph") }}</button>
    </form>
</p>

{% endblock %}


{% block more_headers %}
<link rel="stylesheet" href="{{ url_for('static', filename='spectrum/spectrum.css') }}" />
{% endblock %}
{% block more_js %}
<script src="{{ url_for('static', filename='spectrum/spectrum.js') }}"></script>
<script>
    $(function() {
        $("form.form-delete button").click(function(e) {
            var response = confirm($(this).attr("data-confirm"));
            if (!response) {
                e.preventDefault();
            }
        });
    });
</script>
{% endblock %}
