@extends('dashboard.user.layouts.app')
@section('content')
    <div class="page-container">

        <div class="page-title-head d-flex align-items-sm-center flex-sm-row flex-column gap-2">
            <div class="flex-grow-1">
                <h4 class="fs-18 fw-semibold mb-0">{{ $title }}</h4>
            </div>

            <div class="text-end">
                <x-dashboard.user.breadcrumbs :breadcrumbs="$breadcrumbs" />
            </div>
        </div>

        <div class="row">
            {{-- <div class="col-xl-12 col-lg-12">
                <x-dashboard.admin.card>
                    @slot('header')
                        {{ $title }}
                    @endslot

                    <p>
                        <strong>Requested Amount:</strong>
                        {{ currency($loan->user->currency) }}{{ formatAmount($loan->amount) }}
                    </p>

                    <p><strong>Duration:</strong> {{ $loan->duration }} months</p>

                    <p><strong>Facility:</strong> {{ $loan->facility }}</p>

                    <p><strong>Purpose:</strong> {{ $loan->purpose }}</p>

                    <p><strong>Monthly Income:</strong> {{ $loan->monthly_income }}</p>

                    <p><strong>Status:</strong>
                        <span class="{{ $loan->status->badge() }}">{{ $loan->status->label() }}</span>
                    </p>

                    @if ($loan->isRejected())
                        <p>
                            <strong>Remarks:</strong>
                            {{ $loan->remarks }}
                        </p>
                    @endif

                    @if ($loan->isApproved() || $loan->isDisbursed())
                        <p><strong>Approved Amount:</strong>
                            {{ currency($user->currency) }}{{ formatAmount($loan->approved_amount) }}</p>
                    @endif

                    @if ($loan->isApproved() || $loan->isDisbursed())
                        <p><strong>Interest Rate:</strong> {{ $loan->interest_rate }}%</p>
                    @endif

                    @if ($loan->isApproved() || $loan->isDisbursed())
                        <p><strong>Total Payable:</strong>
                            {{ currency($user->currency) }}{{ formatAmount($loan->total_payable) }}</p>
                    @endif

                    @if ($loan->isDisbursed())
                        <p><strong>Disbursed At:</strong> {{ formatDate($loan->disbursed_at) }}</p>
                    @endif

                    <dl>
                        @if ($loan->loanRepayment)
                            @foreach ($loan->loanRepayment as $loanRepayment)
                                <dt class="col-sm-3">Loan Repayment Status</dt>
                                <dd class="col-sm-9">
                                    Amount:
                                    {{ currency($loan->user->currency) }}{{ formatAmount($loanRepayment->amount) }}<br>
                                    Status: <span
                                        class="{{ $loanRepayment->status->badge() }}">{{ $loanRepayment->status->label() }}</span><br>
                                    Repaid At:
                                    {{ $loanRepayment->repaid_at ? formatDate($loanRepayment->repaid_at) : 'N/A' }}
                                </dd>
                            @endforeach
                        @endif
                    </dl>

                    @if ($loan->isDisbursed() && !$latestLoanRepayment->isPaid())
                        <a href="{{ route('user.loan.repay', $loan->uuid) }}" onclick="return confirm('Are you sure?')"
                            class="btn btn-primary">
                            Repay ({{ currency($loan->user->currency) }}{{ formatAmount($loan->total_payable) }})
                        </a>
                    @endif

                    <div class="float-end">
                        <x-dashboard.user.back-button href="{{ route('user.loan.index') }}" name="Back to Loans" />
                    </div>
                </x-dashboard.admin.card>
            </div> --}}
            <div class="col-xl-12 col-lg-12">
                <div class="col-lg-10 mx-auto">
                    <div class="card border-0 shadow rounded-4 overflow-hidden">

                        {{-- ── CARD HEADER ── --}}
                        <div
                            class="card-header bg-light border-bottom px-4 px-md-5 py-3 d-flex justify-content-between align-items-center gap-2 flex-wrap">
                            <div>
                                <p class="text-uppercase text-muted fw-bold mb-0"
                                    style="font-size:.68rem;letter-spacing:.08em;">Loan Application</p>
                                <span class="fw-semibold">{{ $title }}</span>
                            </div>
                            <span class="{{ $loan->status->badge() }} rounded-pill px-3 py-2"
                                style="font-size:.72rem;letter-spacing:.04em;">
                                {{ $loan->status->label() }}
                            </span>
                        </div>

                        {{-- ── AMOUNT HERO ── --}}
                        <div class="text-center py-5 px-3 position-relative loan-hero">
                            <div class="loan-hero-glow"></div>
                            <div class="position-relative">
                                <p class="text-uppercase fw-bold mb-2 text-muted"
                                    style="font-size:.68rem;letter-spacing:.1em;">Requested Amount</p>
                                <h2 class="display-5 fw-bold text-dark mb-1">
                                    {{ currency($loan->user->currency) }}{{ formatAmount($loan->amount) }}
                                </h2>
                                <div class="d-flex justify-content-center gap-2 flex-wrap mt-2">
                                    <span class="badge bg-white border text-muted fw-semibold px-3 py-2 rounded-pill"
                                        style="font-size:.72rem;">
                                        <i class="bi bi-calendar3 me-1"></i>{{ $loan->duration }} months
                                    </span>
                                    <span class="badge bg-white border text-muted fw-semibold px-3 py-2 rounded-pill"
                                        style="font-size:.72rem;">
                                        <i class="bi bi-building me-1"></i>{{ $loan->facility }}
                                    </span>
                                </div>
                            </div>
                        </div>

                        <div class="card-body px-4 px-md-5 py-4">

                            {{-- ── APPLICATION DETAILS ── --}}
                            <p class="text-uppercase text-muted fw-bold mb-3 pb-2 border-bottom"
                                style="font-size:.68rem;letter-spacing:.1em;">
                                Application Details
                            </p>

                            <div class="row g-0 mb-4">

                                <div class="col-12 col-sm-6 py-3 pe-sm-4 border-bottom">
                                    <p class="text-muted mb-1" style="font-size:.75rem;font-weight:500;">Purpose</p>
                                    <strong>{{ $loan->purpose }}</strong>
                                </div>

                                <div class="col-12 col-sm-6 py-3 ps-sm-4 border-bottom loan-border-start-sm">
                                    <p class="text-muted mb-1" style="font-size:.75rem;font-weight:500;">Monthly Income</p>
                                    <strong>{{ $loan->monthly_income }}</strong>
                                </div>

                                @if ($loan->isRejected() && $loan->remarks)
                                    <div class="col-12 py-3 border-bottom">
                                        <p class="text-muted mb-1" style="font-size:.75rem;font-weight:500;">Rejection
                                            Remarks</p>
                                        <strong class="text-danger">{{ $loan->remarks }}</strong>
                                    </div>
                                @endif

                            </div>

                            {{-- ── APPROVED / DISBURSED DETAILS ── --}}
                            @if ($loan->isApproved() || $loan->isDisbursed())
                                <p class="text-uppercase text-muted fw-bold mb-3 pb-2 border-bottom"
                                    style="font-size:.68rem;letter-spacing:.1em;">
                                    Approval Details
                                </p>

                                <div class="bg-light rounded-3 p-3 p-md-4 mb-4">
                                    <div class="row g-0">

                                        <div class="col-12 col-sm-4 py-3 pe-sm-3 border-bottom">
                                            <p class="text-muted mb-1" style="font-size:.75rem;font-weight:500;">Approved
                                                Amount</p>
                                            <strong
                                                class="fs-6">{{ currency($user->currency) }}{{ formatAmount($loan->approved_amount) }}</strong>
                                        </div>

                                        <div class="col-12 col-sm-4 py-3 px-sm-3 border-bottom loan-border-start-sm">
                                            <p class="text-muted mb-1" style="font-size:.75rem;font-weight:500;">Interest
                                                Rate</p>
                                            <strong class="fs-6">{{ $loan->interest_rate }}%</strong>
                                        </div>

                                        <div class="col-12 col-sm-4 py-3 ps-sm-3 border-bottom loan-border-start-sm">
                                            <p class="text-muted mb-1" style="font-size:.75rem;font-weight:500;">Total
                                                Payable</p>
                                            <strong
                                                class="fs-6">{{ currency($user->currency) }}{{ formatAmount($loan->total_payable) }}</strong>
                                        </div>

                                        @if ($loan->isDisbursed())
                                            <div class="col-12 py-3">
                                                <p class="text-muted mb-1" style="font-size:.75rem;font-weight:500;">
                                                    Disbursed At</p>
                                                <strong>{{ formatDate($loan->disbursed_at) }}</strong>
                                            </div>
                                        @endif

                                    </div>
                                </div>
                            @endif

                            {{-- ── REPAYMENT HISTORY ── --}}
                            @if ($loan->loanRepayment && count($loan->loanRepayment))
                                <p class="text-uppercase text-muted fw-bold mb-3 pb-2 border-bottom"
                                    style="font-size:.68rem;letter-spacing:.1em;">
                                    Repayment History
                                </p>

                                <div class="d-flex flex-column gap-2 mb-4">
                                    @foreach ($loan->loanRepayment as $loanRepayment)
                                        <div
                                            class="border rounded-3 px-4 py-3 d-flex justify-content-between align-items-center flex-wrap gap-2">
                                            <div>
                                                <p class="text-muted mb-0"
                                                    style="font-size:.68rem;font-weight:500;letter-spacing:.06em;text-transform:uppercase;">
                                                    Amount</p>
                                                <strong style="font-family:monospace;">
                                                    {{ currency($loan->user->currency) }}{{ formatAmount($loanRepayment->amount) }}
                                                </strong>
                                            </div>
                                            <div class="text-center">
                                                <p class="text-muted mb-0"
                                                    style="font-size:.68rem;font-weight:500;letter-spacing:.06em;text-transform:uppercase;">
                                                    Repaid At</p>
                                                <strong>{{ $loanRepayment->repaid_at ? formatDate($loanRepayment->repaid_at) : 'N/A' }}</strong>
                                            </div>
                                            <span class="{{ $loanRepayment->status->badge() }} rounded-pill px-3 py-1"
                                                style="font-size:.7rem;">
                                                {{ $loanRepayment->status->label() }}
                                            </span>
                                        </div>
                                    @endforeach
                                </div>
                            @endif

                            {{-- ── REJECTION NOTICE ── --}}
                            @if ($loan->isRejected())
                                <div
                                    class="rounded-3 border border-danger-subtle bg-danger-subtle px-4 py-3 mb-4 d-flex align-items-start gap-3">
                                    <i class="bi bi-x-circle-fill text-danger mt-1 flex-shrink-0"></i>
                                    <p class="mb-0 small text-danger-emphasis">
                                        This loan application was rejected. Please review the remarks above or contact
                                        support for more information.
                                    </p>
                                </div>
                            @endif

                            {{-- ── ACTIONS ── --}}
                            <div class="d-flex flex-wrap justify-content-between align-items-center gap-2 pt-3 border-top">

                                <x-dashboard.user.back-button href="{{ route('user.loan.index') }}" name="Back to Loans" />

                                @if ($loan->isDisbursed() && !$latestLoanRepayment->isPaid())
                                    <a href="{{ route('user.loan.repay', $loan->uuid) }}"
                                        onclick="return confirm('Are you sure?')"
                                        class="btn btn-primary d-inline-flex align-items-center gap-2 rounded-3">
                                        <i class="bi bi-cash-stack"></i>
                                        Repay
                                        ({{ currency($loan->user->currency) }}{{ formatAmount($loan->total_payable) }})
                                    </a>
                                @endif

                            </div>

                        </div>{{-- /card-body --}}
                    </div>{{-- /card --}}
                </div>
            </div>

            <style>
                .loan-hero {
                    /* background-color: #f7f8fa; */
                }

                .loan-hero-glow {
                    position: absolute;
                    inset: 0;
                    pointer-events: none;
                    background: radial-gradient(ellipse 70% 70% at 50% 0%, rgba(13, 110, 253, .07) 0%, transparent 70%);
                }

                @media (min-width: 576px) {
                    .loan-border-start-sm {
                        border-left: 1px solid #dee2e6 !important;
                    }
                }
            </style>
        </div>
    </div>
@endsection
