{% extends "base.html" %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
<div class="container mt-4">
    <h1 class="mb-4">{{ title }}</h1>
    <p>This page lists all the one-time use assessment links that have been generated for your clients.</p>
    <div class="alert alert-info">
        <strong>Note:</strong> To avoid accidental link usage, please copy the link and paste it into a new browser tab or send it to the recipient.
    </div>

    <ul class="nav nav-tabs">
        <li class="nav-item">
            <a class="nav-link {% if active_tab == 'active' %}active{% endif %}" href="{{ url_for('consultant.view_generated_links') }}">Active Links</a>
        </li>
        <li class="nav-item">
            <a class="nav-link {% if active_tab == 'archived' %}active{% endif %}" href="{{ url_for('consultant.view_archived_links') }}">Archived Links</a>
        </li>
    </ul>

    <div class="tab-content mt-3">
        <div class="tab-pane fade show active">
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead class="thead-dark">
                        <tr>
                            <th>Client ID</th>
                            <th>Client</th>
                            <th>Assessment</th>
                            <th>Generated On</th>
                            <th>Status</th>
                            <th>Link</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for invitation in invitations %}
                        <tr>
                            <td>{{ invitation.client_case_id or 'N/A' }}</td>
                            <td>
                                {{ invitation.client_name or 'N/A' }}
                                <br>
                                <small class="text-muted">{{ invitation.recipient_email }}</small>
                            </td>
                            <td>{{ invitation.assessment.name }}</td>
                            <td>{{ invitation.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                            <td>
                                {% if invitation.status == 'pending' %}
                                    <span class="badge badge-success">Pending</span>
                                {% elif invitation.status == 'completed' %}
                                    <span class="badge badge-secondary">Completed</span>
                                {% else %}
                                    <span class="badge badge-light">{{ invitation.status }}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if invitation.status == 'pending' %}
                                    <div class="input-group">
                                        <input type="text" class="form-control" id="link-{{ invitation.id }}" value="{{ request.url_root }}assessment/invite/{{ invitation.get_assessment_token() }}" readonly>
                                        <div class="input-group-append">
                                            <button class="btn btn-outline-secondary btn-sm" type="button" onclick="copyLink('link-{{ invitation.id }}')">Copy</button>
                                        </div>
                                    </div>
                                {% else %}
                                    <span class="text-muted">Link used on {{ invitation.completed_at.strftime('%Y-%m-%d') if invitation.completed_at else 'N/A' }}</span>
                                {% endif %}
                            </td>
                        </tr>
                        {% else %}
                        <tr>
                            <td colspan="5" class="text-center">No assessment links found.</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
function copyLink(elementId) {
    const linkInput = document.getElementById(elementId);
    linkInput.select();
    document.execCommand('copy');
    alert('Link copied to clipboard!');
}
</script>
{% endblock %}
