@extends('layouts.admin', ['heading' => 'Payment Gateways', 'subheading' => 'Configure and enable gateways'])

@section('content')
    <div class="admin-card p-5">
        <div class="overflow-x-auto">
            <table class="min-w-full text-sm">
                <thead class="bg-slate-100 text-left">
                    <tr>
                        <th class="p-3">Gateway</th>
                        <th class="p-3">Status</th>
                        <th class="p-3">Config</th>
                        <th class="p-3">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($gateways as $gateway)
                        <tr class="border-t">
                            <td class="p-3">
                                <div class="font-semibold">{{ $gateway->name }}</div>
                                <div class="text-xs text-slate-500">{{ $gateway->code }}</div>
                            </td>
                            <td class="p-3">
                                <span class="admin-badge {{ $gateway->is_enabled ? 'success' : 'danger' }}">
                                    {{ $gateway->is_enabled ? 'Enabled' : 'Disabled' }}
                                </span>
                            </td>
                            <td class="p-3">
                                @php
                                    $config = is_array($gateway->config_json ?? null)
                                        ? $gateway->config_json
                                        : (is_array($gateway->config ?? null) ? $gateway->config : []);
                                @endphp
                                <form method="POST" action="{{ route('admin.payments.gateways.update', $gateway) }}" class="space-y-2">
                                    @csrf
                                    <input name="name" value="{{ $gateway->name }}" class="w-full rounded border p-2 text-xs" placeholder="Gateway name">
                                    <select name="environment" class="w-full rounded border p-2 text-xs">
                                        <option value="sandbox" @selected(($gateway->environment ?? 'sandbox') === 'sandbox')>Sandbox</option>
                                        <option value="live" @selected(($gateway->environment ?? 'sandbox') === 'live')>Live</option>
                                    </select>
                                    <input name="webhook_secret" value="{{ $config['webhook_secret'] ?? '' }}" class="w-full rounded border p-2 text-xs" placeholder="Webhook secret">
                                    @if($gateway->code === 'razorpay')
                                        <input name="razorpay_key" value="{{ $config['key'] ?? '' }}" class="w-full rounded border p-2 text-xs" placeholder="Key ID">
                                        <input name="razorpay_secret" value="{{ $config['secret'] ?? '' }}" class="w-full rounded border p-2 text-xs" placeholder="Key Secret">
                                    @else
                                        <textarea name="config_json" rows="5" class="w-full rounded border p-2 text-xs" placeholder='{"key":"...","secret":"..."}'>{{ $config ? json_encode($config, JSON_PRETTY_PRINT) : '' }}</textarea>
                                    @endif
                                    <button class="rounded bg-slate-900 px-2 py-1 text-xs text-white">Save</button>
                                </form>
                            </td>
                            <td class="p-3">
                                <form method="POST" action="{{ route('admin.payments.gateways.toggle', $gateway) }}">
                                    @csrf
                                    <button class="rounded bg-amber-600 px-3 py-1 text-xs text-white">
                                        {{ $gateway->is_enabled ? 'Disable' : 'Enable' }}
                                    </button>
                                </form>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
@endsection
