{{-- Вьюха: форма создания/редактирования услуги --}}
@extends('admin.layout')

@section('title', $service->exists ? 'Редактировать услугу' : 'Новая услуга')

@section('content_header')
    <div class="d-flex justify-content-between align-items-center">
        <h1 class="m-0">{{ $service->exists ? 'Услуга: ' . $service->title : 'Новая услуга' }}</h1>
        <a href="{{ route('admin.services.index') }}" class="btn btn-default btn-sm">
            <i class="fas fa-arrow-left"></i> Назад
        </a>
    </div>
@stop

@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('admin.dashboard') }}">Главная</a></li>
    <li class="breadcrumb-item"><a href="{{ route('admin.services.index') }}">Услуги</a></li>
    <li class="breadcrumb-item active">{{ $service->exists ? 'Редактировать' : 'Новая' }}</li>
@stop

@section('content')
    @if($service->exists)
        <form action="{{ route('admin.services.update', $service) }}" method="POST">
        @method('PUT')
    @else
        <form action="{{ route('admin.services.store') }}" method="POST">
    @endif
        @csrf

        <div class="row">

            {{-- Левая колонка --}}
            <div class="col-md-8">

                <div class="card card-primary card-outline">
                    <div class="card-header"><h3 class="card-title">Основное</h3></div>
                    <div class="card-body">

                        <div class="form-group">
                            <label>Название услуги <span class="text-danger">*</span></label>
                            <input type="text" name="title"
                                   class="form-control @error('title') is-invalid @enderror"
                                   value="{{ old('title', $service->title) }}" required>
                            @error('title')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>

                        @if($service->exists)
                            <div class="form-group">
                                <label>Slug (URL)</label>
                                <input type="text" class="form-control" value="{{ $service->slug }}" readonly>
                                <small class="text-muted">
                                    Публичный адрес:
                                    <a href="{{ route('services.show', $service->slug) }}" target="_blank">
                                        /services/{{ $service->slug }}
                                    </a>
                                </small>
                            </div>
                        @endif

                        <div class="form-group">
                            <label>Иконка (emoji)</label>
                            <input type="text" name="icon" maxlength="10"
                                   class="form-control form-control-sm" style="max-width:80px;font-size:20px"
                                   value="{{ old('icon', $service->icon) }}"
                                   placeholder="✈️">
                            <small class="text-muted">Одна emoji — отображается в блоке услуг</small>
                        </div>

                        <div class="form-group">
                            <label>Краткое описание <small class="text-muted">(для списка)</small></label>
                            <textarea name="excerpt" class="form-control @error('excerpt') is-invalid @enderror"
                                      rows="2" maxlength="500"
                                      placeholder="1–2 предложения для карточки на странице услуг">{{ old('excerpt', $service->excerpt) }}</textarea>
                            @error('excerpt')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>

                        @php
                            $countriesSection  = \App\Models\DictSection::where('code','countries')->first();
                            $tagsSection       = \App\Models\DictSection::where('code','service_tags')->first();
                            $allCountries      = $countriesSection
                                ? \App\Models\DictValue::where('section_id',$countriesSection->id)->orderBy('sort_order')->get()
                                : collect();
                            $allServiceTags    = $tagsSection
                                ? \App\Models\DictValue::where('section_id',$tagsSection->id)->orderBy('sort_order')->get()
                                : collect();
                            $selectedTagIds    = array_map('intval', old('tags', $service->tags ?? []));
                        @endphp

                        <div class="form-group mb-0">
                            <label class="font-weight-bold">Теги</label>
                            <small class="text-muted d-block mb-2">Выбирайте из справочника — страны и функциональные метки</small>

                            @if($allCountries->isNotEmpty())
                                <div class="mb-2">
                                    <div class="small text-muted font-weight-bold mb-1 text-uppercase" style="letter-spacing:.06em">Страны</div>
                                    <div class="border rounded p-2" style="background:#f8f9fa">
                                        <div class="row">
                                            @foreach($allCountries as $country)
                                                <div class="col-6 col-md-4">
                                                    <div class="form-check">
                                                        <input class="form-check-input" type="checkbox"
                                                               name="tags[]"
                                                               value="{{ $country->id }}"
                                                               id="tag-{{ $country->id }}"
                                                               @checked(in_array($country->id, $selectedTagIds))>
                                                        <label class="form-check-label" for="tag-{{ $country->id }}">
                                                            {{ $country->flag }} {{ $country->value }}
                                                        </label>
                                                    </div>
                                                </div>
                                            @endforeach
                                        </div>
                                    </div>
                                </div>
                            @endif

                            @if($allServiceTags->isNotEmpty())
                                <div>
                                    <div class="small text-muted font-weight-bold mb-1 text-uppercase" style="letter-spacing:.06em">Метки услуг</div>
                                    <div class="border rounded p-2" style="background:#f8f9fa">
                                        <div class="row">
                                            @foreach($allServiceTags as $tag)
                                                <div class="col-6 col-md-4">
                                                    <div class="form-check">
                                                        <input class="form-check-input" type="checkbox"
                                                               name="tags[]"
                                                               value="{{ $tag->id }}"
                                                               id="tag-{{ $tag->id }}"
                                                               @checked(in_array($tag->id, $selectedTagIds))>
                                                        <label class="form-check-label" for="tag-{{ $tag->id }}">
                                                            {{ $tag->value }}
                                                        </label>
                                                    </div>
                                                </div>
                                            @endforeach
                                        </div>
                                    </div>
                                </div>
                            @endif
                        </div>

                    </div>
                </div>

                <div class="card card-secondary card-outline">
                    <div class="card-header"><h3 class="card-title">Полное описание</h3></div>
                    <div class="card-body">
                        <div class="form-group mb-0">
                            <label>HTML-контент детальной страницы</label>
                            <textarea name="description"
                                      class="form-control @error('description') is-invalid @enderror"
                                      rows="12"
                                      placeholder="Полное описание услуги — HTML-теги допустимы">{{ old('description', $service->description) }}</textarea>
                            <small class="text-muted">Отображается на детальной странице /services/{{ $service->slug ?: '{slug}' }}</small>
                            @error('description')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                    </div>
                </div>

            </div>

            {{-- Правая колонка --}}
            <div class="col-md-4">
                <div class="card card-secondary card-outline">
                    <div class="card-header"><h3 class="card-title">Публикация</h3></div>
                    <div class="card-body">

                        <div class="custom-control custom-switch mb-3">
                            <input type="checkbox" class="custom-control-input" id="is_active"
                                   name="is_active" value="1"
                                   {{ old('is_active', $service->is_active ?? true) ? 'checked' : '' }}>
                            <label class="custom-control-label" for="is_active">Услуга активна</label>
                        </div>

                        <div class="form-group mb-0">
                            <label>Порядок сортировки</label>
                            <input type="number" name="sort_order" min="0"
                                   class="form-control form-control-sm"
                                   style="max-width:100px"
                                   value="{{ old('sort_order', $service->sort_order ?? 0) }}">
                            <small class="text-muted">Чем меньше — тем выше в списке</small>
                        </div>

                    </div>
                    <div class="card-footer">
                        <button type="submit" class="btn btn-primary btn-block">
                            <i class="fas fa-save"></i> Сохранить
                        </button>
                        <a href="{{ route('admin.services.index') }}" class="btn btn-secondary btn-block mt-1">
                            Отмена
                        </a>
                    </div>
                </div>
            </div>

        </div>
    </form>
@stop
