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

{% block title %}{{ title }} - HAVEN GRAZURI INVESTMENT LIMITED{% 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">
                {{ title }}
            </h2>
            <p class="text-gray-600 mt-1">Collection performance for {{ current_month|date:"F Y" }}</p>
        </div>
        <div class="flex space-x-3">
            <a href="{% url 'reports:reports_dashboard' %}" 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 Dashboard
            </a>
            <a href="{% url 'reports:export_report' 'collections' %}" 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>
                Export Report
            </a>
        </div>
    </div>
</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 Collected</dt>
                        <dd class="text-lg font-medium text-green-600">KES {{ total_collected|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">
                    <i class="fas fa-target text-blue-400 text-2xl"></i>
                </div>
                <div class="ml-5 w-0 flex-1">
                    <dl>
                        <dt class="text-sm font-medium text-gray-500 truncate">Expected Collections</dt>
                        <dd class="text-lg font-medium text-blue-600">KES {{ total_expected|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">
                    <i class="fas fa-percentage text-purple-400 text-2xl"></i>
                </div>
                <div class="ml-5 w-0 flex-1">
                    <dl>
                        <dt class="text-sm font-medium text-gray-500 truncate">Collection Rate</dt>
                        <dd class="text-lg font-medium {% if collection_rate >= 80 %}text-green-600{% elif collection_rate >= 60 %}text-yellow-600{% else %}text-red-600{% endif %}">
                            {{ collection_rate|floatformat:1 }}%
                        </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>1.</span>
                </div>
                <div class="ml-5 w-0 flex-1">
                    <dl>
                        <dt class="text-sm font-medium text-gray-500 truncate">Total Payments</dt>
                        <dd class="text-lg font-medium text-gray-900">{{ total_count|intcomma }}</dd>
                    </dl>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Collection Performance Indicator -->
<div class="mb-8">
    <div class="bg-white overflow-hidden shadow rounded-lg">
        <div class="p-5">
            <h3 class="text-lg leading-6 font-medium text-gray-900 mb-4">Collection Performance</h3>
            <div class="w-full bg-gray-200 rounded-full h-4">
                <div class="{% if collection_rate >= 80 %}bg-green-500{% elif collection_rate >= 60 %}bg-yellow-500{% else %}bg-red-500{% endif %} h-4 rounded-full transition-all duration-300" 
                     style="width: {{ collection_rate|floatformat:0 }}%"></div>
            </div>
            <div class="flex justify-between text-sm text-gray-600 mt-2">
                <span>0%</span>
                <span class="font-medium">{{ collection_rate|floatformat:1 }}%</span>
                <span>100%</span>
            </div>
        </div>
    </div>
</div>

<!-- Recent Collections 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">Recent Collections</h3>
        <p class="mt-1 max-w-2xl text-sm text-gray-500">All payments received in {{ current_month|date:"F Y" }}</p>
    </div>
    
    {% if repayments %}
    <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">
                        Borrower
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Loan Amount
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Payment Amount
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Payment Method
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Payment Date
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Status
                    </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 repayment in repayments %}
                <tr class="hover:bg-gray-50">
                    <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">
                                    {{ repayment.loan.borrower.get_full_name }}
                                </div>
                                <div class="text-sm text-gray-500">
                                    {{ repayment.loan.borrower.phone_number }}
                                </div>
                            </div>
                        </div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                        KES {{ repayment.loan.total_amount|floatformat:2|intcomma }}
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <span class="text-sm font-medium text-green-600">
                            KES {{ repayment.amount|floatformat:2|intcomma }}
                        </span>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                        {{ repayment.payment_method|default:"Cash" }}
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                        {{ repayment.payment_date|date:"M d, Y H:i" }}
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-green-100 text-green-800">
                            Completed
                        </span>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                        <a href="{% url 'loans:loan_detail' repayment.loan.pk %}" class="text-primary hover:text-primary-dark">
                            View Loan
                        </a>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    <!-- Pagination -->
    {% if repayments.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 repayments.has_previous %}
                <a href="?page={{ repayments.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 repayments.has_next %}
                <a href="?page={{ repayments.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">{{ repayments.start_index }}</span>
                    to
                    <span class="font-medium">{{ repayments.end_index }}</span>
                    of
                    <span class="font-medium">{{ repayments.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 repayments.has_previous %}
                        <a href="?page={{ repayments.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 repayments.paginator.page_range %}
                        {% if repayments.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 repayments.has_next %}
                        <a href="?page={{ repayments.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 collections this month</h3>
        <p class="text-gray-500">No payments have been recorded for {{ current_month|date:"F Y" }}.</p>
        <div class="mt-6">
            <a href="{% url 'loans:repayments' %}" 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 Repayments
            </a>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}