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

{% block title %}{{ title }} - ATS{% endblock %}

{% block customCSS %}
<style>
    /* KAAT-S UI Variables */
    :root {
        --kaauh-teal: #00636e;
        --kaauh-teal-light: #e0f7f9; /* Added for contrast */
        --kaauh-teal-dark: #004a53;
        --kaauh-border: #eaeff3;
        --kaauh-primary-text: #343a40;
        --kaauh-success: #28a745;
        --kaauh-info: #17a2b8;
        --kaauh-danger: #dc3545;
        --kaauh-warning: #ffc107;
    }
    
    body {
        background-color: #f8f9fa; /* Light background for better contrast */
    }

    .kaauh-card {
        padding: 2.5rem; /* Increased padding */
        border: 1px solid var(--kaauh-border);
        border-radius: 0.75rem;
        box-shadow: 0 4px 12px rgba(0,0,0,0.06);
        background-color: white;
    }

    /* Main Action Button (Teal Fill) */
    .btn-main-action {
        background-color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
        color: white;
        font-weight: 600;
        padding: 0.6rem 1.25rem;
        border-radius: 0.5rem;
        transition: all 0.2s ease;
    }
    .btn-main-action:hover {
        background-color: var(--kaauh-teal-dark);
        border-color: var(--kaauh-teal-dark);
        box-shadow: 0 4px 8px rgba(0,0,0,0.15);
    }
    
    /* Secondary Action Button (Teal Outline) */
    .btn-outline-primary-teal {
        color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
        font-weight: 600;
        padding: 0.6rem 1.25rem;
        border-radius: 0.5rem;
    }
    .btn-outline-primary-teal:hover {
        background-color: var(--kaauh-teal);
        color: white;
    }

    /* Form Consistency */
    .form-control:focus, .form-select:focus {
        border-color: var(--kaauh-teal);
        box-shadow: 0 0 0 0.2rem rgba(0, 99, 110, 0.25);
    }

    .form-label {
        font-weight: 600;
        color: var(--kaauh-primary-text);
    }
    
    /* Applying Bootstrap classes to Django fields if not done in the form definition */
    .kaauh-field-control > input,
    .kaauh-field-control > textarea,
    .kaauh-field-control > select {
        display: block;
        width: 100%;
        padding: 0.5rem 0.75rem;
        font-size: 1rem;
        font-weight: 400;
        line-height: 1.5;
        color: var(--kaauh-primary-text);
        background-color: #fff;
        background-clip: padding-box;
        border: 1px solid #ced4da;
        appearance: none;
        border-radius: 0.5rem;
        transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
    }
    /* Specific overrides for different types */
    .kaauh-field-control > select {
        padding-right: 2.5rem; /* Space for the caret */
    }

</style>
{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <div class="row">
        <div class="col-lg-8 mx-auto">
            <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-tasks me-2"></i>
                        {{ title }}
                    </h1>
                    <p class="text-muted mb-0">
                        {% trans "Assign a job to an external hiring agency" %}
                    </p>
                </div>
                <a href="{% url 'agency_assignment_list' %}" class="btn btn-outline-primary-teal">
                    <i class="fas fa-arrow-left me-1"></i> {% trans "Back to Assignments" %}
                </a>
            </div>

            <div class="kaauh-card">
                <form method="post" novalidate>
                    {% csrf_token %}

                    <div class="row g-3 mb-4">
                        
                        <div class="col-md-6">
                            <label for="{{ form.agency.id_for_label }}" class="form-label">
                                {{ form.agency.label }} <span class="text-danger">*</span>
                            </label>
                            {# Wrapper Div for styling consistency (Assumes agency is a SELECT field) #}
                            <div class="kaauh-field-control">
                                {{ form.agency|attr:'class:form-select' }} 
                            </div>
                            {% if form.agency.errors %}
                                <div class="text-danger small mt-1">
                                    {% for error in form.agency.errors %}{{ error }}{% endfor %}
                                </div>
                            {% endif %}
                        </div> 
                        
                        <div class="col-md-6">
                            <label for="{{ form.job.id_for_label }}" class="form-label">
                                {{ form.job.label }} <span class="text-danger">*</span>
                            </label>
                            {# Wrapper Div for styling consistency (Assumes job is a SELECT field) #}
                            <div class="kaauh-field-control">
                                {{ form.job|attr:'class:form-select' }}
                            </div>
                            {% if form.job.errors %}
                                <div class="text-danger small mt-1">
                                    {% for error in form.job.errors %}{{ error }}{% endfor %}
                                </div>
                            {% endif %}
                        </div>
                    </div>

                    <div class="row g-3 mb-4">
                        <div class="col-md-6">
                            <label for="{{ form.max_candidates.id_for_label }}" class="form-label">
                                {{ form.max_candidates.label }} <span class="text-danger">*</span>
                            </label>
                            {# Wrapper Div for styling consistency (Assumes max_candidates is an INPUT field) #}
                            <div class="kaauh-field-control">
                                {{ form.max_candidates|attr:'class:form-control' }}
                            </div>
                            {% if form.max_candidates.errors %}
                                <div class="text-danger small mt-1">
                                    {% for error in form.max_candidates.errors %}{{ error }}{% endfor %}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">
                                {% trans "Maximum number of candidates the agency can submit" %}
                            </small>
                        </div>
                        <div class="col-md-6">
                            <label for="{{ form.deadline_date.id_for_label }}" class="form-label">
                                {{ form.deadline_date.label }} <span class="text-danger">*</span>
                            </label>
                            {# Wrapper Div for styling consistency (Assumes deadline_date is an INPUT field) #}
                            <div class="kaauh-field-control">
                                {{ form.deadline_date|attr:'class:form-control' }}
                            </div>
                            {% if form.deadline_date.errors %}
                                <div class="text-danger small mt-1">
                                    {% for error in form.deadline_date.errors %}{{ error }}{% endfor %}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">
                                {% trans "Date and time when submission period ends" %}
                            </small>
                        </div>
                    </div>


                    <div class="mb-4">
                        <label for="{{ form.admin_notes.id_for_label }}" class="form-label">
                            {{ form.admin_notes.label }}
                        </label>
                        {# Wrapper Div for styling consistency (Assumes admin_notes is a TEXTAREA field) #}
                        <div class="kaauh-field-control">
                            {{ form.admin_notes|attr:'class:form-control' }}
                        </div>
                        {% if form.admin_notes.errors %}
                            <div class="text-danger small mt-1">
                                {% for error in form.admin_notes.errors %}{{ error }}{% endfor %}
                            </div>
                        {% endif %}
                        <small class="form-text text-muted">
                            {% trans "Internal notes about this assignment (not visible to agency)" %}
                        </small>
                    </div>

                    <div class="d-flex justify-content-between align-items-center pt-3 border-top">
                        <a href="{% url 'agency_assignment_list' %}" class="btn btn-outline-primary-teal">
                            <i class="fas fa-times me-1"></i> {% trans "Cancel" %}
                        </a>
                        <button type="submit" class="btn btn-main-action">
                            <i class="fas fa-save me-1"></i> {{ button_text }}
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block customJS %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    // --- Consistency Check: Ensure Django widgets have the Bootstrap classes ---
    // If your form fields are NOT already adding classes via widget attrs in the Django form, 
    // you MUST add the following utility filter to your project to make this template work:
    // `|attr:'class:form-control'`
    
    // Auto-populate agency field when job is selected
    const jobSelect = document.getElementById('{{ form.job.id_for_label }}');
    const agencySelect = document.getElementById('{{ form.agency.id_for_label }}');

    if (jobSelect && agencySelect) {
        jobSelect.addEventListener('change', function() {
            // You could add logic here to filter agencies based on job requirements
            // For now, just log the selection
            console.log('Job selected:', this.value);
        });
    }

    // Set minimum datetime for deadline to current time
    const deadlineInput = document.getElementById('{{ form.deadline_date.id_for_label }}');
    if (deadlineInput) {
        const now = new Date();
        const localDateTime = new Date(now.getTime() - now.getTimezoneOffset() * 60000)
            .toISOString()
            .slice(0, 16);
        deadlineInput.min = localDateTime;
    }
});
</script>
{% endblock %}