@extends('admin.layouts.app')

@section('title', __('admin/languages.edit_title', ['name' => $language->native_name]))

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

    <x-admin.alert />

    
    <form method="POST" action="{{ route('admin.settings.languages.update', $language) }}">
        @csrf
        @method('PUT')

        <x-admin.card :title="__('admin/languages.section_basic_info')">
            <div class="row mb-3">
                <label class="col-md-3 col-form-label text-md-end">@lang('admin/languages.fields_native_name')</label>
                <div class="col-md-6">
                    <input type="text" class="form-control @error('native_name') is-invalid @enderror"
                           name="native_name" value="{{ old('native_name', $language->native_name) }}">
                    @error('native_name') <div class="invalid-feedback">{{ $message }}</div> @enderror
                </div>
            </div>

            <div class="row mb-3">
                <label class="col-md-3 col-form-label text-md-end">@lang('admin/languages.fields_a2_iso')</label>
                <div class="col-md-6">
                    <input type="text" class="form-control" value="{{ $language->a2_iso }}" readonly>
                </div>
            </div>

            <div class="row mb-3">
                <label class="col-md-3 col-form-label text-md-end">@lang('admin/languages.fields_a3_iso')</label>
                <div class="col-md-6">
                    <input type="text" class="form-control" value="{{ $language->a3_iso }}" readonly>
                </div>
            </div>

            <div class="row mb-3">
                <label class="col-md-3 col-form-label text-md-end">@lang('admin/languages.fields_status')</label>
                <div class="col-md-6">
                    <div class="form-check form-switch d-flex align-items-center gap-2">
                        <input class="form-check-input" type="checkbox" name="status" value="1"
                               {{ old('status', $language->status) ? 'checked' : '' }}>
                        <span class="form-text mb-0">@lang('admin/languages.fields_status_description')</span>
                    </div>
                </div>
            </div>

            <div class="row mb-3">
                <label class="col-md-3 col-form-label text-md-end">@lang('admin/languages.fields_site_default')</label>
                <div class="col-md-6">
                    <div class="form-check form-switch d-flex align-items-center gap-2">
                        <input class="form-check-input" type="checkbox" name="site_default" value="1"
                               {{ old('site_default', $language->site_default) ? 'checked' : '' }}>
                        <span class="form-text mb-0">@lang('admin/languages.fields_site_default_description')</span>
                    </div>
                </div>
            </div>

            <div class="row mb-3">
                <label class="col-md-3 col-form-label text-md-end">@lang('admin/languages.fields_admin_default')</label>
                <div class="col-md-6">
                    <div class="form-check form-switch d-flex align-items-center gap-2">
                        <input class="form-check-input" type="checkbox" name="admin_default" value="1"
                               {{ old('admin_default', $language->admin_default) ? 'checked' : '' }}>
                        <span class="form-text mb-0">@lang('admin/languages.fields_admin_default_description')</span>
                    </div>
                </div>
            </div>

            <x-slot:footer>
                <x-admin.form-actions :cancel-url="route('admin.settings.languages.index')" />
            </x-slot:footer>
        </x-admin.card>
    </form>

    
    <form method="POST" action="{{ route('admin.settings.languages.route-slugs.update', $language) }}"
          enctype="multipart/form-data" class="mt-4">
        @csrf
        @method('PUT')

        <x-admin.card :title="__('admin/languages.section_url_slugs')">
            <p class="text-muted small mb-3">
                <i class="fas fa-info-circle me-1"></i>
                @lang('admin/languages.url_slugs_description', ['locale' => $language->a2_iso])
            </p>

            @if($errors->has('slugs'))
                <div class="alert alert-danger">
                    <strong>@lang('admin/languages.url_slugs_validation_errors'):</strong>
                    <ul class="mb-0">
                        @foreach((array) $errors->get('slugs') as $errArr)
                            @foreach((array) $errArr as $key => $msg)
                                <li><code>{{ is_string($key) ? $key : '' }}</code>: {{ $msg }}</li>
                            @endforeach
                        @endforeach
                    </ul>
                </div>
            @endif

            <div class="accordion" id="routeSlugAccordion">
                @foreach($knownRoutes as $idx => $routeKey)
                    @php
                        $row = $rows[$routeKey] ?? null;
                        $currentSlug = old("slugs.{$routeKey}", $row?->slug ?? '');
                        $metaTitle = old("meta_titles.{$routeKey}", $row?->meta_title ?? '');
                        $metaDesc = old("meta_descriptions.{$routeKey}", $row?->meta_description ?? '');
                        $ogImage = $row?->og_image;
                        $isHomepage = $routeKey === 'store.index';
                        $expanded = $idx === 0 ? 'true' : 'false';
                        $show = $idx === 0 ? 'show' : '';
                    @endphp
                    <div class="accordion-item border-0 shadow-sm mb-2">
                        <h2 class="accordion-header">
                            <button class="accordion-button {{ $idx !== 0 ? 'collapsed' : '' }}" type="button"
                                    data-bs-toggle="collapse" data-bs-target="#accordion-{{ $idx }}"
                                    aria-expanded="{{ $expanded }}">
                                <div>
                                    <code class="me-2">{{ $routeKey }}</code>
                                    @if($currentSlug)
                                        <span class="badge bg-secondary"><i class="fas fa-link me-1"></i>/{{ $currentSlug }}</span>
                                    @endif
                                    @if($metaTitle)
                                        <span class="badge bg-info ms-1"><i class="fas fa-tag me-1"></i>{{ Str::limit($metaTitle, 40) }}</span>
                                    @endif
                                    @if($ogImage)
                                        <span class="badge bg-success ms-1"><i class="fas fa-image me-1"></i>OG</span>
                                    @endif
                                </div>
                            </button>
                        </h2>
                        <div id="accordion-{{ $idx }}" class="accordion-collapse collapse {{ $show }}"
                             data-bs-parent="#routeSlugAccordion">
                            <div class="accordion-body">
                                <div class="form-text small mb-3">
                                    {{ __('admin/languages.url_slug_route_'.str_replace(['.', '-'], ['_', '-'], $routeKey)) }}
                                </div>

                                
                                @if(! $isHomepage)
                                <div class="mb-3">
                                    <label class="form-label">@lang('admin/languages.url_slug_input_label')</label>
                                    <input type="text" class="form-control" name="slugs[{{ $routeKey }}]"
                                           value="{{ $currentSlug }}" placeholder="örn: sepet">
                                    @if($currentSlug)
                                        <div class="form-text small">@lang('admin/languages.url_slug_preview_label'): <code>/{{ $currentSlug }}</code></div>
                                    @endif
                                </div>
                                @else
                                <input type="hidden" name="slugs[{{ $routeKey }}]" value="">
                                <div class="alert alert-info small py-2 mb-3">
                                    <i class="fas fa-home me-1"></i>@lang('admin/languages.url_slug_homepage_note')
                                </div>
                                @endif

                                
                                <div class="mb-3">
                                    <label class="form-label">@lang('admin/languages.seo_meta_title_label')</label>
                                    <input type="text" class="form-control" name="meta_titles[{{ $routeKey }}]"
                                           value="{{ $metaTitle }}" maxlength="255"
                                           placeholder="@lang('admin/languages.seo_meta_title_placeholder')">
                                    <div class="form-text small">@lang('admin/languages.seo_meta_title_hint')</div>
                                </div>

                                
                                <div class="mb-3">
                                    <label class="form-label">@lang('admin/languages.seo_meta_description_label')</label>
                                    <textarea class="form-control" name="meta_descriptions[{{ $routeKey }}]"
                                              rows="2" maxlength="1000"
                                              placeholder="@lang('admin/languages.seo_meta_description_placeholder')">{{ $metaDesc }}</textarea>
                                    <div class="form-text small">@lang('admin/languages.seo_meta_description_hint')</div>
                                </div>

                                
                                <div class="mb-3">
                                    <label class="form-label">@lang('admin/languages.seo_og_image_label')</label>
                                    @if($ogImage)
                                        <div class="mb-2">
                                            <img src="{{ asset('storage/' . $ogImage) }}" alt="OG" style="max-width: 200px; max-height: 100px;" class="border rounded">
                                            <div class="form-check mt-1">
                                                <input class="form-check-input" type="checkbox" name="og_image_remove[{{ $routeKey }}]" value="1" id="rm-{{ $idx }}">
                                                <label class="form-check-label small text-danger" for="rm-{{ $idx }}">
                                                    @lang('admin/languages.seo_og_image_remove')
                                                </label>
                                            </div>
                                        </div>
                                    @endif
                                    <input type="file" class="form-control mb-2" name="og_images[{{ $routeKey }}]"
                                           accept="image/jpeg,image/png,image/webp">
                                    <input type="text" class="form-control" name="og_image_alts[{{ $routeKey }}]"
                                           value="{{ old('og_image_alts.'.$routeKey, $row?->og_image_alt ?? '') }}"
                                           maxlength="255" placeholder="@lang('admin/languages.seo_og_image_alt_placeholder')">
                                    <div class="form-text small">@lang('admin/languages.seo_og_image_hint')</div>
                                </div>

                                
                                <div class="row g-2 mb-3">
                                    <div class="col-md-6">
                                        <label class="form-label">@lang('admin/languages.seo_keywords_label')</label>
                                        <input type="text" class="form-control" name="keywords[{{ $routeKey }}]"
                                               value="{{ old('keywords.'.$routeKey, $row?->keywords ?? '') }}"
                                               maxlength="255" placeholder="@lang('admin/languages.seo_keywords_placeholder')">
                                        <div class="form-text small">@lang('admin/languages.seo_keywords_hint')</div>
                                    </div>
                                    <div class="col-md-6">
                                        <label class="form-label">@lang('admin/languages.seo_robots_label')</label>
                                        @php($currentRobots = old('robots.'.$routeKey, $row?->robots ?? ''))
                                        <select class="form-select" name="robots[{{ $routeKey }}]">
                                            <option value="" {{ $currentRobots === '' ? 'selected' : '' }}>@lang('admin/languages.seo_robots_default')</option>
                                            <option value="index,follow" {{ $currentRobots === 'index,follow' ? 'selected' : '' }}>index,follow</option>
                                            <option value="index,nofollow" {{ $currentRobots === 'index,nofollow' ? 'selected' : '' }}>index,nofollow</option>
                                            <option value="noindex,follow" {{ $currentRobots === 'noindex,follow' ? 'selected' : '' }}>noindex,follow</option>
                                            <option value="noindex,nofollow" {{ $currentRobots === 'noindex,nofollow' ? 'selected' : '' }}>noindex,nofollow</option>
                                        </select>
                                        <div class="form-text small">@lang('admin/languages.seo_robots_hint')</div>
                                    </div>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">@lang('admin/languages.seo_canonical_label')</label>
                                    <input type="url" class="form-control" name="canonical_urls[{{ $routeKey }}]"
                                           value="{{ old('canonical_urls.'.$routeKey, $row?->canonical_url ?? '') }}"
                                           maxlength="500" placeholder="@lang('admin/languages.seo_canonical_placeholder')">
                                    <div class="form-text small">@lang('admin/languages.seo_canonical_hint')</div>
                                </div>
                            </div>
                        </div>
                    </div>
                @endforeach
            </div>

            <x-slot:footer>
                <x-admin.form-actions :cancel-url="route('admin.settings.languages.index')"
                                     :submit-text="__('admin/languages.url_slugs_save')" />
            </x-slot:footer>
        </x-admin.card>
    </form>
</div>
@endsection
