@extends('admin.layouts.app')

@section('title', __('admin/fraud.modules_title'))

@section('content')
<div class="container-fluid px-4">
    <x-admin.page-header :title="__('admin/fraud.modules_title')" icon="fas fa-puzzle-piece"
        :buttons="[['label' => __('admin/fraud.modules_back_to_dashboard'), 'route' => route('admin.fraud.index'), 'icon' => 'fas fa-arrow-left', 'color' => 'outline-secondary']]" />

    <x-admin.alert />

    <x-admin.card :padding="false">
        @if($modules->count() > 0)
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead>
                        <tr>
                            <th style="width: 80px;" class="text-center">{{ __('admin/fraud.modules_priority') }}</th>
                            <th>{{ __('admin/fraud.modules_module') }}</th>
                            <th class="d-none d-md-table-cell">{{ __('admin/fraud.modules_description') }}</th>
                            <th class="text-center" style="width: 100px;">{{ __('admin/fraud.modules_version') }}</th>
                            <th class="text-center" style="width: 120px;">{{ __('admin/fraud.status') }}</th>
                            <th class="text-end" style="width: 200px;">{{ __('admin/fraud.actions') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($modules as $module)
                        <tr>
                            <td class="text-center">
                                <input type="number" class="form-control form-control-sm text-center module-priority"
                                       data-slug="{{ $module['slug'] }}"
                                       value="{{ $module['priority'] }}"
                                       min="0"
                                       style="width: 60px; margin: 0 auto;">
                            </td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <div>
                                        <strong>{{ $module['metadata']['name'] }}</strong>
                                        <br>
                                        <small class="text-muted">
                                            {{ __('admin/fraud.modules_author') }}: {{ $module['metadata']['author'] }}
                                            @if($module['metadata']['supports_rechecks'])
                                                <span class="text-success ms-2">
                                                    <i class="fas fa-check-circle"></i> {{ __('admin/fraud.modules_supports_rechecks') }}
                                                </span>
                                            @endif
                                        </small>
                                    </div>
                                </div>
                            </td>
                            <td class="d-none d-md-table-cell">
                                <small class="text-muted">{{ $module['metadata']['description'] }}</small>
                            </td>
                            <td class="text-center">
                                <small class="text-muted">{{ $module['metadata']['version'] }}</small>
                            </td>
                            <td class="text-center">
                                <div class="form-check form-switch d-flex justify-content-center mb-0">
                                    <input class="form-check-input module-toggle" type="checkbox"
                                           data-slug="{{ $module['slug'] }}"
                                           {{ $module['enabled'] ? 'checked' : '' }}>
                                </div>
                                @if($module['is_configured'])
                                    <small class="text-success">
                                        <i class="fas fa-check-circle"></i>
                                    </small>
                                @else
                                    <small class="text-warning">
                                        <i class="fas fa-exclamation-triangle"></i>
                                    </small>
                                @endif
                            </td>
                            <td class="text-end">
                                <div class="btn-group btn-group-sm">
                                    <a href="{{ route('admin.fraud.modules.edit', $module['slug']) }}"
                                       class="btn btn-outline-primary"
                                       data-bs-toggle="tooltip"
                                       title="{{ __('admin/fraud.modules_configure') }}">
                                        <i class="fas fa-cog"></i>
                                    </a>
                                    <button type="button"
                                            class="btn btn-outline-secondary test-module"
                                            data-slug="{{ $module['slug'] }}"
                                            data-bs-toggle="tooltip"
                                            title="{{ __('admin/fraud.modules_test_connection') }}">
                                        <i class="fas fa-vial"></i>
                                    </button>
                                </div>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @else
            <div class="p-4 text-center">
                <div class="alert alert-info mb-0">
                    <i class="fas fa-info-circle me-2"></i>
                    {{ __('admin/fraud.modules_no_modules') }}
                </div>
            </div>
        @endif
    </x-admin.card>

    <x-admin.card :title="__('admin/fraud.modules_about_title')" class="mt-4">
        <p class="mb-2">{{ __('admin/fraud.modules_about_description') }}</p>
        <ul class="mb-0">
            <li>{{ __('admin/fraud.modules_about_item_1') }}</li>
            <li>{{ __('admin/fraud.modules_about_item_2') }}</li>
            <li>{{ __('admin/fraud.modules_about_item_3') }}</li>
            <li>{{ __('admin/fraud.modules_about_item_4') }}</li>
        </ul>
    </x-admin.card>
</div>
@endsection

@push('scripts')
<script>
window.fraudModulesConfig = {
    routes: {
        toggle: '{{ route("admin.fraud.modules.toggle", ":slug") }}',
        test: '{{ route("admin.fraud.modules.test", ":slug") }}',
        updatePriorities: '{{ route("admin.fraud.modules.update-priorities") }}'
    },
    translations: {
        toggleSuccess: '{{ __("admin/fraud.messages_module_toggled") }}',
        toggleFailed: '{{ __("admin/fraud.messages_module_toggle_failed") }}',
        testingModule: '{{ __("admin/fraud.messages_testing_module") }}',
        testSuccess: '{{ __("admin/fraud.messages_test_success") }}',
        testFailed: '{{ __("admin/fraud.messages_test_failed") }}',
        priorityUpdated: '{{ __("admin/fraud.messages_priorities_updated") }}'
    }
};
</script>
@vite('resources/js/admin/fraud/modules-index.js')
@endpush
