{% extends "admin/change_form.html" %}
{% load static %}

{% block extrahead %}
{{ block.super }}
<style>
    .loan-preview-panel {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        padding: 24px;
        border-radius: 8px;
        margin: 20px 0;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    }
    
    .loan-preview-header {
        font-size: 18px;
        font-weight: bold;
        margin-bottom: 16px;
        display: flex;
        align-items: center;
    }
    
    .loan-preview-header i {
        margin-right: 10px;
    }
    
    .loan-preview-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 16px;
        margin-top: 16px;
    }
    
    .loan-preview-item {
        background: rgba(255, 255, 255, 0.15);
        padding: 12px;
        border-radius: 6px;
        backdrop-filter: blur(10px);
    }
    
    .loan-preview-label {
        font-size: 12px;
        opacity: 0.9;
        margin-bottom: 4px;
    }
    
    .loan-preview-value {
        font-size: 20px;
        font-weight: bold;
    }
    
    .borrower-info-panel {
        background: #f8f9fa;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        padding: 20px;
        margin: 20px 0;
    }
    
    .borrower-info-header {
        font-size: 16px;
        font-weight: bold;
        color: #333;
        margin-bottom: 12px;
        display: flex;
        align-items: center;
    }
    
    .borrower-info-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
        gap: 12px;
    }
    
    .borrower-info-item {
        display: flex;
        align-items: center;
        padding: 8px 0;
    }
    
    .borrower-info-label {
        font-weight: 500;
        color: #666;
        min-width: 120px;
    }
    
    .borrower-info-value {
        color: #333;
        font-weight: 600;
    }
    
    .action-buttons {
        background: #fff;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        padding: 16px;
        margin: 20px 0;
        display: flex;
        gap: 12px;
        flex-wrap: wrap;
    }
    
    .preview-action-btn {
        padding: 10px 20px;
        border-radius: 6px;
        border: none;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s;
        text-decoration: none;
        display: inline-block;
    }
    
    .btn-approve {
        background: #28a745;
        color: white;
    }
    
    .btn-approve:hover {
        background: #218838;
        color: white;
    }
    
    .btn-reject {
        background: #dc3545;
        color: white;
    }
    
    .btn-reject:hover {
        background: #c82333;
        color: white;
    }
    
    .btn-edit {
        background: #007bff;
        color: white;
    }
    
    .btn-edit:hover {
        background: #0056b3;
        color: white;
    }
    
    .warning-banner {
        background: #fff3cd;
        border: 1px solid #ffc107;
        border-left: 4px solid #ffc107;
        padding: 12px 16px;
        margin: 16px 0;
        border-radius: 4px;
    }
    
    .warning-banner-icon {
        color: #856404;
        margin-right: 8px;
    }
    
    .info-banner {
        background: #d1ecf1;
        border: 1px solid #17a2b8;
        border-left: 4px solid #17a2b8;
        padding: 12px 16px;
        margin: 16px 0;
        border-radius: 4px;
    }
    
    .loan-history-panel {
        background: #fff;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        padding: 20px;
        margin: 20px 0;
    }
    
    .loan-history-header {
        font-size: 16px;
        font-weight: bold;
        color: #333;
        margin-bottom: 16px;
        display: flex;
        align-items: center;
        border-bottom: 2px solid #667eea;
        padding-bottom: 8px;
    }
    
    .loan-history-stats {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
        gap: 16px;
        margin-bottom: 20px;
    }
    
    .stat-card {
        background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        padding: 16px;
        border-radius: 8px;
        text-align: center;
    }
    
    .stat-label {
        font-size: 12px;
        color: #666;
        margin-bottom: 4px;
    }
    
    .stat-value {
        font-size: 24px;
        font-weight: bold;
        color: #333;
    }
    
    .loan-history-table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 16px;
    }
    
    .loan-history-table th {
        background: #f8f9fa;
        padding: 12px;
        text-align: left;
        font-weight: 600;
        color: #495057;
        border-bottom: 2px solid #dee2e6;
    }
    
    .loan-history-table td {
        padding: 12px;
        border-bottom: 1px solid #dee2e6;
    }
    
    .loan-history-table tr:hover {
        background: #f8f9fa;
    }
    
    .status-badge {
        padding: 4px 8px;
        border-radius: 4px;
        font-size: 11px;
        font-weight: 600;
    }
    
    .status-active {
        background: #d4edda;
        color: #155724;
    }
    
    .status-paid {
        background: #cce5ff;
        color: #004085;
    }
    
    .status-defaulted {
        background: #f8d7da;
        color: #721c24;
    }
    
    .no-history {
        text-align: center;
        padding: 40px;
        color: #6c757d;
    }
</style>
{% endblock %}

{% block content %}
{% if original and original.status == 'pending' %}
<div class="loan-preview-panel">
    <div class="loan-preview-header">
        <span>🧮</span>
        Loan Application Preview
    </div>
    
    <div class="loan-preview-grid">
        <div class="loan-preview-item">
            <div class="loan-preview-label">Principal Amount</div>
            <div class="loan-preview-value">KES {{ original.requested_amount|floatformat:2|default:"0.00" }}</div>
        </div>
        
        <div class="loan-preview-item">
            <div class="loan-preview-label">Interest Amount</div>
            <div class="loan-preview-value">KES {{ original.interest_amount|floatformat:2|default:"0.00" }}</div>
        </div>
        
        <div class="loan-preview-item">
            <div class="loan-preview-label">Processing Fee</div>
            <div class="loan-preview-value">KES {{ original.processing_fee_amount|floatformat:2|default:"0.00" }}</div>
        </div>
        
        <div class="loan-preview-item">
            <div class="loan-preview-label">Total Amount</div>
            <div class="loan-preview-value">KES {{ original.total_amount|floatformat:2|default:"0.00" }}</div>
        </div>
        
        <div class="loan-preview-item">
            <div class="loan-preview-label">Duration</div>
            <div class="loan-preview-value">{{ original.requested_duration }} days</div>
        </div>
        
        <div class="loan-preview-item">
            <div class="loan-preview-label">Product</div>
            <div class="loan-preview-value">{{ original.loan_product.name }}</div>
        </div>
    </div>
</div>

<div class="borrower-info-panel">
    <div class="borrower-info-header">
        <span>👤</span>
        Borrower Information
    </div>
    
    <div class="borrower-info-grid">
        <div class="borrower-info-item">
            <span class="borrower-info-label">Name:</span>
            <span class="borrower-info-value">{{ original.borrower.get_full_name }}</span>
        </div>
        
        <div class="borrower-info-item">
            <span class="borrower-info-label">Phone:</span>
            <span class="borrower-info-value">{{ original.borrower.phone_number }}</span>
        </div>
        
        {% if original.borrower.id_number %}
        <div class="borrower-info-item">
            <span class="borrower-info-label">ID Number:</span>
            <span class="borrower-info-value">{{ original.borrower.id_number }}</span>
        </div>
        {% endif %}
        
        {% if original.borrower.branch %}
        <div class="borrower-info-item">
            <span class="borrower-info-label">Branch:</span>
            <span class="borrower-info-value">{{ original.borrower.branch.name }}</span>
        </div>
        {% endif %}
        
        {% if original.borrower.monthly_income %}
        <div class="borrower-info-item">
            <span class="borrower-info-label">Monthly Income:</span>
            <span class="borrower-info-value">KES {{ original.borrower.monthly_income|floatformat:2 }}</span>
        </div>
        {% endif %}
    </div>
</div>

<div class="loan-history-panel">
    <div class="loan-history-header">
        <i class="fas fa-history" style="margin-right: 8px;"></i>
        Previous Loans & Repayment History
    </div>
    
    <div class="loan-history-stats">
        <div class="stat-card">
            <div class="stat-label">Total Loans</div>
            <div class="stat-value">{{ previous_loans|length|default:0 }}</div>
        </div>
        
        <div class="stat-card">
            <div class="stat-label">Active Loans</div>
            <div class="stat-value">{{ active_loans_count|default:0 }}</div>
        </div>
        
        <div class="stat-card">
            <div class="stat-label">Completed Loans</div>
            <div class="stat-value">{{ completed_loans_count|default:0 }}</div>
        </div>
        
        <div class="stat-card">
            <div class="stat-label">Total Borrowed</div>
            <div class="stat-value" style="font-size: 18px;">KES {{ total_borrowed|floatformat:0|default:"0" }}</div>
        </div>
        
        <div class="stat-card">
            <div class="stat-label">Total Repaid</div>
            <div class="stat-value" style="font-size: 18px;">KES {{ total_repaid|floatformat:0|default:"0" }}</div>
        </div>
    </div>
    
    {% if previous_loans %}
    <table class="loan-history-table">
        <thead>
            <tr>
                <th>Loan Number</th>
                <th>Amount</th>
                <th>Disbursed</th>
                <th>Total Repaid</th>
                <th>Outstanding</th>
                <th>Status</th>
                <th>Repayment Rate</th>
            </tr>
        </thead>
        <tbody>
            {% for loan in previous_loans %}
            <tr>
                <td><strong>{{ loan.loan_number }}</strong></td>
                <td>KES {{ loan.principal_amount|floatformat:2 }}</td>
                <td>{{ loan.disbursement_date|date:"Y-m-d" }}</td>
                <td>KES {{ loan.amount_paid|floatformat:2 }}</td>
                <td>KES {{ loan.outstanding_amount|floatformat:2 }}</td>
                <td>
                    {% if loan.status == 'active' %}
                        <span class="status-badge status-active">Active</span>
                    {% elif loan.status == 'paid' %}
                        <span class="status-badge status-paid">Paid</span>
                    {% elif loan.status == 'defaulted' %}
                        <span class="status-badge status-defaulted">Defaulted</span>
                    {% else %}
                        <span class="status-badge">{{ loan.status|title }}</span>
                    {% endif %}
                </td>
                <td>
                    {% if loan.status == 'paid' %}
                        <strong style="color: #28a745;">100%</strong>
                    {% elif loan.status == 'active' %}
                        <strong style="color: #007bff;">{{ loan.repayment_percentage|default:"0" }}%</strong>
                    {% elif loan.status == 'defaulted' %}
                        <strong style="color: #dc3545;">{{ loan.repayment_percentage|default:"0" }}%</strong>
                    {% else %}
                        <strong style="color: #6c757d;">{{ loan.repayment_percentage|default:"0" }}%</strong>
                    {% endif %}
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    {% else %}
    <div class="no-history">
        <span>📥</span>
        <p style="margin: 0; font-size: 14px;">No previous loan history for this borrower</p>
        <p style="margin: 4px 0 0 0; font-size: 12px; color: #999;">This will be their first loan</p>
    </div>
    {% endif %}
</div>

<div class="info-banner">
    <span>ℹ️</span>
    <strong>Review Instructions:</strong> You can edit the loan amount, duration, and calculated fees below before approving. 
    Make any necessary adjustments and click "Save" to update the application, then approve it.
</div>
{% endif %}

{{ block.super }}
{% endblock %}
