@extends('layouts.admin.theme')
@section("title", env("APP_NAME") . " :: " . __("Editar Menu Reutilizável") . " - " . $menu->title)
@section('content')
    <div class="container mt-4">
        <div class="row">
            <div class="col-12">
                <div class="card shadow-sm">
                    @php
                        $headLine = __('Editar Menu Reutilizável') . ' – ' . $menu->title;
                    @endphp
                    @include('components.buttons._headLineButtonsBackAdd', [
                        'headLine' => $headLine,
                        'routeBack' => 'admin.menusReutilizaveis.index',
                        'routeBackParams' => $filterParams ?? [],
                        'nameBack' => __('Voltar'),
                        'permission' => 'GERENCIADOR_DE_MENUS_REUTILIZAVEIS',
                    ])

                    <div class="card-body">
                        @if (session('success'))
                            <div class="alert alert-success">{{ session('success') }}</div>
                        @endif

                        @if (session('error'))
                            <div class="alert alert-danger">{{ session('error') }}</div>
                        @endif

                        <form action="{{ route('admin.menusReutilizaveis.update', $menu->id) }}" method="post">
                            @csrf
                            @method('patch')

                            @if(!empty($filterParams))
                                @foreach($filterParams as $key => $value)
                                    @if($value !== null && $value !== '')
                                        <input type="hidden" name="{{ $key }}" value="{{ $value }}">
                                    @endif
                                @endforeach
                            @endif

                            @if ($errors->any())
                                @foreach ($errors->all() as $error)
                                    <div class="alert alert-danger">
                                        {!! $error !!}
                                    </div>
                                @endforeach
                            @endif

                            <div class="row">
                                <div class="col-12 col-md-6 mb-3">
                                    <label for="title" class="form-label text-bold">
                                        {{ __('Título do Menu') }} <span class="span-admin-create">*</span>
                                    </label>
                                    <input type="text"
                                        class="form-control @error('title') is-invalid @enderror"
                                        name="title"
                                        id="title"
                                        autocomplete="off"
                                        value="{{ old('title', $menu->title) }}">
                                    @error('title')
                                        <small class="text-danger text-bold">{{ $message }}</small>
                                    @enderror
                                </div>

                                <div class="col-12 col-md-6 mb-3">
                                    <label for="status" class="form-label text-bold">{{ __('* Status') }}</label>
                                    <select class="form-control @error('status') is-invalid @enderror" name="status" id="status">
                                        @foreach(App\Enums\StatusEnum::cases() as $statusOption)
                                            <option value="{{ $statusOption->name }}" {{ old('status', $menu->status) === $statusOption->name ? 'selected' : '' }}>
                                                {{ $statusOption->value }}
                                            </option>
                                        @endforeach
                                    </select>
                                    @error('status')
                                        <small class="text-danger text-bold">{{ $message }}</small>
                                    @enderror
                                </div>
                            </div>

                            <div class="mb-3">
                                <label for="summary" class="form-label text-bold">{{ __('Descrição sumária') }}</label>
                                <textarea class="form-control @error('summary') is-invalid @enderror" name="summary" id="summary">{{ old('summary', $menu->summary) }}</textarea>
                                @error('summary')
                                    <small class="text-danger text-bold">{{ $message }}</small>
                                @enderror
                            </div>

                            <div class="d-flex gap-2 mb-4">
                                <a href="{{ route('admin.menusReutilizaveis.index', $filterParams ?? []) }}" class="btn btn-secondary">
                                    {{ __('Voltar') }}
                                </a>
                                <button class="btn btn-dark" type="submit">{{ __('Salvar Menu') }}</button>
                            </div>
                        </form>

                        <hr />

                        <div class="row">
                            <div class="col-12">
                                <h5 class="mb-3">{{ __('Links do Menu') }}</h5>

                                <div class="card mb-4">
                                    <div class="card-body">
                                        <h6 class="mb-3">{{ __('Novo link') }}</h6>
                                        <form action="{{ route('admin.menusReutilizaveis.links.store', $menu->id) }}" method="post">
                                            @csrf
                                            @if(!empty($filterParams))
                                                @foreach($filterParams as $key => $value)
                                                    @if($value !== null && $value !== '')
                                                        <input type="hidden" name="{{ $key }}" value="{{ $value }}">
                                                    @endif
                                                @endforeach
                                            @endif
                                            <div class="row">
                                                <div class="col-12 col-md-4 mb-3">
                                                    <label for="titulo" class="form-label text-bold">{{ __('Título') }}</label>
                                                    <input type="text" name="titulo" id="titulo"
                                                        class="form-control @error('titulo') is-invalid @enderror"
                                                        value="{{ old('titulo') }}">
                                                    @error('titulo')
                                                        <small class="text-danger text-bold">{{ $message }}</small>
                                                    @enderror
                                                </div>

                                                <div class="col-12 col-md-4 mb-3">
                                                    <label for="link" class="form-label text-bold">{{ __('Link') }}</label>
                                                    <input type="text" name="link" id="link"
                                                        class="form-control @error('link') is-invalid @enderror"
                                                        value="{{ old('link') }}">
                                                    @error('link')
                                                        <small class="text-danger text-bold">{{ $message }}</small>
                                                    @enderror
                                                </div>

                                                <div class="col-12 col-md-4 mb-3">
                                                    <label for="order" class="form-label text-bold">{{ __('Ordem de exibição') }}</label>
                                                    <input type="number" name="order" id="order"
                                                        class="form-control @error('order') is-invalid @enderror"
                                                        value="{{ old('order', 1) }}">
                                                    @error('order')
                                                        <small class="text-danger text-bold">{{ $message }}</small>
                                                    @enderror
                                                </div>
                                            </div>

                                            <div class="mb-3">
                                                <label for="description" class="form-label text-bold">{{ __('Descrição sumária do link') }}</label>
                                                <textarea name="description" id="description"
                                                    class="form-control @error('description') is-invalid @enderror">{{ old('description') }}</textarea>
                                                @error('description')
                                                    <small class="text-danger text-bold">{{ $message }}</small>
                                                @enderror
                                            </div>

                                            <div class="mb-3">
                                                <label for="link_status" class="form-label text-bold">{{ __('* Tipo de link') }}</label>
                                                <select class="form-control @error('link_status') is-invalid @enderror" name="link_status" id="link_status">
                                                    @foreach(App\Enums\StatusLinkEnum::cases() as $statusOption)
                                                        <option value="{{ $statusOption->name }}" {{ old('link_status') === $statusOption->name ? 'selected' : '' }}>
                                                            {{ $statusOption->value }}
                                                        </option>
                                                    @endforeach
                                                </select>
                                                @error('link_status')
                                                    <small class="text-danger text-bold">{{ $message }}</small>
                                                @enderror
                                            </div>

                                            <div class="d-flex gap-2 mt-3">
                                                <button type="submit" class="btn btn-primary">{{ __('Salvar link') }}</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>

                                <div class="table-responsive">
                                    <table class="table table-bordered table-hover table-striped align-middle text-center">
                                        @include('components.tables._thead', ['thead' => [__('Título'), __('Tipo de Link'), __('Descrição'), __('Ordem'), __('Ações')]])
                                        <tbody>
                                            @forelse($menu->menuLinks as $link)
                                                <tr>
                                                    <td>{{ $link->titulo }}</td>
                                                    <td>
                                                        @if($link->status === "_SELF")
                                                            {{ __("Link interno") }}
                                                        @else
                                                            {{ __("Link externo") }}
                                                        @endif
                                                    </td>
                                                    <td>{{ $link->description }}</td>
                                                    <td>{{ $link->order }}</td>
                                                    <td>
                                                        <button type="button" class="btn btn-warning px-3 mb-2" data-bs-toggle="modal" data-bs-target="#modal-{{ $link->id }}">
                                                            {{ __('Editar') }}
                                                        </button>

                                                        <form action="{{ route('admin.menusReutilizaveis.links.destroy', [$menu->id, $link->id]) }}" method="post" class="d-inline">
                                                            @csrf
                                                            @if(!empty($filterParams))
                                                                @foreach($filterParams as $key => $value)
                                                                    @if($value !== null && $value !== '')
                                                                        <input type="hidden" name="{{ $key }}" value="{{ $value }}">
                                                                    @endif
                                                                @endforeach
                                                            @endif
                                                            @method('delete')
                                                            <button type="submit" class="btn btn-danger mb-2"
                                                                onclick="return confirm('{{ __('Tem certeza que deseja excluir este link?') }}')">
                                                                {{ __('Excluir') }}
                                                            </button>
                                                        </form>
                                                    </td>
                                                </tr>
                                            @empty
                                                <tr>
                                                    <td colspan="5">
                                                        <strong>{{ __('* Nenhum link cadastrado no menu reutilizável.') }}</strong>
                                                    </td>
                                                </tr>
                                            @endforelse
                                        </tbody>
                                    </table>
                                </div>

                            </div>
                        </div>

                        @foreach($menu->menuLinks as $link)
                            <div class="modal fade" id="modal-{{ $link->id }}" tabindex="-1" aria-labelledby="modalLabel-{{ $link->id }}" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <form action="{{ route('admin.menusReutilizaveis.links.update', [$menu->id, $link->id]) }}" method="post">
                                            @csrf
                                            @if(!empty($filterParams))
                                                @foreach($filterParams as $key => $value)
                                                    @if($value !== null && $value !== '')
                                                        <input type="hidden" name="{{ $key }}" value="{{ $value }}">
                                                    @endif
                                                @endforeach
                                            @endif
                                            <div class="modal-header">
                                                <h1 class="modal-title fs-5" id="modalLabel-{{ $link->id }}">{{ __('Editar Link') }}</h1>
                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{{ __('Fechar') }}"></button>
                                            </div>

                                            <div class="modal-body">
                                                <div class="form-group mb-3">
                                                    <label for="titulo-{{ $link->id }}" class="form-label">{{ __('Título') }}</label>
                                                    <input type="text" name="titulo" id="titulo-{{ $link->id }}"
                                                        class="form-control @error('titulo') is-invalid @enderror"
                                                        value="{{ old('titulo', $link->titulo) }}">
                                                    @error('titulo')
                                                        <small class="text-danger text-bold">{{ $message }}</small>
                                                    @enderror
                                                </div>

                                                <div class="form-group mb-3">
                                                    <label for="link-{{ $link->id }}" class="form-label">{{ __('Link') }}</label>
                                                    <input type="text" name="link" id="link-{{ $link->id }}"
                                                        class="form-control @error('link') is-invalid @enderror"
                                                        value="{{ old('link', $link->link) }}">
                                                    @error('link')
                                                        <small class="text-danger text-bold">{{ $message }}</small>
                                                    @enderror
                                                </div>

                                                <div class="form-group mb-3">
                                                    <label for="description-{{ $link->id }}" class="form-label">{{ __('Descrição sumária do link') }}</label>
                                                    <textarea name="description" id="description-{{ $link->id }}"
                                                        class="form-control @error('description') is-invalid @enderror">{{ old('description', $link->description) }}</textarea>
                                                    @error('description')
                                                        <small class="text-danger text-bold">{{ $message }}</small>
                                                    @enderror
                                                </div>

                                                <div class="form-group mb-3">
                                                    <label for="order-{{ $link->id }}" class="form-label">{{ __('Ordem de exibição') }}</label>
                                                    <input type="number" name="order" id="order-{{ $link->id }}"
                                                        class="form-control @error('order') is-invalid @enderror"
                                                        value="{{ old('order', $link->order) }}">
                                                    @error('order')
                                                        <small class="text-danger text-bold">{{ $message }}</small>
                                                    @enderror
                                                </div>

                                                <div class="mb-3">
                                                    <label for="link_status-{{ $link->id }}" class="form-label">{{ __('* Tipo de link') }}</label>
                                                    <select class="form-control @error('link_status') is-invalid @enderror" name="link_status" id="link_status-{{ $link->id }}">
                                                        @foreach(App\Enums\StatusLinkEnum::cases() as $statusOption)
                                                            @php
                                                                $selectedStatus = old('link_status', $link->status) === $statusOption->name;
                                                            @endphp
                                                            <option value="{{ $statusOption->name }}" {{ $selectedStatus ? 'selected' : '' }}>
                                                                {{ $statusOption->value }}
                                                            </option>
                                                        @endforeach
                                                    </select>
                                                    @error('link_status')
                                                        <small class="text-danger text-bold">{{ $message }}</small>
                                                    @enderror
                                                </div>
                                            </div>

                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ __('Fechar') }}</button>
                                                <button type="submit" class="btn btn-primary">{{ __('Atualizar') }}</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        @endforeach

                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

