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

{% block title %}Missed Payments Report{% 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.daily {
        background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
    }
    
    .metric-card.weekly {
        background: linear-gradient(135deg, #f97316 0%, #ea580c 100%);
    }
    
    .metric-card.monthly {
        background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
    }
    
    .metric-card.total {
        background: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 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.daily {
        border-left-color: #ef4444;
    }
    
    .loan-card.weekly {
        border-left-color: #f97316;
    }
    
    .loan-card.monthly {
        border-left-color: #f59e0b;
    }
    
    .missed-badge {
        padding: 4px 12px;
        border-radius: 20px;
        font-size: 0.75rem;
        font-weight: 600;
        text-transform: uppercase;
    }
    
    .missed-high {
        background: #fee2e2;
        color: #dc2626;
    }
    
    .missed-medium {
        background: #fef3c7;
        color: #d97706;
    }
    
    .missed-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;
    }
    
    .missed-periods {
        background: #f3f4f6;
        border-radius: 8px;
        padding: 10px;
        margin-top: 10px;
    }
    
    .missed-period-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 5px 0;
        border-bottom: 1px solid #e5e7eb;
    }
    
    .missed-period-item:last-child {
        border-bottom: none;
    }
</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">
                        <span>⏰</span>
                        Missed Payments Report
                    </h1>
                    <p class="text-muted mb-0">{{ today|date:"F d, Y" }}</p>
                </div>
                <div class="d-flex gap-2">
                    <a href="?export=excel" class="btn btn-success">
                        <span>📊</span> Export Excel
                    </a>
                    <a href="?export=pdf" class="btn btn-danger">
                        <span>📄</span> Export PDF
                    </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 daily">
                <div class="metric-value">{{ summary.total_daily_missed }}</div>
                <div class="metric-label">Daily Missed</div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="metric-card weekly">
                <div class="metric-value">{{ summary.total_weekly_missed }}</div>
                <div class="metric-label">Weekly Missed</div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="metric-card monthly">
                <div class="metric-value">{{ summary.total_monthly_missed }}</div>
                <div class="metric-label">Monthly Missed</div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="metric-card total">
                <div class="metric-value">KES {{ summary.total_outstanding_missed|floatformat:0|intcomma }}</div>
                <div class="metric-label">Total Outstanding</div>
            </div>
        </div>
    </div>

    <div class="row">
        <!-- Daily Missed Payments -->
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm">
                <div class="section-header">
                    <h5 class="mb-0">
                        <i class="fas fa-calendar-day me-2"></i>
                        Daily Missed ({{ summary.total_daily_missed }})
                    </h5>
                </div>
                <div class="section-content">
                    {% if daily_missed %}
                        {% for loan_data in daily_missed %}
                        <div class="loan-card daily">
                            <div class="d-flex justify-content-between align-items-start mb-2">
                                <div>
                                    <h6 class="mb-1">{{ loan_data.loan.loan_number }}</h6>
                                    <p class="mb-1 text-muted">{{ loan_data.borrower_name }}</p>
                                    <small class="text-muted">{{ loan_data.product_name }}</small>
                                </div>
                                <span class="missed-badge missed-high">
                                    {{ loan_data.total_missed_periods }} missed
                                </span>
                            </div>
                            
                            <div class="row align-items-center">
                                <div class="col-12">
                                    <div class="fw-bold text-primary">KES {{ loan_data.outstanding_balance|floatformat:0|intcomma }}</div>
                                    <small class="text-muted">Outstanding</small>
                                </div>
                            </div>
                            
                            <div class="row mt-2">
                                <div class="col-12 text-end">
                                    <a href="{% url 'reports:missed_payments_detail' loan_data.borrower_id %}" class="btn btn-sm btn-info me-1">
                                        <i class="fas fa-list me-1"></i>View All Missed Payments
                                    </a>
                                    <a href="{% url 'loans:loan_detail' loan_data.loan.id %}" class="btn btn-sm btn-outline-primary me-1">
                                        <i class="fas fa-eye me-1"></i>View
                                    </a>
                                    <a href="tel:{{ loan_data.borrower_phone }}" class="btn btn-sm btn-success">
                                        <span>📞</span>Call
                                    </a>
                                </div>
                            </div>
                            
                            {% if loan_data.missed_periods %}
                            <div class="missed-periods">
                                <small class="text-muted fw-bold">Missed Dates:</small>
                                {% for period in loan_data.missed_periods|slice:":3" %}
                                <div class="missed-period-item">
                                    <span>{{ period.due_date|date:"M d, Y" }}</span>
                                    <span class="text-danger">KES {{ period.expected_amount|floatformat:0|intcomma }}</span>
                                </div>
                                {% endfor %}
                                {% if loan_data.missed_periods|length > 3 %}
                                <div class="text-center mt-2">
                                    <small class="text-muted">+{{ loan_data.missed_periods|length|add:"-3" }} more</small>
                                </div>
                                {% endif %}
                            </div>
                            {% endif %}
                        </div>
                        {% endfor %}
                    {% else %}
                        <div class="empty-state">
                            <span>✓</span>
                            <h6>No Daily Missed Payments</h6>
                            <p class="small">All daily payment borrowers are up to date.</p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- Weekly Missed Payments -->
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm">
                <div class="section-header">
                    <h5 class="mb-0">
                        <i class="fas fa-calendar-week me-2"></i>
                        Weekly Missed ({{ summary.total_weekly_missed }})
                    </h5>
                </div>
                <div class="section-content">
                    {% if weekly_missed %}
                        {% for loan_data in weekly_missed %}
                        <div class="loan-card weekly">
                            <div class="d-flex justify-content-between align-items-start mb-2">
                                <div>
                                    <h6 class="mb-1">{{ loan_data.loan.loan_number }}</h6>
                                    <p class="mb-1 text-muted">{{ loan_data.borrower_name }}</p>
                                    <small class="text-muted">{{ loan_data.product_name }}</small>
                                </div>
                                <span class="missed-badge missed-medium">
                                    {{ loan_data.total_missed_periods }} missed
                                </span>
                            </div>
                            
                            <div class="row align-items-center">
                                <div class="col-12">
                                    <div class="fw-bold text-primary">KES {{ loan_data.outstanding_balance|floatformat:0|intcomma }}</div>
                                    <small class="text-muted">Outstanding</small>
                                </div>
                            </div>
                            
                            <div class="row mt-2">
                                <div class="col-12 text-end">
                                    <a href="{% url 'reports:missed_payments_detail' loan_data.borrower_id %}" class="btn btn-sm btn-info me-1">
                                        <i class="fas fa-list me-1"></i>View All Missed Payments
                                    </a>
                                    <a href="{% url 'loans:loan_detail' loan_data.loan.id %}" class="btn btn-sm btn-outline-primary me-1">
                                        <i class="fas fa-eye me-1"></i>View
                                    </a>
                                    <a href="tel:{{ loan_data.borrower_phone }}" class="btn btn-sm btn-success">
                                        <span>📞</span>Call
                                    </a>
                                </div>
                            </div>
                            
                            {% if loan_data.missed_periods %}
                            <div class="missed-periods">
                                <small class="text-muted fw-bold">Missed Dates:</small>
                                {% for period in loan_data.missed_periods|slice:":3" %}
                                <div class="missed-period-item">
                                    <span>{{ period.due_date|date:"M d, Y" }}</span>
                                    <span class="text-danger">KES {{ period.expected_amount|floatformat:0|intcomma }}</span>
                                </div>
                                {% endfor %}
                                {% if loan_data.missed_periods|length > 3 %}
                                <div class="text-center mt-2">
                                    <small class="text-muted">+{{ loan_data.missed_periods|length|add:"-3" }} more</small>
                                </div>
                                {% endif %}
                            </div>
                            {% endif %}
                        </div>
                        {% endfor %}
                    {% else %}
                        <div class="empty-state">
                            <span>✓</span>
                            <h6>No Weekly Missed Payments</h6>
                            <p class="small">All weekly payment borrowers are up to date.</p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- Monthly Missed Payments -->
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm">
                <div class="section-header">
                    <h5 class="mb-0">
                        <span>📅</span>
                        Monthly Missed ({{ summary.total_monthly_missed }})
                    </h5>
                </div>
                <div class="section-content">
                    {% if monthly_missed %}
                        {% for loan_data in monthly_missed %}
                        <div class="loan-card monthly">
                            <div class="d-flex justify-content-between align-items-start mb-2">
                                <div>
                                    <h6 class="mb-1">{{ loan_data.loan.loan_number }}</h6>
                                    <p class="mb-1 text-muted">{{ loan_data.borrower_name }}</p>
                                    <small class="text-muted">{{ loan_data.product_name }}</small>
                                </div>
                                <span class="missed-badge missed-low">
                                    {{ loan_data.total_missed_periods }} missed
                                </span>
                            </div>
                            
                            <div class="row align-items-center">
                                <div class="col-12">
                                    <div class="fw-bold text-primary">KES {{ loan_data.outstanding_balance|floatformat:0|intcomma }}</div>
                                    <small class="text-muted">Outstanding</small>
                                </div>
                            </div>
                            
                            <div class="row mt-2">
                                <div class="col-12 text-end">
                                    <a href="{% url 'reports:missed_payments_detail' loan_data.borrower_id %}" class="btn btn-sm btn-info me-1">
                                        <i class="fas fa-list me-1"></i>View All Missed Payments
                                    </a>
                                    <a href="{% url 'loans:loan_detail' loan_data.loan.id %}" class="btn btn-sm btn-outline-primary me-1">
                                        <i class="fas fa-eye me-1"></i>View
                                    </a>
                                    <a href="tel:{{ loan_data.borrower_phone }}" class="btn btn-sm btn-success">
                                        <span>📞</span>Call
                                    </a>
                                </div>
                            </div>
                            
                            {% if loan_data.missed_periods %}
                            <div class="missed-periods">
                                <small class="text-muted fw-bold">Missed Dates:</small>
                                {% for period in loan_data.missed_periods|slice:":3" %}
                                <div class="missed-period-item">
                                    <span>{{ period.due_date|date:"M d, Y" }}</span>
                                    <span class="text-danger">KES {{ period.expected_amount|floatformat:0|intcomma }}</span>
                                </div>
                                {% endfor %}
                                {% if loan_data.missed_periods|length > 3 %}
                                <div class="text-center mt-2">
                                    <small class="text-muted">+{{ loan_data.missed_periods|length|add:"-3" }} more</small>
                                </div>
                                {% endif %}
                            </div>
                            {% endif %}
                        </div>
                        {% endfor %}
                    {% else %}
                        <div class="empty-state">
                            <span>✓</span>
                            <h6>No Monthly Missed Payments</h6>
                            <p class="small">All monthly payment borrowers are up to date.</p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
