@extends('layouts.admin.theme')
@section('title', env('APP_NAME') . ' :: Gerenciar Assuntos e Subassuntos')

@section('content')
    <div class="card shadow-sm">

        @include('components.buttons._headLineButtonsBackAdd', [
            'headLine' => "<i class='fa-solid fa-sliders'></i> " . __($headLine),
            'routeBack' => 'admin.indicadoresTi.index',
            'routeBackParams' => [],
            'nameBack' => __('Voltar'),
            'permission' => 'INDICADORES_TI',
        ])

        <div class="card-body">

            @if (session('success'))
                <div class="alert alert-success alert-dismissible fade show mt-3" role="alert">
                    {{ session('success') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            @endif

            @if ($errors->any())
                <div class="alert alert-danger alert-dismissible fade show mt-3" role="alert">
                    <ul class="mb-0">
                        @foreach ($errors->all() as $erro)
                            <li>{{ $erro }}</li>
                        @endforeach
                    </ul>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            @endif

            <div class="card shadow-sm mb-4 mt-4">
                <div class="card-header bg-success text-white fw-bold">
                    <i class="fa-solid fa-plus me-2"></i>{{ __('Novo Setor') }}
                </div>
                <div class="card-body">
                    <form action="{{ route('admin.indicadoresTi.gerenciar.setores.store') }}" method="POST"
                        class="d-flex gap-2 align-items-start">
                        @csrf
                        <div class="flex-grow-1">
                            <input type="text" name="nome" class="form-control"
                                placeholder="{{ __('Nome do setor...') }}" value="{{ old('nome') }}" required>
                            @error('nome_setor')
                                <small class="text-danger">{{ $message }}</small>
                            @enderror
                        </div>
                        <button class="btn btn-success text-nowrap" type="submit">
                            <i class="fa-solid fa-plus me-1"></i>{{ __('Adicionar Setor') }}
                        </button>
                    </form>
                </div>
            </div>

            <div class="mt-2">
                @forelse ($setores as $setor)
                    <div class="card shadow-sm mb-3">
                        <div
                            class="card-header bg-primary text-white d-flex justify-content-between align-items-center gap-2">
                            <span class="fw-bold flex-grow-1">
                                <i class="fa-solid fa-layer-group me-2"></i>{{ $setor->nome }}
                            </span>

                            <button class="btn btn-sm btn-outline-light" type="button" data-bs-toggle="collapse"
                                data-bs-target="#editar-setor-{{ $setor->id }}" title="{{ __('Editar setor') }}">
                                <i class="fa-solid fa-pen"></i>
                            </button>

                            <form action="{{ route('admin.indicadoresTi.gerenciar.setores.destroy', $setor) }}"
                                method="POST" class="d-inline"
                                onsubmit="return confirm('{{ __('Excluir o setor') }} \'{{ addslashes($setor->nome) }}\'? {{ __('Apenas setores sem assuntos podem ser excluídos.') }}')">
                                @csrf @method('DELETE')
                                <button class="btn btn-sm btn-outline-light" type="submit"
                                    title="{{ __('Excluir setor') }}">
                                    <i class="fa-solid fa-trash"></i>
                                </button>
                            </form>

                            <button class="btn btn-sm btn-outline-light js-gerenciar-setor-toggle" type="button"
                                data-target-id="setor-{{ $setor->id }}" title="{{ __('Expandir / Recolher') }}">
                                <i class="fa-solid fa-chevron-down" style="transition: transform 0.3s"></i>
                            </button>
                        </div>

                        <div class="collapse px-3 py-2 bg-white border-bottom" id="editar-setor-{{ $setor->id }}">
                            <form action="{{ route('admin.indicadoresTi.gerenciar.setores.update', $setor) }}"
                                method="POST" class="d-flex gap-2 align-items-center">
                                @csrf @method('PUT')
                                <input type="text" name="nome" class="form-control form-control-sm"
                                    value="{{ old('nome', $setor->nome) }}" required>
                                <button class="btn btn-sm btn-primary text-nowrap" type="submit">
                                    <i class="fa-solid fa-floppy-disk me-1"></i>{{ __('Salvar') }}
                                </button>
                            </form>
                            @error('nome_setor_' . $setor->id)
                                <small class="text-danger">{{ $message }}</small>
                            @enderror
                        </div>

                        <div id="setor-{{ $setor->id }}" style="display:none">
                            <div class="card-body p-0">

                                @forelse ($setor->assuntos as $assunto)
                                    <div class="border-bottom">
                                        <div class="d-flex align-items-center gap-2 px-3 py-2 bg-light">
                                            <i class="fa-solid fa-tag text-secondary"></i>
                                            <strong class="flex-grow-1">{{ $assunto->nome }}</strong>
                                            <span class="badge bg-secondary">{{ $assunto->subassuntos_count }}
                                                {{ __('subassunto(s)') }}</span>

                                            <button class="btn btn-sm btn-outline-primary" type="button"
                                                data-bs-toggle="collapse"
                                                data-bs-target="#editar-assunto-{{ $assunto->id }}">
                                                <i class="fa-solid fa-pen"></i>
                                            </button>

                                            <form
                                                action="{{ route('admin.indicadoresTi.gerenciar.assuntos.destroy', $assunto) }}"
                                                method="POST"
                                                onsubmit="return confirm('{{ __('Excluir o assunto') }} \'{{ addslashes($assunto->nome) }}\'? {{ __('Todos os subassuntos serão removidos.') }}')">
                                                @csrf @method('DELETE')
                                                <button class="btn btn-sm btn-outline-danger" type="submit"
                                                    title="{{ __('Excluir assunto') }}">
                                                    <i class="fa-solid fa-trash"></i>
                                                </button>
                                            </form>

                                            <button class="btn btn-sm btn-outline-success" type="button"
                                                data-bs-toggle="collapse"
                                                data-bs-target="#add-subassunto-{{ $assunto->id }}">
                                                <i class="fa-solid fa-plus"></i> {{ __('Subassunto') }}
                                            </button>
                                        </div>

                                        <div class="collapse px-3 py-2 bg-white border-top"
                                            id="editar-assunto-{{ $assunto->id }}">
                                            <form
                                                action="{{ route('admin.indicadoresTi.gerenciar.assuntos.update', $assunto) }}"
                                                method="POST" class="d-flex gap-2 align-items-center">
                                                @csrf @method('PUT')
                                                <input type="text" name="nome" class="form-control form-control-sm"
                                                    value="{{ old('nome', $assunto->nome) }}" required>
                                                <button class="btn btn-sm btn-primary text-nowrap" type="submit">
                                                    <i class="fa-solid fa-floppy-disk me-1"></i>{{ __('Salvar') }}
                                                </button>
                                            </form>
                                            @error('nome_assunto_' . $assunto->id)
                                                <small class="text-danger">{{ $message }}</small>
                                            @enderror
                                        </div>

                                        <div class="collapse px-3 py-2 bg-white border-top"
                                            id="add-subassunto-{{ $assunto->id }}">
                                            <form action="{{ route('admin.indicadoresTi.gerenciar.subassuntos.store') }}"
                                                method="POST" class="d-flex gap-2 align-items-center">
                                                @csrf
                                                <input type="hidden" name="assunto_id" value="{{ $assunto->id }}">
                                                <input type="text" name="nome" class="form-control form-control-sm"
                                                    placeholder="{{ __('Nome do subassunto...') }}" required>
                                                <button class="btn btn-sm btn-success text-nowrap" type="submit">
                                                    <i class="fa-solid fa-plus me-1"></i>{{ __('Adicionar') }}
                                                </button>
                                            </form>
                                            @error('nome_subassunto')
                                                <small class="text-danger">{{ $message }}</small>
                                            @enderror
                                        </div>

                                        @if ($assunto->subassuntos->isNotEmpty())
                                            @foreach ($assunto->subassuntos as $subassunto)
                                                <div class="d-flex align-items-center gap-2 px-4 py-1 border-top bg-white">
                                                    <i class="fa-solid fa-circle-dot text-muted"
                                                        style="font-size:.7rem"></i>
                                                    <span
                                                        class="flex-grow-1 text-secondary">{{ $subassunto->nome }}</span>

                                                    <button class="btn btn-sm btn-outline-primary py-0 px-1"
                                                        type="button" data-bs-toggle="collapse"
                                                        data-bs-target="#editar-sub-{{ $subassunto->id }}">
                                                        <i class="fa-solid fa-pen" style="font-size:.75rem"></i>
                                                    </button>

                                                    <form
                                                        action="{{ route('admin.indicadoresTi.gerenciar.subassuntos.destroy', $subassunto) }}"
                                                        method="POST"
                                                        onsubmit="return confirm('{{ __('Excluir o subassunto') }} \'{{ addslashes($subassunto->nome) }}\'?')">
                                                        @csrf @method('DELETE')
                                                        <button class="btn btn-sm btn-outline-danger py-0 px-1"
                                                            type="submit" title="{{ __('Excluir') }}">
                                                            <i class="fa-solid fa-trash" style="font-size:.75rem"></i>
                                                        </button>
                                                    </form>
                                                </div>

                                                <div class="collapse px-4 py-2 bg-white border-top"
                                                    id="editar-sub-{{ $subassunto->id }}">
                                                    <form
                                                        action="{{ route('admin.indicadoresTi.gerenciar.subassuntos.update', $subassunto) }}"
                                                        method="POST" class="d-flex gap-2 align-items-center">
                                                        @csrf @method('PUT')
                                                        <input type="text" name="nome"
                                                            class="form-control form-control-sm"
                                                            value="{{ old('nome', $subassunto->nome) }}" required>
                                                        <button class="btn btn-sm btn-primary text-nowrap" type="submit">
                                                            <i
                                                                class="fa-solid fa-floppy-disk me-1"></i>{{ __('Salvar') }}
                                                        </button>
                                                    </form>
                                                    @error('nome_subassunto_' . $subassunto->id)
                                                        <small class="text-danger">{{ $message }}</small>
                                                    @enderror
                                                </div>
                                            @endforeach
                                        @endif
                                    </div>
                                @empty
                                    <div class="text-muted small px-3 py-2">
                                        {{ __('Nenhum assunto cadastrado neste setor.') }}
                                    </div>
                                @endforelse

                                <div class="p-3 border-top bg-light">
                                    <form action="{{ route('admin.indicadoresTi.gerenciar.assuntos.store') }}"
                                        method="POST" class="d-flex gap-2 align-items-center">
                                        @csrf
                                        <input type="hidden" name="setor_id" value="{{ $setor->id }}">
                                        <input type="text" name="nome" class="form-control form-control-sm"
                                            placeholder="{{ __('Novo assunto para') }} {{ $setor->nome }}..." required>
                                        <button class="btn btn-sm btn-primary text-nowrap" type="submit">
                                            <i class="fa-solid fa-plus me-1"></i>{{ __('Adicionar Assunto') }}
                                        </button>
                                    </form>
                                    @error('nome_assunto')
                                        <small class="text-danger">{{ $message }}</small>
                                    @enderror
                                </div>

                            </div>
                        </div>
                    </div>
                @empty
                    <div class="alert alert-warning">{{ __('Nenhum setor cadastrado.') }}</div>
                @endforelse
            </div>

        </div>
    </div>
@endsection
