<x-app-layout>
    <x-slot name="header">
        <div class="rounded-2xl border border-sky-200/70 bg-gradient-to-r from-sky-50 to-green-50 px-5 py-4">
            <h2 class="text-xl font-semibold leading-tight text-sky-900">
                {{ __('Edit Payment Method') }}
            </h2>
        </div>
    </x-slot>

    <div class="py-8">
        <div class="mx-auto max-w-2xl px-4 sm:px-6 lg:px-8">
            <div class="overflow-hidden rounded-2xl border border-sky-200/70 bg-white shadow-sm">
                <div class="p-6">
                    <form action="{{ route('admin.payment-methods.update', $paymentMethod) }}" method="POST" class="space-y-6">
                        @csrf
                        @method('PUT')

                        <div>
                            <label for="name" class="block text-sm font-medium text-gray-700">{{ __('Payment Method Name') }}</label>
                            <input type="text" id="name" name="name" value="{{ old('name', $paymentMethod->name) }}"
                                class="mt-1 block w-full rounded-md border px-3 py-2 shadow-sm focus:border-sky-500 focus:outline-none focus:ring-sky-500 {{ $errors->has('name') ? 'border-red-500' : 'border-sky-300' }}"
                                placeholder="e.g., Stripe Production, PayPal Development" required />
                            @error('name')
                                <p class="mt-1 text-sm text-red-500">{{ $message }}</p>
                            @enderror
                        </div>

                        <div>
                            <label for="provider" class="block text-sm font-medium text-gray-700">{{ __('Provider') }}</label>
                            <select id="provider" name="provider" onchange="updateCredentialFields()"
                                class="mt-1 block w-full rounded-md border px-3 py-2 shadow-sm focus:border-sky-500 focus:outline-none focus:ring-sky-500 {{ $errors->has('provider') ? 'border-red-500' : 'border-sky-300' }}"
                                required>
                                <option value="">{{ __('-- Select Provider --') }}</option>
                                @foreach ($providers as $provider)
                                    <option value="{{ $provider }}" {{ old('provider', $paymentMethod->provider) === $provider ? 'selected' : '' }}>
                                        {{ ucfirst($provider) }}
                                    </option>
                                @endforeach
                            </select>
                            @error('provider')
                                <p class="mt-1 text-sm text-red-500">{{ $message }}</p>
                            @enderror
                        </div>

                        <div>
                            <label for="description" class="block text-sm font-medium text-gray-700">{{ __('Description') }}</label>
                            <textarea id="description" name="description" rows="2"
                                class="mt-1 block w-full rounded-md border px-3 py-2 shadow-sm focus:border-sky-500 focus:outline-none focus:ring-sky-500 {{ $errors->has('description') ? 'border-red-500' : 'border-sky-300' }}"
                                placeholder="Notes about this payment method">{{ old('description', $paymentMethod->description) }}</textarea>
                            @error('description')
                                <p class="mt-1 text-sm text-red-500">{{ $message }}</p>
                            @enderror
                        </div>

                        <!-- Credentials Section -->
                        <div id="credentials-section" class="border-t border-sky-100 pt-6">
                            <h3 class="text-lg font-semibold text-gray-900 mb-4">{{ __('Credentials') }}</h3>
                            <p class="text-sm text-gray-600 mb-4">
                                {{ __('Leave credential fields empty to keep existing values') }}
                            </p>
                            <div id="credentials-container" class="space-y-4">
                                <!-- Will be populated by JavaScript based on selected provider -->
                            </div>
                        </div>

                        <div class="flex items-center">
                            <input type="checkbox" id="is_active" name="is_active" value="1" {{ old('is_active', $paymentMethod->is_active) ? 'checked' : '' }}
                                class="rounded border-sky-300 text-sky-600 shadow-sm focus:border-sky-400 focus:ring focus:ring-sky-200 focus:ring-opacity-50" />
                            <label for="is_active" class="ml-2 text-sm text-gray-700">{{ __('Active') }}</label>
                        </div>

                        <div class="flex gap-4">
                            <button type="submit"
                                class="rounded bg-sky-600 px-4 py-2 font-semibold text-white transition hover:bg-sky-700">
                                {{ __('Update Payment Method') }}
                            </button>
                            <a href="{{ route('admin.payment-methods.index') }}"
                                class="rounded bg-gray-600 px-4 py-2 font-semibold text-white transition hover:bg-gray-700">
                                {{ __('Cancel') }}
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
        const credentialFields = {
            stripe: [
                { key: 'api_key', label: 'Secret API Key', type: 'password' }
            ],
            paypal: [
                { key: 'client_id', label: 'Client ID', type: 'password' },
                { key: 'client_secret', label: 'Client Secret', type: 'password' }
            ],
            wise: [
                { key: 'api_token', label: 'API Token', type: 'password' }
            ]
        };

        const existingCredentials = {!! json_encode($credentials) !!};

        function updateCredentialFields() {
            const provider = document.getElementById('provider').value;
            const container = document.getElementById('credentials-container');
            container.innerHTML = '';

            if (provider && credentialFields[provider]) {
                credentialFields[provider].forEach((field, index) => {
                    const currentValue = existingCredentials[field.key] || '';
                    const fieldHtml = `
                        <div>
                            <label class="block text-sm font-medium text-gray-700">{{ __('Credentials') }}: ${field.label}</label>
                            <input type="${field.type}" name="credentials[${index}][key]" value="${field.key}"
                                class="hidden" />
                            <input type="${field.type}" name="credentials[${index}][value]"
                                class="mt-1 block w-full rounded-md border border-sky-300 px-3 py-2 shadow-sm focus:border-sky-500 focus:outline-none focus:ring-sky-500"
                                placeholder="${field.label}" />
                            <p class="mt-1 text-xs text-gray-500">
                                ${currentValue ? '{{ __("Current value is set") }}' : '{{ __("No value set yet") }}'}
                            </p>
                        </div>
                    `;
                    container.insertAdjacentHTML('beforeend', fieldHtml);
                });
            }
        }

        // Initialize on page load
        document.addEventListener('DOMContentLoaded', updateCredentialFields);
    </script>
</x-app-layout>
