{% extends "twocols.html" %}
{% load i18n static sfilters_tags %}

{% block pagetitle %}{% trans "Message filters" %}{% endblock %}

{% block extra_css %}
<link rel="stylesheet" type="text/css" href="{% static 'kalabash_sievefilters/css/style.css' %}" />
{% endblock %}

{% block extra_js %}
<script type="text/javascript" src="{% static 'js/history.js' %}"></script>
<script type="text/javascript" src="{% static 'kalabash_sievefilters/js/lib.js' %}"></script>
<script type="text/javascript" src="{% static 'js/dynamic_input.js' %}"></script>
<script type="text/javascript" src="{% static 'kalabash_sievefilters/js/jquery.dynfields.js' %}"></script>
<script type="text/javascript">
var histomanager = new History({
    deflocation: "{{ default_script }}",
    defcallback: loadfs
});
var cond_templates = null;
var action_templates = null;

function display_filter_errors(data) {
    $("ul.errors").remove();
    $.each(data.form_errors, function(id, value) {
        if (id.indexOf('cond') && id.indexOf('action')) {
            return;                     
        }
        var fullid = "id_" + (data.prefix ? data.prefix + "-" : "") + id;
        var $widget = $("#" + fullid);
        var ulid = fullid + "-error";
        var $ul = $("<ul />", {
            "class": "errors",
            "id": ulid
        }).append($("<li />", {
            html: value[0]
        }));
        $widget.after($ul);
    });
}

function filterform_cb() {
    cond_templates = fetch_templates("{% url 'kalabash_sievefilters:templates_get' 'condition' %}", cond_templates);
    action_templates = fetch_templates("{% url 'kalabash_sievefilters:templates_get' 'action' %}", action_templates);

    var $dc = $("#condition_0").dyncondition({
        templates: cond_templates
    }).data('dyncondition');
    var $da = $("#action_0").dynaction({
        templates: action_templates,
        hdelimiter: '{{ hdelimiter }}'
    }).data('dynaction');

    $("input[name=match_type]").click(function(e) {
        if ($(this).val() == "all") {
            $("div[id*=condition]").hide();
        } else {
            $("div[id*=condition]").show();
        }
    });

    $(".submit").on('click', function(e) {
        var counters = "conds=" + $dc.fields_cnt + "&actions=" + $da.fields_cnt;

        simple_ajax_form_post(e, {
            formid: 'filterform',
            reload_mode: 'history',
            extradata: counters,
            error_cb: display_filter_errors
        });
    });
}

$(document).ready(function() {
  $("#curfset").change(changefs);
});
</script>
{% endblock %}

{% block leftcol %}
<ul id="menu" class="nav nav-sidebar">
  <li class="nav-header">{% trans "Filter sets" %}</li>
  <li class="fset_container">
    <select id="curfset" class="form-control">
      {% if active_script %}
      <option value="{{ active_script }}">{{ active_script }} ({% trans "active" %})</option>
      {% endif %}
      {% for script in scripts %}
      <option value="{{ script }}">{{ script }}</option>
      {% endfor %}
    </select>
  </li>
  {# It will add a li item : #}
  {% sfilters_menu user %}
</ul>
{% endblock %}

{% block apparea %}
<div id="set_content">
  {% if not active_script and not scripts %}
  <div class="alert alert-info">{% trans "No filters set defined yet. You must create one in order to define filters." %}</div>
  {% endif %}
</div>
{% endblock %}
