{% extends 'base.html' %}
{% load crispy_forms_tags %}

{% block content %}
    <div class='row'>
        <div class='col-xl-4'>
            <div class="card card-primary card-outline">
                <div class="card-header">
                    <h3 class="card-title">
                        <a href="/router/details/?uuid={{ backup.router.uuid }}" >
                            {{ backup.router.name }}
                        </a>

                    </h3>
                    {% comment %}
                    <span class="float-right">
                        <a href="/router/manage/?uuid={{ backup.router.uuid }}" >
                            <i class="fas fa-edit"></i>
                        </a>
                    </span>
                    {% endcomment %}
                </div>

                <div class="card-body row">
                    <div class="col-lg-12">

                        <ul class="list-group list-group-unbordered mb-3">
                            {% comment %}
                            <li class="list-group-item">
                                <b></b>
                                <span class="float-right">{{ backup }}</span>
                            </li>
                            {% endcomment %}
                            <li class="list-group-item">
                                <b>Backup ID</b>
                                <span class="float-right">
                                    {{ backup.id }}
                                </span>
                            </li>


                            {% if not backup.success and not backup.error %}
                                <li class="list-group-item">
                                    <b><i class="far fa-question-circle" title="This debug information is only displayed while the backup is not finished."></i> Backup Status</b>
                                    <span class="float-right">
                                    {% if backup.success %}
                                        <i class="far fa-check-circle text-success"></i>
                                    {% elif backup.error %}
                                        <i class="far fa-times-circle text-danger"
                                           title="{{ backup.error_message }}"></i>
                                    {% elif backup.backup_pending_retrieval %}
                                        <i class="fas fa-cloud-download-alt text-info"
                                           title="Backup pending Retrieval"></i>
                                    {% else %}
                                        <i class="far fa-clock text-warning"></i>
                                    {% endif %}
                                </span>
                                </li>

                                <li class="list-group-item">
                                    <b><i class="far fa-question-circle" title="This debug information is only displayed while the backup is not finished."></i> Host Monitoring</b>
                                    <span class="float-right">
                                    {% if backup.router.monitoring %}
                                        {% if backup.router.routerstatus.status_online %}
                                            <i class="far fa-check-circle text-success"></i>
                                        {% else %}
                                            <i class="far fa-times-circle text-danger"></i> Host offline. Waiting for it to come online
                                        {% endif %}
                                    {% else %}
                                        Not enabled
                                    {% endif %}
                                </span>
                                </li>
                                <li class="list-group-item">
                                    <b><i class="far fa-question-circle" title="This debug information is only displayed while the backup is not finished."></i> Retry Count</b>
                                    <span class="float-right">
                                        <span {% if backup.retry_count > 0 %}class="text-danger"{% endif %}>
                                            {{ backup.retry_count }}
                                        </span>
                                </span>
                                </li>

                                <li class="list-group-item">
                                    <b><i class="far fa-question-circle" title="This debug information is only displayed while the backup is not finished."></i> Next Retry</b>
                                    <span class="float-right">
                                    {% if backup.next_retry %}
                                        {% if backup.next_retry > now %}
                                            <a href="/backup/backup_details/?uuid={{ backup.uuid }}&action=anticipate">
                                            (anticipate)
                                            </a>
                                            <span class="text-success" style="text-decoration: underline" title="This backup is scheduled to execute after {{ backup.next_retry }}">
                                                 {{ backup.next_retry }} <i class="far fa-clock"></i>
                                            </span>
                                        {% else %}
                                            <span class="text-info" style="text-decoration: underline" title="This backup is queued to be executed as soon as possible.">
                                                {{ backup.next_retry }} <i class="far fa-clock"></i>
                                            </span>
                                        {% endif %}
                                    {% else %}
                                        <span class="text-info" style="text-decoration: underline" title="This backup is queued to be executed as soon as possible.">
                                                queued <i class="far fa-clock"></i>
                                        </span>
                                    {% endif %}
                                    
                                    
                                </span>
                                </li>

                                <li class="list-group-item">
                                    <b><i class="far fa-question-circle" title="This debug information is only displayed while the backup is not finished."></i> Cron last run</b>
                                    <span class="float-right">
                                    {% if webadmin_settings.cron_last_run %}
                                        {% if webadmin_settings.cron_last_run < 5_minutes_ago %}
                                            <span class="text-danger" style="text-decoration: underline" title="Your cron tasks are not being executed properly.">
                                            {{ webadmin_settings.cron_last_run }}
                                            </span>
                                        {% else %}

                                            {{ webadmin_settings.cron_last_run }}
                                        {% endif %}
                                    {% endif %}
                                </span>
                                </li>

                                <li class="list-group-item">
                                    <b><i class="far fa-question-circle" title="This debug information is only displayed while the backup is not finished."></i> Error message</b>
                                    <span class="float-right">
                                    {{ backup.error_message|default_if_none:"" }}
                                </span>
                                </li>


                            {% endif %}

                            <li class="list-group-item">
                                <b>Schedule Type</b>
                                <span class="float-right">
                                    {{ backup.get_schedule_type_display }}
                                </span>
                            </li>
                            <li class="list-group-item">
                                <b>Task Created</b>
                                <span class="float-right">{{ backup.created }}</span>
                            </li>
                            <li class="list-group-item">
                                <b>Schedule Time</b>
                                <span class="float-right">{{ backup.schedule_time|default_if_none:"" }}</span>
                            </li>

                            <li class="list-group-item">
                                <b>Finish Time</b>
                                <span class="float-right">{{ backup.finish_time|default_if_none:"" }}</span>
                            </li>

                            <li class="list-group-item">
                                <b>Queue length</b>
                                <span class="float-right">{{ backup.queue_length }}s</span>
                            </li>

                            <li class="list-group-item">
                                <b>Backup hash</b>
                                <span class="float-right" title="{{ backup.backup_text_hash }}">{{ backup.backup_text_hash|slice:":16" }}...</span>
                            </li>

                            <li class="list-group-item">
                                <b>Download</b>
                                <span class="float-right">
                                {% if backup.backup_text %}
                                    <a href="/backup/download/?uuid={{ backup.uuid }}&type=text" download>
                                    Text
                                </a>
                                    {% if backup.backup_binary %} - {% endif %}
                                {% endif %}
                                    {% if backup.backup_binary %}
                                        <a href="/backup/download/?uuid={{ backup.uuid }}&type=binary" download>
                                    Binary
                                </a>
                                    {% endif %}
                                </span>
                            </li>

                            <li class="list-group-item">
                                <b>Delete backup</b>
                                <span class="float-right">

                                <a href='javascript:void(0)' class='text-danger' data-command='delete' onclick='openCommandDialog(this)'>
                                    <i class="far fa-trash-alt"></i>
                                </a>

                                </span>
                            </li>


                        </ul>
                    </div>
                </div>
            </div>

            <div class="card card-primary card-outline">
                <div class="card-header">
                    <h3 class="card-title">
                        Latest Configuration changes
                    </h3>

                </div>

                <div class="card-body row">
                    <div class="col-lg-12">
                        <table class="table">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>Date</th>
                                <th>Compare</th>

                            </tr>
                            </thead>
                            <tbody>
                            {% for compare_backup in backup_list %}
                                <tr>
                                    <td class="min-width">
                                        <a href="/backup/backup_details/?uuid={{ compare_backup.uuid }}">
                                            {{ compare_backup.id }}
                                        </a>
                                    </td>
                                    <td>
                                        {% if compare_backup.finish_time %}
                                            {{ compare_backup.finish_time }}
                                        {% else %}
                                            {{ compare_backup.created }}
                                        {% endif %}

                                    </td>

                                    <td>
                                        <a href="/backup/compare/?uuid={{ backup.uuid }}&compare_uuid={{ compare_backup.uuid }}">
                                            Compare
                                        </a>
                                    </td>
                                </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>

                </div>
            </div>
        </div>
        <div class='col-xl-8'>
            <div class="card card-primary card-outline">

                <div class="card-header">
                    <h3 class="card-title">Text Backup</h3>
                </div>

                <div class="card-body row">
                    <div class="col-lg-12">
                        <pre>{{ backup.backup_text|default_if_none:"" }}</pre>
                    </div>
                </div>
            </div>
        </div>


    </div>
{% endblock %}

{% block custom_page_scripts %}

    <script>
        function openCommandDialog(element) {
            var command = element.getAttribute('data-command');
            var confirmation = prompt("Please type 'delete{{ backup.id }}' to proceed.");
            if (confirmation) {
                var url = "/backup/delete/?uuid={{ backup.uuid }}&action=delete&confirmation=" + encodeURIComponent(confirmation);
                window.location.href = url;
            }
        }
    </script>


{% endblock %}