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

{% block title %}Missed Payments - {{ client.get_full_name }}{% endblock %}

{% block extra_css %}
<style>
    .client-header {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 15px;
        padding: 30px;
        color: white;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        margin-bottom: 30px;
    }
    
    .client-info {
        display: flex;
        align-items: center;
        gap: 20px;
    }
    
    .client-avatar {
        width: 80px;
        height: 80px;
        border-radius: 50%;
        background: white;
        color: #667eea;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 2rem;
        font-weight: bold;
    }
    
    .metric-card {
        background: white;
        border-radius: 12px;
        padding: 20px;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
        transition: transform 0.3s ease;
    }
    
    .metric-card:hover {
        transform: translateY(-5px);
    }
    
    .metric-value {
        font-size: 2rem;
        font-weight: 700;
        color: #667eea;
        margin-bottom: 5px;
    }
    
    .metric-label {
        font-size: 0.9rem;
        color: #6b7280;
    }
    
    .payment-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;
    }
    
    .payment-card:hover {
        transform: translateX(5px);
        box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
    }
    
    .payment-card.critical {
        border-left-color: #ef4444;
        background: #fef2f2;
    }
    
    .payment-card.warning {
        border-left-color: #f97316;
        background: #fff7ed;
    }
    
    .payment-card.normal {
        border-left-color: #f59e0b;
        background: #fffbeb;
    }
    
    .overdue-badge {
        padding: 6px 12px;
        border-radius: 20px;
        font-size: 0.75rem;
        font-weight: 600;
        text-transform: uppercase;
    }
    
    .overdue-critical {
        background: #fee2e2;
        color: #dc2626;
    }
    
    .overdue-warning {
        background: #fed7aa;
        color: #ea580c;
    }
    
    .overdue-normal {
        background: #fef3c7;
        color: #d97706;
    }
    
    .empty-state {
        text-align: center;
        padding: 60px 20px;
        color: #6b7280;
    }
    
    .empty-state i {
        font-size: 4rem;
        margin-bottom: 20px;
        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 mb-3">
                <div>
                    <h1 class="h3 mb-1">
                        <span>⏰</span>
                        Missed Payments Detail
                    </h1>
                    <p class="text-muted mb-0">{{ today|date:"F d, Y" }}</p>
                </div>
                <div class="d-flex gap-2">
                    <a href="{% url 'reports:missed_payments_report' %}" class="btn btn-secondary">
                        <span>←</span> Back to Missed Payments
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Client Header -->
    <div class="client-header">
        <div class="client-info">
            <div class="client-avatar">
                {{ client.first_name.0|upper }}{{ client.last_name.0|upper }}
            </div>
            <div>
                <h2 class="mb-1">{{ client.get_full_name }}</h2>
                <p class="mb-1 opacity-90">
                    <i class="fas fa-phone me-2"></i>{{ client.phone_number }}
                </p>
                <p class="mb-0 opacity-90">
                    <i class="fas fa-envelope me-2"></i>{{ client.email|default:"No email" }}
                </p>
            </div>
        </div>
    </div>

    <!-- Summary Metrics -->
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="metric-card">
                <div class="metric-value">{{ total_missed_count }}</div>
                <div class="metric-label">Total Missed Payments</div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="metric-card">
                <div class="metric-value">KES {{ total_outstanding|floatformat:0|intcomma }}</div>
                <div class="metric-label">Total Outstanding</div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="metric-card">
                <div class="metric-value">
                    {% if most_overdue %}
                        {{ most_overdue.days_overdue }} days
                    {% else %}
                        0 days
                    {% endif %}
                </div>
                <div class="metric-label">Most Overdue</div>
            </div>
        </div>
    </div>

    <!-- Missed Payments List -->
    <div class="row">
        <div class="col-12">
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-gradient-primary text-white">
                    <h5 class="mb-0">
                        <i class="fas fa-list me-2"></i>
                        All Missed Payments ({{ total_missed_count }})
                    </h5>
                </div>
                <div class="card-body">
                    {% if missed_payments %}
                        {% for payment in missed_payments %}
                        <div class="payment-card {% if payment.days_overdue > 30 %}critical{% elif payment.days_overdue > 14 %}warning{% else %}normal{% endif %}">
                            <div class="row align-items-center">
                                <div class="col-md-3">
                                    <h6 class="mb-1">{{ payment.loan_number }}</h6>
                                    <small class="text-muted">{{ payment.loan_product }}</small>
                                </div>
                                <div class="col-md-2">
                                    <div class="fw-bold">{{ payment.due_date|date:"M d, Y" }}</div>
                                    <small class="text-muted">Due Date</small>
                                </div>
                                <div class="col-md-2">
                                    <div class="fw-bold text-danger">KES {{ payment.amount_due|floatformat:0|intcomma }}</div>
                                    <small class="text-muted">Amount Due</small>
                                </div>
                                <div class="col-md-2">
                                    <span class="overdue-badge {% if payment.days_overdue > 30 %}overdue-critical{% elif payment.days_overdue > 14 %}overdue-warning{% else %}overdue-normal{% endif %}">
                                        {{ payment.days_overdue }} days overdue
                                    </span>
                                </div>
                                <div class="col-md-3 text-end">
                                    <a href="{% url 'loans:loan_detail' payment.loan.id %}" class="btn btn-sm btn-outline-primary">
                                        <i class="fas fa-eye me-1"></i>View Loan
                                    </a>
                                    <a href="tel:{{ client.phone_number }}" class="btn btn-sm btn-success">
                                        <span>📞</span> Call
                                    </a>
                                </div>
                            </div>
                            
                            <!-- Additional loan details -->
                            <div class="row mt-3 pt-3 border-top">
                                <div class="col-md-4">
                                    <small class="text-muted">Total Amount:</small>
                                    <div class="fw-bold">KES {{ payment.total_amount|floatformat:0|intcomma }}</div>
                                </div>
                                <div class="col-md-4">
                                    <small class="text-muted">Amount Paid:</small>
                                    <div class="fw-bold text-success">KES {{ payment.amount_paid|floatformat:0|intcomma }}</div>
                                </div>
                                <div class="col-md-4">
                                    <small class="text-muted">Status:</small>
                                    <div class="fw-bold text-danger">{{ payment.payment_status|upper }}</div>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    {% else %}
                        <div class="empty-state">
                            <i class="fas fa-check-circle text-success"></i>
                            <h5>No Missed Payments</h5>
                            <p>This client has no missed payments. All loans are up to date!</p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
