@extends('admin.layouts.app')

@section('title', __('admin/currencies.title'))

@section('content')
    <div class="container-fluid px-4">
        <x-admin.page-header :title="__('admin/currencies.title')" icon="fas fa-coins">
            @can('updateAny', App\Models\Currency::class)
            <button type="button" id="updateRatesBtn" class="btn btn-primary btn-sm">
                <i class="fas fa-sync-alt me-2"></i>
                @lang('admin/currencies.buttons_update_rates')
            </button>
            @endcan
        </x-admin.page-header>

        @can('manage settings')
        <x-admin.card class="mb-3">
            <button class="btn btn-link p-0 mb-2 d-flex align-items-center gap-2 text-decoration-none" type="button" data-bs-toggle="collapse" data-bs-target="#currencyFxSettings" aria-expanded="false">
                <i class="fas fa-cog"></i>
                <strong>{{ __('admin/currencies.settings_title') }}</strong>
                <i class="fas fa-chevron-down small ms-auto"></i>
            </button>
            <div class="collapse" id="currencyFxSettings">
                <form method="POST" action="{{ route('admin.settings.currencies.settings.update') }}" class="pt-2">
                    @csrf
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label">{{ __('admin/currencies.settings_default_currency') }}</label>
                            <select class="form-select" name="default_currency">
                                @foreach ($currencies as $cur)
                                    <option value="{{ $cur->id }}" @selected(($settings['default_currency'] ?? 1) === $cur->id)>{{ $cur->code }} — {{ $cur->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">{{ __('admin/currencies.settings_fx_failover_order') }}</label>
                            <input type="text" class="form-control" name="fx_provider_failover_order" value="{{ $settings['fx_provider_failover_order'] ?? 'ecb,open_er_api,open_exchange_rates,fixer' }}" maxlength="255">
                            <div class="form-text small">{{ __('admin/currencies.settings_fx_failover_order_help') }}</div>
                        </div>
                        <div class="col-12">
                            <div class="alert alert-info border-0 small mb-0">
                                <i class="fas fa-info-circle me-1"></i>{{ __('admin/currencies.settings_fx_keys_moved') }}
                                <a href="{{ route('admin.currency-providers.index') }}">/admin/currency-providers</a>
                            </div>
                        </div>
                    </div>
                    <div class="text-end mt-3">
                        <button type="submit" class="btn btn-primary btn-sm">
                            <i class="fas fa-save me-1"></i>{{ __('admin/common.save') }}
                        </button>
                    </div>
                </form>
            </div>
        </x-admin.card>
        @endcan

        <x-admin.card>
            <x-admin.data-table id="currenciesTable" :records-url="route('api.admin.settings.currencies.records')">
                <th>@lang('admin/currencies.fields_id')</th>
                <th>@lang('admin/currencies.fields_name')</th>
                <th>@lang('admin/currencies.fields_code')</th>
                <th>@lang('admin/currencies.fields_prefix')</th>
                <th>@lang('admin/currencies.fields_suffix')</th>
                <th>@lang('admin/currencies.fields_rate')</th>
                <th>@lang('admin/currencies.fields_is_default')</th>
                <th>@lang('admin/currencies.fields_status')</th>
                <th>@lang('admin/currencies.fields_actions')</th>
            </x-admin.data-table>
        </x-admin.card>
    </div>

    <!-- Para Birimi Düzenleme Modal -->
    <x-admin.modal id="currencyModal" :title="__('admin/currencies.modal_edit_title')">
        <form id="currencyForm">
            <input type="hidden" id="currencyId" value="">

            <div class="mb-3">
                <label for="name" class="form-label">@lang('admin/currencies.fields_name')</label>
                <input type="text" class="form-control" id="name" name="name">
                <div class="invalid-feedback" id="name_error"></div>
            </div>

            <div class="mb-3">
                <label for="code" class="form-label">@lang('admin/currencies.fields_code')</label>
                <input type="text" class="form-control" id="code" name="code" maxlength="3" readonly>
                <div class="invalid-feedback" id="code_error"></div>
            </div>

            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="prefix" class="form-label">@lang('admin/currencies.fields_prefix')</label>
                        <input type="text" class="form-control" id="prefix" name="prefix">
                        <div class="invalid-feedback" id="prefix_error"></div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="suffix" class="form-label">@lang('admin/currencies.fields_suffix')</label>
                        <input type="text" class="form-control" id="suffix" name="suffix">
                        <div class="invalid-feedback" id="suffix_error"></div>
                    </div>
                </div>
            </div>

            <div class="mb-3">
                <label for="rate" class="form-label">@lang('admin/currencies.fields_rate')</label>
                <input type="number" class="form-control" id="rate" name="rate" step="0.00001">
                <div class="invalid-feedback" id="rate_error"></div>
            </div>

            <div class="mb-3">
                <div class="form-check form-switch">
                    <input class="form-check-input" type="checkbox" id="is_default" name="is_default">
                    <label class="form-check-label" for="is_default">@lang('admin/currencies.fields_is_default')</label>
                </div>
                <div class="invalid-feedback" id="is_default_error"></div>
            </div>

            <div class="mb-3">
                <label class="form-label">@lang('admin/currencies.fields_status')</label>
                <div>
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="radio" name="status" id="status_active" value="1">
                        <label class="form-check-label" for="status_active">@lang('admin/currencies.status_active')</label>
                    </div>
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="radio" name="status" id="status_inactive" value="0">
                        <label class="form-check-label" for="status_inactive">@lang('admin/currencies.status_inactive')</label>
                    </div>
                </div>
            </div>
        </form>

        <x-slot:footer>
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">@lang('admin/currencies.buttons_cancel')</button>
            <button type="button" class="btn btn-primary" id="saveCurrencyBtn">@lang('admin/currencies.buttons_save')</button>
        </x-slot:footer>
    </x-admin.modal>
@endsection

@push('scripts')
<script>
    // Yetki kontrolü için permissions nesnesi
    window.permissions = {
        currencies: {
            update: {{ json_encode(Auth::user()->can('updateAny', App\Models\Currency::class)) }}
        }
    };

    // Dil çevirileri
    window.translations = {
        currencies: {
            title: '{{ __('admin/currencies.title') }}',
            fields: {
                name: '{{ __('admin/currencies.fields_name') }}',
                code: '{{ __('admin/currencies.fields_code') }}',
                rate: '{{ __('admin/currencies.fields_rate') }}',
                status: '{{ __('admin/currencies.fields_status') }}',
                actions: '{{ __('admin/currencies.fields_actions') }}'
            },
            status: {
                active: '{{ __('admin/currencies.status_active') }}',
                inactive: '{{ __('admin/currencies.status_inactive') }}',
                default: '{{ __('admin/currencies.fields_is_default') }}'
            },
            buttons: {
                save: '{{ __('admin/currencies.buttons_save') }}',
                cancel: '{{ __('admin/currencies.buttons_cancel') }}',
                edit: '{{ __('admin/currencies.modal_edit_title') }}',
                update_rates: '{{ __('admin/currencies.buttons_update_rates') }}'
            },
            messages: {
                rates_last_updated: '{{ __('admin/currencies.messages_rates_last_updated') }}',
                rates_never_updated: '{{ __('admin/currencies.messages_rates_never_updated') }}',
                rates_update_error: '{{ __('admin/currencies.messages_rates_update_error') }}',
                load_error: '{{ __('admin/currencies.messages_load_error') }}',
                save_error: '{{ __('admin/currencies.messages_save_error') }}',
                not_found: '{{ __('admin/currencies.messages_not_found') }}'
            },
            modal: {
                edit_title: '{{ __('admin/currencies.modal_edit_title') }}'
            }
        }
    };

    // Route bilgilerini JavaScript'e aktar
    window.routes = {
        currencies: {
            update: '{{ route("admin.settings.currencies.update", ":id") }}',
            rates: {
                update: '{{ route("api.admin.settings.currencies.rates.update") }}',
                info: '{{ route("api.admin.settings.currencies.rates.info") }}'
            }
        }
    };
</script>
@endpush

@push('scripts')
    @vite('resources/js/admin/currencies/index.js')
@endpush
