{% extends 'base.html' %}

{% block content %}
    <div class='row'>
        <div class='col-xl-5'>
            <div class="card card-primary card-outline">
                <div class="card-header">
                    <h3 class="card-title">{{ router.name }}
                    </h3>
                    <span class="float-right">
                        <a href="/router/manage/?uuid={{ router.uuid }}" >
                            <i class="fas fa-edit"></i>
                        </a>
                    </span>
                </div>
                <div class="card-body row">
                    <div class="col-lg-12">
                        <ul class="list-group list-group-unbordered mb-3">
                            <li class="list-group-item">
                                <b>Enabled</b>
                                <span class="float-right">
                                    {% if router.enabled %}
                                        <i class="far fa-check-circle text-success" ></i>
                                    {% else %}
                                        <i class="far fa-times-circle text-danger" ></i>
                                    {% endif %}
                                </span>
                            </li>
                            <li class="list-group-item">
                                <b>Address</b>
                                <span class="float-right">{{ router.address }}</span>
                            </li>
                            {% if router.router_type != 'monitoring' %}
                            <li class="list-group-item">
                                <b>username</b>
                                <span class="float-right">{{ router.username }}</span>
                            </li>
                            <li class="list-group-item">
                                <b>Authentication</b>
                                <span class="float-right">
                                    {% if router.ssh_key %}
                                        <i class="fas fa-key"></i> SSH Key: {{ router.ssh_key }}
                                    {% elif router.password %}
                                        <i class="fas fa-keyboard"></i> Password
                                    {% else %}
                                        <i class="fas fa-exclamation-triangle text-warning"></i> Missing authentication
                                    {% endif %}
                                </span>
                            </li>
                            {% endif %}
                            <li class="list-group-item">
                                <b>Monitoring</b>
                                <span class="float-right">
                                {% if router.monitoring %}
                                    {% if router.routerstatus.status_online %}
                                        <i class="far fa-check-circle text-success" title="Host is Online"></i>
                                    {% else %}
                                        <i class="far fa-times-circle text-danger" title="Host is unavailable"></i>
                                    {% endif %}
                                {% else %}
                                    Disabled
                                {% endif %}
                                </span>
                            </li>
                            {% if router.monitoring %}
                            <li class="list-group-item">
                                <b>Availability (last 7 days)</b>
                                <span class="float-right">
                                    <a href="/router/availability/?uuid={{ router.uuid }}">
                                    {{ last_week_availability }}%
                                    </a>
                                </span>
                            </li>
                            {% endif %}
                            <li class="list-group-item">
                                <b>Router Type</b>
                                <span class="float-right">{{ router.get_router_type_display }}</span>
                            </li>
                            
                            <li class="list-group-item">
                                <b>Groups</b>
                                <span class="float-right">
                                {% for router_group in router.routergroup_set.all %}
                                    <span class="badge badge-primary">{{ router_group.name }}</span>
                                {% endfor %}
                                </span>
                            </li>
                            {% if router.router_type != 'monitoring' %}
                            <li class="list-group-item">
                                <b>Backup Profile</b>
                                <span class="float-right">
                                {% if router.backup_profile %}
                                    {{ router.backup_profile }}
                                    {% if router.routerstatus.last_backup_failed %}
                                        <i class="fas fa-exclamation-triangle text-danger" title="Last backup failed to complete"></i>
                                    {% endif %}
                                {% else %}
                                    <i class="fas fa-exclamation-triangle text-warning" title="No backup profile selected"></i>
                                {% endif %}
                                </span>
                            </li>
                            <li class="list-group-item">
                                <b>Instant Backup</b>
                                <span class="float-right">
                                    <a href="/router/create_instant_backup/?uuid={{ router.uuid }}">
                                        Backup now
                                    </a>
                                </span>
                            </li>
                            {% endif %}
                            <li class="list-group-item">
                                <b>Notes</b>
                                <span class="float-right">{{ router.internal_notes|default_if_none:""|linebreaksbr }}</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <div class='col-xl-7'>
        {% if router_backup_list or router.router_type != 'monitoring' %}
            <div class="card card-primary card-outline">
                <div class="card-header">
                    <h3 class="card-title">Backup history</h3>
                    <span class="float-right">
                        {% if router.backupschedule %}
                        <a onclick="$('.backup-schedule-information').slideToggle();" href="#">
                            show schedule <i class="far fa-clock"></i>
                        </a>
                        {% else %}
                        <a onclick="alert('There\'s no backup schedule for this equipment.');" href="#">
                            show schedule <i class="far fa-clock"></i>
                        </a>
                        {% endif %}
                        
                    </span>
                </div>
                <div class="card-body row">
                    <div class="col-lg-12">
                        <table class="table table-hover datatables-no-export" >
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>Schedule</th>
                                <th>Time</th>
                                <th>Status</th>
                                <th><i class="fas fa-code-branch" title="backup hash"></i></th>
                            </tr>
                            </thead>
                            <tbody>
                            {% if router.backupschedule.next_daily_backup %}
                            <tr class="backup-schedule-information"  style="display: none;">
                                <td><i class="far fa-clock text-info"></i></td>
                                <td>Daily</td>
                                <td>{{ router.backupschedule.next_daily_backup }}</td>
                                <td><i class="far fa-clock text-info"></i></td>
                                <td>-</td>
                            </tr>
                            {% endif %}
                            {% if router.backupschedule.next_weekly_backup %}
                            <tr class="backup-schedule-information"  style="display: none;">
                                <td><i class="far fa-clock text-info"></i></td>
                                <td>Weekly</td>
                                <td>{{ router.backupschedule.next_weekly_backup }}</td>
                                <td><i class="far fa-clock text-info"></i></td>
                                <td>-</td>
                            </tr>
                            {% endif %}
                            {% if router.backupschedule.next_monthly_backup %}
                            <tr class="backup-schedule-information" style="display: none;">
                                <td><i class="far fa-clock text-info"></i></td>
                                <td>Monthly</td>
                                <td>{{ router.backupschedule.next_monthly_backup }}</td>
                                <td><i class="far fa-clock text-info"></i></td>
                                <td>-</td>
                            </tr>
                            {% endif %}

                            {% for backup in router_backup_list %}
                                <tr>
                                    <td class="min-width">
                                        <a href="/backup/backup_details/?uuid={{ backup.uuid }}">
                                            {{ backup.id }}
                                        </a>
                                    </td>
                                    <td>{{ backup.get_schedule_type_display }}</td>
                                    <td>{% if backup.finish_time %}{{ backup.finish_time }}{% else %}
                                        {% if not backup.error %}<i class="far fa-clock text-warning"></i>{% endif %}{% endif %}</td>
                                    <td>
                                        {% 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 %}
                                    </td>
                                    <td class="min-width">
                                        {% if backup.backup_text_hash %}
                                            <a href="#" title="{{ backup.backup_text_hash }}">{{ backup.backup_text_hash|default_if_none:""|slice:":8" }}...</a>
                                        {% endif %}
                                    </td>
                                </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        {% endif %}
        </div>
    </div>
{% endblock %}
