@extends('admin.layouts.app')

@section('title', __('admin/registrars.edit_title'))

@section('content')
<div class="container-fluid px-4">
    <x-admin.page-header
        :title="__('admin/registrars.edit_title') . ': ' . $module['label']"
        icon="fas fa-globe"
        :buttons="[
            ['label' => __('admin/common.buttons_back'), 'route' => route('admin.registrars.index'), 'icon' => 'fas fa-arrow-left', 'color' => 'outline-secondary'],
        ]"
    />

    <x-admin.alert />

    <form action="{{ route('admin.registrars.update', $module['name']) }}" method="POST">
        @csrf
        @method('PUT')
        <x-admin.card :title="__('admin/registrars.edit_config_title')" icon="fas fa-cog">
            <div class="row mb-4">
                <div class="col-md-4">
                    <x-admin.card>
                        <div class="text-center">
                            @if(!empty($module['logo']))
                                <img src="{{ $module['logo'] }}" alt="{{ $module['label'] }}" class="img-fluid mb-3" style="max-height: 80px;">
                            @else
                                <div class="mb-3">
                                    <i class="fas fa-globe fa-3x text-secondary"></i>
                                </div>
                            @endif

                            <h4>{{ $module['label'] }}</h4>
                            <p class="text-muted">{{ $module['description'] }}</p>

                            <div class="mb-3">
                                <small class="text-muted">{{ __('admin/registrars.fields_version') }}: {{ $module['version'] }}</small>
                            </div>

                            @if(!empty($module['author']))
                                <div class="mb-3">
                                    <small class="text-muted">{{ __('admin/registrars.fields_author') }}: {{ $module['author'] }}</small>
                                </div>
                            @endif

                            @if(!empty($module['website']))
                                <div class="mb-3">
                                    <a href="{{ $module['website'] }}" target="_blank" class="btn btn-sm btn-outline-primary">
                                        <i class="fas fa-external-link-alt me-1"></i> {{ __('admin/registrars.buttons_website') }}
                                    </a>
                                </div>
                            @endif

                            @if(!empty($module['documentation']))
                                <div class="mb-3">
                                    <a href="{{ $module['documentation'] }}" target="_blank" class="btn btn-sm btn-outline-secondary">
                                        <i class="fas fa-book me-1"></i> {{ __('admin/registrars.buttons_documentation') }}
                                    </a>
                                </div>
                            @endif
                        </div>
                    </x-admin.card>
                </div>

                <div class="col-md-8">
                    <div class="form-group mb-3">
                        <label for="status">{{ __('admin/registrars.fields_status') }}</label>
                        <select name="status" id="status" class="form-control">
                            <option value="active" @selected($registrar && $registrar->status == 'active')>{{ __('admin/common.status_active') }}</option>
                            <option value="inactive" @selected($registrar && $registrar->status == 'inactive')>{{ __('admin/common.status_inactive') }}</option>
                        </select>
                    </div>

                    <h4 class="mt-4 mb-3">{{ __('admin/registrars.edit_api_settings') }}</h4>

                    @if(!empty($module['features']))
                        <div class="mb-4">
                            <h5>{{ __('admin/registrars.edit_supported_features') }}</h5>
                            <div class="row">
                                @foreach($module['features'] as $feature => $isSupported)
                                    <div class="col-md-4 mb-2">
                                        <span class="{{ $isSupported ? 'text-success' : 'text-muted' }}">
                                            <i class="fas {{ $isSupported ? 'fa-check' : 'fa-times' }} me-1"></i>
                                            {{ __('admin/registrars.features_' . $feature) }}
                                        </span>
                                    </div>
                                @endforeach
                            </div>
                        </div>
                    @endif

                    <!-- Dinamik Form Alanları -->
                    @foreach($configFields as $fieldKey => $field)
                        <div class="form-group mb-3">
                            @if($field['type'] === 'checkbox')
                                <div class="form-check">
                                    <input type="hidden" name="settings[{{ $fieldKey }}]" value="false">
                                    <input type="checkbox" class="form-check-input @error('settings.'.$fieldKey) is-invalid @enderror"
                                        id="settings_{{ $fieldKey }}" name="settings[{{ $fieldKey }}]"
                                        value="true" {{ $field['value'] === 'true' ? 'checked' : '' }}>
                                    <label class="form-check-label" for="settings_{{ $fieldKey }}">
                                        {{ $field['label'] }}
                                        @if(!empty($field['required']) && $field['required'])
                                            <span class="text-danger">*</span>
                                        @endif
                                    </label>
                                    @error('settings.'.$fieldKey)
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                    @if(!empty($field['description']))
                                        <small class="form-text text-muted">{{ $field['description'] }}</small>
                                    @endif
                                </div>
                            @elseif($field['type'] === 'select')
                                <label for="settings_{{ $fieldKey }}">
                                    {{ $field['label'] }}
                                    @if(!empty($field['required']) && $field['required'])
                                        <span class="text-danger">*</span>
                                    @endif
                                </label>
                                <select class="form-control @error('settings.'.$fieldKey) is-invalid @enderror"
                                    id="settings_{{ $fieldKey }}" name="settings[{{ $fieldKey }}]">
                                    @foreach($field['options'] as $optionKey => $optionLabel)
                                        <option value="{{ $optionKey }}" @selected($field['value'] == $optionKey)>
                                            {{ $optionLabel }}
                                        </option>
                                    @endforeach
                                </select>
                                @error('settings.'.$fieldKey)
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                                @if(!empty($field['description']))
                                    <small class="form-text text-muted">{{ $field['description'] }}</small>
                                @endif
                            @else
                                <label for="settings_{{ $fieldKey }}">
                                    {{ $field['label'] }}
                                    @if(!empty($field['required']) && $field['required'])
                                        <span class="text-danger">*</span>
                                    @endif
                                </label>
                                <input type="{{ $field['type'] }}" class="form-control @error('settings.'.$fieldKey) is-invalid @enderror"
                                    id="settings_{{ $fieldKey }}" name="settings[{{ $fieldKey }}]"
                                    value="{{ old('settings.'.$fieldKey, $field['value']) }}"
                                    placeholder="{{ $field['placeholder'] ?? '' }}">
                                @error('settings.'.$fieldKey)
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                                @if(!empty($field['description']))
                                    <small class="form-text text-muted">{{ $field['description'] }}</small>
                                @endif
                            @endif
                        </div>
                    @endforeach
                </div>
            </div>

            <x-slot:footer>
                <x-admin.form-actions
                    :cancel-url="route('admin.registrars.index')"
                    :cancel-text="__('admin/registrars.buttons_cancel')"
                    :submit-text="__('admin/registrars.buttons_save')"
                />
            </x-slot:footer>
        </x-admin.card>
    </form>

    {{-- Module Dashboard (loaded via AJAX after page load) --}}
    @if($registrar && $registrar->status === 'active')
    <div class="row mt-4">
        <div class="col-md-12">
            <x-admin.card :title="__('admin/registrars.dashboard_title')" icon="fas fa-tachometer-alt">
                <div id="moduleDashboard">
                    <div class="text-center py-4">
                        <div class="spinner-border text-primary" role="status"></div>
                        <div class="mt-2 text-muted">{{ __('admin/registrars.dashboard_loading') }}</div>
                    </div>
                </div>
            </x-admin.card>
        </div>
    </div>

    <div class="row mt-4">
        <div class="col-md-12">
            <x-admin.card :title="__('admin/registrars.test_actions_title')" icon="fas fa-vial">
                <div id="testActionsContainer">
                    <div class="text-center py-3">
                        <div class="spinner-border spinner-border-sm text-primary" role="status"></div>
                    </div>
                </div>
                <div id="testResultContainer" class="mt-3" style="display: none;">
                    <div class="alert" id="testResultAlert"></div>
                </div>
            </x-admin.card>
        </div>
    </div>
    @endif
</div>

@push('scripts')
<script>
window.registrarConfig = {
    moduleName: '{{ $module['name'] }}',
    isActive: {{ ($registrar && $registrar->status === 'active') ? 'true' : 'false' }},
    routes: {
        dashboard: '{{ route("api.admin.registrars.dashboard", $module["name"]) }}',
        testAction: '{{ route("api.admin.registrars.test-action", $module["name"]) }}'
    },
    translations: {
        dashboard_error: '{{ __("admin/registrars.dashboard_error") }}',
        test_running: '{{ __("admin/registrars.test_running") }}',
        test_no_actions: '{{ __("admin/registrars.test_no_actions") }}'
    }
};
</script>
@vite('resources/js/admin/registrars/edit.js')
@endpush
@endsection
