{% extends "modeladmin/wagtail_personalisation/segment/base.html" %}
{% load i18n l10n staticfiles modeladmin_tags wagtail_personalisation_filters %}

{% block toggle_view %}to List {% endblock%}

{% block content_main %}
    <div>
        <div class="row">
            {% block content_cols %}

                {% block filters %}
                    {% if view.has_filters and all_count %}
                    <div class="changelist-filter col3">
                        <h2>{% trans 'Filter' %}</h2>
                        {% for spec in view.filter_specs %}{% admin_list_filter view spec %}{% endfor %}
                    </div>
                    {% endif %}
                {% endblock %}

                <div>
                    {% block result_list %}
                        <div class="nice-padding block_container">
                            {% if all_count %}
                                {% for segment in object_list %}
                                    <div class="block block--{{ segment.status }}" onclick="location.href = '{% url 'wagtail_personalisation_segment_modeladmin_edit' segment.pk %}'">
                                        <h2>{{ segment }}</h2>

                                        <div class="inspect_container">
                                            <ul class="inspect segment_stats">
                                                <li class="stat_card">
                                                    {% trans "This segment has been visited" %}
                                                    <span class="icon icon-fa-rocket">{{ segment.visit_count|localize }} {% trans "time" %}{{ segment.visit_count|pluralize }}</span>
                                                </li>
                                                <li class="stat_card">
                                                    {% trans "This segment has been active for" %}
                                                    <span class="icon icon-fa-calendar">{{ segment.enable_date|days_since:segment.disable_date }} {% trans "day" %}{{ segment.enable_date|days_since:segment.disable_date|pluralize }}</span>
                                                </li>
                                            {% if segment.is_static %}
                                                <li class="stat_card">
                                                    {% trans "This segment is Static" %}
                                                    <span class="icon icon-fa-user">
                                                        {{ segment.static_users.count|localize }}
                                                        {% if segment.static_users.count < segment.count %}
                                                        / {{ segment.count }} {% trans "member" %}{{ segment.count|pluralize }}
                                                        {% else %}
                                                            {% trans "member" %}{{ segment.count|pluralize }}
                                                        {% endif %}
                                                    </span>
                                                </li>
                                            {% endif %}
                                            </ul>

                                            <hr />

                                            <ul class="inspect segment_rules">
                                                <li class="stat_card {{ segment.match_any|yesno:"any,all" }}">
                                                    {% trans "The visitor must match" %}
                                                    {% if segment.match_any %}
                                                        <span class="icon icon-fa-cube">{% trans "Any rule" %}</span>
                                                    {% else %}
                                                        <span class="icon icon-fa-cubes">{% trans "All rules" %}</span>
                                                    {% endif %}
                                                </li>

                                                <li class="stat_card {{ segment.persistent|yesno:"persistent,fleeting" }}">
                                                    {% trans "The persistence of this segment is" %}
                                                    {% if segment.persistent %}
                                                        <span class="icon icon-fa-bookmark" title="{% trans "This segment persists in between visits" %}">{% trans "Persistent" %}</span>
                                                    {% else %}
                                                        <span class="icon icon-fa-bookmark-o" title="{% trans "This segment is reevaluated on every visit" %}">{% trans "Fleeting" %}</span>
                                                    {% endif %}
                                                </li>

                                                {% if segment.randomisation_percent is not None %}
                                                    <li class="stat_card">
                                                        <span>{{ segment.randomisation_percent }} %</span>
                                                        {% trans "Chance that visitors matching the rules are added to the segment" %}
                                                    </li>
                                                {% endif %}

                                                {% for rule in segment.get_rules %}
                                                    <li class="stat_card {{ rule.encoded_name }}">
                                                        {{ rule.description.title }}
                                                        {% if rule.description.code %}
                                                            <pre>{{ rule.description.value }}</pre>
                                                        {% else %}
                                                            <span class="icon icon-{{ rule.icon }}">{{ rule.description.value }}</span>
                                                        {% endif %}
                                                    </li>
                                                {% endfor %}
                                                {% if segment.matched_users_count > 0 %}
                                                    <li class="stat_card">
                                                        <span class="icon icon-fa-user"> {{ segment.matched_users_count }} {% trans "user" %}{{ segment.matched_users_count|pluralize }}</span>  {% trans "were possible matches for this segment at creation" %}
                                                    </li>
                                                {% endif %}
                                            </ul>
                                        </div>

                                        {% if user_can_create %}
                                        <ul class="block_actions">
                                            {% if segment.status == segment.STATUS_DISABLED %}
                                                <li><a href="{% url 'segment:toggle' segment.pk %}" title="{% trans "Enable this segment" %}">enable</a></li>
                                            {% elif segment.status == segment.STATUS_ENABLED %}
                                                <li><a href="{% url 'segment:toggle' segment.pk %}" title="{% trans "Disable this segment" %}">disable</a></li>
                                            {% endif %}
                                            <li><a href="{% url 'wagtail_personalisation_segment_modeladmin_edit' segment.pk %}" title="{% trans "Configure this segment" %}">configure this</a></li>
                                            {% if segment.is_static %}
                                                <li><a href="{% url 'segment:segment_user_data' segment.pk %}" title="{% trans "Download user info" %}">download users csv</a></li>
                                            {% endif %}
                                        </ul>
                                        {% endif %}
                                    </div>
                                {% endfor %}
                            {% endif %}

                            {% if user_can_create %}
                                {% blocktrans with url=view.create_url name=view.verbose_name %}
                                    <a class="block suggestion" href="{{ url }}">
                                        <span class="suggestive_text">Add a new {{name}}</span>
                                    </a>
                                {% endblocktrans %}
                            {% endif %}
                        </div>
                    {% endblock %}
                </div>

                {% block pagination %}
                    {% if paginator.num_pages > 1 %}
                    <div class="pagination {% if view.has_filters and all_count %}col9{% else %}col12{% endif %}">
                        <p>{% blocktrans with page_obj.number as current_page and paginator.num_pages as num_pages %}Page {{ current_page }} of {{ num_pages }}.{% endblocktrans %}</p>
                        <ul>
                            {% pagination_link_previous page_obj view %}
                            {% pagination_link_next page_obj view %}
                        </ul>
                    </div>
                    {% endif %}
                {% endblock %}

            {% endblock %}
        </div>
    </div>
{% endblock %}
