@extends('backend.master')
@section('title','Installments for Loan #'.$loan->id)

@push('styles')
    {{-- Bootstrap Icons --}}
    <link rel="stylesheet"
          href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css">
@endpush

@section('content')

<style>
    :root{
        --inst-primary:#dc2626;
        --inst-primary-soft:#fee2e2;
        --inst-bg:#f5f5f5;
    }

    .inst-bg{
        background:var(--inst-bg);
    }

    .inst-card{
        border-radius:1rem;
        box-shadow:0 10px 28px rgba(15,23,42,.08);
        border:1px solid #e5e7eb;
        background:#fff;
    }

    .inst-section-title{
        font-size:.8rem;
        font-weight:600;
        text-transform:uppercase;
        letter-spacing:.08em;
        color:#6b7280;
        margin-bottom:.25rem;
    }

    .pill-title{
        display:inline-flex;
        align-items:center;
        gap:.4rem;
        padding:.25rem .8rem;
        border-radius:999px;
        background:var(--inst-primary-soft);
        color:var(--inst-primary);
        font-size:.78rem;
        font-weight:600;
    }

    .badge-status{
        border-radius:999px;
        padding:.25rem .6rem;
        font-size:.7rem;
        font-weight:600;
        display:inline-flex;
        align-items:center;
        gap:.25rem;
    }
    .badge-status .dot{
        width:.45rem;
        height:.45rem;
        border-radius:999px;
    }

    .badge-upcoming{
        background:#eff6ff;
        color:#1d4ed8;
    }
    .badge-upcoming .dot{ background:#3b82f6; }

    .badge-paid{
        background:#ecfdf3;
        color:#15803d;
    }
    .badge-paid .dot{ background:#22c55e; }

    .badge-late{
        background:#fef2f2;
        color:#b91c1c;
    }
    .badge-late .dot{ background:#ef4444; }

    .table-inst th{
        font-size:.76rem;
        text-transform:uppercase;
        letter-spacing:.05em;
        color:#9ca3af;
        border-bottom:1px solid #e5e7eb!important;
        white-space:nowrap;
    }
    .table-inst td{
        font-size:.82rem;
        border-bottom:1px solid #f3f4f6!important;
        vertical-align:middle;
    }

    .btn-soft-primary{
        background:#eff6ff;
        color:#1d4ed8;
        border:1px solid #bfdbfe;
        border-radius:999px;
        font-size:.8rem;
        padding:.3rem .65rem;
    }
    .btn-soft-primary:hover{
        background:#1d4ed8;
        color:#fff;
        border-color:#1d4ed8;
    }

    .mini-label{
        font-size:.75rem;
        color:#6b7280;
        text-transform:uppercase;
        letter-spacing:.06em;
    }
    .mini-value{
        font-size:.9rem;
        font-weight:600;
        color:#111827;
    }

    @media (max-width:768px){
        .inst-header-stack{
            flex-direction:column;
            align-items:flex-start!important;
            gap:.4rem;
        }
    }
</style>

<div class="inst-bg py-3 py-md-4">
    <div class="container-fluid">

        <div class="d-flex inst-header-stack justify-content-between align-items-center mb-3">
            <div>
                <h5 class="mb-1 fw-semibold d-flex align-items-center gap-2">
                    <i class="bi bi-calendar-check text-danger fs-5"></i>
                    <span>Installments for Loan #{{ $loan->id }}</span>
                </h5>
                <small class="text-muted">
                    User: {{ $loan->user?->name ?? 'N/A' }} · Amount: ৳ {{ number_format($loan->amount) }} · Tenure: {{ $loan->tenure_months }} মাস
                </small>
            </div>

            <a href="{{ route('admin.loans.show', $loan) }}"
               class="btn btn-sm btn-outline-secondary rounded-pill d-inline-flex align-items-center gap-1">
                <i class="bi bi-arrow-left"></i>
                <span>Back to loan</span>
            </a>
        </div>

        @if(session('success'))
            <div class="alert alert-success border-0 shadow-sm py-2 px-3 mb-3">
                <i class="bi bi-check-circle-fill me-1"></i>
                {{ session('success') }}
            </div>
        @endif

        {{-- Loan info small card --}}
        <div class="card inst-card mb-3">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center flex-wrap gap-3">
                    <div>
                        <div class="inst-section-title mb-1">Loan summary</div>
                        <div class="d-flex flex-wrap gap-3">
                            <div>
                                <div class="mini-label">Total payable</div>
                                <div class="mini-value">৳ {{ number_format($loan->total_payable) }}</div>
                            </div>
                            <div>
                                <div class="mini-label">Monthly installment</div>
                                <div class="mini-value">৳ {{ number_format($loan->monthly_installment) }}</div>
                            </div>
                            <div>
                                <div class="mini-label">Status</div>
                                <div class="mini-value text-capitalize">
                                    {{ str_replace('_',' ', $loan->status) }}
                                </div>
                            </div>
                        </div>
                    </div>

                    <span class="pill-title">
                        <i class="bi bi-list-check"></i>
                        <span>Total installments: {{ $installments->count() }}</span>
                    </span>
                </div>
            </div>
        </div>

        {{-- Installment table --}}
        <div class="card inst-card">
            <div class="card-body pb-2">
                <div class="inst-section-title mb-2">Installment schedule</div>

                <div class="table-responsive">
                    <table class="table table-sm table-hover align-middle table-inst mb-0">
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>Due date</th>
                            <th>Amount</th>
                            <th>Principal / Interest</th>
                            <th>Status</th>
                            <th>Paid info</th>
                            <th class="text-end">Action</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($installments as $ins)
                            @php
                                if ($ins->status === 'paid') {
                                    $statusClass = 'badge-paid';
                                    $statusLabel = 'Paid';
                                } elseif ($ins->status === 'late') {
                                    $statusClass = 'badge-late';
                                    $statusLabel = 'Late';
                                } else {
                                    $statusClass = 'badge-upcoming';
                                    $statusLabel = 'Upcoming';
                                }
                            @endphp
                            <tr>
                                <td>#{{ $ins->id }}</td>
                                <td>
                                    <div class="fw-semibold">
                                        {{ $ins->due_date?->format('d M Y') }}
                                    </div>
                                    <small class="text-muted">
                                        {{ $ins->due_date?->diffForHumans() }}
                                    </small>
                                </td>
                                <td class="fw-semibold">
                                    ৳ {{ number_format($ins->amount) }}
                                </td>
                                <td>
                                    <small class="text-muted">
                                        P: {{ number_format($ins->principal_part) }},
                                        I: {{ number_format($ins->interest_part) }}
                                    </small>
                                </td>
                                <td>
                                    <span class="badge-status {{ $statusClass }}">
                                        <span class="dot"></span>
                                        {{ $statusLabel }}
                                    </span>
                                    @if($ins->paid_at)
                                        <div class="text-muted small">
                                            <i class="bi bi-clock-history me-1"></i>
                                            {{ $ins->paid_at->format('d M, H:i') }}
                                        </div>
                                    @endif
                                </td>
                                <td>
                                    @if($ins->status === 'paid')
                                        <div class="fw-semibold small">
                                            Paid: ৳ {{ number_format($ins->paid_amount) }}
                                        </div>
                                        <div class="text-muted small">
                                            {{ $ins->payment_method ? strtoupper($ins->payment_method).' · ' : '' }}
                                            {{ $ins->payment_trx_id }}
                                        </div>
                                    @else
                                        <span class="text-muted small">—</span>
                                    @endif
                                </td>
                                <td class="text-end">
                                    <a href="{{ route('admin.installments.edit', $ins) }}"
                                       class="btn btn-soft-primary btn-sm d-inline-flex align-items-center gap-1">
                                        <i class="bi bi-pencil-square"></i>
                                        <span class="d-none d-md-inline">Update</span>
                                    </a>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="text-center text-muted py-3">
                                    <i class="bi bi-inbox me-1"></i>
                                    No installments found for this loan.
                                </td>
                            </tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>

            </div>
        </div>

    </div>
</div>
@endsection
