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

{% block title %}Rolled Over Loans - Haven Grazuri Investment Limited{% endblock %}

{% block extra_head %}
<link href="https://cdn.tailwindcss.com/3.4.17" rel="stylesheet">
<style>
  .bg-primary { background-color: #1a56db; }
  .bg-primary-dark { background-color: #1e429f; }
  .text-primary { color: #1a56db; }
  .hover\:bg-primary-dark:hover { background-color: #1e429f; }
  body { font-family: 'Inter', sans-serif; }
</style>
{% endblock %}

{% block content %}
<div class="mb-8">
    <div class="flex justify-between items-center">
        <div>
            <h2 class="text-2xl font-bold leading-7 text-gray-900 sm:truncate sm:text-3xl sm:tracking-tight">
                <span>🔄</span>
                Rolled Over Loans
            </h2>
            <p class="text-gray-600 mt-1">{{ total_count }} rolled over loans found</p>
            <p class="text-sm text-gray-500 mt-1">These loans have been rolled over and replaced with new active loans</p>
        </div>
        <div class="flex space-x-3">
            <a href="?export=excel{% if filter_params.start_date %}&start_date={{ filter_params.start_date|date:'Y-m-d' }}{% endif %}{% if filter_params.end_date %}&end_date={{ filter_params.end_date|date:'Y-m-d' }}{% endif %}{% if filter_params.product_id %}&product={{ filter_params.product_id }}{% endif %}" class="inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-green-600 hover:bg-green-700">
                <span>📊</span>
                Export Excel
            </a>
            <a href="?export=pdf{% if filter_params.start_date %}&start_date={{ filter_params.start_date|date:'Y-m-d' }}{% endif %}{% if filter_params.end_date %}&end_date={{ filter_params.end_date|date:'Y-m-d' }}{% endif %}{% if filter_params.product_id %}&product={{ filter_params.product_id }}{% endif %}" class="inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-red-600 hover:bg-red-700">
                <span>📄</span>
                Export PDF
            </a>
            <a href="{% url 'loans:loans' %}" class="inline-flex items-center px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50">
                <span>←</span>
                Back to All Loans
            </a>
            <a href="{% url 'loans:filtered_loans' %}?status=active" class="inline-flex items-center px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50">
                <span>📋</span>
                Active Loans
            </a>
            <a href="{% url 'loans:new_application' %}" class="inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-primary hover:bg-primary-dark">
                <span>+</span>
                New Loan
            </a>
        </div>
    </div>
</div>

<!-- Filter Section -->
<div class="bg-white shadow rounded-lg p-6 mb-6">
    <form method="GET" action="{% url 'loans:rolled_over_loans' %}" class="space-y-4">
        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
            <!-- Rollover Date Range Filter -->
            <div>
                <label for="start_date" class="block text-sm font-medium text-gray-700 mb-1">
                    Rollover Start Date
                </label>
                <input
                    type="date"
                    id="start_date"
                    name="start_date"
                    value="{{ filter_params.start_date|date:'Y-m-d' }}"
                    class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-primary focus:border-primary"
                />
            </div>
            
            <div>
                <label for="end_date" class="block text-sm font-medium text-gray-700 mb-1">
                    Rollover End Date
                </label>
                <input
                    type="date"
                    id="end_date"
                    name="end_date"
                    value="{{ filter_params.end_date|date:'Y-m-d' }}"
                    class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-primary focus:border-primary"
                />
            </div>
            
            <!-- Loan Product Filter -->
            <div>
                <label for="product" class="block text-sm font-medium text-gray-700 mb-1">
                    Loan Product
                </label>
                <select
                    id="product"
                    name="product"
                    class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-primary focus:border-primary"
                >
                    <option value="">All Products</option>
                    {% for product in loan_products %}
                        <option value="{{ product.id }}" {% if filter_params.product_id == product.id|stringformat:"s" %}selected{% endif %}>
                            {{ product.name }}
                        </option>
                    {% endfor %}
                </select>
            </div>
        </div>
        
        <!-- Filter Actions -->
        <div class="flex justify-end space-x-3">
            <a href="{% url 'loans:rolled_over_loans' %}" class="inline-flex items-center px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50">
                Clear Filters
            </a>
            <button type="submit" class="inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-primary hover:bg-primary-dark">
                <span>🔍</span>
                Apply Filters
            </button>
        </div>
    </form>
</div>

<!-- Summary Stats -->
<div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8">
    <div class="bg-white overflow-hidden shadow rounded-lg">
        <div class="p-5">
            <div class="flex items-center">
                <div class="flex-shrink-0">
                    <span>🔄</span>
                </div>
                <div class="ml-5 w-0 flex-1">
                    <dl>
                        <dt class="text-sm font-medium text-gray-500 truncate">Total Rolled Over</dt>
                        <dd class="text-lg font-medium text-gray-900">{{ total_count|intcomma }}</dd>
                    </dl>
                </div>
            </div>
        </div>
    </div>

    <div class="bg-white overflow-hidden shadow rounded-lg">
        <div class="p-5">
            <div class="flex items-center">
                <div class="flex-shrink-0">
                    <span>💵</span>
                </div>
                <div class="ml-5 w-0 flex-1">
                    <dl>
                        <dt class="text-sm font-medium text-gray-500 truncate">Total Original Amount</dt>
                        <dd class="text-lg font-medium text-gray-900">KES {{ total_amount|floatformat:2|intcomma }}</dd>
                    </dl>
                </div>
            </div>
        </div>
    </div>

    <div class="bg-white overflow-hidden shadow rounded-lg">
        <div class="p-5">
            <div class="flex items-center">
                <div class="flex-shrink-0">
                    <span>💰</span>
                </div>
                <div class="ml-5 w-0 flex-1">
                    <dl>
                        <dt class="text-sm font-medium text-gray-500 truncate">Total Rollover Fees</dt>
                        <dd class="text-lg font-medium text-green-600">KES {{ total_rollover_fees|floatformat:2|intcomma }}</dd>
                    </dl>
                </div>
            </div>
        </div>
    </div>

    <div class="bg-white overflow-hidden shadow rounded-lg">
        <div class="p-5">
            <div class="flex items-center">
                <div class="flex-shrink-0">
                    <span>ℹ️</span>
                </div>
                <div class="ml-5 w-0 flex-1">
                    <dl>
                        <dt class="text-sm font-medium text-gray-500 truncate">Status</dt>
                        <dd class="text-lg font-medium text-yellow-600">Rolled Over</dd>
                    </dl>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Information Banner -->
<div class="bg-yellow-50 border border-yellow-200 rounded-md p-4 mb-6">
    <div class="flex">
        <div class="flex-shrink-0">
            <span>ℹ️</span>
        </div>
        <div class="ml-3">
            <h3 class="text-sm font-medium text-yellow-800">About Rolled Over Loans</h3>
            <div class="mt-2 text-sm text-yellow-700">
                <p>These loans have been rolled over and are no longer active for collections. Each rolled over loan has been replaced with a new active loan that contains the updated terms, amount, and duration.</p>
            </div>
        </div>
    </div>
</div>

<!-- Loans Table -->
<div class="bg-white shadow overflow-hidden sm:rounded-md">
    <div class="px-4 py-5 sm:px-6">
        <h3 class="text-lg leading-6 font-medium text-gray-900">Rolled Over Loan Details</h3>
        <p class="mt-1 max-w-2xl text-sm text-gray-500">Historical record of loans that have been rolled over</p>
    </div>
    
    {% if loans %}
    <div class="overflow-x-auto">
        <table class="min-w-full divide-y divide-gray-200">
            <thead class="bg-gray-50">
                <tr>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Original Loan Number
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Borrower
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Original Amount
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Rollover Date
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Rollover Fee
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        New Loan Number
                    </th>
                    <th scope="col" class="relative px-6 py-3">
                        <span class="sr-only">Actions</span>
                    </th>
                </tr>
            </thead>
            <tbody class="bg-white divide-y divide-gray-200">
                {% for loan in loans %}
                {% with rollover_fee=loan.application.loan_product.rollover_fee_percentage|floatformat:2 %}
                {% with rollover_fee_amount=loan.principal_amount|multiply:loan.application.loan_product.rollover_fee_percentage|divide:100 %}
                <tr class="hover:bg-gray-50">
                    <td class="px-6 py-4 whitespace-nowrap">
                        <div class="text-sm font-medium text-gray-900">
                            {{ loan.loan_number }}
                        </div>
                        <div class="text-sm text-gray-500">
                            {{ loan.application.loan_product.name }}
                        </div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <div class="flex items-center">
                            <div class="flex-shrink-0 h-10 w-10">
                                <div class="h-10 w-10 rounded-full bg-gray-300 flex items-center justify-center">
                                    <span>👤</span>
                                </div>
                            </div>
                            <div class="ml-4">
                                <div class="text-sm font-medium text-gray-900">
                                    {{ loan.borrower.get_full_name }}
                                </div>
                                <div class="text-sm text-gray-500">
                                    {{ loan.borrower.phone_number }}
                                </div>
                            </div>
                        </div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <div class="text-sm text-gray-900">KES {{ loan.principal_amount|floatformat:2|intcomma }}</div>
                        <div class="text-sm text-gray-500">Total: KES {{ loan.total_amount|floatformat:2|intcomma }}</div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <div class="text-sm text-gray-900">{{ loan.updated_at|date:"M d, Y" }}</div>
                        <div class="text-sm text-gray-500">{{ loan.updated_at|date:"h:i A" }}</div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <div class="text-sm text-gray-900">KES {{ rollover_fee_amount|floatformat:2|intcomma }}</div>
                        <div class="text-sm text-gray-500">({{ rollover_fee }}%)</div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        {% for new_loan in loan.borrower.loans.all %}
                            {% if new_loan.original_loan == loan %}
                                <a href="{% url 'loans:loan_detail' new_loan.pk %}" class="text-primary hover:text-primary-dark text-sm font-medium">
                                    {{ new_loan.loan_number }}
                                </a>
                                <div class="text-xs text-gray-500">
                                    {% if new_loan.status == 'active' %}
                                        <span class="text-green-600">✓ Active</span>
                                    {% elif new_loan.status == 'paid' %}
                                        <span class="text-blue-600">✓ Paid</span>
                                    {% else %}
                                        {{ new_loan.get_status_display }}
                                    {% endif %}
                                </div>
                            {% endif %}
                        {% empty %}
                            <span class="text-sm text-gray-400">No new loan found</span>
                        {% endfor %}
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                        <a href="{% url 'loans:loan_detail' loan.pk %}" class="text-primary hover:text-primary-dark">
                            View Details
                        </a>
                    </td>
                </tr>
                {% endwith %}
                {% endwith %}
                {% endfor %}
            </tbody>
        </table>
    </div>

    <!-- Pagination -->
    {% if loans.has_other_pages %}
    <div class="bg-white px-4 py-3 flex items-center justify-between border-t border-gray-200 sm:px-6">
        <div class="flex-1 flex justify-between sm:hidden">
            {% if loans.has_previous %}
                <a href="?page={{ loans.previous_page_number }}" class="relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50">
                    Previous
                </a>
            {% endif %}
            {% if loans.has_next %}
                <a href="?page={{ loans.next_page_number }}" class="ml-3 relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50">
                    Next
                </a>
            {% endif %}
        </div>
        <div class="hidden sm:flex-1 sm:flex sm:items-center sm:justify-between">
            <div>
                <p class="text-sm text-gray-700">
                    Showing
                    <span class="font-medium">{{ loans.start_index }}</span>
                    to
                    <span class="font-medium">{{ loans.end_index }}</span>
                    of
                    <span class="font-medium">{{ loans.paginator.count }}</span>
                    results
                </p>
            </div>
            <div>
                <nav class="relative z-0 inline-flex rounded-md shadow-sm -space-x-px" aria-label="Pagination">
                    {% if loans.has_previous %}
                        <a href="?page={{ loans.previous_page_number }}" class="relative inline-flex items-center px-2 py-2 rounded-l-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                            <span class="sr-only">Previous</span>
                            <span>‹</span>
                        </a>
                    {% endif %}
                    
                    {% for num in loans.paginator.page_range %}
                        {% if loans.number == num %}
                            <span class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-primary text-sm font-medium text-white">
                                {{ num }}
                            </span>
                        {% else %}
                            <a href="?page={{ num }}" class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50">
                                {{ num }}
                            </a>
                        {% endif %}
                    {% endfor %}
                    
                    {% if loans.has_next %}
                        <a href="?page={{ loans.next_page_number }}" class="relative inline-flex items-center px-2 py-2 rounded-r-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                            <span class="sr-only">Next</span>
                            <span>›</span>
                        </a>
                    {% endif %}
                </nav>
            </div>
        </div>
    </div>
    {% endif %}
    
    {% else %}
    <div class="text-center py-12">
        <span>🔄</span>
        <h3 class="text-lg font-medium text-gray-900 mb-2">No rolled over loans found</h3>
        <p class="text-gray-500">There are currently no loans that have been rolled over.</p>
        <div class="mt-6">
            <a href="{% url 'loans:loans' %}" class="inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-primary hover:bg-primary-dark">
                View All Loans
            </a>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}