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

{% block title %}Delinquent Loans - Management Dashboard{% endblock %}

{% block extra_css %}
<style>
    * {
        box-sizing: border-box;
    }

    body {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        min-height: 100vh;
        font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    }

    .main-container {
        background: rgba(255, 255, 255, 0.95);
        backdrop-filter: blur(10px);
        border-radius: 20px;
        margin: 2rem;
        padding: 2rem;
        box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    }

    .page-header {
        text-align: center;
        margin-bottom: 3rem;
        padding: 2rem 0;
        background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
        color: white;
        border-radius: 16px;
        position: relative;
        overflow: hidden;
    }

    .page-header::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="25" cy="25" r="1" fill="white" opacity="0.1"/><circle cx="75" cy="75" r="1" fill="white" opacity="0.1"/><circle cx="50" cy="10" r="0.5" fill="white" opacity="0.1"/><circle cx="10" cy="60" r="0.5" fill="white" opacity="0.1"/><circle cx="90" cy="40" r="0.5" fill="white" opacity="0.1"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
        opacity: 0.3;
    }

    .page-title {
        font-size: 2.5rem;
        font-weight: 800;
        margin-bottom: 0.5rem;
        position: relative;
        z-index: 1;
    }

    .page-subtitle {
        font-size: 1.1rem;
        opacity: 0.9;
        position: relative;
        z-index: 1;
    }

    .metrics-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
        gap: 1.5rem;
        margin-bottom: 3rem;
    }

    .metric-card {
        background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
        transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
        color: white;
        border-radius: 16px;
        padding: 2rem;
        box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .metric-card::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: linear-gradient(45deg, rgba(255,255,255,0.1) 0%, transparent 100%);
        opacity: 0;
        transition: opacity 0.3s ease;
    }

    .metric-card:hover {
        transform: translateY(-8px) scale(1.02);
        box-shadow: 0 20px 40px -10px rgba(239, 68, 68, 0.4);
    }

    .metric-card:hover::before {
        opacity: 1;
    }

    .metric-card.success {
        background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    }

    .metric-card.warning {
        background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
    }

    .metric-card.info {
        background: linear-gradient(135deg, #06b6d4 0%, #0891b2 100%);
    }

    .metric-value {
        font-size: 2.5rem;
        font-weight: 800;
        margin-bottom: 0.5rem;
        position: relative;
        z-index: 1;
    }

    .metric-label {
        font-size: 0.95rem;
        opacity: 0.9;
        font-weight: 600;
        position: relative;
        z-index: 1;
    }

    .metric-icon {
        position: absolute;
        top: 1rem;
        right: 1rem;
        font-size: 2rem;
        opacity: 0.3;
    }

    .filter-section {
        background: white;
        border-radius: 16px;
        padding: 2rem;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        border: 1px solid #e5e7eb;
        margin-bottom: 2rem;
    }

    .filter-title {
        font-size: 1.25rem;
        font-weight: 700;
        color: #1f2937;
        margin-bottom: 1.5rem;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .filter-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 1rem;
    }

    .filter-group {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
    }

    .filter-label {
        font-size: 0.875rem;
        font-weight: 600;
        color: #374151;
    }

    .filter-select, .filter-input {
        padding: 0.75rem;
        border: 2px solid #e5e7eb;
        border-radius: 8px;
        font-size: 0.875rem;
        transition: all 0.2s;
        background: white;
    }

    .filter-select:focus, .filter-input:focus {
        outline: none;
        border-color: #ef4444;
        box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1);
    }

    .data-table-container {
        background: white;
        border-radius: 16px;
        overflow: hidden;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        border: 1px solid #e5e7eb;
    }

    .table-header {
        background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
        padding: 1.5rem;
        border-bottom: 1px solid #e5e7eb;
    }

    .table-title {
        font-size: 1.5rem;
        font-weight: 700;
        color: #1f2937;
        margin-bottom: 0.5rem;
    }

    .table-subtitle {
        color: #6b7280;
        font-size: 0.875rem;
    }

    .data-table {
        width: 100%;
        border-collapse: collapse;
    }

    .data-table th {
        background: #f8fafc;
        padding: 1rem;
        text-align: left;
        font-weight: 600;
        color: #374151;
        border-bottom: 2px solid #e5e7eb;
        font-size: 0.875rem;
        text-transform: uppercase;
        letter-spacing: 0.05em;
    }

    .data-table td {
        padding: 1rem;
        border-bottom: 1px solid #f3f4f6;
        vertical-align: middle;
    }

    .data-table tbody tr:hover {
        background: #f8fafc;
    }

    .delinquency-badge {
        padding: 0.5rem 1rem;
        border-radius: 9999px;
        font-size: 0.75rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        display: inline-block;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    .delinquency-mild {
        background: linear-gradient(135deg, #dcfce7 0%, #bbf7d0 100%);
        color: #166534;
    }

    .delinquency-moderate {
        background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
        color: #92400e;
    }

    .delinquency-severe {
        background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%);
        color: #991b1b;
    }

    .delinquency-critical {
        background: linear-gradient(135deg, #fecaca 0%, #fca5a5 100%);
        color: #7f1d1d;
    }

    .action-btn {
        padding: 0.5rem 1rem;
        border-radius: 8px;
        font-size: 0.75rem;
        font-weight: 600;
        transition: all 0.2s;
        border: none;
        cursor: pointer;
        display: inline-flex;
        align-items: center;
        gap: 0.25rem;
    }

    .action-btn.primary {
        background: linear-gradient(135deg, #3b82f6 0%, #1e40af 100%);
        color: white;
    }

    .action-btn.primary:hover {
        transform: translateY(-1px);
        box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
    }

    .action-btn.success {
        background: linear-gradient(135deg, #10b981 0%, #059669 100%);
        color: white;
    }

    .action-btn.success:hover {
        transform: translateY(-1px);
        box-shadow: 0 4px 12px rgba(16, 185, 129, 0.4);
    }

    .action-btn.warning {
        background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
        color: white;
    }

    .action-btn.warning:hover {
        transform: translateY(-1px);
        box-shadow: 0 4px 12px rgba(245, 158, 11, 0.4);
    }

    .amount-cell {
        font-weight: 700;
        font-size: 1.1rem;
    }

    .days-overdue {
        font-weight: 600;
        padding: 0.25rem 0.75rem;
        border-radius: 9999px;
        font-size: 0.75rem;
        background: #fee2e2;
        color: #991b1b;
    }

    .no-data {
        text-align: center;
        padding: 3rem;
        color: #6b7280;
    }

    .no-data-icon {
        font-size: 3rem;
        margin-bottom: 1rem;
        opacity: 0.5;
    }

    .export-buttons {
        display: flex;
        gap: 1rem;
        margin-bottom: 2rem;
        justify-content: flex-end;
    }

    .export-btn {
        padding: 0.75rem 1.5rem;
        border-radius: 8px;
        font-weight: 600;
        transition: all 0.2s;
        border: none;
        cursor: pointer;
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
    }

    .export-btn.excel {
        background: linear-gradient(135deg, #10b981 0%, #059669 100%);
        color: white;
    }

    .export-btn.pdf {
        background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
        color: white;
    }

    .export-btn:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    }

    @media (max-width: 768px) {
        .main-container {
            margin: 1rem;
            padding: 1rem;
        }
        
        .page-title {
            font-size: 2rem;
        }
        
        .metrics-grid {
            grid-template-columns: 1fr;
        }
        
        .filter-grid {
            grid-template-columns: 1fr;
        }
        
        .data-table {
            font-size: 0.75rem;
        }
        
        .data-table th,
        .data-table td {
            padding: 0.5rem;
        }
    }
</style>
{% endblock %}

{% block content %}
<div class="main-container">
    <!-- Page Header -->
    <div class="page-header">
        <h1 class="page-title">
            <span>⚠️</span>
            Delinquent Loans
        </h1>
        <p class="page-subtitle">Track and manage overdue loan accounts by severity</p>
    </div>

    <!-- Summary Metrics -->
    <div class="metrics-grid">
        <div class="metric-card danger">
            <div class="metric-icon">
                <span>⚠️</span>
            </div>
            <div class="metric-value">{{ report_data.summary.total_delinquent_loans|intcomma }}</div>
            <div class="metric-label">Total Delinquent Loans</div>
        </div>
        
        <div class="metric-card warning">
            <div class="metric-icon">
                <span>$</span>
            </div>
            <div class="metric-value">KSh {{ report_data.summary.total_delinquent_amount|intcomma }}</div>
            <div class="metric-label">Total Delinquent Amount</div>
        </div>
        
        <div class="metric-card info">
            <div class="metric-icon">
                <span>📈</span>
            </div>
            <div class="metric-value">KSh {{ report_data.summary.average_delinquent_amount|intcomma }}</div>
            <div class="metric-label">Average Delinquent Amount</div>
        </div>
        
        <div class="metric-card success">
            <div class="metric-icon">
                <span>⏰</span>
            </div>
            <div class="metric-value">{{ report_data.delinquency_breakdown.mild_count|intcomma }}</div>
            <div class="metric-label">Mild Delinquencies</div>
        </div>
    </div>

    <!-- Filters -->
    <div class="filter-section">
        <h3 class="filter-title">
            <span>🔍</span>
            Filter & Search
        </h3>
        <form method="GET" class="filter-grid">
            <div class="filter-group">
                <label class="filter-label">Days Overdue</label>
                <select name="days_filter" class="filter-select">
                    <option value="">All Periods</option>
                    <option value="1_30" {% if request.GET.days_filter == '1_30' %}selected{% endif %}>1-30 Days</option>
                    <option value="31_60" {% if request.GET.days_filter == '31_60' %}selected{% endif %}>31-60 Days</option>
                    <option value="61_90" {% if request.GET.days_filter == '61_90' %}selected{% endif %}>61-90 Days</option>
                    <option value="90_plus" {% if request.GET.days_filter == '90_plus' %}selected{% endif %}>90+ Days</option>
                </select>
            </div>
            
            <div class="filter-group">
                <label class="filter-label">Sort By</label>
                <select name="sort_by" class="filter-select">
                    <option value="days_desc" {% if request.GET.sort_by == 'days_desc' %}selected{% endif %}>Days Overdue (High to Low)</option>
                    <option value="days_asc" {% if request.GET.sort_by == 'days_asc' %}selected{% endif %}>Days Overdue (Low to High)</option>
                    <option value="amount_desc" {% if request.GET.sort_by == 'amount_desc' %}selected{% endif %}>Amount (High to Low)</option>
                    <option value="amount_asc" {% if request.GET.sort_by == 'amount_asc' %}selected{% endif %}>Amount (Low to High)</option>
                </select>
            </div>
            
            <div class="filter-group">
                <label class="filter-label">Start Date</label>
                <input type="date" name="start_date" class="filter-input" value="{{ request.GET.start_date }}">
            </div>
            
            <div class="filter-group">
                <label class="filter-label">End Date</label>
                <input type="date" name="end_date" class="filter-input" value="{{ request.GET.end_date }}">
            </div>
            
            <div class="filter-group">
                <button type="submit" class="action-btn primary" style="margin-top: 1.5rem;">
                    <span>🔍</span>
                    Apply Filters
                </button>
            </div>
        </form>
    </div>

    <!-- Export Buttons -->
    <div class="export-buttons">
        <a href="?export=excel" class="export-btn excel">
            <span>📊</span>
            Export to Excel
        </a>
        <a href="?export=pdf" class="export-btn pdf">
            <span>📄</span>
            Export to PDF
        </a>
    </div>

    <!-- Data Table -->
    <div class="data-table-container">
        <div class="table-header">
            <h3 class="table-title">Delinquency Details</h3>
            <p class="table-subtitle">{{ report_data.loans|length }} loan{{ report_data.loans|length|pluralize }} found</p>
        </div>
        
        {% if report_data.loans %}
        <table class="data-table">
            <thead>
                <tr>
                    <th>Loan Details</th>
                    <th>Outstanding Balance</th>
                    <th>Days Overdue</th>
                    <th>Delinquency Level</th>
                    <th>Last Payment</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                {% for loan in report_data.loans %}
                <tr>
                    <td>
                        <div style="font-weight: 600; color: #111827;">{{ loan.loan_number }}</div>
                        <div style="font-size: 0.875rem; color: #6b7280;">{{ loan.borrower_name }}</div>
                        <div style="font-size: 0.875rem; color: #6b7280;">{{ loan.borrower_phone }}</div>
                        <div style="font-size: 0.875rem; color: #6b7280;">Due: {{ loan.due_date|date:"M d, Y" }}</div>
                    </td>
                    <td class="amount-cell" style="color: #dc2626;">
                        KSh {{ loan.outstanding_balance|floatformat:0|intcomma }}
                    </td>
                    <td style="text-align: center;">
                        <span class="days-overdue">
                            {{ loan.days_overdue }} days
                        </span>
                    </td>
                    <td style="text-align: center;">
                        <span class="delinquency-badge delinquency-{{ loan.delinquency_level|default:'mild' }}">
                            {{ loan.delinquency_label|default:'Mild' }}
                        </span>
                    </td>
                    <td>
                        {% if loan.last_payment_date %}
                        <div style="font-size: 0.875rem; color: #111827;">{{ loan.last_payment_date|date:"M d, Y" }}</div>
                        <div style="font-size: 0.875rem; color: #6b7280;">KSh {{ loan.last_payment_amount|floatformat:0|intcomma }}</div>
                        {% else %}
                        <span style="font-size: 0.875rem; color: #9ca3af;">No payments</span>
                        {% endif %}
                    </td>
                    <td style="text-align: center;">
                        <div style="display: flex; justify-content: center; gap: 0.5rem;">
                            <button onclick="contactBorrower('{{ loan.id }}')" class="action-btn primary" title="Contact">
                                <span>📞</span>
                            </button>
                            <button onclick="schedulePayment('{{ loan.id }}')" class="action-btn success" title="Schedule Payment">
                                <i class="fas fa-calendar-plus"></i>
                            </button>
                            <button onclick="viewDetails('{{ loan.id }}')" class="action-btn warning" title="View Details">
                                <i class="fas fa-eye"></i>
                            </button>
                        </div>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        {% else %}
        <div class="no-data">
            <div class="no-data-icon">
                <span>✓</span>
            </div>
            <h3>No Delinquent Loans</h3>
            <p>All loans are current with their payments.</p>
        </div>
        {% endif %}
    </div>
</div>

<script>
function contactBorrower(loanId) {
    alert('Contact borrower functionality will be implemented soon for loan: ' + loanId);
}

function schedulePayment(loanId) {
    alert('Schedule payment functionality will be implemented soon for loan: ' + loanId);
}

function viewDetails(loanId) {
    alert('View details functionality will be implemented soon for loan: ' + loanId);
}
</script>
{% endblock %}
