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

{% block title %}Registration Fees Settings{% endblock %}

{% block content %}
<div class="container mx-auto px-4 sm:px-6 lg:px-8 py-6">
    <!-- Page Header -->
    <div class="mb-8">
        <div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
            <div>
                <h1 class="text-3xl font-bold text-gray-900 dark:text-white">Registration Fees Settings</h1>
                <p class="text-gray-600 dark:text-gray-400 mt-1">Configure registration fees for different products and services</p>
            </div>
            <div class="flex gap-2">
                <a href="{% url 'reports:registration_fees_list' %}"
                    class="inline-flex items-center px-4 py-2 text-sm font-medium text-gray-600 bg-gray-100 rounded-lg hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600 transition-colors">
                    <span>📋</span> View Payments
                </a>
                <a href="{% url 'reports:registration_fee_audit_trail' %}"
                    class="inline-flex items-center px-4 py-2 text-sm font-medium text-gray-600 bg-gray-100 rounded-lg hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600 transition-colors">
                    <i class="fas fa-history mr-2"></i> Audit Trail
                </a>
                <a href="{% url 'reports:reports_dashboard' %}"
                    class="inline-flex items-center px-4 py-2 text-sm font-medium text-gray-600 bg-gray-100 rounded-lg hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600 transition-colors">
                    <span>←</span> Back to Dashboard
                </a>
            </div>
        </div>
    </div>

    <!-- Create New Fee Form -->
    <div class="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6 mb-8">
        <h2 class="text-xl font-semibold text-gray-900 dark:text-white mb-6">Create New Registration Fee</h2>
        
        <form method="POST" class="space-y-6">
            {% csrf_token %}
            <input type="hidden" name="action" value="create">
            
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div>
                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                        Product Type <span class="text-red-500">*</span>
                    </label>
                    {{ form.product_type }}
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                        Fee Name <span class="text-red-500">*</span>
                    </label>
                    {{ form.fee_name }}
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                        Amount (KES) <span class="text-red-500">*</span>
                    </label>
                    {{ form.amount }}
                </div>
            </div>

            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                    Description
                </label>
                {{ form.description }}
            </div>

            <div class="flex items-center">
                {{ form.is_active }}
                <label class="ml-2 text-sm font-medium text-gray-700 dark:text-gray-300">
                    Active (fee is available for use)
                </label>
            </div>

            <div class="flex justify-end">
                <button type="submit"
                    class="px-6 py-2 text-sm font-medium text-white bg-gradient-to-r from-blue-600 to-purple-600 rounded-lg hover:from-blue-700 hover:to-purple-700 transition-all transform hover:-translate-y-0.5">
                    <span>+</span> Create Fee
                </button>
            </div>
        </form>
    </div>

    <!-- Active Registration Fees -->
    <div class="bg-white dark:bg-gray-800 rounded-xl shadow-lg p-6">
        <h2 class="text-xl font-semibold text-gray-900 dark:text-white mb-6">Registration Fees</h2>
        
        {% if registration_fees %}
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
                <thead class="bg-gradient-to-r from-blue-600 to-purple-600">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-white uppercase tracking-wider">Product Type</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-white uppercase tracking-wider">Fee Name</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-white uppercase tracking-wider">Amount</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-white uppercase tracking-wider">Status</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-white uppercase tracking-wider">Created</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-white uppercase tracking-wider">Actions</th>
                    </tr>
                </thead>
                <tbody class="bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700">
                    {% for fee in registration_fees %}
                    <tr class="hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors">
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 dark:text-white">
                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200">
                                {{ fee.get_product_type_display }}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900 dark:text-white">
                            {{ fee.fee_name }}
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-semibold text-green-600 dark:text-green-400">
                            KES {{ fee.amount|floatformat:2|intcomma }}
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 dark:text-white">
                            {% if fee.is_active %}
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200">
                                    Active
                                </span>
                            {% else %}
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-200">
                                    Inactive
                                </span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 dark:text-white">
                            {{ fee.created_at|date:"M d, Y" }}
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                            <div class="flex space-x-2">
                                <form method="POST" class="inline">
                                    {% csrf_token %}
                                    <input type="hidden" name="action" value="toggle_status">
                                    <input type="hidden" name="fee_id" value="{{ fee.id }}">
                                    {% if fee.is_active %}
                                        <button type="submit" class="text-yellow-600 hover:text-yellow-900 dark:text-yellow-400 dark:hover:text-yellow-300" 
                                                onclick="return confirm('Are you sure you want to deactivate this fee?')">
                                            <span>⏸</span> Deactivate
                                        </button>
                                    {% else %}
                                        <button type="submit" class="text-green-600 hover:text-green-900 dark:text-green-400 dark:hover:text-green-300"
                                                onclick="return confirm('Are you sure you want to activate this fee?')">
                                            <span>▶</span> Activate
                                        </button>
                                    {% endif %}
                                </form>
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <div class="text-center py-12">
            <span>⚙️</span>
            <h3 class="text-lg font-medium text-gray-900 dark:text-white mb-2">No registration fees configured</h3>
            <p class="text-gray-600 dark:text-gray-400">Create your first registration fee using the form above.</p>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}