@extends('layouts.admin')

@section('title', 'Payment Gateways')

@section('content')
<div class="max-w-[1600px] mx-auto">
    <div class="flex flex-col md:flex-row md:items-center justify-between gap-6 mb-10">
        <div class="space-y-1">
            <h1 class="text-3xl font-bold text-gray-900 tracking-tight">Payment Methods</h1>
            <p class="text-gray-500 text-sm">Manage online and offline channels to accept payments.</p>
        </div>
        <div>
            <a href="{{ route('admin.payment-methods.create') }}" class="inline-flex items-center gap-2 px-5 py-3 btn-brand text-white rounded-xl text-sm font-semibold shadow-md shadow-brand/20">
                <i class="fas fa-plus text-[12px]"></i>
                Add Payment Method
            </a>
        </div>
    </div>

    @if(session('success'))
        <div x-data="{ show: true }" x-show="show" class="bg-emerald-50 text-emerald-800 px-5 py-4 rounded-xl mb-8 border border-emerald-100 flex items-center justify-between">
            <div class="flex items-center gap-3">
                <i class="fas fa-check-circle"></i>
                <p class="text-sm font-medium">Success: {{ session('success') }}</p>
            </div>
            <button @click="show = false" class="w-8 h-8 flex items-center justify-center hover:bg-emerald-100 rounded-lg transition-colors">
                <i class="fas fa-times text-xs"></i>
            </button>
        </div>
    @endif

    <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
        <div class="bg-white rounded-2xl p-6 border border-gray-100">
            <div class="flex items-center gap-3 text-gray-500 mb-2">
                <i class="fas fa-check-circle"></i>
                <span class="text-sm font-medium">Active channels</span>
            </div>
            <div class="text-3xl font-bold text-gray-900">{{ $methods->where('is_active', true)->count() }}</div>
        </div>
        <div class="bg-white rounded-2xl p-6 border border-gray-100">
            <div class="flex items-center gap-3 text-gray-500 mb-2">
                <i class="fas fa-list"></i>
                <span class="text-sm font-medium">Total methods</span>
            </div>
            <div class="text-3xl font-bold text-gray-900">{{ $methods->count() }}</div>
        </div>
        <div class="bg-white rounded-2xl p-6 border border-gray-100">
            <div class="flex items-center gap-3 text-gray-500 mb-2">
                <i class="fas fa-vial"></i>
                <span class="text-sm font-medium">Sandbox mode</span>
            </div>
            <div class="text-3xl font-bold text-gray-900">
                {{ $methods->filter(function($m) {
                    $c = $m->config ?? [];
                    return ($c['environment'] ?? '') == 'TEST' || ($c['stripe_mode'] ?? '') == 'sandbox' || ($c['paypal_mode'] ?? '') == 'sandbox';
                })->count() }}
            </div>
        </div>
    </div>

    <!-- Payment Methods -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
        @forelse($methods as $method)
        <div class="bg-white rounded-2xl border border-gray-100 shadow-sm hover:shadow-md transition-all flex flex-col group relative overflow-hidden h-full">
            <div class="absolute top-0 left-0 w-full h-1 {{ $method->is_active ? 'bg-brand' : 'bg-gray-100' }}"></div>

            <div class="p-6 flex-1">
                <div class="flex items-start justify-between mb-6">
                    <div class="h-16 w-16 rounded-2xl bg-gray-50 border border-gray-100 flex items-center justify-center p-3">
                        @php
                            $logo = $method->logo;
                            if ($logo && !\Illuminate\Support\Str::startsWith($logo, ['http://', 'https://'])) {
                                $logo = asset('storage/' . $logo);
                            }
                        @endphp
                        @if($logo)
                            <img class="h-full w-full object-contain" src="{{ $logo }}" alt="{{ $method->name }}">
                        @else
                            <i class="fas fa-credit-card text-gray-300 text-xl"></i>
                        @endif
                    </div>
                    
                    <div class="relative" x-data="{ open: false }">
                        <button @click="open = !open" @click.away="open = false" class="w-9 h-9 rounded-xl bg-gray-50 flex items-center justify-center text-gray-400 hover:text-brand hover:bg-emerald-50 transition-all">
                            <i class="fas fa-ellipsis-h text-xs"></i>
                        </button>
                        <div x-show="open" class="absolute right-0 mt-3 w-48 bg-white rounded-2xl shadow-lg border border-gray-100 py-2 z-20" x-cloak>
                            <a href="{{ route('admin.payment-methods.edit', $method) }}" class="flex items-center gap-3 px-4 py-2 text-sm text-gray-600 hover:bg-emerald-50 hover:text-brand">
                                <i class="fas fa-edit text-xs"></i> Edit Method
                            </a>
                            <div class="my-2 border-t border-gray-50"></div>
                            <form action="{{ route('admin.payment-methods.destroy', $method) }}" method="POST" onsubmit="return confirm('Are you sure you want to delete this payment method?');">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="w-full flex items-center gap-3 px-4 py-2 text-sm text-red-500 hover:bg-red-50">
                                    <i class="fas fa-trash-alt text-xs"></i> Delete Method
                                </button>
                            </form>
                        </div>
                    </div>
                </div>

                <div class="space-y-3">
                    <div class="flex items-center gap-2">
                        <h3 class="text-lg font-semibold text-gray-900">{{ $method->name }}</h3>
                        @if($method->is_active)
                            <span class="w-2 h-2 rounded-full bg-brand"></span>
                        @endif
                    </div>
                    
                    <div class="flex flex-wrap items-center gap-2">
                        <span class="px-2.5 py-1 bg-gray-100 text-gray-600 text-xs font-medium rounded-lg border border-gray-200/50">
                            {{ strtoupper($method->type) }}
                        </span>
                        @php
                            $isLive = false;
                            $config = $method->config ?? [];
                            if (($config['environment'] ?? '') == 'PROD' || ($config['stripe_mode'] ?? '') == 'live' || ($config['paypal_mode'] ?? '') == 'live') {
                                $isLive = true;
                            }
                        @endphp
                        @if($isLive)
                            <span class="px-2.5 py-1 bg-blue-50 text-blue-600 text-xs font-medium rounded-lg border border-blue-100/50">
                                Live
                            </span>
                        @else
                            <span class="px-2.5 py-1 bg-amber-50 text-amber-600 text-xs font-medium rounded-lg border border-amber-100/50">
                                Sandbox
                            </span>
                        @endif
                    </div>
                    
                    <p class="text-gray-500 text-sm leading-relaxed mt-2">
                        {{ $method->type === 'cod' ? 'Process physical cash payments upon delivery.' : 'Accept digital payments securely via ' . $method->name . '.' }}
                    </p>
                </div>
            </div>

            <div class="px-6 py-4 bg-gray-50/50 border-t border-gray-50 flex items-center justify-between">
                <span class="text-xs text-gray-500">Status</span>
                <div class="flex items-center gap-2 text-sm">
                    @if($method->is_active)
                        <span class="text-emerald-600 font-medium">Active</span>
                    @else
                        <span class="text-gray-500 font-medium">Inactive</span>
                    @endif
                </div>
            </div>
            
            <a href="{{ route('admin.payment-methods.edit', $method) }}" class="absolute inset-0 z-0 opacity-0 group-hover:opacity-10 transition-opacity"></a>
        </div>
        @empty
        <div class="col-span-full bg-white rounded-2xl border border-gray-100 p-16 flex flex-col items-center justify-center text-center shadow-sm">
            <i class="fas fa-credit-card text-gray-300 text-4xl mb-4"></i>
            <h3 class="text-xl font-semibold text-gray-900 mb-2">No payment methods</h3>
            <p class="text-gray-500 text-sm max-w-sm mb-6">Add your first payment method to start accepting payments.</p>
            <a href="{{ route('admin.payment-methods.create') }}" class="inline-flex items-center gap-2 px-5 py-3 btn-brand text-white rounded-xl text-sm font-semibold shadow-md shadow-brand/20">
                <i class="fas fa-plus text-[12px]"></i>
                Add Payment Method
            </a>
        </div>
        @endforelse

        <a href="{{ route('admin.payment-methods.create') }}" class="border-2 border-dashed border-gray-200 rounded-2xl flex flex-col items-center justify-center p-10 hover:border-brand/40 hover:bg-brand/5 transition-all cursor-pointer h-full min-h-[280px]">
            <i class="fas fa-plus text-gray-300 text-2xl mb-3"></i>
            <h3 class="text-sm font-semibold text-gray-600">Add New Method</h3>
            <p class="text-xs text-gray-500 mt-1">Setup Stripe, PayPal, or Cash on Delivery</p>
        </a>
    </div>
</div>
@endsection
