@extends('layouts.idara.panel')


@section('content')

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js" integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js" integrity="sha512-F636MAkMAhtTplahL9F6KmTfxTmYcAcjcCkyu0f0voT3N/6vzAuJ4Num55a0gEJ+hRLHhdz3vDvZpf6kqgEa5w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" integrity="sha512-hievggED+/IcfxhYRSr4Auo1jbiOczpqpLZwfTVL/6hFACdbI3WQ8S9NCX50gsM9QVE+zLk/8wb9TlgriFbX+Q==" crossorigin="anonymous" referrerpolicy="no-referrer" />
@include('backend.partials.import')




<div class="container">
    <section style="margin-bottom: 44px;">
        <div class="card">
            <div class="card-header">
               Présentation
            </div>
            <div class="card-body">


                @forelse ($articles as $article)

                <!-- Button trigger modal -->
                <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
                    + Ajouter
                </button>

                <!-- Modal -->
                <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content" style="background:#ff6767">

                            <div class="modal-body" style="color:white">
                                <strong>Présentation déjà existante</strong>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">fermer</button>

                            </div>
                        </div>
                    </div>
                </div>

                @empty
                <a href="{{route('presentation.add')}}" class="btn btn-primary">+ Ajouter</a>

                @endforelse
                <a href="javascript:window.location.href=window.location.href" class="btn btn-secondary"><i class="fa-solid fa-arrows-rotate"></i>Actualiser</a>




            </div>
        </div>
    </section>

    @if(session('success'))
    <div class="alert alert-success">
        {{ session('success') }}
    </div>
    @endif
    @if (count($errors) > 0)
    <div class="alert alert-danger">
        <strong>Whoops!</strong> Some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
    @endif
    <section>



        <div class="card">
            <div class="card-header">
                Présentation
            </div>
            <div class="card-body">


                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th scope="col">Titre</th>
                                <th scope="col">Date</th>

                                <th scope="col">Action</th>

                            </tr>
                        </thead>
                        <tbody>
                        @foreach($articles as $article)

                            <tr>
                                <th scope="row">
                      <a class="title" href="{{route('presentation.show', ['id' => $article])}}" >{{$article->titre}}</a>

                                </th>
                                <th scope="row">

                                    {{$article->created_at}}

                                </th>

                                <th scope="row" class="buttonOffOut">

                                    <div>
                                        <form action="{{url('presentations/'.$article->id)}}" method="post">

                                            @csrf

                                            @if($article->etat == 1)
                                                <Tooltip title="Masquer">

                                                    <input data-id="{{$article->id}}" class="toggle-class" type="checkbox"  data-onstyle="success"  data-offstyle="danger" data-toggle="toggle" data-on="✔" data-off="⊘" {{ $article->etat ? 'checked' : '' }}>


                                                </Tooltip>

                                                @else
                                                    <Tooltip title="Afficher">

                                                        <input data-id="{{$article->id}}" class="toggle-class" type="checkbox"  data-onstyle="success"  data-offstyle="danger" data-toggle="toggle" data-on="✔" data-off="⊘" {{ $article->etat ? 'checked' : '' }}>
    
    
                                                    </Tooltip>  
                                                
                                                @endif

                                            <Tooltip title="Voir Plus"> <a href="{{route('presentation.show', ['id' => $article])}}" class="btn btn-xs btn-primary"><i class="fa fa-sign-out" aria-hidden="true"></i></a></Tooltip>



                                            <input name="_method" type="hidden" value="DELETE">
                                            <Tooltip title="Supprimer"> <button type="submit" class="btn btn-xs btn-danger btn-flat show_confirm" data-toggle="tooltip" ref="{{ url('presentations/'.$article->id) }}"><i class="fa-solid fa-trash" aria-hidden="true"></i></button></Tooltip>


                                        </form>



                                    </div>
                                </th>
                            </tr>

                            @endforeach

                        </tbody>
                    </table>
                    <div>

                        
                    </div>
                </div>
    </section>

</div>


@include('backend.partials.alert')


<script>

    function myFunction() {
        document.getElementById("demo").innerHTML = "Hello World";
    }

    var myModal = document.getElementById('myModal')
    var myInput = document.getElementById('myInput')

    myModal.addEventListener('shown.bs.modal', function() {
        myInput.focus()
    })
    $('form input').on('change', function() {
        $(this).closest('form').submit();
    });

</script>
<script>
    $(function() {
        $('.toggle-class').change(function() {
            var etat = $(this).prop('checked') == true ? 1 : 0;
            var Id = $(this).data('id');

            $.ajax({
                type: "GET"
                , dataType: "json"
                , url: '/changeStatusPresentation'
                , data: {
                    'etat': etat
                    , 'Id': Id
                }
                , success: function(data) {
                    console.log(data.success)
                }
            });
        })
    })

</script>
@endsection
