{% extends "admin_base.html" %}
{% block content %}
<h1 class="mb-4">Manage Tour Categories</h1>

<h2>Add Parent Category</h2>
<form method="POST">
    {{ parent_form.hidden_tag() }}
    
    <div class="mb-3">
        {{ parent_form.name.label(class="form-label") }}
        {{ parent_form.name(class="form-control") }}
    </div>
    
    {{ parent_form.submit(class="btn btn-primary") }}
</form>

<h2 class="mt-5">Add Subcategory</h2>
<form method="POST" enctype="multipart/form-data">
    {{ sub_form.hidden_tag() }}
    
    <div class="mb-3">
        {{ sub_form.name.label(class="form-label") }}
        {{ sub_form.name(class="form-control") }}
    </div>
    
    <div class="mb-3">
        {{ sub_form.parent_category.label(class="form-label") }}
        {{ sub_form.parent_category(class="form-control") }}
    </div>
    
    <div class="mb-3">
        {{ sub_form.image.label(class="form-label") }}
        {{ sub_form.image(class="form-control") }}
    </div>
    
    {{ sub_form.submit(class="btn btn-primary") }}
</form>

<h2 class="mt-5">Existing Categories</h2>
<ul class="list-group">
{% for category in parent_categories %}
    <li class="list-group-item">
        {{ category.name }}
        <a href="{{ url_for('admin_blueprint.edit_parent_category', id=category.id) }}" class="btn btn-sm btn-primary">Edit</a>
        <form action="{{ url_for('admin_blueprint.delete_parent_category', id=category.id) }}" method="POST" class="d-inline">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
            <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this parent category?');">Delete</button>
        </form>
        <ul class="list-group mt-2">
        {% for subcategory in category.subcategories %}
            <li class="list-group-item">
                {{ subcategory.name }}
                {% if subcategory.image %}
                    <img src="{{ url_for('static', filename='uploads/' + subcategory.image) }}" alt="{{ subcategory.name }}" class="img-thumbnail" style="max-width: 100px;">
                {% endif %}
                <a href="{{ url_for('admin_blueprint.edit_subcategory', id=subcategory.id) }}" class="btn btn-sm btn-primary">Edit</a>
                <form action="{{ url_for('admin_blueprint.delete_subcategory', id=subcategory.id) }}" method="POST" class="d-inline">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                    <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this subcategory?');">Delete</button>
                </form>
            </li>
        {% endfor %}
        </ul>
    </li>
{% endfor %}
</ul>
{% endblock %}