{% extends "main/layout.html" %}
{% from 'global_macros.html' import data, preload_circle, select %}

{% block page_vendor_css %}
{% endblock page_vendor_css %}

{% block page_lvl_css %}
    {{ process_css_sources(src="main/home.css")|safe }}
    {% if settings.background and settings.background != 'None' %}
        <style>
            #main {
                background-image: url("{{ settings.background }}");
                background-size: cover;
                background-attachment: fixed;
                min-height: 100vh;
            }
        </style>
    {% endif %}
{% endblock page_lvl_css %}

{% block content %}
    <div id="main" class="main-full">
        <div class="container">
            <div class="row card-filter-container">
                <div class="col s12 l4 input-field">
                    <span>
                        <i class="material-icons prefix card-search-icon">search</i>
                        <input type="text" id="apps-filter" class="card-filter theme-surface-transparent" placeholder="Search apps">
                    </span>
                </div>
                {% if tags_form.tags.choices|count > 1 %}
                    <div class="input-field col s12 l2 tags-select-col theme-surface-transparent">
                        {{ tags_form.tags(id='tags-select') }}
                    </div>
                {% endif %}
            </div>
            <div class="row">
                {% if apps %}

                    {% for app in apps %}
                        {% if app.open_in == 'iframe' %}
                            <a href="{{ url_for('main.app_view', app_id=app.id) }}" class="app-a" data-name="{{ app.name }}" data-description="{{ app.description }}" data-tags="{{ app.tags }}">
                        {% elif app.open_in == 'this_tab' %}
                            <a href="{{ app.prefix }}{{ app.url }}" class="app-a" data-name="{{ app.name }}" data-description="{{ app.description }}" data-tags="{{ app.tags }}">
                        {% elif app.open_in == "new_tab" %}
                            <a href="{{ app.prefix }}{{ app.url }}" target="_blank" class="app-a" data-name="{{ app.name }}" data-description="{{ app.description }}" data-tags="{{ app.tags }}">
                        {% endif %}
                    <div class="col s12 m6 l3">
                        <div class="card theme-surface-transparent app-card">
                            <div class="card-content center-align scrollbar" style="max-height: 118px; min-height: 118px; scrollbar-width: none;">
                                {% if app.data_sources.count() > 0 %}
                                    <div class="row">
                                        <div class="col s6 center-align">
                                            <img src="{{ app.icon }}" height="64px">
                                        </div>

                                        <div class="col s6 left-align">
                                            <span class="data-source-loading">{{ preload_circle() }}</span>
                                            {% for data_source in app.data_sources %}
                                                <p class="data-source-container"
                                                   data-url="{{ url_for('main.load_data_source') }}"
                                                   data-id="{{ data_source.id }}">
                                                </p>
                                            {% endfor %}
                                        </div>
                                    </div>
                                {% else %}
                                    <img src="{{ app.icon }}" height="64px">
                                {% endif %}
                            </div>
                            <div class="card-action center-align scrollbar" style="max-height: 127px; min-height: 127px; scrollbar-width: none;">
                                <h5>{{ app.name }}
                                </h5>
                                <span class="theme-secondary-text">{{ app.description }}</span>
                            </div>
                        </div>
                    </div>
                    </a>
                    {% endfor %}
                {% else %}
                    <a href="{{ url_for('settings_system.settings') }}">
                        <div class="col s12 m6 l3">
                            <div class="card theme-surface-transparent">
                                <div class="card-action center-align">
                                    <h5>No apps yet, go to settings.</h5>
                                </div>
                            </div>
                        </div>
                    </a>
                {% endif %}
            </div>
        </div>
    </div>
{% endblock content %}

{% block page_vendor_js %}
{% endblock page_vendor_js %}

{% block page_lvl_js %}
    {{ process_js_sources(src="main/home.js")|safe }}
{% endblock page_lvl_js %}