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

{% block title %}{% trans "Create Access Link" %} - ATS{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-1" style="color: var(--kaauh-teal-dark); font-weight: 700;">
                <i class="fas fa-link me-2"></i>
                {% trans "Create Access Link" %}
            </h1>
            <p class="text-muted mb-0">{% trans "Generate a secure access link for agency to submit candidates" %}</p>
        </div>
        <a href="{% url 'agency_assignment_list' %}" class="btn btn-outline-secondary">
            <i class="fas fa-arrow-left me-1"></i> {% trans "Back to Assignments" %}
        </a>
    </div>

    <div class="kaauh-card shadow-sm">
        <div class="card-body p-4">
            {% if form.non_field_errors %}
                <div class="alert alert-danger">
                    {% for error in form.non_field_errors %}
                        {{ error }}
                    {% endfor %}
                </div>
            {% endif %}

            <form method="post" novalidate>
                {% csrf_token %}

                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label for="{{ form.assignment.id_for_label }}" class="form-label">
                            {% trans "Assignment" %} <span class="text-danger">*</span>
                        </label>
                        {{ form.assignment }}
                        {% if form.assignment.errors %}
                            <div class="text-danger small mt-1">
                                {% for error in form.assignment.errors %}
                                    {{ error }}
                                {% endfor %}
                            </div>
                        {% endif %}
                        <div class="form-text">{% trans "Select the agency job assignment" %}</div>
                    </div>

                    <div class="col-md-6 mb-3">
                        <label for="{{ form.expires_at.id_for_label }}" class="form-label">
                            {% trans "Expires At" %} <span class="text-danger">*</span>
                        </label>
                        {{ form.expires_at }}
                        {% if form.expires_at.errors %}
                            <div class="text-danger small mt-1">
                                {% for error in form.expires_at.errors %}
                                    {{ error }}
                                {% endfor %}
                            </div>
                        {% endif %}
                        <div class="form-text">{% trans "When will this access link expire?" %}</div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label for="{{ form.max_submissions.id_for_label }}" class="form-label">
                            {% trans "Max Submissions" %}
                        </label>
                        {{ form.max_submissions }}
                        {% if form.max_submissions.errors %}
                            <div class="text-danger small mt-1">
                                {% for error in form.max_submissions.errors %}
                                    {{ error }}
                                {% endfor %}
                            </div>
                        {% endif %}
                        <div class="form-text">{% trans "Maximum number of candidates agency can submit (leave blank for unlimited)" %}</div>
                    </div>

                    <div class="col-md-6 mb-3">
                        <label for="{{ form.is_active.id_for_label }}" class="form-label">
                            {% trans "Status" %}
                        </label>
                        <div class="form-check mt-2">
                            {{ form.is_active }}
                            <label class="form-check-label" for="{{ form.is_active.id_for_label }}">
                                {% trans "Active" %}
                            </label>
                        </div>
                        {% if form.is_active.errors %}
                            <div class="text-danger small mt-1">
                                {% for error in form.is_active.errors %}
                                    {{ error }}
                                {% endfor %}
                            </div>
                        {% endif %}
                        <div class="form-text">{% trans "Whether this access link is currently active" %}</div>
                    </div>
                </div>

                <div class="mb-3">
                    <label for="{{ form.notes.id_for_label }}" class="form-label">
                        {% trans "Notes" %}
                    </label>
                    {{ form.notes }}
                    {% if form.notes.errors %}
                        <div class="text-danger small mt-1">
                            {% for error in form.notes.errors %}
                                {{ error }}
                            {% endfor %}
                        </div>
                    {% endif %}
                    <div class="form-text">{% trans "Additional notes or instructions for the agency" %}</div>
                </div>

                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <small class="text-muted">
                            <i class="fas fa-info-circle me-1"></i>
                            {% trans "Access links will be generated with a secure token that agencies can use to log in" %}
                        </small>
                    </div>
                    <div>
                        <a href="{% url 'agency_assignment_list' %}" class="btn btn-outline-secondary me-2">
                            {% trans "Cancel" %}
                        </a>
                        <button type="submit" class="btn btn-main-action">
                            <i class="fas fa-plus me-1"></i> {% trans "Create Access Link" %}
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}

{% block customJS %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Auto-populate expires_at with 7 days from now
    const expiresAtField = document.getElementById('{{ form.expires_at.id_for_label }}');
    if (expiresAtField && !expiresAtField.value) {
        const sevenDaysFromNow = new Date();
        sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
        expiresAtField.value = sevenDaysFromNow.toISOString().slice(0, 16);
    }
});
</script>
{% endblock %}
