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

{% block title %}Loans Due Today{% endblock %}

{% block extra_css %}
<style>
    .metric-card {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 15px;
        padding: 20px;
        color: white;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        transition: transform 0.3s ease;
    }
    
    .metric-card:hover {
        transform: translateY(-5px);
    }
    
    .metric-card.registration {
        background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    }
    
    .metric-card.overdue {
        background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
    }
    
    .metric-card.due-today {
        background: linear-gradient(135deg, #4ecdc4 0%, #44a08d 100%);
    }
    
    .metric-value {
        font-size: 2.5rem;
        font-weight: 700;
        margin-bottom: 5px;
    }
    
    .metric-label {
        font-size: 0.9rem;
        opacity: 0.9;
    }
    
    .loan-card {
        background: white;
        border-radius: 12px;
        padding: 20px;
        margin-bottom: 15px;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
        border-left: 4px solid #e5e7eb;
        transition: all 0.3s ease;
    }
    
    .loan-card:hover {
        transform: translateX(5px);
        box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
    }
    
    .loan-card.overdue {
        border-left-color: #ef4444;
    }
    
    .loan-card.due-today {
        border-left-color: #f59e0b;
    }
    
    .registration-fee-card {
        background: white;
        border-radius: 12px;
        padding: 15px;
        margin-bottom: 10px;
        box-shadow: 0 2px 15px rgba(0, 0, 0, 0.06);
        border-left: 4px solid #8b5cf6;
        transition: all 0.3s ease;
    }
    
    .registration-fee-card:hover {
        transform: translateX(3px);
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    }
    
    .urgency-badge {
        padding: 4px 12px;
        border-radius: 20px;
        font-size: 0.75rem;
        font-weight: 600;
        text-transform: uppercase;
    }
    
    .urgency-high {
        background: #fee2e2;
        color: #dc2626;
    }
    
    .urgency-medium {
        background: #fef3c7;
        color: #d97706;
    }
    
    .urgency-low {
        background: #d1fae5;
        color: #059669;
    }
    
    .section-header {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        padding: 15px 20px;
        border-radius: 12px 12px 0 0;
        margin-bottom: 0;
    }
    
    .section-content {
        background: #f8fafc;
        padding: 20px;
        border-radius: 0 0 12px 12px;
        min-height: 200px;
    }
    
    .empty-state {
        text-align: center;
        padding: 40px 20px;
        color: #6b7280;
    }
    
    .empty-state i {
        font-size: 3rem;
        margin-bottom: 15px;
        opacity: 0.5;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Header -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h1 class="h3 mb-1">
                        <i class="fas fa-calendar-day text-primary me-2"></i>
                        Loans Due Today
                    </h1>
                    <p class="text-muted mb-0">{{ today|date:"F d, Y" }}</p>
                </div>
                <div class="d-flex gap-2">
                    <a href="{% url 'reports:enhanced_loans_due_report' %}" class="btn btn-outline-primary">
                        <span>📈</span>Enhanced View
                    </a>
                    <a href="{% url 'reports:reports_dashboard' %}" class="btn btn-secondary">
                        <span>←</span>Back to Reports
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Summary Metrics -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="metric-card due-today">
                <div class="metric-value">{{ summary.due_today_count }}</div>
                <div class="metric-label">Due Today</div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="metric-card overdue">
                <div class="metric-value">{{ summary.overdue_count }}</div>
                <div class="metric-label">Overdue</div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="metric-card">
                <div class="metric-value">KES {{ summary.total_amount_due|floatformat:0|intcomma }}</div>
                <div class="metric-label">Total Amount Due</div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="metric-card registration">
                <div class="metric-value">{{ summary.total_unpaid_registrations }}</div>
                <div class="metric-label">Unpaid Registration Fees</div>
            </div>
        </div>
    </div>

    <div class="row">
        <!-- Loans Due Today -->
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm">
                <div class="section-header">
                    <h5 class="mb-0">
                        <span>💵</span>
                        Loans Due Today ({{ summary.total_loans_due }})
                    </h5>
                </div>
                <div class="section-content">
                    {% if loans_data %}
                        {% for loan in loans_data %}
                        <div class="loan-card {{ loan.urgency }}">
                            <div class="row align-items-center">
                                <div class="col-md-6">
                                    <div class="d-flex align-items-center">
                                        <div class="me-3">
                                            <span class="urgency-badge urgency-{{ loan.urgency }}">
                                                {% if loan.days_overdue > 0 %}
                                                    {{ loan.days_overdue }} day{{ loan.days_overdue|pluralize }} overdue
                                                {% else %}
                                                    Due Today
                                                {% endif %}
                                            </span>
                                        </div>
                                        <div>
                                            <h6 class="mb-1">{{ loan.loan_number }}</h6>
                                            <p class="mb-1 text-muted">{{ loan.borrower_name }}</p>
                                            <small class="text-muted">{{ loan.product_name }}</small>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="text-end">
                                        <div class="fw-bold text-primary">KES {{ loan.outstanding_balance|floatformat:0|intcomma }}</div>
                                        <small class="text-muted">Outstanding</small>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="text-end">
                                        <a href="{% url 'loans:loan_detail' loan.id %}" class="btn btn-sm btn-outline-primary">
                                            <i class="fas fa-eye me-1"></i>View
                                        </a>
                                        <a href="tel:{{ loan.borrower_phone }}" class="btn btn-sm btn-success">
                                            <span>📞</span>Call
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    {% else %}
                        <div class="empty-state">
                            <span>✓</span>
                            <h5>No Loans Due Today</h5>
                            <p>Great! All loans are up to date.</p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- Registration Fees -->
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm">
                <div class="section-header">
                    <h5 class="mb-0">
                        <span>👤+</span>
                        Registration Fees ({{ summary.total_unpaid_registrations }})
                    </h5>
                </div>
                <div class="section-content">
                    {% if registration_fees_data %}
                        <div class="mb-3">
                            <div class="text-center p-3 bg-light rounded">
                                <h4 class="text-primary mb-1">KES {{ summary.total_registration_fees|floatformat:0|intcomma }}</h4>
                                <small class="text-muted">Total Outstanding</small>
                            </div>
                        </div>
                        
                        {% for fee in registration_fees_data %}
                        <div class="registration-fee-card">
                            <div class="d-flex justify-content-between align-items-start">
                                <div>
                                    <h6 class="mb-1">{{ fee.client_name }}</h6>
                                    <p class="mb-1 text-muted small">{{ fee.phone_number }}</p>
                                    <small class="text-muted">
                                        {{ fee.days_since_registration }} day{{ fee.days_since_registration|pluralize }} ago
                                    </small>
                                </div>
                                <div class="text-end">
                                    <div class="fw-bold text-primary">KES {{ fee.fee_amount|floatformat:0|intcomma }}</div>
                                    <a href="{% url 'users:client_detail' fee.id %}" class="btn btn-sm btn-outline-primary mt-1">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                        
                        {% if summary.total_unpaid_registrations > 20 %}
                        <div class="text-center mt-3">
                            <small class="text-muted">
                                Showing first 20 of {{ summary.total_unpaid_registrations }} unpaid registrations
                            </small>
                        </div>
                        {% endif %}
                    {% else %}
                        <div class="empty-state">
                            <span>✓</span>
                            <h6>No Unpaid Registration Fees</h6>
                            <p class="small">All registration fees are up to date.</p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
