{% extends 'base.html' %}
{% load static %}
{% load i18n %}

{% block custom_head_css_js %}
    <link href="{% static 'css/plugins/select2/select2.min.css' %}" rel="stylesheet">
    <script src="{% static 'js/plugins/select2/select2.full.min.js' %}"></script>
{% endblock %}
{% block content %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="panel-options">
                        <ul class="nav nav-tabs">
                            <li>
                                <a href="{% url 'perms:database-permission-detail' pk=database_permission.id %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'Detail' %} </a>
                            </li>
                            <li>
                                <a href="{% url 'perms:database-permission-user-list' pk=database_permission.id %}" class="text-center">
                                    <i class="fa fa-group"></i> {% trans 'Users and user groups' %}
                                </a>
                            </li>
                            <li class="active">
                                <a href="{% url 'perms:database-permission-database-list' pk=database_permission.id %}" class="text-center">
                                    <i class="fa fa-database"></i> {% trans 'Database' %}</a>
                            </li>
                        </ul>
                    </div>
                    <div class="tab-content">
                        <div class="col-sm-8" style="padding-left: 0;">
                            <div class="ibox float-e-margins">
                                <div class="ibox-title">
                                    <span style="float: left">{% trans 'Database list of ' %} <b>{{ database_permission.name }}</b></span>
                                    <div class="ibox-tools">
                                        <a class="collapse-link">
                                            <i class="fa fa-chevron-up"></i>
                                        </a>
                                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                            <i class="fa fa-wrench"></i>
                                        </a>
                                        <ul class="dropdown-menu dropdown-user">
                                        </ul>
                                        <a class="close-link">
                                            <i class="fa fa-times"></i>
                                        </a>
                                    </div>
                                </div>
                                <div class="ibox-content">
                                    <table class="table table-hover">
                                        <thead>
                                        <tr>
                                            <th>{% trans 'Name' %}</th>
                                            <th>{% trans 'Type' %}</th>
                                            <th>{% trans 'Host' %}</th>
                                            <th>{% trans 'Port' %}</th>
                                            <th>{% trans 'User' %}</th>
                                            <th></th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        {% for database in object_list %}
                                            <tr>
                                                <td>{{ database.name }}</td>
                                                <td>{{ database.get_type_display }}</td>
                                                <td>{{ database.host }}</td>
                                                <td>{{ database.port }}</td>
                                                <td>{{ database.user }}</td>
                                                <td>
                                                    <button data-did="{{ database.id }}" class="btn btn-danger btn-xs btn-remove-database" type="button" style="float: right;"><i class="fa fa-minus"></i></button>
                                                </td>
                                            </tr>
                                        {% endfor %}
                                        </tbody>
                                    </table>
                                    <div class="row">
                                        {% include '_pagination.html' %}
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-4" style="padding-left: 0;padding-right: 0">
                            <div class="panel panel-primary">
                                <div class="panel-heading">
                                    <i class="fa fa-info-circle"></i> {% trans 'Add database to this permission' %}
                                </div>
                                <div class="panel-body">
                                    <table class="table">
                                        <tbody>
                                        <form>
                                            <tr class="no-borders-tr">
                                                <td colspan="2">
                                                    <select data-placeholder="{% trans 'Select database' %}" class="select2" id="database_select2" style="width: 100%" multiple="" tabindex="4">
                                                        {% for database in database_remain %}
                                                            <option value="{{ database.id }}">{{ database }}</option>
                                                        {% endfor %}
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr class="no-borders-tr">
                                                <td colspan="2">
                                                    <button type="button" class="btn btn-primary btn-sm btn-add-database">{% trans 'Add' %}</button>
                                                </td>
                                            </tr>
                                        </form>
                                        </tbody>
                                    </table>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

{% endblock %}
{% block custom_foot_js %}
<script>
function addDatabase(databases) {
    var the_url = "{% url 'api-perms:database-permission-add-database' pk=database_permission.id %}";
    var body = {
        databases: databases
    };
    var success = function(data) {
        location.reload();
    };
    requestApi({
        url: the_url,
        body: JSON.stringify(body),
        success: success
    });
}
function removeDatabase(databases) {
    var the_url = "{% url 'api-perms:database-permission-remove-database' pk=database_permission.id %}";
    var body = {
        databases: databases
    };
    var success = function(data) {
        location.reload();
    };
    requestApi({
        url: the_url,
        body: JSON.stringify(body),
        success: success
    });
}
$(document).ready(function () {
    $('.select2').select2();
})
    .on('click', '.btn-add-database', function () {
        var databases_selected = $("#database_select2 option:selected").map(function () {
            return $(this).attr('value');
        }).get();
        if (databases_selected.length === 0) {
            return false;
        }
        addDatabase(databases_selected);
    })
    .on('click', '.btn-remove-database', function () {
        var database_id= $(this).data("did");
        if (database_id === "") {
            return
        }
        var databases = [database_id];
        removeDatabase(databases)
    })
</script>
{% endblock %}
