{% extends 'generic/object.html' %}
{% load static %}

{% block content %}
    <div class="row mb-3">
        <div class="col col-md-6">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">NetBox Endpoint</h5>
                    <span
                        id="netbox-status-badge-{{ object.pk }}"
                        class="badge text-bg-grey"
                        data-service-status-url="{% url 'plugins:netbox_proxbox:keepalive_status' 'netbox' object.pk %}"
                    >Unknown</span>
                </div>
                
                <div class="card-body">
    
                    <table class="table table-hover attr-table">
                        <tr>
                            <th scope="row">Name</th>
                            <td>{{ object.name }}</td>
                        </tr>
                        <tr>
                            <th scope="row">IP Address</th>
                            <td>
                                <a href="{{ object.ip_address.get_absolute_url }}">
                                    {{ object.ip_address }}
                                </a>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">Port</th>
                            <td>{{ object.port }}</td>
                        </tr>
                        <tr>
                            <th scope="row">Domain</th>
                            <td>{{ object.domain }}</td>
                        </tr>
                        <tr>
                            <th scope="row">Token Version</th>
                            <td>{{ object.token_version_label }}</td>
                        </tr>
                        <tr>
                            <th scope="row">Token</th>
                            <td>
                                {% if object.has_configured_token %}
                                    <span class="badge text-bg-gray">Configured</span>
                                {% else %}
                                    <span class="badge text-bg-grey">Not configured</span>
                                {% endif %}
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">Verify SSL</th>
                            <td>
                                {% if object.verify_ssl %}
                                    <span class="text-success"><i class="mdi mdi-check-bold"></i></span>
                                {% else %}
                                    <span class="text-danger"><i class="mdi mdi-close-thick"></i></span>
                                {% endif %}
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">Target</th>
                            <td>
                                <span id="netbox-target-{{ object.pk }}">Loading...</span>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">Authentication</th>
                            <td>
                                <span id="netbox-auth-{{ object.pk }}">Loading...</span>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">API Access</th>
                            <td>
                                <span id="netbox-api-{{ object.pk }}">Loading...</span>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
            {% include 'inc/panels/custom_fields.html' %}
        </div>
        <div class="col col-md-6">
            {% include 'inc/panels/tags.html' %}
            {% include 'inc/panels/comments.html' %}
        </div>
    </div>
    <script type="module" src="{% static 'netbox_proxbox/js/endpoint-status.js' %}"></script>
    <script type="module">
        import { fetchJson } from "{% static 'netbox_proxbox/js/common.js' %}";

        (async function() {
            const badge = document.getElementById("netbox-status-badge-{{ object.pk }}");
            if (!badge) return;
            const url = badge.dataset.serviceStatusUrl;
            try {
                const data = await fetchJson(url);
                const targetCell = document.getElementById("netbox-target-{{ object.pk }}");
                const authCell = document.getElementById("netbox-auth-{{ object.pk }}");
                const apiCell = document.getElementById("netbox-api-{{ object.pk }}");
                if (targetCell && data.target_address) {
                    targetCell.textContent = data.target_address + ":" + (data.target_port || "");
                }
                if (authCell) {
                    authCell.textContent = data.authentication === "success" ? "Success" : "Failed";
                    authCell.className = data.authentication === "success" ? "text-success" : "text-danger";
                }
                if (apiCell) {
                    apiCell.textContent = data.api_access === "success" ? "Success" : "Failed";
                    apiCell.className = data.api_access === "success" ? "text-success" : "text-danger";
                }
            } catch (e) {}
        })();
    </script>
{% endblock %}
