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

{% block title %}{% trans "Cookbook Setup" %}{% endblock %}

{% block extra_head %}
    {{ form.media }}
{% endblock %}

{% block content %}

    <h1>{% trans 'System' %}</h1>
    {% blocktrans %}
        Django Recipes is an open source free software application. It can be found on
        <a href="https://github.com/vabene1111/recipes">GitHub</a>.
        Changelogs can be found <a href="https://github.com/vabene1111/recipes/releases">here</a>.
    {% endblocktrans %}

    <h3 class="mt-5">{% trans 'System Information' %}</h3>
    {% if version_info %}
        <div class="row">
            <div class="col">
                <div class="list-group">
                    {% for v in version_info %}
                        <div class="list-group-item list-group-item-action">
                            <div class="d-flex w-100 justify-content-between">
                                <h5 class="mb-1">{{ v.name }} ({{ v.branch }}) {% if v.tag %}- {{ v.tag }}{% endif %}</h5>
                            </div>
                            <pre class="card-text p-2" style="border: 1px solid lightgrey; border-radius: 5px" target="_blank">{{ v.version }}</pre>
                            <a href="{{ v.website }}">Website</a>
                            {% if v.commit_link %}
                                - <a href="{{ v.commit_link }}" target="_blank">Commit</a>
                            {% endif %}
                        </div>
                    {% endfor %}
                </div>
            </div>
        </div>
    {% else %}
        {% blocktrans %}
            You need to execute <code>version.py</code> in your update script to generate version information (done automatically in docker).
        {% endblocktrans %}
    {% endif %}

    <h4 class="mt-3">{% trans 'Media Serving' %} <span class="badge badge-{% if gunicorn_media %}danger{% else %}success{% endif %}">{% if gunicorn_media %}
        {% trans 'Warning' %}{% else %}{% trans 'Ok' %}{% endif %}</span></h4>
    {% if gunicorn_media %}
        {% blocktrans %}Serving media files directly using gunicorn/python is <b>not recommend</b>!
            Please follow the steps described
            <a href="https://github.com/vabene1111/recipes/releases/tag/0.8.1">here</a> to update
            your installation.
        {% endblocktrans %}
    {% else %}
        {% trans 'Everything is fine!' %}
    {% endif %}


    <h4 class="mt-3">{% trans 'Secret Key' %} <span
            class="badge badge-{% if secret_key %}danger{% else %}success{% endif %}">{% if secret_key %}
        {% trans 'Warning' %}{% else %}{% trans 'Ok' %}{% endif %}</span></h4>
    {% if secret_key %}
        {% blocktrans %}
            You do not have a <code>SECRET_KEY</code> configured in your <code>.env</code> file. Django defaulted to the
            standard key
            provided with the installation which is publicly know and insecure! Please set
            <code>SECRET_KEY</code> int the <code>.env</code> configuration file.
        {% endblocktrans %}
    {% else %}
        {% trans 'Everything is fine!' %}
    {% endif %}

    <h4 class="mt-3">{% trans 'Debug Mode' %} <span
            class="badge badge-{% if debug %}danger{% else %}success{% endif %}">{% if debug %}
        {% trans 'Warning' %}{% else %}{% trans 'Ok' %}{% endif %}</span></h4>
    {% if debug %}
        {% blocktrans %}
            This application is still running in debug mode. This is most likely not needed. Turn of debug mode by
            setting
            <code>DEBUG=0</code> int the <code>.env</code> configuration file.
        {% endblocktrans %}
    {% else %}
        {% trans 'Everything is fine!' %}
    {% endif %}

    <h4 class="mt-3">{% trans 'Database' %}
        <span class="badge badge-{{ postgres_status }}">
            {% if postgres_status == 'warning' %}
                {% trans 'Info' %}
            {% elif postgres_status == 'danger' %}
                {% trans 'Warning' %}
            {% else %}
                {% trans 'Ok' %}
            {% endif %}
        </span>
    </h4>
    {{ postgres_message }}

    <h4 class="mt-3">{% trans 'Migrations' %}
        <span
                class="badge badge-{% if missing_migration %}danger{% else %}success{% endif %}">{% if missing_migration %}
            {% trans 'Warning' %}{% else %}{% trans 'Ok' %}{% endif %}</span></h4>

    <p>
        {% blocktrans %}
            Migrations should never fail!
            Failed migrations will likely cause major parts of the app to not function correctly.
            If a migration fails make sure you are on the latest version and if so please post the migration log and the overview below in a GitHub issue.
        {% endblocktrans %}
    </p>

    <table class="table mt-3">
        <thead>
        <tr>
            <th>App</th>
            <th class="text-right">{% trans 'Migrations' %}</th>
        </tr>
        </thead>
        {% for key,value in migration_info.items %}
            <tr>

                <td>{{ value.app }}</td>
                <td class="text-right">
                     <span class="badge badge-{% if value.unapplied_migrations|length > 0 %}danger{% else %}success{% endif %}">
                         {{ value.applied_migrations|length }} / {{ value.total }}
                     </span>
                </td>
            </tr>
            {% for u in value.unapplied_migrations %}
                <tr>
                    <td>
                        {{ u }}
                    </td>
                    <td></td>
                </tr>
            {% endfor %}

        {% endfor %}
    </table>


    {#    <h4 class="mt-3">#}
    {#    {% trans 'Orphaned Files' %}#}
    {##}
    {#    <span class="badge badge-{% if orphans|length == 0 %}success{% elif orphans|length <= 25 %}warning{% else %}danger{% endif %}">#}
    {#        {% if orphans|length == 0 %}{% trans 'Success' %}#}
    {#        {% elif orphans|length <= 25 %}{% trans 'Warning' %}#}
    {#        {% else %}{% trans 'Danger' %}#}
    {#        {% endif %}#}
    {#    </span>#}
    {#    </h4>#}

    {#    {% if orphans|length == 0 %}#}
    {#        {% trans 'Everything is fine!' %}#}
    {#    {% else %}#}
    {#        {% blocktrans with orphan_count=orphans|length %}#}
    {#            There are currently {{ orphan_count }} orphaned files.#}
    {#        {% endblocktrans %}#}
    {#        <br>#}
    {#        <button id="toggle-button" class="btn btn-info btn-sm" onclick="toggleOrphans()">{% trans 'Show' %}</button>#}
    {#        <button class="btn btn-info btn-sm" onclick="deleteOrphans()">{% trans 'Delete' %}</button>#}
    {#    {% endif %}#}
    {#    <textarea id="orphans-list" style="display:none;" class="form-control" rows="20">#}
    {#{% for orphan in orphans %}{{ orphan }}#}
    {#{% endfor %}#}
    {#    </textarea>#}

    <h4 class="mt-3">Debug</h4>
    <textarea class="form-control" rows="20">
Gunicorn Media: {{ gunicorn_media }}
Sqlite: {% if postgres %} {% trans 'False' %} {% else %} {% trans 'True' %} {% endif %}
        {% if postgres %}PostgreSQL: {{ postgres_version }} {% endif %}
        Debug: {{ debug }}

{% for key,value in request.META.items %}{% if key in 'SERVER_PORT,REMOTE_HOST,REMOTE_ADDR,SERVER_PROTOCOL' %}{{ key }}:{{ value }}
{% endif %}{% endfor %}
{% for key,value in request.META.items %}{% if 'HTTP_' in key %}{{ key }}:{{ value }}
{% endif %}{% endfor %}
{% for key,value in request.META.items %}{% if 'wsgi.' in key %}{{ key }}:{{ value }}
{% endif %}{% endfor %}
    </textarea>
    <br/>
    <br/>
    <form method="POST" id="delete-form">
        {% csrf_token %}
        <input type="hidden" name="delete_orphans" value="false">
    </form>
    {% block script %}
        <script>
            function toggleOrphans() {
                var orphansList = document.getElementById('orphans-list');
                var button = document.getElementById('toggle-button');

                if (orphansList.style.display === 'none') {
                    orphansList.style.display = 'block';
                    button.innerText = "{% trans 'Hide' %}";
                } else {
                    orphansList.style.display = 'none';
                    button.innerText = "{% trans 'Show' %}";
                }
            }

            function deleteOrphans() {
                document.getElementById('delete-form').delete_orphans.value = 'true';
                document.getElementById('delete-form').submit();
            }
        </script>
    {% endblock script %}
{% endblock %}


