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

{% block title %}Job Postings - University ATS{% endblock %}

{% block customCSS %}
<style>
    /* UI Variables for the KAAT-S Theme */
    :root {
        --kaauh-teal: #00636e;
        --kaauh-teal-dark: #004a53;
        --kaauh-border: #dce3eb; /* Lighter border for clean look */
        --kaauh-primary-text: #343a40;
        --kaauh-secondary-text: #6c757d;
        --kaauh-success: #28a745;
        --kaauh-danger: #dc3545;
        --kaauh-info: #17a2b8;
    }
    
    body {
        background-color: #f8f9fa; /* Subtle light background */
        font-family: 'Inter', sans-serif;
    }

    /* Primary Color Overrides */
    .text-primary-theme { color: var(--kaauh-teal) !important; }
    .bg-primary-theme { background-color: var(--kaauh-teal) !important; }

    /* Enhanced Card Styling */
    .card {
        border: 1px solid var(--kaauh-border);
        border-radius: 0.75rem;
        box-shadow: 0 4px 12px rgba(0,0,0,0.06);
        transition: transform 0.2s, box-shadow 0.2s;
    }
    .card:not(.no-hover):hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 20px rgba(0,0,0,0.1); /* Slightly deeper hover shadow */
    }

    /* Main Action Button Style (Teal Theme) */
    .btn-main-action {
        background-color: var(--kaauh-teal);
        border-color: var(--kaauh-teal);
        color: white;
        font-weight: 600;
        transition: all 0.2s ease;
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        border-radius: 0.5rem;
        padding: 0.6rem 1.25rem;
    }

    .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 Button Style (using theme border) */
    .btn-outline-secondary {
        color: var(--kaauh-secondary-text);
        border-color: var(--kaauh-border);
        transition: all 0.2s ease;
    }
    .btn-outline-secondary:hover {
        background-color: var(--kaauh-teal-dark);
        color: white;
        border-color: var(--kaauh-teal-dark);
    }

    /* Status Badges */
    .status-badge {
        font-size: 0.75rem; /* Slightly smaller for compactness */
        padding: 0.3em 0.7em;
        border-radius: 0.5rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        color: white;
    }
    .bg-DRAFT { background-color: #6c757d !important; }
    .bg-ACTIVE { background-color: var(--kaauh-teal) !important; }
    .bg-CLOSED { background-color: var(--kaauh-danger) !important; }
    .bg-ARCHIVED { background-color: #343a40 !important; }

    /* --- TABLE STYLING --- */
    .table {
        table-layout: fixed;
        width: 100%;
        border-collapse: collapse;
    }
    .table thead th {
        color: var(--kaauh-primary-text);
        font-weight: 600;
        font-size: 0.8rem;
        vertical-align: middle;
        padding: 0.75rem 0.5rem;
        background-color: #edf2f7; /* Light background for header */
    }
    .table tbody td {
        padding: 0.5rem 0.5rem;
        font-size: 0.85rem;
    }
    .table-hover tbody tr:hover {
        background-color: #f0f4f7;
    }
    
    /* --------------------------------------------------------
    * OPTIMIZED MAIN TABLE COLUMN WIDTHS (Total must be 100%)
    * --------------------------------------------------------
    * 1. Job Title/ID: 20%
    * 2. Source: 8%
    * 3. Max Apps: 6%
    * 4. Deadline: 10%
    * 5. Submissions/Actions: 8%
    * 6. Metrics (Colspan 6): 48% (8% each)
    */
    .table th:nth-child(1) { width: 20%; } /* Job Title */
    .table th:nth-child(2) { width: 8%; }  /* Source */
    .table th:nth-child(3) { width: 6%; }  /* Max Apps */
    .table th:nth-child(4) { width: 10%; } /* Deadline */
    .table th:nth-child(5) { width: 8%; }  /* Submissions */


    /* Candidate Management Header Row (The one with the stage names) */
    .nested-metrics-row th {
        font-weight: 500;
        color: var(--kaauh-secondary-text);
        font-size: 0.7rem;
        padding: 0.3rem 0;
        border-bottom: 2px solid var(--kaauh-teal);
        text-align: center;
        border-left: 1px solid var(--kaauh-border);
        width: 8%; /* 6 columns * 8% = 48% total for metrics */
        background-color: #e4f0f7; /* Slightly darker background for nested header */
    }

    /* Main TH for Candidate Management Header Title */
    .candidate-management-header-title {
        text-align: center;
        padding: 0.5rem 0.25rem;
        border-left: 2px solid var(--kaauh-teal-dark); /* Stronger separator line */
        font-weight: 700;
        color: var(--kaauh-teal-dark);
        background-color: #edf2f7;
    }

    /* Candidate Management Data Cells */
    .candidate-data-cell {
        text-align: center;
        vertical-align: middle;
        font-weight: 600;
        font-size: 0.9rem;
        padding: 0;
    }
    
    /* Strong visual separator before metrics data */
    .table tbody tr td:nth-child(6) {
        border-left: 2px solid var(--kaauh-teal-dark) !important; 
    }
    
    /* Subtle vertical lines between metric data cells */
    .table tbody td.candidate-data-cell:not(:first-child) {
        border-left: 1px solid #f0f4f7;
    }

    .candidate-data-cell a {
        display: block;
        text-decoration: none;
        padding: 0.6rem 0.25rem;
        transition: background-color 0.1s;
    }
    .candidate-data-cell a:hover {
        background-color: #e9ecef;
    }

    /* Fix action button sizing */
    .btn-group-sm > .btn {
        padding: 0.2rem 0.4rem;
        font-size: 0.75rem;
    }

    /* Card View Enhancements */
    .card-view .card {
        height: 100%;
    }
    .card-view .card-title {
        color: var(--kaauh-teal-dark);
        font-weight: 700;
        font-size: 1.25rem;
    }
    .card-view .card-body {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        flex-grow: 1;
        padding: 1.25rem;
    }
    .card-view .list-unstyled li {
        margin-bottom: 0.5rem;
        font-size: 0.9rem;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    
    {# --- MAIN HEADER AND ACTION BUTTON --- #}
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 style="color: var(--kaauh-teal-dark); font-weight: 700;">
            <i class="fas fa-briefcase me-2"></i> {% trans "Job Postings" %}
        </h1>
        <a href="{% url 'job_create' %}" class="btn btn-main-action">
            <i class="fas fa-plus me-1"></i> {% trans "Create New Job" %}
        </a>
    </div>

    {# --- FILTER CARD --- #}
    <div class="card mb-4 shadow-sm no-hover">
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <label for="search" class="form-label small text-muted">{% trans "Search by Title or Department" %}</label>
                    <div class="input-group input-group-lg mb-3">
                        <form method="get" action="" class="w-100">
                            {% include 'includes/search_form.html' %}
                        </form>
                    </div>
                </div>
                <div class="col-md-6">
                    <form method="GET" class="row g-3 align-items-end" >
                        <div class="col-md-5">
                            <label for="status" class="form-label small text-muted">{% trans "Filter by Status" %}</label>
                            <select name="status" id="status" class="form-select form-select-sm">
                                <option value="">{% trans "All Statuses" %}</option>
                                <option value="DRAFT" {% if status_filter == 'DRAFT' %}selected{% endif %}>{% trans "Draft" %}</option>
                                <option value="ACTIVE" {% if status_filter == 'ACTIVE' %}selected{% endif %}>{% trans "Active" %}</option>
                                <option value="CLOSED" {% if status_filter == 'CLOSED' %}selected{% endif %}>{% trans "Closed" %}</option>
                                <option value="ARCHIVED" {% if status_filter == 'ARCHIVED' %}selected{% endif %}>{% trans "Archived" %}</option>
                            </select>
                        </div>
                        <div class="col-md-7">
                            <div class="filter-buttons">
                                <button type="submit" class="btn btn-main-action btn-sm">
                                    <i class="fas fa-filter me-1"></i> {% trans "Apply Filters" %}
                                </button>
                                {% if status_filter or search_query %}
                                    <a href="{% url 'job_list' %}" class="btn btn-outline-secondary btn-sm">
                                        <i class="fas fa-times me-1"></i> {% trans "Clear Filters" %}
                                    </a>
                                {% endif %}
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

    {# --- START OF JOB LIST CONTAINER --- #}
    <div id="job-list">
        {% include "includes/_list_view_switcher.html" with list_id="job-list" %}

        {# 1. TABLE VIEW (Desktop Default) - Hidden on Mobile #}
        <div class="table-view d-none d-lg-block active">
            <div class="card shadow-sm">
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">

                        {# --- Corrected Multi-Row Header Structure --- #}
                        <thead>
                            <tr style="border-bottom: none;">
                                <th scope="col" rowspan="2">{% trans "Job Title / ID" %}</th>
                                <th scope="col" rowspan="2">{% trans "Source" %}</th>
                                <th scope="col" rowspan="2">{% trans "Max Apps" %}</th>
                                <th scope="col" rowspan="2">{% trans "Deadline" %}</th>
                                <th scope="col" rowspan="2">{% trans "Submission" %}</th>
                               

                                <th scope="col" colspan="6" class="candidate-management-header-title">
                                    {% trans "Applicants Metrics (Current Stage Count)" %}
                                </th>
                            </tr>

                            <tr class="nested-metrics-row">
                                <th scope="col">{% trans "All" %}</th>
                                <th scope="col">{% trans "Screened" %}</th>
                                <th scope="col">{% trans "Exam" %}</th>
                                <th scope="col">{% trans "Interview" %}</th>
                                <th scope="col">{% trans "DOC Review" %}</th>
                                <th scope="col">{% trans "Offer" %}</th>
                            </tr>
                        </thead>

                        <tbody>
                            {% for job in jobs %}
                            <tr>
                                <td class="fw-medium">
                                    <a href="{% url 'job_detail' job.slug  %}" class="text-decoration-none text-primary-theme">{{ job.title }}</a>
                                    <br>
                                    <small class="text-muted">{{ job.pk }} / </small>
                                    <span class="badge bg-{{ job.status }} status-badge">{{ job.status }}</span>
                                </td>
                                <td>{{ job.get_source }}</td>
                                <td>{{ job.max_applications }}</td>
                                <td>{{ job.application_deadline|date:"d-m-Y" }}</td>
                                <td>
                                    {% if job.form_template %}
                                    <div class="btn-group btn-group-sm" role="group">
                                        <a href="{% url 'form_template_submissions_list' job.form_template.slug %}" class="btn btn-outline-secondary" title="{% trans 'All Application Submissions' %}">
                                                <i class="fas fa-file-alt text-primary-theme"></i>
                                        </a>
                                        
                                    </div>
                                    {% else %}
                                        <span class="text-muted small"></span>
                                    {% endif%}
                                </td>
                                
                                {# CANDIDATE MANAGEMENT DATA #}
                                <td class="candidate-data-cell text-primary-theme"><a href="{% url 'candidate_screening_view' job.slug %}" class="text-primary-theme">{% if job.all_candidates.count %}{{ job.all_candidates.count }}{% else %}-{% endif %}</a></td>
                                <td class="candidate-data-cell text-info"><a href="{% url 'candidate_screening_view' job.slug %}" class="text-info">{% if job.screening_candidates.count %}{{ job.screening_candidates.count }}{% else %}-{% endif %}</a></td>
                                <td class="candidate-data-cell text-success"><a href="{% url 'candidate_exam_view' job.slug %}" class="text-success">{% if job.exam_candidates.count %}{{ job.exam_candidates.count }}{% else %}-{% endif %}</a></td>
                                <td class="candidate-data-cell text-success"><a href="{% url 'candidate_interview_view' job.slug %}" class="text-success">{% if job.interview_candidates.count %}{{ job.interview_candidates.count }}{% else %}-{% endif %}</a></td>
                                <td class="candidate-data-cell text-success"><a href="{% url 'candidate_document_review_view' job.slug %}" class="text-success">{% if job.document_review_candidates.count %}{{ job.document_review_candidates.count }}{% else %}-{% endif %}</a></td>
                                <td class="candidate-data-cell text-success"><a href="{% url 'candidate_offer_view' job.slug %}" class="text-success">{% if job.offer_candidates.count %}{{ job.offer_candidates.count }}{% else %}-{% endif %}</a></td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        {# 2. CARD VIEW (Mobile Default) - Hidden on Desktop #}
        <div class="card-view row g-4 d-lg-none">
            {% for job in jobs %}
            <div class="col-xl-4 col-lg-6 col-md-6 col-sm-12">
                <div class="card shadow-sm">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start mb-2">
                            <h5 class="card-title mb-0">
                                <a href="{% url 'job_detail' job.slug %}" class="text-decoration-none text-primary-theme">{{ job.title }}</a>
                            </h5>
                            <span class="badge bg-{{ job.status }} status-badge">{{ job.status }}</span>
                        </div>
                        <p class="text-muted small mb-3">ID: {{ job.pk }} | Source: {{ job.get_source }}</p>

                        <ul class="list-unstyled small mb-3">
                            <li><i class="fas fa-calendar-alt text-primary-theme me-2"></i>{% trans "Deadline" %}: {{ job.application_deadline|date:"d-m-Y" }}</li>
                            <li><i class="fas fa-users text-primary-theme me-2"></i>{% trans "Total Applicants" %}: {{ job.all_candidates.count|default:"0" }}</li>
                            <li><i class="fas fa-clipboard-check text-success me-2"></i> {% trans "Offers Made" %}: {{ job.offer_candidates.count|default:"0" }}</li>
                            <li><i class="fas fa-file-alt text-info me-2"></i> {% trans "Form" %}:
                                {% if job.form_template %}
                                    <a href="{% url 'form_template_submissions_list' job.form_template.slug %}" class="text-info">{{ job.form_template.name }} ({{ job.form_template.submissions.count }} submissions)</a>
                                {% else %}
                                    {% trans "N/A" %}
                                {% endif %}
                            </li>
                        </ul>

                        <div class="d-flex justify-content-between mt-auto pt-3 border-top">
                            <a href="{% url 'job_detail' job.slug %}" class="btn btn-outline-secondary btn-sm" title="{% trans 'View' %}">
                                <i class="fas fa-eye me-1"></i> {% trans "View Job Details" %}
                            </a>
                            <div class="btn-group btn-group-sm">
                                
                                {% if job.form_template %}
                                <a href="{% url 'form_template_submissions_list' job.form_template.slug %}" class="btn btn-outline-secondary" title="{% trans 'Submissions' %}">
                                    <i class="fas fa-file-alt me-1"></i>{% trans "Submissions" %}
                                </a>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>
        {# --- END CARD VIEW --- #}
    </div>
    {# --- END OF JOB LIST CONTAINER --- #}
    
    {% include "includes/paginator.html" %}
    
    {% if not jobs and not job_list_data and not page_obj %}
        <div class="text-center py-5 card shadow-sm mt-4">
            <div class="card-body">
                <i class="fas fa-briefcase fa-3x mb-3" style="color: var(--kaauh-teal-dark);"></i>
                <h3>{% trans "No job postings found" %}</h3>
                <p class="text-muted">{% trans "Create your first job posting to get started or adjust your filters." %}</p>
                <a href="{% url 'job_create' %}" class="btn btn-main-action mt-3">
                    <i class="fas fa-plus me-1"></i> {% trans "Create Job" %}
                </a>
            </div>
        </div>
    {% endif %}
</div>
{% endblock %}