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

{% block content %}
<div class="container mt-5">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="mb-4">
                <a href="{% url 'dashboard' %}" class="text-dark text-decoration-none text-uppercase fw-bold">
                    <i class="bi bi-arrow-left"></i> Back to Dashboard
                </a>
            </div>
            
            <h1 class="display-5 fw-bold text-uppercase mb-4">{{ title }}</h1>
            
            <div class="card border-2 rounded-0 shadow-sm mb-5">
                <div class="card-body p-4">
                    <form method="POST" enctype="multipart/form-data">
                        {% csrf_token %}
                        {% for field in form %}
                        <div class="mb-4">
                            <label class="form-label fw-bold text-uppercase small">{{ field.label }}</label>
                            {{ field }}
                            {% if field.errors %}
                            <div class="text-danger small mt-1">{{ field.errors }}</div>
                            {% endif %}
                        </div>
                        {% endfor %}
                        <button type="submit" class="btn btn-dark btn-lg w-100 rounded-0 border-2">Save Course</button>
                    </form>
                </div>
            </div>

            {% if form.instance.pk %}
            <div class="d-flex justify-content-between align-items-center mb-4 mt-5">
                <h2 class="h3 fw-bold text-uppercase m-0">Modules</h2>
                <a href="{% url 'module_create' form.instance.pk %}" class="btn btn-outline-dark btn-sm rounded-0 border-2">Add Module</a>
            </div>
            
            <div class="list-group rounded-0 shadow-sm border-2 mb-5">
                {% for module in form.instance.modules.all %}
                <div class="list-group-item border-start-0 border-end-0 border-top-0 p-4">
                    <div class="d-flex justify-content-between align-items-start">
                        <div>
                            <h5 class="fw-bold text-uppercase mb-1">{{ module.title }}</h5>
                            <small class="text-muted">Order: {{ module.order }}</small>
                        </div>
                        <div>
                            <a href="{% url 'module_edit' module.pk %}" class="btn btn-sm btn-outline-dark rounded-0 border-2">Edit Module</a>
                            <a href="{% url 'lesson_create' module.pk %}" class="btn btn-sm btn-dark rounded-0 border-2 ms-2">Add Lesson</a>
                        </div>
                    </div>
                    
                    {% if module.lessons.all %}
                    <div class="mt-3 ps-4 border-start border-2">
                        <h6 class="small fw-bold text-uppercase text-muted mb-2">Lessons:</h6>
                        <ul class="list-unstyled mb-0">
                            {% for lesson in module.lessons.all %}
                            <li class="mb-2 d-flex justify-content-between align-items-center">
                                <span class="small">{{ lesson.title }} (Order: {{ lesson.order }})</span>
                                <a href="{% url 'lesson_edit' lesson.pk %}" class="btn btn-link btn-sm p-0 text-dark text-decoration-none">Edit</a>
                            </li>
                            {% endfor %}
                        </ul>
                    </div>
                    {% endif %}
                </div>
                {% empty %}
                <div class="list-group-item p-4 text-center">
                    <p class="text-muted mb-0">No modules yet.</p>
                </div>
                {% endfor %}
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
