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

{% block title %}Loan Statement - {{ loan.loan_number }}{% endblock %}

{% block content %}
<div class="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100 p-6">
    <div class="max-w-4xl mx-auto">
        <!-- Header -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-6">
            <div class="flex items-center justify-between mb-4">
                <div>
                    <h1 class="text-2xl font-bold text-gray-900">
                        <span>🧾</span>
                        Loan Statement
                    </h1>
                    <p class="text-gray-600">Loan Number: {{ loan.loan_number }}</p>
                </div>
                <div class="flex space-x-3">
                    {% if can_download %}
                    <div class="flex space-x-2">
                        <a href="{% url 'utils:download_report' 'loan_statement' 'pdf' %}?loan_id={{ loan.id }}" 
                           class="bg-red-600 hover:bg-red-700 text-white px-4 py-2 rounded-lg transition-colors">
                            <span>📄</span>PDF
                        </a>
                        <a href="{% url 'utils:download_report' 'loan_statement' 'excel' %}?loan_id={{ loan.id }}" 
                           class="bg-green-600 hover:bg-green-700 text-white px-4 py-2 rounded-lg transition-colors">
                            <span>📊</span>Excel
                        </a>
                        <a href="{% url 'utils:download_report' 'loan_statement' 'csv' %}?loan_id={{ loan.id }}" 
                           class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg transition-colors">
                            <i class="fas fa-file-csv mr-2"></i>CSV
                        </a>
                    </div>
                    {% endif %}
                    <a href="{% url 'loans:loan_detail' loan.id %}" 
                       class="bg-gray-600 hover:bg-gray-700 text-white px-4 py-2 rounded-lg transition-colors">
                        <span>←</span>Back to Loan
                    </a>
                </div>
            </div>
        </div>

        {% if statement_data and 'error' not in statement_data %}
        <!-- Loan Details -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-6">
            <h2 class="text-xl font-bold text-gray-900 mb-4">
                <span>ℹ️</span>
                Loan Details
            </h2>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="space-y-4">
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Borrower:</span>
                        <span class="text-gray-900">{{ statement_data.loan.borrower_name }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Phone:</span>
                        <span class="text-gray-900">{{ statement_data.loan.borrower_phone }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Principal Amount:</span>
                        <span class="text-gray-900 font-semibold">KES {{ statement_data.loan.principal_amount|floatformat:2 }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Interest Rate:</span>
                        <span class="text-gray-900">{{ statement_data.loan.interest_rate|floatformat:2 }}%</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Interest Amount:</span>
                        <span class="text-gray-900 font-semibold">KES {{ statement_data.loan.interest_amount|floatformat:2 }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Processing Fee:</span>
                        <span class="text-gray-900 font-semibold">KES {{ statement_data.loan.processing_fee|floatformat:2 }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Total Amount:</span>
                        <span class="text-gray-900 font-bold text-lg">KES {{ statement_data.loan.total_amount|floatformat:2 }}</span>
                    </div>
                </div>
                
                <div class="space-y-4">
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Loan Term:</span>
                        <span class="text-gray-900">{{ statement_data.loan.loan_term }} days</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Disbursement Date:</span>
                        <span class="text-gray-900">{{ statement_data.loan.disbursement_date|default:"N/A" }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Due Date:</span>
                        <span class="text-gray-900">{{ statement_data.loan.due_date|default:"N/A" }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Status:</span>
                        <span class="px-3 py-1 rounded-full text-sm font-medium
                            {% if statement_data.loan.status == 'active' %}bg-green-100 text-green-800
                            {% elif statement_data.loan.status == 'completed' %}bg-blue-100 text-blue-800
                            {% elif statement_data.loan.status == 'defaulted' %}bg-red-100 text-red-800
                            {% else %}bg-gray-100 text-gray-800{% endif %}">
                            {{ statement_data.loan.status|title }}
                        </span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Payment Summary -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-6">
            <h2 class="text-xl font-bold text-gray-900 mb-4">
                <span>🧮</span>
                Payment Summary
            </h2>
            
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div class="bg-blue-50 rounded-lg p-4">
                    <div class="text-sm font-medium text-blue-600 mb-1">Amount Paid</div>
                    <div class="text-2xl font-bold text-blue-900">KES {{ statement_data.loan.amount_paid|floatformat:2 }}</div>
                </div>
                
                <div class="bg-yellow-50 rounded-lg p-4">
                    <div class="text-sm font-medium text-yellow-600 mb-1">Outstanding Balance</div>
                    <div class="text-2xl font-bold text-yellow-900">KES {{ statement_data.loan.balance|floatformat:2 }}</div>
                </div>
                
                <div class="bg-purple-50 rounded-lg p-4">
                    <div class="text-sm font-medium text-purple-600 mb-1">Total Repayments</div>
                    <div class="text-2xl font-bold text-purple-900">{{ statement_data.summary.total_repayments }}</div>
                </div>
            </div>
            
            {% if statement_data.summary.days_overdue > 0 %}
            <div class="mt-4 bg-red-50 border border-red-200 rounded-lg p-4">
                <div class="flex items-center">
                    <span>⚠️</span>
                    <span class="font-medium text-red-800">
                        This loan is {{ statement_data.summary.days_overdue }} days overdue
                    </span>
                </div>
            </div>
            {% endif %}
        </div>

        <!-- Repayment History -->
        {% if statement_data.repayments %}
        <div class="bg-white rounded-xl shadow-lg p-6">
            <h2 class="text-xl font-bold text-gray-900 mb-4">
                <i class="fas fa-history text-purple-600 mr-2"></i>
                Repayment History
            </h2>
            
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Date</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Amount</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Method</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Reference</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Balance After</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        {% for repayment in statement_data.repayments %}
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ repayment.date }}</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-green-600">
                                KES {{ repayment.amount|floatformat:2 }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ repayment.method|title }}</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ repayment.reference|default:"N/A" }}</td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                KES {{ repayment.balance_after|floatformat:2 }}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
        {% else %}
        <div class="bg-white rounded-xl shadow-lg p-6">
            <div class="text-center py-8">
                <span>🧾</span>
                <h3 class="text-lg font-medium text-gray-900 mb-2">No Repayments Yet</h3>
                <p class="text-gray-600">No repayments have been recorded for this loan.</p>
            </div>
        </div>
        {% endif %}
        
        {% else %}
        <div class="bg-white rounded-xl shadow-lg p-6">
            <div class="text-center py-8">
                <span>⚠️</span>
                <h3 class="text-lg font-medium text-gray-900 mb-2">Error Loading Statement</h3>
                <p class="text-gray-600">{{ statement_data.error|default:"Unable to load loan statement data." }}</p>
            </div>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}