@extends('admin.layouts.app')

@section('title', __('admin/tlds.create_title'))

@push('styles')
<style>
    .pricing-table th, .pricing-table td {
        vertical-align: middle;
        text-align: center;
        padding: 0.4rem !important;
        font-size: 0.8rem;
    }
    .pricing-table .form-control {
        font-size: 0.75rem;
        padding: 0.25rem 0.4rem;
        width: 80px;
        display: inline-block;
    }
    .pricing-table .form-check-input {
        width: 1.1em;
        height: 1.1em;
        margin-left: 5px;
    }
    .year-group-label {
        writing-mode: vertical-rl;
        text-orientation: mixed;
        white-space: nowrap;
        font-weight: bold;
        text-align: center;
        vertical-align: middle !important;
        padding: 5px !important;
        width: 40px;
    }
    .currency-label-cell {
        text-align: left !important;
        padding-left: 15px !important;
        width: 80px;
    }
    .segment-header {
        min-width: 180px;
    }
    .price-input-group {
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .price-input-group .currency-symbol {
        margin: 0 5px;
    }
    .currency-row.hidden-currency {
        display: none;
    }
</style>
@endpush

@section('content')
<div class="container-fluid px-4">
    <form action="{{ route('admin.tlds.store') }}" method="POST" id="tldCreateForm">
        @csrf

        <x-admin.page-header
            :title="__('admin/tlds.create_title')"
            icon="fas fa-globe"
            :buttons="[
                ['label' => __('admin/global.buttons_cancel'), 'route' => route('admin.tlds.index'), 'icon' => 'fa fa-arrow-left', 'color' => 'secondary'],
            ]"
        >
            <button type="submit" class="btn btn-primary btn-sm">
                <i class="fas fa-save me-1"></i>{{ __('admin/global.buttons_save') }}
            </button>
        </x-admin.page-header>

        <x-admin.alert />

        <div class="row">
            <!-- Left Block: Pricing -->
            <div class="col-lg-8 mb-4">
                <x-admin.card :padding="false">
                    <x-slot:header>
                        <h5 class="card-title mb-0">{{ __('admin/tlds.create_pricing_title') }}</h5>
                    </x-slot:header>
                    <x-slot:headerActions>
                        <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox" role="switch" id="adaptivePricingSwitch" name="adaptive_pricing" value="1" checked>
                            <label class="form-check-label" for="adaptivePricingSwitch">{{ __('admin/tlds.fields_adaptive_pricing') }}</label>
                        </div>
                    </x-slot:headerActions>

                    @if($allCurrencies->isEmpty())
                        <div class="alert alert-warning m-3">{{ __('admin/tlds.messages_no_active_currencies') }}</div>
                    @else
                        <div class="table-responsive">
                            <table class="table table-bordered table-hover pricing-table mb-0" id="pricingTable">
                                <thead>
                                    <tr>
                                        <th style="width: 40px;">&nbsp;</th>
                                        <th class="currency-label-cell">{{ __('admin/tlds.fields_currency') }}</th>
                                        <th class="segment-header">{{ __('admin/tlds.fields_pricing_register') }}</th>
                                        <th class="segment-header">{{ __('admin/tlds.fields_pricing_transfer') }}</th>
                                        <th class="segment-header">{{ __('admin/tlds.fields_pricing_renew') }}</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @php
                                        $min_render_year = old('min_years', 1);
                                        $max_render_year = old('max_years', 10);
                                    @endphp
                                    @for ($year = $min_render_year; $year <= $max_render_year; $year++)
                                        @php $firstCurrencyInYear = true; @endphp
                                        @foreach($allCurrencies as $currency_idx => $currency)
                                            <tr data-year-row="{{ $year }}" data-currency-id="{{ $currency->id }}" class="currency-row {{ $currency->is_default ? 'default-currency-row' : 'other-currency-row' }}">
                                                @if($firstCurrencyInYear)
                                                    <td rowspan="1" class="year-group-label" data-year-label-for="{{ $year }}">
                                                        {{ __('admin/tlds.fields_year_with_val', ['year' => $year]) }}
                                                    </td>
                                                    @php $firstCurrencyInYear = false; @endphp
                                                @endif
                                                <td class="currency-label-cell">{{ $currency->code }} {{ $currency->is_default ? '(Default)' : '' }}</td>
                                                @foreach(['register', 'transfer', 'renew'] as $segment)
                                                    @php
                                                        $is_disabled_for_adaptive = old('adaptive_pricing') && !$currency->is_default;
                                                    @endphp
                                                    <td>
                                                        <div class="price-input-group">
                                                            <input type="number" step="0.01"
                                                                   name="pricing[{{ $currency->id }}][{{ $year }}][{{ $segment }}][amount]"
                                                                   class="form-control form-control-sm price-input"
                                                                   value="{{ old("pricing.{$currency->id}.{$year}.{$segment}.amount") }}"
                                                                   placeholder="-"
                                                                   {{ $is_disabled_for_adaptive ? 'disabled' : '' }}>
                                                            <span class="currency-symbol">{{ $currency->prefix ?: $currency->suffix ?: $currency->code }}</span>
                                                            <input class="form-check-input price-active-toggle"
                                                                   type="checkbox"
                                                                   name="pricing[{{ $currency->id }}][{{ $year }}][{{ $segment }}][active]"
                                                                   value="1"
                                                                   {{ old("pricing.{$currency->id}.{$year}.{$segment}.active") ? 'checked' : '' }}
                                                                   {{ $is_disabled_for_adaptive ? 'disabled' : '' }}>
                                                        </div>
                                                    </td>
                                                @endforeach
                                            </tr>
                                        @endforeach
                                    @endfor
                                </tbody>
                            </table>
                        </div>

                        <hr class="my-3">
                        <h5 class="mb-3 px-3">{{ __('admin/tlds.create_other_fees_title') }}</h5>
                        <div class="table-responsive">
                            <table class="table table-bordered table-hover pricing-table mb-0" id="otherFeesTable">
                                <thead>
                                    <tr>
                                        <th>{{ __('admin/tlds.fields_pricing_fee_type') }}</th>
                                        @foreach($allCurrencies as $currency)
                                            <th class="text-center currency-header-cell {{ $currency->is_default ? 'default-currency-header' : 'other-currency-header' }}" data-currency-id="{{ $currency->id }}">{{ $currency->code }} {{ $currency->is_default ? '(Default)' : '' }}</th>
                                        @endforeach
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach(['grace', 'redemption', 'whois'] as $segment)
                                    <tr>
                                        <td>{{ __('admin/tlds.fields_pricing_' . $segment) }}</td>
                                        @foreach($allCurrencies as $currency)
                                            @php
                                                $is_disabled_for_adaptive_other = old('adaptive_pricing') && !$currency->is_default;
                                            @endphp
                                            <td class="currency-data-cell {{ $currency->is_default ? 'default-currency-cell' : 'other-currency-cell' }}" data-currency-id="{{ $currency->id }}">
                                                <div class="price-input-group">
                                                    <input type="number" step="0.01"
                                                           name="pricing_other[{{ $currency->id }}][{{ $segment }}][amount]"
                                                           class="form-control form-control-sm price-input"
                                                           value="{{ old("pricing_other.{$currency->id}.{$segment}.amount") }}"
                                                           placeholder="-"
                                                           {{ $is_disabled_for_adaptive_other ? 'disabled' : '' }}>
                                                    <span class="currency-symbol">{{ $currency->prefix ?: $currency->suffix ?: $currency->code }}</span>
                                                    <input class="form-check-input price-active-toggle"
                                                           type="checkbox"
                                                           name="pricing_other[{{ $currency->id }}][{{ $segment }}][active]"
                                                           value="1"
                                                           {{ old("pricing_other.{$currency->id}.{$segment}.active") ? 'checked' : '' }}
                                                           {{ $is_disabled_for_adaptive_other ? 'disabled' : '' }}>
                                                </div>
                                            </td>
                                        @endforeach
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    @endif
                </x-admin.card>
            </div>

            <!-- Right Block: TLD Settings -->
            <div class="col-lg-4 mb-4">
                <x-admin.card :title="__('admin/tlds.create_settings_title')">
                    <div class="mb-3">
                        <label for="tld_name" class="form-label">{{ __('admin/tlds.fields_tld') }} <span class="text-danger">*</span></label>
                        <input type="text" class="form-control @error('tld') is-invalid @enderror" id="tld_name" name="tld" value="{{ old('tld') }}" required>
                        @error('tld')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="min_years_for_registration" class="form-label">{{ __('admin/tlds.fields_min_years') }} <span class="text-danger">*</span></label>
                            <input type="number" class="form-control @error('min_years') is-invalid @enderror" id="min_years_for_registration" name="min_years" value="{{ old('min_years', 1) }}" min="1" max="10" required>
                            @error('min_years')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="max_years_for_registration" class="form-label">{{ __('admin/tlds.fields_max_years') }} <span class="text-danger">*</span></label>
                            <input type="number" class="form-control @error('max_years') is-invalid @enderror" id="max_years_for_registration" name="max_years" value="{{ old('max_years', 10) }}" min="1" max="10" required>
                            @error('max_years')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="min_chars" class="form-label">{{ __('admin/tlds.fields_min_chars') }} <span class="text-danger">*</span></label>
                            <input type="number" class="form-control @error('min_chars') is-invalid @enderror" id="min_chars" name="min_chars" value="{{ old('min_chars', 3) }}" min="1" max="63" required>
                            @error('min_chars')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="max_chars" class="form-label">{{ __('admin/tlds.fields_max_chars') }} <span class="text-danger">*</span></label>
                            <input type="number" class="form-control @error('max_chars') is-invalid @enderror" id="max_chars" name="max_chars" value="{{ old('max_chars', 63) }}" min="1" max="63" required>
                            @error('max_chars')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="grace_period" class="form-label">{{ __('admin/tlds.fields_grace_period') }}</label>
                            <input type="number" class="form-control @error('grace_period') is-invalid @enderror" id="grace_period" name="grace_period" value="{{ old('grace_period', 30) }}" min="0">
                            @error('grace_period')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="redemption_period" class="form-label">{{ __('admin/tlds.fields_redemption_period') }}</label>
                            <input type="number" class="form-control @error('redemption_period') is-invalid @enderror" id="redemption_period" name="redemption_period" value="{{ old('redemption_period', 30) }}" min="0">
                            @error('redemption_period')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="registrar" class="form-label">{{ __('admin/tlds.fields_registrar_module') }}</label>
                            <select class="form-select @error('registrar') is-invalid @enderror" id="registrar" name="registrar">
                                <option value="">{{ __('admin/global.select_placeholder') }}</option>
                                @foreach($availableRegistrars as $regModule)
                                    <option value="{{ $regModule['name'] }}" {{ old('registrar') == $regModule['name'] ? 'selected' : '' }}>{{ $regModule['label'] }}</option>
                                @endforeach
                            </select>
                            @error('registrar')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="whois_provider" class="form-label">{{ __('admin/tlds.fields_whois_provider') }}</label>
                            <select class="form-select @error('whois_provider') is-invalid @enderror" id="whois_provider" name="whois_provider">
                                <option value="public" {{ old('whois_provider', 'public') == 'public' ? 'selected' : '' }}>Public</option>
                                <option value="registrar" {{ old('whois_provider', 'registrar') == 'registrar' ? 'selected' : '' }}>Registrar</option>
                            </select>
                            @error('whois_provider')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label for="lookup_provider" class="form-label">{{ __('admin/tlds.fields_lookup_provider') }}</label>
                            <select class="form-select @error('lookup_provider') is-invalid @enderror" id="lookup_provider" name="lookup_provider">
                                <option value="public" {{ old('lookup_provider', 'public') == 'public' ? 'selected' : '' }}>Public</option>
                                <option value="registrar" {{ old('lookup_provider', 'registrar') == 'registrar' ? 'selected' : '' }}>Registrar</option>
                            </select>
                            @error('lookup_provider')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="order" class="form-label">{{ __('admin/global.fields_order') }}</label>
                            <input type="number" class="form-control @error('order') is-invalid @enderror" id="order" name="order" value="{{ old('order', 1) }}">
                            @error('order')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>

                    <div class="mb-3">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" id="is_premium" name="is_premium" value="1" {{ old('is_premium') ? 'checked' : '' }}>
                            <label class="form-check-label" for="is_premium">{{ __('admin/tlds.fields_is_premium') }}</label>
                        </div>
                    </div>

                    <div class="mb-3">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" id="dns_management" name="dns_management" value="1" {{ old('dns_management') ? 'checked' : '' }}>
                            <label class="form-check-label" for="dns_management">{{ __('admin/tlds.fields_dns_management') }}</label>
                        </div>
                    </div>

                    <div class="mb-3">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" id="id_protection" name="id_protection" value="1" {{ old('id_protection') ? 'checked' : '' }}>
                            <label class="form-check-label" for="id_protection">{{ __('admin/tlds.fields_id_protection') }}</label>
                        </div>
                    </div>

                    <div class="mb-3">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" id="epp_code" name="epp_code" value="1" {{ old('epp_code') ? 'checked' : '' }}>
                            <label class="form-check-label" for="epp_code">{{ __('admin/tlds.fields_epp_code') }}</label>
                        </div>
                    </div>

                    <div class="mb-3">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" id="email_forwarding" name="email_forwarding" value="1" {{ old('email_forwarding') ? 'checked' : '' }}>
                            <label class="form-check-label" for="email_forwarding">{{ __('admin/tlds.fields_email_forwarding') }}</label>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label for="status" class="form-label">{{ __('admin/tlds.fields_status') }}</label>
                        <select class="form-select @error('status') is-invalid @enderror" id="status" name="status">
                            <option value="1" {{ old('status', 1) == 1 ? 'selected' : '' }}>{{ __('admin/tlds.status_active') }}</option>
                            <option value="0" {{ old('status') == 0 ? 'selected' : '' }}>{{ __('admin/tlds.status_inactive') }}</option>
                        </select>
                        @error('status')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </x-admin.card>
            </div>
        </div>
    </form>
</div>
@endsection

@push('scripts')
<script>
    // Para birimlerini JavaScript'e aktar
    window.allCurrencies = @json($allCurrencies);
</script>
@vite('resources/js/admin/tlds/create.js')
@endpush