@extends('layouts.admin.theme')
@section("title", env("APP_NAME") . " :: Menu Personalizado - " . $html->title)
@section('content')

    <section>
        <div class="card shadow-sm">
            <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
                <h5 class="mb-0">{{ __('Menu Personalizado') }} – <span class="text-warning">{{ $html->title }}</span></h5>
            </div>
            <div class="card-body">
                @if (session('success'))
                    <div class="alert alert-success mx-3">{{ session('success') }}</div>
                @endif
                @if (session('error'))
                    <div class="alert alert-danger mx-3">{{ session('error') }}</div>
                @endif
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <h3 class="m-3 p-3">{{ __('Novo link') }}</h3>
                            <div class="card-body">
                                <form action="{{ route("admin.htmls.menu.index", $html->id) }}" method="post">
                                    @csrf
                                    @method('post')
                                    <input type="hidden" name="html_id" value="{{ $html->id }}">
                                    <div class="form-group">
                                        <label for="titulo">{{ __('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="form-group">
                                        <label for="link">{{ __('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="form-group">
                                        <label for="description">{{ __("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="form-group">
                                        <label for="order">{{ __("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 class="mb-3">
                                        <label for="status" class="form-label text-bold">{{ __("* Tipo de link") }}</label>
                                        <select class="form-control @error('status') is-invalid @enderror" name="status"
                                            id="status">
                                            @foreach(App\Enums\StatusLinkEnum::cases() as $status)
                                                <option value="{{ $status->name }}">{{ $status->value }}</option>
                                            @endforeach
                                        </select>
                                        @error('status')
                                            <small class="text-danger text-bold">{{ $message }}</small>
                                        @enderror
                                    </div>
                                    <div class="d-flex gap-2 mt-3">
                                        <a href="{{ route('admin.htmls.index', $filterParams ?? []) }}" class="btn btn-secondary">
                                            {{ __('Voltar') }}
                                        </a>
                                        <button type="submit" class="btn btn-primary">{{ __('Salvar') }}</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <hr />
                <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($links 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.htmls.menu.destroy', [$html->id, $link->id]) }}"
                                            method="POST" style="display:inline;">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-danger"
                                                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 personalizado.') }}</strong>
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </section>

    @foreach($links 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.htmls.menu.update", [$html->id, $link->id]) }}" method="post">
                    <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="Close"></button>
                    </div>
                    <div class="modal-body">
                        @csrf
                        @method('post')
                        <div class="form-group">
                            <label for="titulo-{{ $link->id }}">{{ __('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">
                            <label for="link-{{ $link->id }}">{{ __('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">
                            <label for="description-{{ $link->id }}">{{ __("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">
                            <label for="order-{{ $link->id }}">{{ __("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="status-{{ $link->id }}" class="form-label text-bold">{{ __("* Tipo de link") }}</label>
                            <select class="form-control @error('status') is-invalid @enderror" name="status" id="status-{{ $link->id }}">
                                @foreach(App\Enums\StatusLinkEnum::cases() as $status)
                                    @if($link->status == $status->name)
                                        <option value="{{ $status->name }}" selected="">{{ $status->value }}</option>
                                    @else
                                        <option value="{{ $status->name }}">{{ $status->value }}</option>
                                    @endif
                                @endforeach
                            </select>
                            @error('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
@endsection
