{% extends "moderation/admin/base.html" %}
{% load static %}

{% block moderation_content %}
<div class="submission-list">
    <h1 class="mb-6 text-2xl font-bold">Content Submissions</h1>
    
    <div class="mb-6 filters">
        <form method="get" class="flex flex-wrap gap-4">
            <select name="status" class="form-select">
                <option value="">All Statuses</option>
                <option value="PENDING" {% if request.GET.status == 'PENDING' %}selected{% endif %}>Pending</option>
                <option value="APPROVED" {% if request.GET.status == 'APPROVED' %}selected{% endif %}>Approved</option>
                <option value="REJECTED" {% if request.GET.status == 'REJECTED' %}selected{% endif %}>Rejected</option>
                <option value="AUTO_APPROVED" {% if request.GET.status == 'AUTO_APPROVED' %}selected{% endif %}>Auto Approved</option>
            </select>
            <select name="type" class="form-select">
                <option value="">All Types</option>
                <option value="EDIT" {% if request.GET.type == 'EDIT' %}selected{% endif %}>Edits</option>
                <option value="CREATE" {% if request.GET.type == 'CREATE' %}selected{% endif %}>New Additions</option>
            </select>
            <button type="submit" class="px-4 py-2 text-white bg-blue-600 rounded-lg hover:bg-blue-700 dark:bg-blue-500 dark:hover:bg-blue-600">
                <i class="mr-2 fas fa-filter"></i>Filter
            </button>
        </form>
    </div>

    {% for submission in submissions %}
    <div class="p-6 mb-6 bg-white rounded-lg shadow dark:bg-gray-800">
        <div class="flex items-start justify-between mb-4">
            <div>
                <h3 class="text-lg font-semibold">
                    {% if submission.submission_type == 'CREATE' %}
                        New {{ submission.content_type.name }}
                    {% else %}
                        Edit to {{ submission.content_type.name }}: {{ submission.content_object }}
                    {% endif %}
                </h3>
                <div class="mt-1 text-sm text-gray-600 dark:text-gray-400">
                    Submitted by {{ submission.user.username }} on {{ submission.submitted_at|date:"M d, Y H:i" }}
                </div>
            </div>
            <div class="flex items-center space-x-2">
                <div class="px-2 py-1 text-sm rounded-full {% if submission.submission_type == 'CREATE' %}bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200{% else %}bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200{% endif %}">
                    {{ submission.get_submission_type_display }}
                </div>
                <div class="px-2 py-1 text-sm rounded-full {% if submission.status == 'PENDING' %}bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200
                    {% elif submission.status == 'APPROVED' or submission.status == 'AUTO_APPROVED' %}bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200
                    {% else %}bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200{% endif %}">
                    {{ submission.get_status_display }}
                </div>
            </div>
        </div>

        <div class="p-4 mb-4 rounded-lg bg-gray-50 dark:bg-gray-700/50">
            <div class="mb-2">
                <strong>Reason:</strong> {{ submission.reason }}
            </div>
            {% if submission.source %}
            <div>
                <strong>Source:</strong> {{ submission.source }}
            </div>
            {% endif %}
        </div>

        <div class="p-4 mb-4 rounded-lg bg-gray-50 dark:bg-gray-700/50">
            <h4 class="mb-2 font-medium">
                {% if submission.submission_type == 'CREATE' %}
                    Details:
                {% else %}
                    Changes:
                {% endif %}
            </h4>
            {% for field, value in submission.changes.items %}
            <div class="mb-2">
                <span class="font-medium">{{ field }}:</span>
                <span>{{ value }}</span>
            </div>
            {% endfor %}
        </div>

        {% if submission.status == 'PENDING' %}
        <form method="post" class="mt-4">
            {% csrf_token %}
            <input type="hidden" name="submission_id" value="{{ submission.id }}">
            <div class="mb-4">
                <label class="block mb-2 font-medium">Review Notes:</label>
                <textarea name="review_notes" rows="3" 
                          class="w-full border-gray-300 rounded-lg form-textarea dark:border-gray-600 dark:bg-gray-700 dark:text-white" 
                          placeholder="{% if submission.submission_type == 'CREATE' %}Explain why you're approving/rejecting this new addition{% else %}Explain why you're approving/rejecting these changes{% endif %}"></textarea>
            </div>
            <div class="flex justify-end space-x-4">
                <button type="submit" name="action" value="reject" 
                        class="px-4 py-2 text-white bg-red-600 rounded-lg hover:bg-red-700 dark:bg-red-500 dark:hover:bg-red-600">
                    <i class="mr-2 fas fa-times"></i>Reject
                </button>
                <button type="submit" name="action" value="approve" 
                        class="px-4 py-2 text-white bg-green-600 rounded-lg hover:bg-green-700 dark:bg-green-500 dark:hover:bg-green-600">
                    <i class="mr-2 fas fa-check"></i>Approve
                </button>
            </div>
        </form>
        {% else %}
        <div class="p-4 rounded-lg bg-gray-50 dark:bg-gray-700/50">
            <div class="mb-2">
                <strong>Reviewed by:</strong> {{ submission.reviewed_by.username }} on {{ submission.reviewed_at|date:"M d, Y H:i" }}
            </div>
            {% if submission.review_notes %}
            <div>
                <strong>Review Notes:</strong> {{ submission.review_notes }}
            </div>
            {% endif %}
        </div>
        {% endif %}
    </div>
    {% empty %}
    <div class="p-8 text-center text-gray-500 dark:text-gray-400">
        No submissions found.
    </div>
    {% endfor %}

    {% if is_paginated %}
    <div class="flex justify-center mt-6">
        <div class="inline-flex rounded-md shadow-sm">
            {% if page_obj.has_previous %}
                <a href="?page=1{{ request.GET.urlencode }}" class="px-3 py-2 text-gray-700 bg-white border border-gray-300 rounded-l-lg hover:bg-gray-50 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300 dark:hover:bg-gray-700">&laquo; First</a>
                <a href="?page={{ page_obj.previous_page_number }}{{ request.GET.urlencode }}" class="px-3 py-2 text-gray-700 bg-white border-t border-b border-gray-300 hover:bg-gray-50 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300 dark:hover:bg-gray-700">Previous</a>
            {% endif %}

            <span class="px-3 py-2 text-gray-700 bg-white border border-gray-300 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300">
                Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}
            </span>

            {% if page_obj.has_next %}
                <a href="?page={{ page_obj.next_page_number }}{{ request.GET.urlencode }}" class="px-3 py-2 text-gray-700 bg-white border-t border-b border-gray-300 hover:bg-gray-50 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300 dark:hover:bg-gray-700">Next</a>
                <a href="?page={{ page_obj.paginator.num_pages }}{{ request.GET.urlencode }}" class="px-3 py-2 text-gray-700 bg-white border border-gray-300 rounded-r-lg hover:bg-gray-50 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300 dark:hover:bg-gray-700">Last &raquo;</a>
            {% endif %}
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
