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

{% 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 'settings:basic-setting' %}" class="text-center"><i class="fa fa-cubes"></i> {% trans 'Basic setting' %}</a>
                            </li>
                            <li>
                                <a href="{% url 'settings:email-setting' %}"  class="text-center"><i class="fa fa-envelope"></i> {% trans 'Email setting' %} </a>
                            </li>
                            <li>
                                <a href="{% url 'settings:email-content-setting' %}"  class="text-center"><i class="fa fa-file-text"></i> {% trans 'Email content setting' %} </a>
                            </li>
                            <li class="active">
                                <a href="{% url 'settings:ldap-setting' %}" class="text-center"><i class="fa fa-archive"></i> {% trans 'LDAP setting' %} </a>
                            </li>
                            <li>
                                <a href="{% url 'settings:terminal-setting' %}" class="text-center"><i class="fa fa-hdd-o"></i> {% trans 'Terminal setting' %} </a>
                            </li>
                            <li>
                                <a href="{% url 'settings:security-setting' %}" class="text-center"><i class="fa fa-lock"></i> {% trans 'Security setting' %} </a>
                            </li>
                        </ul>
                    </div>
                    <div class="tab-content">
                        <div class="col-sm-12" style="padding-left:0">
                                <div class="ibox-content" style="border-width: 0;padding-top: 40px;">
                                    <form id="ldap_form" action="" method="post" class="form-horizontal">
                                    {% if form.non_field_errors %}
                                         <div class="alert alert-danger">
                                             {{ form.non_field_errors }}
                                         </div>
                                    {% endif %}
                                    {% csrf_token %}
                                    {% for field in form %}
                                        {% if not field.field|is_bool_field %}
                                            {% bootstrap_field field layout="horizontal" %}
                                        {% else %}
                                            <div class="form-group">
                                                <label for="{{ field.id_for_label }}" class="col-sm-2 control-label">{{ field.label }}</label>
                                                <div class="col-sm-8">
                                                    <div class="col-sm-1">
                                                        {{ field }}
                                                    </div>
                                                    <div class="col-sm-9">
                                                        <span class="help-block" >{{ field.help_text }}</span>
                                                    </div>
                                                </div>
                                            </div>
                                        {% endif %}
                                    {% endfor %}
                                    <div class="hr-line-dashed"></div>
                                    <div class="form-group">
                                        <div class="col-sm-4 col-sm-offset-2">
                                            <button class="btn btn-default" type="reset"> {% trans 'Reset' %}</button>
                                            <button class="btn btn-default btn-test" type="button"> {% trans 'Test connection' %}</button>
{#                                            <button class="btn btn-primary sync_button " data-toggle="modal" data-target="#sync_users_modal" type="button">{% trans 'Synchronization' %}</button>#}
                                            <button id="submit_button" class="btn btn-primary" type="submit">{% trans 'Submit' %}</button>
                                            <button class="btn btn-default sync_button " data-toggle="modal" data-target="#ldap_list_users_modal" type="button">{% trans 'Bulk import' %}</button>
                                        </div>
                                    </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% include 'settings/_ldap_list_users_modal.html' %}
{% endblock %}
{% block custom_foot_js %}
<script>
$(document).ready(function () {

})
.on("click", ".btn-test", function () {
    var data = {};
    var form = $("form").serializeArray();
    $.each(form, function (i, field) {
        data[field.name] = field.value;
    });

    var the_url = "{% url 'api-settings:ldap-testing' %}";

    function error(message) {
        toastr.error(message)
    }

    function success(message) {
        toastr.success(message.msg)
    }
    APIUpdateAttr({
        url: the_url,
        body: JSON.stringify(data),
        method: "POST",
        flash_message: false,
        success: success,
        error: error
    });
})
.on("click","#btn_ldap_modal_confirm",function () {
    var username_list=[];
    $("tbody input[type='checkbox']:checked").each(function () {
        username_list.push($(this).attr('id'));
    });

    if (username_list.length === 0){
        var msg = "{% trans 'User is not currently selected, please check the user you want to import'%}";
        toastr.error(msg);
        return
     }

    var the_url =  "{% url "api-settings:ldap-user-sync" %}";

    function error(message) {
        toastr.error(message)
    }

    function success(message) {
        toastr.success(message.msg)
    }
    APIUpdateAttr({
        url: the_url,
        body: JSON.stringify({'username_list':username_list}),
        method: "POST",
        flash_message: false,
        success: success,
        error: error
    });
 })

</script>
{% endblock %}
