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

{% block title %}Edit {{ job.title }} - University ATS{% endblock %}

{% block customCSS %}
{# 💡 1. Add Summernote CSS Media in the head #}
{{ form.media.css }}

<style>
    /* ================================================= */
    /* THEME VARIABLES AND GLOBAL STYLES (Keep existing) */
    /* ================================================= */
    :root {
        --kaauh-teal: #00636e;
        --kaauh-teal-dark: #004a53;
        --kaauh-border: #eaeff3;
        --kaauh-primary-text: #343a40;
    }

    .text-primary { color: var(--kaauh-teal) !important; }
    .btn-main-action, .btn-primary {
        background-color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
        color: white;
        font-weight: 600;
        padding: 0.6rem 1.2rem;
        transition: all 0.2s ease;
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
    }
    .btn-main-action:hover, .btn-primary:hover {
        background-color: var(--kaauh-teal-dark);
        border-color: var(--kaauh-teal-dark);
        transform: translateY(-1px);
        box-shadow: 0 4px 8px rgba(0,0,0,0.15);
    }
    .btn-secondary {
        background-color: #f8f9fa;
        color: var(--kaauh-teal-dark);
        border: 1px solid var(--kaauh-border);
        font-weight: 500;
    }
    .btn-secondary:hover {
        background-color: #e9ecef;
        color: var(--kaauh-teal-dark); /* Reverting color to dark theme text */
        border-color: var(--kaauh-teal-dark);
    }
    .card {
        border: 1px solid var(--kaauh-border);
        border-radius: 0.75rem;
        overflow: hidden;
        box-shadow: 0 4px 12px rgba(0,0,0,0.06);
        background-color: white;
    }
    .card-header-themed {
        background-color: #f0f8ff;
        border-bottom: 2px solid var(--kaauh-teal);
        color: var(--kaauh-teal-dark);
        font-weight: 700;
        padding: 1rem 1.5rem;
    }
    .form-label {
        font-weight: 600;
        color: var(--kaauh-primary-text);
        margin-bottom: 0.3rem;
        display: block;
    }
    .card-body input:not([type="checkbox"]):not([type="radio"]):not([type="file"]):not([type="submit"]):not([type="button"]):not([type="reset"]),
    .card-body select {
        border-radius: 0.5rem;
        border: 1px solid #ced4da;
        width: 100%;
        padding: 0.375rem 0.75rem;
        box-sizing: border-box; 
    }

    /* ================================================= */
    /* ✅ CORRECTED SUMMERNOTE FULL-WIDTH STYLING (Active Fix) */
    /* ================================================= */

    /* The most aggressive, universal fix for Summernote within Bootstrap columns */
    .col-12 .note-editor {
        /* This compensates for the 0.75rem padding on each side of the col-12 */
        width: calc(100% + 1.5rem) !important;
        margin-left: -0.75rem !important;
        margin-right: -0.75rem !important;

        /* General cleanup to maintain look */
        box-sizing: border-box; 
        margin-bottom: 0 !important;
        border-radius: 0.5rem;
    }
    
    /* Set minimum heights for specific fields using sibling selector */
    #id_description + .note-editor { min-height: 300px; }
    #id_qualifications + .note-editor { min-height: 200px; }
    #id_benefits + .note-editor,
    #id_application_instructions + .note-editor { min-height: 150px; }

</style>
{% endblock %}

{% block content %}
<div class="container-fluid py-4"> 
    <h1 class="h3 mb-4 text-primary fw-bold">
        {# UPDATED TITLE FOR EDIT CONTEXT #}
        <i class="fas fa-edit me-2"></i> {% trans "Edit Job Posting" %}
        {% if job.title %} - {{ job.title }} {% endif %}
    </h1>

    <form method="post" id="jobForm" class="mb-5" enctype="multipart/form-data">
        {% csrf_token %}

        {# ================================================= #}
        {# SECTION 1: CORE POSITION DETAILS #}
        {# ================================================= #}
        <div class="card mb-4 shadow-sm">
            <div class="card-header-themed">
                <h5><i class="fas fa-info-circle"></i> {% trans "Core Position Details" %}</h5>
            </div>
            <div class="card-body">
                <div class="row g-4">
                    <div class="col-md-8">
                        <div>
                            <label for="{{ form.title.id_for_label }}" class="form-label">{% trans "Job Title" %} <span class="text-danger">*</span></label>
                            {{ form.title }}
                            {% if form.title.errors %}<div class="text-danger small mt-1">{{ form.title.errors }}</div>{% endif %}
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div>
                            <label for="{{ form.job_type.id_for_label }}" class="form-label">{% trans "Job Type" %} <span class="text-danger">*</span></label>
                            {{ form.job_type }}
                            {% if form.job_type.errors %}<div class="text-danger small mt-1">{{ form.job_type.errors }}</div>{% endif %}
                        </div>
                    </div>

                    <div class="col-md-6">
                        <div>
                            <label for="{{ form.department.id_for_label }}" class="form-label">{% trans "Department" %}</label>
                            {{ form.department }}
                            {% if form.department.errors %}<div class="text-danger small mt-1">{{ form.department.errors }}</div>{% endif %}
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div>
                            <label for="{{ form.workplace_type.id_for_label }}" class="form-label">{% trans "Workplace Type" %} <span class="text-danger">*</span></label>
                            {{ form.workplace_type }}
                            {% if form.workplace_type.errors %}<div class="text-danger small mt-1">{{ form.workplace_type.errors }}</div>{% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>

        {# ================================================= #}
        {# SECTION 2: JOB CONTENT (All Summernote Fields) #}
        {# ================================================= #}
        <div class="card mb-4 shadow-sm">
            <div class="card-header-themed">
                <h5><i class="fas fa-file-alt"></i> {% trans "Job Content" %}</h5>
            </div>
            <div class="card-body">
                <div class="row g-4">
                    <div class="col-12">
                        <div>
                            <label for="{{ form.description.id_for_label }}" class="form-label">{% trans "Job Description" %} <span class="text-danger">*</span></label>
                            {{ form.description }}
                            {% if form.description.errors %}<div class="text-danger small mt-1">{{ form.description.errors }}</div>{% endif %}
                        </div>
                    </div>

                    <div class="col-12">
                        <div>
                            <label for="{{ form.qualifications.id_for_label }}" class="form-label">{% trans "Qualifications and Requirements" %}</label>
                            {{ form.qualifications }}
                            {% if form.qualifications.errors %}<div class="text-danger small mt-1">{{ form.qualifications.errors }}</div>{% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>

        {# ================================================= #}
        {# SECTION 3: COMPENSATION AND APPLICATION #}
        {# ================================================= #}
        <div class="card mb-4 shadow-sm">
            <div class="card-header-themed">
                <h5><i class="fas fa-dollar-sign"></i> {% trans "Compensation & Application" %}</h5>
            </div>
            <div class="card-body">
                <div class="row g-4">
                    <div class="col-md-6">
                        <div>
                            <label for="{{ form.salary_range.id_for_label }}" class="form-label">{% trans "Salary Range" %}</label>
                            {{ form.salary_range }}
                            {% if form.salary_range.errors %}<div class="text-danger small mt-1">{{ form.salary_range.errors }}</div>{% endif %}
                        </div>
                    </div>
                    {% comment %} <div class="col-md-6">
                        <div>
                            <label for="{{ form.application_url.id_for_label }}" class="form-label">{% trans "Application URL" %} <span class="text-danger">*</span></label>
                            {{ form.application_url }}
                            {% if form.application_url.errors %}<div class="text-danger small mt-1">{{ form.application_url.errors }}</div>{% endif %}
                            <div class="form-text">{% trans "Full URL where candidates will apply" %}</div>
                        </div>
                    </div> {% endcomment %}

                    <div class="col-12">
                        <div>
                            <label for="{{ form.benefits.id_for_label }}" class="form-label">{% trans "Benefits" %}</label>
                            {{ form.benefits }}
                            {% if form.benefits.errors %}<div class="text-danger small mt-1">{{ form.benefits.errors }}</div>{% endif %}
                        </div>
                    </div>

                    <div class="col-12">
                        <div>
                            <label for="{{ form.application_instructions.id_for_label }}" class="form-label">{% trans "Application Instructions" %}</label>
                            {{ form.application_instructions }}
                            {% if form.application_instructions.errors %}<div class="text-danger small mt-1">{{ form.application_instructions.errors }}</div>{% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>

        {# ================================================= #}
        {# SECTION 4: LOCATION AND DATES #}
        {# ================================================= #}
        <div class="card mb-4 shadow-sm">
            <div class="card-header-themed">
                <h5><i class="fas fa-map-marker-alt"></i> {% trans "Location, Dates, & Status" %}</h5>
            </div>
            <div class="card-body">
                <div class="row g-4">
                    <div class="col-md-4">
                        <div>
                            <label for="{{ form.location_city.id_for_label }}" class="form-label">{% trans "City" %}</label>
                            {{ form.location_city }}
                            {% if form.location_city.errors %}<div class="text-danger small mt-1">{{ form.location_city.errors }}</div>{% endif %}
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div>
                            <label for="{{ form.location_state.id_for_label }}" class="form-label">{% trans "State/Province" %}</label>
                            {{ form.location_state }}
                            {% if form.location_state.errors %}<div class="text-danger small mt-1">{{ form.location_state.errors }}</div>{% endif %}
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div>
                            <label for="{{ form.location_country.id_for_label }}" class="form-label">{% trans "Country" %}</label>
                            {{ form.location_country }}
                            {% if form.location_country.errors %}<div class="text-danger small mt-1">{{ form.location_country.errors }}</div>{% endif %}
                        </div>
                    </div>

                    <div class="col-md-4">
                        <div>
                            <label for="{{ form.application_deadline.id_for_label }}" class="form-label">{% trans "Application Deadline" %}</label>
                            {{ form.application_deadline }}
                            {% if form.application_deadline.errors %}<div class="text-danger small mt-1">{{ form.application_deadline.errors }}</div>{% endif %}
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div>
                            <label for="{{ form.start_date.id_for_label }}" class="form-label">{% trans "Desired Start Date" %}</label>
                            {{ form.start_date }}
                            {% if form.start_date.errors %}<div class="text-danger small mt-1">{{ form.start_date.errors }}</div>{% endif %}
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div>
                            <label for="{{ form.status.id_for_label }}" class="form-label">{% trans "Status" %}</label>
                            {{ form.status }}
                            {% if form.status.errors %}<div class="text-danger small mt-1">{{ form.status.errors }}</div>{% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>

        {# ================================================= #}
        {# SECTION 5: INTERNAL AND PROMOTION #}
        {# ================================================= #}
        <div class="card mb-4 shadow-sm">
            <div class="card-header-themed">
                <h5><i class="fas fa-tags"></i> {% trans "Internal & Promotion" %}</h5>
            </div>
            <div class="card-body">
                <div class="row g-4">
                    <div class="col-md-6">
                        <div>
                            <label for="{{ form.position_number.id_for_label }}" class="form-label">{% trans "Position Number" %}</label>
                            {{ form.position_number }}
                            {% if form.position_number.errors %}<div class="text-danger small mt-1">{{ form.position_number.errors }}</div>{% endif %}
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div>
                            <label for="{{ form.reporting_to.id_for_label }}" class="form-label">{% trans "Reports To" %}</label>
                            {{ form.reporting_to }}
                            {% if form.reporting_to.errors %}<div class="text-danger small mt-1">{{ form.reporting_to.errors }}</div>{% endif %}
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div>
                            <label for="{{ form.open_positions.id_for_label }}" class="form-label">{% trans "Open Positions" %}</label>
                            {{ form.open_positions }}
                            {% if form.open_positions.errors %}<div class="text-danger small mt-1">{{ form.open_positions.errors }}</div>{% endif %}
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div>
                            <label for="{{ form.created_by.id_for_label }}" class="form-label">{% trans "Created By" %}</label>
                            {{ form.created_by }}
                            {% if form.created_by.errors %}<div class="text-danger small mt-1">{{ form.created_by.errors }}</div>{% endif %}
                        </div>
                    </div>
                    <div class="col-12">
                        <div>
                            <label for="{{ form.hash_tags.id_for_label }}" class="form-label">{% trans "Hashtags (For Promotion/Search)" %}</label>
                            {{ form.hash_tags }}
                            {% if form.hash_tags.errors %}<div class="text-danger small mt-1">{{ form.hash_tags.errors }}</div>{% endif %}
                            <div class="form-text">{% trans "Comma-separated list of hashtags, e.g., #hiring, #professor" %}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        {# ================================================= #}
        {# ACTION BUTTONS #}
        {# ================================================= #}
        <div class="d-flex justify-content-between pt-2">
            {# UPDATED CANCEL URL for Job Detail #}
            <a href="{% url 'job_detail' job.slug %}" class="btn btn-secondary">
                <i class="fas fa-arrow-left me-1"></i> {% trans "Cancel" %}
            </a>
            <button type="submit" class="btn btn-main-action">
                {# UPDATED BUTTON TEXT for Edit action #}
                <i class="fas fa-save me-1"></i> {% trans "Update Job" %}
            </button>
        </div>
    </form>
</div>

{# 💡 2. Add Summernote JS Media at the end of the body #}
{{ form.media.js }}

{% endblock %}