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

{% block title %}Client Statement - {{ client.get_full_name }}{% endblock %}

{% block content %}
<div class="min-h-screen bg-gradient-to-br from-green-50 to-emerald-100 p-6">
    <div class="max-w-6xl 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>
                        Client Statement
                    </h1>
                    <p class="text-gray-600">{{ client.get_full_name }}</p>
                </div>
                <div class="flex space-x-3">
                    {% if can_download %}
                    <div class="flex space-x-2">
                        <a href="{% url 'utils:download_report' 'client_statement' 'pdf' %}?client_id={{ client.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' 'client_statement' 'excel' %}?client_id={{ client.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' 'client_statement' 'csv' %}?client_id={{ client.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 'users:client_detail' client.id %}" 
                       class="bg-gray-600 hover:bg-gray-700 text-white px-4 py-2 rounded-lg transition-colors">
                        <span>←</span>Back to Client
                    </a>
                </div>
            </div>
        </div>

        {% if statement_data and 'error' not in statement_data %}
        <!-- Client Information -->
        <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>
                Client Information
            </h2>
            
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                <div class="space-y-4">
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Full Name:</span>
                        <span class="text-gray-900">{{ statement_data.client.name }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Phone:</span>
                        <span class="text-gray-900">{{ statement_data.client.phone }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Email:</span>
                        <span class="text-gray-900">{{ statement_data.client.email|default:"N/A" }}</span>
                    </div>
                </div>
                
                <div class="space-y-4">
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">ID Number:</span>
                        <span class="text-gray-900">{{ statement_data.client.id_number|default:"N/A" }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Monthly Income:</span>
                        <span class="text-gray-900 font-semibold">KES {{ statement_data.client.monthly_income|floatformat:2 }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Credit Score:</span>
                        <span class="text-gray-900">{{ statement_data.client.credit_score|default:"N/A" }}</span>
                    </div>
                </div>
                
                <div class="space-y-4">
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Member Since:</span>
                        <span class="text-gray-900">{{ statement_data.client.member_since }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="font-medium text-gray-700">Address:</span>
                        <span class="text-gray-900">{{ statement_data.client.address|default:"N/A" }}</span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Loan 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>
                Loan Portfolio Summary
            </h2>
            
            <div class="grid grid-cols-1 md:grid-cols-2 lg: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">Total Loans</div>
                    <div class="text-2xl font-bold text-blue-900">{{ statement_data.summary.total_loans }}</div>
                </div>
                
                <div class="bg-green-50 rounded-lg p-4">
                    <div class="text-sm font-medium text-green-600 mb-1">Active Loans</div>
                    <div class="text-2xl font-bold text-green-900">{{ statement_data.summary.active_loans }}</div>
                </div>
                
                <div class="bg-purple-50 rounded-lg p-4">
                    <div class="text-sm font-medium text-purple-600 mb-1">Completed Loans</div>
                    <div class="text-2xl font-bold text-purple-900">{{ statement_data.summary.completed_loans }}</div>
                </div>
                
                <div class="bg-yellow-50 rounded-lg p-4">
                    <div class="text-sm font-medium text-yellow-600 mb-1">Total Borrowed</div>
                    <div class="text-2xl font-bold text-yellow-900">KES {{ statement_data.summary.total_borrowed|floatformat:2 }}</div>
                </div>
                
                <div class="bg-indigo-50 rounded-lg p-4">
                    <div class="text-sm font-medium text-indigo-600 mb-1">Total Repaid</div>
                    <div class="text-2xl font-bold text-indigo-900">KES {{ statement_data.summary.total_repaid|floatformat:2 }}</div>
                </div>
                
                <div class="bg-red-50 rounded-lg p-4">
                    <div class="text-sm font-medium text-red-600 mb-1">Outstanding Balance</div>
                    <div class="text-2xl font-bold text-red-900">KES {{ statement_data.summary.outstanding_balance|floatformat:2 }}</div>
                </div>
            </div>
        </div>

        <!-- Loan History -->
        {% if statement_data.loans %}
        <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>
                Loan 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">Loan Number</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Principal</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Disbursed</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Due Date</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Balance</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        {% for loan in statement_data.loans %}
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-blue-600">
                                {{ loan.loan_number }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                KES {{ loan.principal_amount|floatformat:2 }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                {{ loan.disbursement_date|default:"N/A" }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                {{ loan.due_date|default:"N/A" }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <span class="px-3 py-1 rounded-full text-sm font-medium
                                    {% if loan.status == 'active' %}bg-green-100 text-green-800
                                    {% elif loan.status == 'completed' %}bg-blue-100 text-blue-800
                                    {% elif loan.status == 'defaulted' %}bg-red-100 text-red-800
                                    {% else %}bg-gray-100 text-gray-800{% endif %}">
                                    {{ loan.status|title }}
                                </span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                KES {{ loan.balance|floatformat:2 }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                <a href="{% url 'utils:loan_statement_view' loan.id %}" 
                                   class="text-blue-600 hover:text-blue-900">
                                    <span>🧾</span>Statement
                                </a>
                            </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 Loans Yet</h3>
                <p class="text-gray-600">This client has not taken any loans yet.</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 client statement data." }}</p>
            </div>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}