@extends('layouts.admin.theme')
@section("title", env("APP_NAME") . " :: Lista de Telefones")
@section('content')

    <section>
        <div class="card shadow">
            <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
                <h5 class="my-2">Lista de Telefones da <span class="text-danger">{{ $secretaria->name }}</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 Telefone</h3>
                            <div class="card-body">
                                <form action="{{ route("admin.telefones.index", $secretaria->id) }}" method="post">
                                    @csrf
                                    @method('post')
                                    <div class="form-group">
                                        <label for="titulo">Título</label>
                                        <input type="hidden" name="structure_id" value="{{ old('id', $secretaria->id) }}">
                                        <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="description">Telefone</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><br />
                                        @enderror
                                        <small>(*) Separe os telefones por vírgula.</small>
                                    </div>
                                    <div class="d-flex gap-2 mt-3">
                                        <button type="button" class="btn btn-secondary"
                                            onclick="window.location='{{ route('admin.secretarias.index') }}'">
                                            Voltar
                                        </button>
                                        <button type="submit" class="btn btn-primary">
                                            Salvar
                                        </button>
                                    </div>

                                </form>
                            </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', 'Telefones', 'Ações']])
                            
                            </thead>
                            <tbody>
                                @foreach($telefones as $telefone)
                                    <tr>
                                        <td>{{ $telefone->titulo }}</td>
                                        <td>{{ $telefone->description }}</td>
                                        <td>
                                            <!-- Button trigger modal -->
                                            <button type="button" class="btn btn-warning" data-bs-toggle="modal"
                                                data-bs-target="#{{ $telefone->id }}">
                                                Editar
                                            </button>

                                            <!-- Modal -->
                                            <div class="modal fade" id="{{ $telefone->id }}" tabindex="-1"
                                                aria-labelledby="exampleModalLabel" aria-hidden="true">
                                                <div class="modal-dialog">
                                                    <div class="modal-content">
                                                        <form
                                                            action="{{ route("admin.telefones.update", [$secretaria->id, $telefone->id]) }}"
                                                            method="post">
                                                            <div class="modal-header">
                                                                <h1 class="modal-title fs-5" id="exampleModalLabel">Editar
                                                                    Telefone
                                                                </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">Título</label>
                                                                    <input type="hidden" name="structure_id"
                                                                        value="{{ old('id', $secretaria->id) }}">
                                                                    <input type="text" name="titulo" id="titulo"
                                                                        class="form-control @error('titulo') is-invalid @enderror"
                                                                        value="{{ old("titulo", $telefone->titulo) }}">
                                                                    @error('titulo')
                                                                        <small class="text-danger text-bold">{{ $message }}</small>
                                                                    @enderror
                                                                </div>
                                                                <div class="form-group">
                                                                    <label for="description">Telefone</label>
                                                                    <textarea name="description" id="description"
                                                                        class="form-control @error('description') is-invalid @enderror">{{ old("description", $telefone->description) }}</textarea>
                                                                    @error('description')
                                                                        <small
                                                                            class="text-danger text-bold">{{ $message }}</small><br />
                                                                    @enderror
                                                                    <small>(*) Separe os telefones por vírgula.</small>
                                                                </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>
                                            <form
                                                action="{{ route('admin.telefones.destroy', [$secretaria->id, $telefone->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 telefone?')">Excluir</button>
                                            </form>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection