@extends('admin.layouts.app')

@section('title', 'EMI Card Details | EMI CREDITS')

@section('page-title', 'EMI Card Details')

@section(
    'page-description',
    'View client EMI card information and approval details'
)


@push('styles')
<link rel="stylesheet" href="{{ asset('admin/css/emicard-show.css') }}">
@endpush


@section('content')


{{-- =====================================================
     BACK
====================================================== --}}

<a
    href="{{ route('admin.emi-cards') }}"
    class="back-btn"
>

    ← Back to EMI Cards

</a>



{{-- =====================================================
     ALERTS
====================================================== --}}

@if(session('success'))

    <div
        style="
            padding:12px 15px;
            margin-bottom:18px;
            border-radius:9px;
            background:#ecfdf5;
            border:1px solid #bbf7d0;
            color:#166534;
            font-size:10px;
            font-weight:700;
        "
    >

        ✓ {{ session('success') }}

    </div>

@endif


@if(session('error'))

    <div
        style="
            padding:12px 15px;
            margin-bottom:18px;
            border-radius:9px;
            background:#fff1f2;
            border:1px solid #fecdd3;
            color:#be123c;
            font-size:10px;
            font-weight:700;
        "
    >

        ✕ {{ session('error') }}

    </div>

@endif



{{-- =====================================================
     PAGE HEADING
====================================================== --}}

<div class="page-heading">

    <h2>
        EMI Card Details
    </h2>

    <p>
        Complete information for this EMI card and client.
    </p>

</div>



<div class="card-grid">


    {{-- =================================================
         LEFT
    ================================================== --}}

    <div>


        {{-- CARD PREVIEW --}}

        <div class="panel">

            <div class="panel-header">

                <h3>
                    Card Preview
                </h3>


                @if(
                    $card->expires_at &&
                    $card->expires_at->isPast()
                )

                    <span class="badge badge-expired">

                        <span class="dot"></span>

                        Expired

                    </span>

                @else

                    @switch($card->status)

                        @case('active')

                            <span
                                class="badge badge-active"
                            >

                                <span class="dot"></span>

                                Active

                            </span>

                            @break


                        @case('pending')

                            <span
                                class="badge badge-pending"
                            >

                                <span class="dot"></span>

                                Pending

                            </span>

                            @break


                        @case('blocked')

                            <span
                                class="badge badge-blocked"
                            >

                                <span class="dot"></span>

                                Blocked

                            </span>

                            @break


                        @default

                            <span
                                class="badge badge-expired"
                            >

                                {{ ucfirst(
                                    $card->status
                                ) }}

                            </span>

                    @endswitch

                @endif

            </div>


            <div class="panel-body">


                <div class="emi-card-preview">


                    <div class="brand">

                        <div class="brand-name">
                            EMI CREDITS
                        </div>


                        <div class="card-chip"></div>

                    </div>


                    <div class="card-number-preview">

                        {{ $card->card_number }}

                    </div>


                    <div class="card-bottom">


                        <div>

                            <span
                                class="card-small-label"
                            >
                                Card Holder
                            </span>

                            <span
                                class="card-small-value"
                            >

                                {{
                                    $card->user->name
                                    ?? 'CLIENT'
                                }}

                            </span>

                        </div>


                        <div>

                            <span
                                class="card-small-label"
                            >
                                Valid Until
                            </span>

                            <span
                                class="card-small-value"
                            >

                                {{
                                    $card->expires_at
                                        ? $card->expires_at->format(
                                            'm/Y'
                                        )
                                        : '-'
                                }}

                            </span>

                        </div>


                    </div>


                </div>


                {{-- CARD INFO --}}

                <div class="info-grid">


                    <div class="info-item">

                        <span class="info-label">
                            Card Limit
                        </span>

                        <span class="info-value">

                            {{ money((float)($card->card_limit ?? 0)) }}

                        </span>

                    </div>


                    <div class="info-item">

                        <span class="info-label">
                            Available Limit
                        </span>

                        <span class="info-value green">

                            {{ money((float)($card->available_limit ?? 0)) }}

                        </span>

                    </div>


                    <div class="info-item">

                        <span class="info-label">
                            Issued Date
                        </span>

                        <span class="info-value">

                            {{
                                $card->issued_at
                                    ? $card->issued_at->format(
                                        'd M Y'
                                    )
                                    : '-'
                            }}

                        </span>

                    </div>


                    <div class="info-item">

                        <span class="info-label">
                            Expiry Date
                        </span>

                        <span
                            class="
                                info-value
                                {{
                                    $card->expires_at &&
                                    $card->expires_at->isPast()
                                        ? 'red'
                                        : ''
                                }}
                            "
                        >

                            {{
                                $card->expires_at
                                    ? $card->expires_at->format(
                                        'd M Y'
                                    )
                                    : '-'
                            }}

                        </span>

                    </div>


                </div>


            </div>

        </div>



        {{-- CLIENT DETAILS --}}

        <div
            class="panel"
            style="margin-top:18px;"
        >

            <div class="panel-header">

                <h3>
                    Client Information
                </h3>

            </div>


            <div class="panel-body">


                <div class="client-box">


                    <div class="client-avatar">

                        {{
                            strtoupper(
                                substr(
                                    $card->user->name
                                    ?? 'C',
                                    0,
                                    1
                                )
                            )
                        }}

                    </div>


                    <div>

                        <div class="client-name">

                            {{
                                $card->user->name
                                ?? 'Unknown Client'
                            }}

                        </div>


                        <div class="client-contact">

                            {{
                                $card->user->email
                                ?? '-'
                            }}

                            <br>

                            {{
                                $card->user->mobile
                                ?? '-'
                            }}

                        </div>

                    </div>


                </div>


                <div class="info-grid">


                    <div class="info-item">

                        <span class="info-label">
                            Personal Details
                        </span>

                        <span
                            class="
                                info-value
                                {{
                                    $card->user->clientProfile
                                        ? 'green'
                                        : 'red'
                                }}
                            "
                        >

                            {{
                                $card->user->clientProfile
                                    ? '✓ Completed'
                                    : '✕ Incomplete'
                            }}

                        </span>

                    </div>


                    <div class="info-item">

                        <span class="info-label">
                            KYC
                        </span>

                        <span
                            class="
                                info-value
                                {{
                                    $card->user->clientKyc &&
                                    $card->user->clientKyc->status === 'verified'
                                        ? 'green'
                                        : 'red'
                                }}
                            "
                        >

                            {{
                                $card->user->clientKyc &&
                                $card->user->clientKyc->status === 'verified'
                                    ? '✓ Verified'
                                    : '✕ Not Verified'
                            }}

                        </span>

                    </div>


                    <div class="info-item">

                        <span class="info-label">
                            Job Details
                        </span>

                        <span
                            class="
                                info-value
                                {{
                                    $card->user->clientEmployment
                                        ? 'green'
                                        : 'red'
                                }}
                            "
                        >

                            {{
                                $card->user->clientEmployment
                                    ? '✓ Completed'
                                    : '✕ Incomplete'
                            }}

                        </span>

                    </div>


                    <div class="info-item">

                        <span class="info-label">
                            Card Status
                        </span>

                        <span class="info-value">

                            {{ ucfirst(
                                $card->status
                            ) }}

                        </span>

                    </div>


                </div>


            </div>

        </div>


    </div>



    {{-- =================================================
         RIGHT
    ================================================== --}}

    <div>


        {{-- ACTION PANEL --}}

        <div class="panel">

            <div class="panel-header">

                <h3>
                    Card Actions
                </h3>

            </div>


            <div class="panel-body">


                @php

                    $hasPaidPayment =
                        $card->user
                            ? $card->user->payments()
                                ->where(
                                    'status',
                                    'paid'
                                )
                                ->exists()
                            : false;

                    $isExpired =
                        $card->expires_at &&
                        $card->expires_at->isPast();

                @endphp


                {{-- PAYMENT STATUS --}}

                <div class="info-item">

                    <span class="info-label">
                        Payment Status
                    </span>


                    @if($hasPaidPayment)

                        <span
                            class="info-value green"
                        >

                            ✓ Payment Completed

                        </span>

                    @else

                        <span
                            class="info-value orange"
                        >

                            ⏳ Payment Pending

                        </span>

                    @endif

                </div>



                {{-- APPROVAL STATUS --}}

                <div
                    class="info-item"
                    style="margin-top:10px;"
                >

                    <span class="info-label">
                        Approval
                    </span>


                    @if($card->approved_at)

                        <span
                            class="info-value green"
                        >

                            ✓ Approved

                        </span>


                        @if($card->approver)

                            <div
                                style="
                                    margin-top:5px;
                                    color:#9aa6b6;
                                    font-size:7px;
                                "
                            >

                                By:
                                {{ $card->approver->name }}

                                <br>

                                {{
                                    $card->approved_at->format(
                                        'd M Y h:i A'
                                    )
                                }}

                            </div>

                        @endif

                    @else

                        <span
                            class="info-value orange"
                        >

                            Pending Approval

                        </span>

                    @endif

                </div>



                {{-- LIMIT UPDATE --}}

                @if(!$isExpired)

                    <form
                        method="POST"
                        action="{{ route(
                            'admin.emi-cards.limit',
                            $card
                        ) }}"
                        style="margin-top:10px;"
                    >

                        @csrf
                        @method('PUT')

                        <div
                            style="
                                display:flex;
                                gap:7px;
                                align-items:center;
                            "
                        >

                            <input
                                type="number"
                                name="card_limit"
                                value="{{ $card->card_limit ?? 0 }}"
                                min="0"
                                step="0.01"
                                required
                                style="
                                    flex:1;
                                    height:38px;
                                    padding:0 10px;
                                    border:1px solid #dce4ef;
                                    border-radius:8px;
                                    color:#344966;
                                    font-size:9px;
                                    outline:none;
                                "
                            >

                            <button
                                type="submit"
                                class="action-btn limit-btn"
                                style="width:auto;padding:0 14px;"
                                onclick="
                                    return confirm(
                                        'Update this EMI Card limit?'
                                    )
                                "
                            >
                                Update Limit
                            </button>

                        </div>

                    </form>

                @endif


                {{-- ACTIONS --}}

                <div class="actions">


                    {{-- APPROVE --}}

                    @if(
                        $card->status === 'pending'
                    )

                        @if($hasPaidPayment)

                            @if(
                                (float)
                                $card->card_limit > 0
                            )

                                <form
                                    method="POST"
                                    action="{{ route(
                                        'admin.emi-cards.approve',
                                        $card
                                    ) }}"
                                >

                                    @csrf

                                    <button
                                        type="submit"
                                        class="action-btn approve-btn"
                                        onclick="
                                            return confirm(
                                                'Approve and unlock this EMI Card?'
                                            )
                                        "
                                    >

                                        ✓ Approve & Unlock Card

                                    </button>

                                </form>

                            @else

                                <div
                                    style="
                                        padding:11px;
                                        background:#fff7e8;
                                        color:#92400e;
                                        border-radius:8px;
                                        font-size:8px;
                                        text-align:center;
                                    "
                                >

                                    Please set the loan limit
                                    before approval.

                                </div>

                            @endif

                        @else

                            <div
                                style="
                                    padding:11px;
                                    background:#fff7e8;
                                    color:#92400e;
                                    border-radius:8px;
                                    font-size:8px;
                                    text-align:center;
                                "
                            >

                                Payment must be completed
                                before approval.

                            </div>

                        @endif

                    @endif



                    {{-- BLOCK --}}

                    @if(
                        $card->status === 'active'
                    )

                        <form
                            method="POST"
                            action="{{ route(
                                'admin.emi-cards.block',
                                $card
                            ) }}"
                        >

                            @csrf

                            <button
                                type="submit"
                                class="action-btn block-btn"
                                onclick="
                                    return confirm(
                                        'Block this EMI Card?'
                                    )
                                "
                            >

                                Block EMI Card

                            </button>

                        </form>

                    @endif


                    {{-- BLOCKED → ACTIVATE --}}

                    @if(
                        $card->status === 'blocked' &&
                        !$isExpired
                    )

                        <form
                            method="POST"
                            action="{{ route(
                                'admin.emi-cards.activate',
                                $card
                            ) }}"
                        >

                            @csrf

                            <button
                                type="submit"
                                class="action-btn activate-btn"
                                onclick="
                                    return confirm(
                                        'Activate this blocked EMI Card?'
                                    )
                                "
                            >

                                ✓ Activate EMI Card

                            </button>

                        </form>

                    @endif


                </div>


            </div>

        </div>



        {{-- LOAN SUMMARY --}}

        @if(
            $card->user &&
            method_exists($card->user, 'loans')
        )

            @php
                $cardLoans = $card->user->loans()
                    ->latest('created_at')
                    ->get();
            @endphp

            <div
                class="panel loan-summary"
            >

                <div class="panel-header">

                    <h3>
                        Loan Summary
                    </h3>

                </div>

                <div class="panel-body">

                    @if($cardLoans->count())

                        @php
                            $activeLoans = $cardLoans->whereIn(
                                'status',
                                ['approved', 'disbursed', 'active', 'overdue']
                            );

                            $totalBorrowed = $cardLoans->sum(
                                fn ($loan) => (float) ($loan->loan_amount ?? 0)
                            );

                            $totalOutstanding = $cardLoans->sum(
                                fn ($loan) => (float) ($loan->outstanding_amount ?? 0)
                            );

                            $monthlyEmi = $activeLoans->sum(
                                fn ($loan) => (float) ($loan->emi_amount ?? 0)
                            );
                        @endphp

                        <div class="loan-stat-grid">

                            <div class="loan-stat">
                                <span class="loan-stat-label">Loans</span>
                                <span class="loan-stat-value">{{ $cardLoans->count() }}</span>
                            </div>

                            <div class="loan-stat">
                                <span class="loan-stat-label">Active</span>
                                <span class="loan-stat-value">{{ $activeLoans->count() }}</span>
                            </div>

                            <div class="loan-stat">
                                <span class="loan-stat-label">Total Borrowed</span>
                                <span class="loan-stat-value">
                                    {{ money((float)$totalBorrowed) }}
                                </span>
                            </div>

                            <div class="loan-stat">
                                <span class="loan-stat-label">Outstanding</span>
                                <span class="loan-stat-value">
                                    {{ money((float)$totalOutstanding) }}
                                </span>
                            </div>

                            <div
                                class="loan-stat"
                                style="grid-column:1 / -1;"
                            >
                                <span class="loan-stat-label">Monthly EMI</span>
                                <span class="loan-stat-value">
                                    {{ money((float)$monthlyEmi) }}
                                </span>
                            </div>

                        </div>

                        <div style="margin-top:12px;">

                            @foreach($cardLoans->take(5) as $loan)

                                <div class="payment-row">

                                    <div>
                                        <div class="payment-title">
                                            {{ $loan->loan_number ?? 'Loan #'.$loan->id }}
                                        </div>

                                        <div class="payment-date">
                                            {{ ucfirst($loan->status ?? '—') }}
                                        </div>
                                    </div>

                                    <div style="text-align:right;">
                                        <div
                                            style="
                                                color:#273b59;
                                                font-size:9px;
                                                font-weight:800;
                                            "
                                        >
                                            {{ money((float)($loan->outstanding_amount ?? 0)) }}
                                        </div>

                                        <div
                                            style="
                                                margin-top:3px;
                                                color:#9aa6b6;
                                                font-size:7px;
                                            "
                                        >
                                            Outstanding
                                        </div>
                                    </div>

                                </div>

                            @endforeach

                        </div>

                    @else

                        <div
                            style="
                                padding:25px 0;
                                text-align:center;
                                color:#9aa6b6;
                                font-size:9px;
                            "
                        >
                            No loan records found.
                        </div>

                    @endif

                </div>

            </div>

        @endif


        {{-- PAYMENT HISTORY --}}

        <div
            class="panel"
            style="margin-top:18px;"
        >

            <div class="panel-header">

                <h3>
                    Payment History
                </h3>

            </div>


            <div class="panel-body">


                @if(
                    $card->user &&
                    $card->user->payments->count()
                )


                    @foreach(
                        $card->user->payments
                            ->sortByDesc('created_at')
                            ->take(10)
                        as $payment
                    )


                        <div class="payment-row">


                            <div>

                                <div class="payment-title">

                                    Payment

                                </div>


                                <div class="payment-date">

                                    {{
                                        $payment->created_at
                                            ? $payment->created_at->format(
                                                'd M Y h:i A'
                                            )
                                            : '-'
                                    }}

                                </div>

                            </div>


                            <div>

                                <div
                                    style="
                                        text-align:right;
                                        color:#273b59;
                                        font-size:9px;
                                        font-weight:800;
                                    "
                                >

                                    {{ money((float)($payment->amount ?? 0)) }}

                                </div>


                                @if(
                                    $payment->status === 'paid'
                                )

                                    <div
                                        class="paid"
                                        style="text-align:right;"
                                    >

                                        ✓ Paid

                                    </div>

                                @else

                                    <div
                                        class="unpaid"
                                        style="text-align:right;"
                                    >

                                        {{ ucfirst(
                                            $payment->status
                                        ) }}

                                    </div>

                                @endif

                            </div>


                        </div>


                    @endforeach


                @else


                    <div
                        style="
                            padding:25px 0;
                            text-align:center;
                            color:#9aa6b6;
                            font-size:9px;
                        "
                    >

                        No payment records found.

                    </div>


                @endif


            </div>

        </div>


    </div>


</div>


@endsection