@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.user.card>
                    @slot('header')
                        Transaction History
                    @endslot

                    {{-- ── DESKTOP TABLE (lg and above) ── --}}
                    <div class="table-responsive d-none d-lg-block">
                        <table id="myTable" class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>Reference ID</th>
                                    <th>Type</th>
                                    <th>Description</th>
                                    <th>Amount</th>
                                    <th>Balance</th>
                                    <th>Date</th>
                                    <th>Status</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($transactions as $transaction)
                                    <tr>
                                        <td>{{ $loop->iteration }}</td>
                                        <td>{{ $transaction->reference_id }}</td>
                                        <td>{{ $transaction->type->label() }}</td>
                                        <td>{{ $transaction->description }}</td>
                                        <td>
                                            @if ($transaction->isDirectionCredit())
                                                <span
                                                    class="text-success">+{{ currency($user->currency) }}{{ formatAmount($transaction->amount) }}</span>
                                            @else
                                                <span
                                                    class="text-danger">-{{ currency($user->currency) }}{{ formatAmount($transaction->amount) }}</span>
                                            @endif
                                        </td>
                                        <td>{{ currency($user->currency) }}{{ formatAmount($transaction->current_balance) }}
                                        </td>
                                        <td>{{ formatDateTime($transaction->transaction_at) }}</td>
                                        <td>
                                            <span class="{{ $transaction->status->badge() }}">
                                                {{ $transaction->status->label() }}
                                            </span>
                                        </td>
                                        <td>
                                            <a href="{{ route('user.transaction.show', $transaction->uuid) }}"
                                                class="btn btn-warning btn-sm">
                                                <i class="ti ti-eye me-1"></i>View
                                            </a>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>

                    {{-- ── MOBILE / TABLET CARDS (below lg) ── --}}
                    <div class="d-lg-none">
                        @foreach ($transactions as $transaction)
                            <div class="txn-card-item border rounded-3 p-3 mb-3">

                                {{-- Top row: index + amount + status --}}
                                <div class="d-flex justify-content-between align-items-start mb-2">
                                    <div class="d-flex align-items-center gap-2">
                                        <span class="txn-index-badge">#{{ $loop->iteration }}</span>
                                        <span class="text-muted"
                                            style="font-size:.75rem;">{{ $transaction->type->label() }}</span>
                                    </div>
                                    <span class="{{ $transaction->status->badge() }} rounded-pill px-3 py-1"
                                        style="font-size:.7rem;">
                                        {{ $transaction->status->label() }}
                                    </span>
                                </div>

                                {{-- Amount + Balance --}}
                                <div class="d-flex justify-content-between align-items-center mb-3">
                                    <div>
                                        @if ($transaction->isDirectionCredit())
                                            <span class="fs-5 fw-bold text-success">
                                                +{{ currency($user->currency) }}{{ formatAmount($transaction->amount) }}
                                            </span>
                                        @else
                                            <span class="fs-5 fw-bold text-danger">
                                                -{{ currency($user->currency) }}{{ formatAmount($transaction->amount) }}
                                            </span>
                                        @endif
                                    </div>
                                    <div class="text-end">
                                        <p class="text-muted mb-0"
                                            style="font-size:.68rem;font-weight:500;letter-spacing:.06em;text-transform:uppercase;">
                                            Balance After</p>
                                        <span class="fw-semibold" style="font-family:monospace;font-size:.85rem;">
                                            {{ currency($user->currency) }}{{ formatAmount($transaction->current_balance) }}
                                        </span>
                                    </div>
                                </div>

                                {{-- Ref + Date --}}
                                <div class="d-flex justify-content-between align-items-center pt-2 border-top mb-3">
                                    <div>
                                        <p class="text-muted mb-0"
                                            style="font-size:.68rem;font-weight:500;letter-spacing:.06em;text-transform:uppercase;">
                                            Reference</p>
                                        <span
                                            style="font-family:monospace;font-size:.78rem;">{{ $transaction->reference_id }}</span>
                                    </div>
                                    <div class="text-end">
                                        <p class="text-muted mb-0"
                                            style="font-size:.68rem;font-weight:500;letter-spacing:.06em;text-transform:uppercase;">
                                            Date</p>
                                        <span
                                            style="font-size:.8rem;">{{ formatDateTime($transaction->transaction_at) }}</span>
                                    </div>
                                </div>

                                {{-- Description (if present) --}}
                                @if ($transaction->description)
                                    <p class="text-muted mb-3" style="font-size:.82rem;">
                                        <i class="bi bi-chat-left-text me-1"></i>{{ $transaction->description }}
                                    </p>
                                @endif

                                {{-- Action --}}
                                <a href="{{ route('user.transaction.show', $transaction->uuid) }}"
                                    class="btn btn-warning btn-sm w-100 d-inline-flex align-items-center justify-content-center gap-1">
                                    <i class="ti ti-eye"></i> View Transaction
                                </a>

                            </div>
                        @endforeach
                    </div>

                </x-dashboard.user.card>

                <style>
                    .txn-card-item {
                        /* background: #fff; */
                        transition: box-shadow .15s;
                    }

                    .txn-card-item:hover {
                        box-shadow: 0 4px 16px rgba(0, 0, 0, .08);
                    }

                    .txn-index-badge {
                        font-size: .72rem;
                        font-weight: 700;
                        background: #f1f3f5;
                        color: #6c757d;
                        border-radius: 6px;
                        padding: 2px 7px;
                    }
                </style>
            </div>
        </div>
    </div>
@endsection
