@extends('layouts.app')

@section('title', 'Transaction Details')

@section('content')
<div class="mx-auto max-w-xl space-y-6">
    <div class="flex items-center gap-3">
        <a href="{{ route('transactions.index') }}" class="text-gray-400 hover:text-gray-600">
            <svg class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5L8.25 12l7.5-7.5" /></svg>
        </a>
        <div>
            <h1 class="text-2xl font-bold text-gray-900">Transaction Details</h1>
            <p class="text-sm text-gray-500 font-mono">{{ $transaction->reference }}</p>
        </div>
    </div>

    <div class="rounded-xl bg-white p-6 shadow-sm border border-gray-100 space-y-4">
        <div class="flex items-center justify-center">
            @if($transaction->status === 'successful')
                <div class="h-16 w-16 rounded-full bg-green-100 flex items-center justify-center">
                    <svg class="h-8 w-8 text-green-500" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5" /></svg>
                </div>
            @elseif($transaction->status === 'failed')
                <div class="h-16 w-16 rounded-full bg-red-100 flex items-center justify-center">
                    <svg class="h-8 w-8 text-red-500" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" /></svg>
                </div>
            @else
                <div class="h-16 w-16 rounded-full bg-yellow-100 flex items-center justify-center">
                    <svg class="h-8 w-8 text-yellow-500" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M12 6v6h4.5m4.5 0a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>
                </div>
            @endif
        </div>

        <div class="text-center">
            <h2 class="text-lg font-semibold text-gray-900">{{ $transaction->service_name }}</h2>
            <p class="text-2xl font-bold text-gray-900 mt-1">₦{{ number_format($transaction->total_amount, 2) }}</p>
        </div>

        <div class="border-t border-gray-100 pt-4 space-y-3">
            <div class="flex justify-between">
                <span class="text-sm text-gray-500">Status</span>
                <span class="text-sm font-medium text-gray-900">{{ ucfirst($transaction->status) }}</span>
            </div>
            <div class="flex justify-between">
                <span class="text-sm text-gray-500">Service</span>
                <span class="text-sm font-medium text-gray-900">{{ $transaction->service_name }}</span>
            </div>
            <div class="flex justify-between">
                <span class="text-sm text-gray-500">Customer Number</span>
                <span class="text-sm font-medium text-gray-900">{{ $transaction->customer_number }}</span>
            </div>
            <div class="flex justify-between">
                <span class="text-sm text-gray-500">Amount</span>
                <span class="text-sm font-medium text-gray-900">₦{{ number_format($transaction->amount, 2) }}</span>
            </div>
            <div class="flex justify-between">
                <span class="text-sm text-gray-500">Service Charge</span>
                <span class="text-sm font-medium text-gray-900">₦{{ number_format($transaction->fee, 2) }}</span>
            </div>
            <div class="flex justify-between">
                <span class="text-sm text-gray-500">Total</span>
                <span class="text-sm font-bold text-gray-900">₦{{ number_format($transaction->total_amount, 2) }}</span>
            </div>
            <div class="flex justify-between">
                <span class="text-sm text-gray-500">Reference</span>
                <span class="text-sm font-mono text-gray-900">{{ $transaction->reference }}</span>
            </div>
            @if($transaction->provider_reference)
            <div class="flex justify-between">
                <span class="text-sm text-gray-500">Provider Ref</span>
                <span class="text-sm font-mono text-gray-900">{{ $transaction->provider_reference }}</span>
            </div>
            @endif
            <div class="flex justify-between">
                <span class="text-sm text-gray-500">Date</span>
                <span class="text-sm text-gray-900">{{ $transaction->created_at->format('M d, Y H:i:s') }}</span>
            </div>

            @if($transaction->token_data && is_array($transaction->token_data))
            <div class="border-t border-gray-100 pt-3 space-y-3">
                <p class="text-sm font-medium text-gray-500">
                    {{ $transaction->type === 'exam_pin' ? 'Exam PIN(s)' : 'Electricity Token' }}
                </p>

                @if(!empty($transaction->token_data['cards']) && is_array($transaction->token_data['cards']))
                    @foreach($transaction->token_data['cards'] as $index => $card)
                    <div class="rounded-lg bg-green-50 p-4 border border-green-200 space-y-1">
                        <p class="text-xs text-green-950 font-medium">{{ count($transaction->token_data['cards']) > 1 ? 'Card #' . ($index + 1) : 'Card Details' }}</p>
                        @if($card['Serial'] ?? null)
                        <div>
                            <p class="text-xs text-green-950">Serial No</p>
                            <p class="text-lg font-mono font-bold text-green-800 break-all">{{ $card['Serial'] }}</p>
                        </div>
                        @endif
                        @if($card['Pin'] ?? null)
                        <div>
                            <p class="text-xs text-green-950">PIN</p>
                            <p class="text-lg font-mono font-bold text-green-800 break-all">{{ $card['Pin'] }}</p>
                        </div>
                        @endif
                    </div>
                    @endforeach
                @elseif(!empty($transaction->token_data['pin']))
                <div class="rounded-lg bg-green-50 p-4 border border-green-200">
                    <p class="text-xs text-green-950 mb-1">PIN</p>
                    <p class="text-lg font-mono font-bold text-green-800 break-all">{{ $transaction->token_data['pin'] }}</p>
                </div>
                @elseif(!empty($transaction->token_data['tokens']) && is_array($transaction->token_data['tokens']))
                    @foreach($transaction->token_data['tokens'] as $index => $token)
                    <div class="rounded-lg bg-green-50 p-4 border border-green-200">
                        <p class="text-xs text-green-950 mb-1">{{ count($transaction->token_data['tokens']) > 1 ? 'PIN #' . ($index + 1) : 'PIN / Token' }}</p>
                        <p class="text-lg font-mono font-bold text-green-800 break-all">{{ $token }}</p>
                    </div>
                    @endforeach
                @elseif($transaction->token_data['token'] ?? null)
                <div class="rounded-lg bg-green-50 p-4 border border-green-200">
                    <p class="text-xs text-green-950 mb-1">Token / Unit</p>
                    <p class="text-lg font-mono font-bold text-green-800 break-all">{{ $transaction->token_data['token'] }}</p>
                </div>
                @endif

                @if($transaction->token_data['exchange_reference'] ?? null)
                <div class="rounded-lg bg-gray-50 p-3 border border-gray-200">
                    <p class="text-xs text-gray-500 mb-1">Exchange Reference</p>
                    <p class="text-sm font-mono font-medium text-gray-900">{{ $transaction->token_data['exchange_reference'] }}</p>
                </div>
                @endif

                @if($transaction->token_data['response_description'] ?? null)
                <div class="rounded-lg bg-gray-50 p-3 border border-gray-200">
                    <p class="text-xs text-gray-500 mb-1">Provider Response</p>
                    <p class="text-sm font-medium text-gray-900">{{ $transaction->token_data['response_description'] }}</p>
                </div>
                @endif

                @if($transaction->token_data['transaction_date'] ?? null)
                <div class="rounded-lg bg-gray-50 p-3 border border-gray-200">
                    <p class="text-xs text-gray-500 mb-1">Transaction Date</p>
                    <p class="text-sm font-medium text-gray-900">{{ $transaction->token_data['transaction_date'] }}</p>
                </div>
                @endif
            </div>
            @elseif($transaction->token_data && is_string($transaction->token_data))
            <div class="border-t border-gray-100 pt-3">
                <p class="text-sm font-medium text-gray-500 mb-1">Token</p>
                <div class="rounded-lg bg-green-50 p-3 border border-green-200">
                    <p class="text-sm font-mono font-bold text-green-800 break-all">{{ $transaction->token_data }}</p>
                </div>
            </div>
            @endif
        </div>

        @if($receipt)
        <div class="flex gap-3">
            <a href="{{ route('transactions.receipt', $transaction->reference) }}" class="flex-1 text-center rounded-lg bg-gray-100 px-4 py-2.5 text-sm font-semibold text-gray-700 hover:bg-gray-200">
                View Receipt
            </a>
            <a href="{{ route('transactions.receipt.pdf', $transaction->reference) }}" class="flex-1 text-center rounded-lg bg-green-950 px-4 py-2.5 text-sm font-semibold text-white hover:bg-green-700">
                Download PDF
            </a>
        </div>
        @endif
    </div>
</div>
@endsection
