{% extends "base.html" %}

{% block body %}
<h1>Database: {{ name }}</h1>
<h2>Version: {{ meta.version }}</h2>

<div class="span-12">
    {% if depends %}
    <h2>Depends on:</h2>
    <ul>
        {% for d in depends %}
        <li><a href="{{ d.url }}">{{ d.name }}</a></li>
        {% endfor %}
    </ul>
    {% endif %}

    <p>
        <div id="backup-div"><button id="backup-button" class="positive">Backup</button></div>
        <div><a class="button positive" href="{{ health_check_url }}">Health check</a></div>
        <button id="delete-button" class="negative">Delete</button>
    </p>
</div>

<div class="span-12 last">
    <h2>Facet by:</h2>
    <div><a class="button positive" href="{{ location_facet_url }}">Location</a></div> <div><a class="button positive" href="{{ unit_facet_url }}">Unit</a></div>
</div>

<div class="clear"></div>

<div id="bgtable"></div>

<div id="delete-confirm" title="Delete database">
    <p>Are you sure you want to delete this database?</p>
</div>

<script type="text/javascript">
var columns = [{
    name: "name",
    label: "Name",
    cell: "string",
    editable: false
}, {
    name: "unit",
    label: "Unit",
    cell: "string",
    editable: false
}, {
    name: "categories",
    label: "Categories",
    cell: "string",
    editable: false
}, {
    name: "location",
    label: "Location",
    cell: "string",
    editable: false
}, {
    name: "num_exchanges",
    label: "Number of exchanges",
    cell: "integer",
    editable: false
}];

var data = {{ data|safe }};

var callback = function (model) {
    window.location = model.attributes.url;
};

BackgridTable(data, columns, "#bgtable", ['name'], "Filter by name", 50, callback);

$(document).ready(function() {
    $("#delete-confirm").dialog({
        autoOpen: false,
        modal: true,
        buttons: {
            "Cancel": function () {
                $(this).dialog("close");
            },
            "Delete": function () {
                var callback = function () {
                    window.location.href = "/";
                }
                $.post("{{ delete_url }}", "", callback);
            }
        }
    });
});

var backup_callback = function (fp) {
    $("#backup-button").hide();
    $("#backup-div").html("Database backup up to: " + fp);
}

$("#backup-button").click(function () {
    $("#backup-button").text("working...");
    $.post("{{ backup_url }}", "", backup_callback);
});

$("#delete-button").click(function () {
    $("#delete-confirm").dialog("open");
});
</script>
{% endblock %}
