<table id="datatable-responsive"
       class="table table-striped table-bordered  dt-responsive nowrap" cellspacing="0"
       width="100%">
    <thead>
    <tr>
        @foreach($titles as $title)
            <?php $key = array_search($title,$model->getFields());?>
            @if(!in_array($key,$model->getColumnsHiddens()))
                <th @if(in_array($key,$model->getImages())) style="width: 12%" @endif  @if(in_array($key,$model->getLongTexts()) || in_array($key,$model->getRichTexts())) style="width: 35%" @endif>{{$title}}</th>
            @endif
        @endforeach
        @if(count($model->getActions())!=0 || count($model->getCustomBouttons())!=0)
            <th>Actions</th>
        @endif
    </tr>
    </thead>
    <?php
    $customs_columns =$model->getCustomColumns();
    ?>
    <tbody>
    @foreach($elements as $element)
        <tr>
            @include('inc.cruds.inc.table_columns')

            @if(count($model->getActions())!=0 || count($model->getCustomBouttons())!=0)
                <td class="actions" align="center">
                    @if(in_array('edit',$model->getActions()))
                        <a  href="{{url(Request::path().'/edit/'.$element->id)}}" class="btn btn-xs waves-effect waves-light btn-default m-b-5">
                            Modifier
                        </a>
                    @endif

                    @if(in_array('delete',$model->getActions()))
                        <a href="{{url(Request::path().'/delete/'.$element->id)}}" resource="{{$model->getTable()}}"
                           class="btn btn-xs waves-effect waves-light btn-danger supprimeur m-b-5">Supprimer
                        </a>
                    @endif
                    @foreach($model->getCustomBouttons() as $b)
                        {!! $element->$b() !!}
                    @endforeach
                </td>
            @endif
        </tr>
    @endforeach
    </tbody>
</table>


@section('my-scripts')
    <script>

        $(document).ready(function () {
            $('#btn_nouveau').on('click',function () {
                $('.modal-title').html('Enregsitrement : '+ '{{$model->getTitle()}}');

                $('#_id').val(0);

                @foreach($columns as $id)

                @if(!in_array($id,$model->getFiles()))
                @if(in_array($id,$model->getLongTexts()))
                $('#{{$id}}').html('');
                @elseif(in_array($id,array_keys($model->getBelongsTo())))
                $('#{{$id}}_id').val('');
                @else
                $('#{{$id}}').val('');
                @endif
                @endif

                @endforeach

                $('#other_model').html($('#other_model_temp').html());
                $('#operner-modal').click();
            });

            function gabi() {
                $('.sorting_1').on('mouseleave',function () {
                    chercheur.keyup();
                    $('.supprimeur').on('mouseenter',function () {
                        chercheur.keyup();
                    });
                });
            }

            $('#datatable-responsive')
                .on( 'order.dt',  function () { gabi() } )
                .on( 'search.dt', function () { gabi() } )
                .on( 'page.dt',   function () { gabi() } )
                .DataTable({
                    "dom": 'C<"clear">lfrtip',
                    "pageLength": 25,
                    "colVis": {
                        "buttonText": "Colonnes affichées"
                    }
                });


            var chercheur = $('.chercheur');
            var gabi_n =0;
            chercheur.on('keyup',function () {

                $('.modifieur').click(function (e) {
                    e.preventDefault();
                });
            });

            chercheur.on('change',function () {
                chercheur.keyup();
            });

            $('.sorting_1').on('mouseleave',function () {
                chercheur.keyup();
                $('.supprimeur').on('mouseenter',function () {
                    chercheur.keyup();
                });
            });


            chercheur.keyup();

            $(document).on('click','.supprimeur',function (e) {
                e.preventDefault();
                var href_supprimeur = $(this).attr('href');
                swal({
                    title: "Confirmation!",
                    text: "Voulez vous effectuer la suppression?",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonClass: 'btn-warning',
                    confirmButtonText: "OUI, Continuer",
                    cancelButtonText: "NON",
                    closeOnConfirm: false
                }, function () {
                    var req = $.ajax({
                        url: href_supprimeur,
                        type: "GET",
                        data: {param: ''},
                        dataType: "html"
                    });
                    req.done(function (response, textStatus, jqXHR) {
                        swal("Succès!", "Suppression effectuée avec succès.", "success");
                        $('.confirm').on('click',function () {
                            window.location = '{{url(Request::path())}}';
                        })
                    });
                    req.fail(function (jqXHR, textStatus) {
                        swal("Erreur!", "Erreur lors de l'opération.", "warning");
                    });
                });
            });

        });

        function modifier(href_modifieur) {
            var req = $.ajax({
                url: href_modifieur,
                type: "GET",
                data: {param: ''},
                dataType: "JSON"
            });

            req.done(function (response, textStatus, jqXHR) {
                if(response!=null){
                    $('.modal-title').html('Modification : '+ '{{$model->getTitle()}}');
                    $('#_id').val(response.model.id);

                    @foreach($columns as $id)
                    @if(!in_array($id,$model->getFiles()))

                    @if(in_array($id,$model->getLongTexts()))
                    $('#{{$id}}').html(response.model.{{$id}});
                    @elseif(in_array($id,array_keys($model->getBelongsTo())))
                    $('#{{$id}}_id').val(response.model.{{$id}}_id);
                    @elseif(in_array($id,array_keys($model->getDates())))
                    $('#{{$id}}').val(formaDate(response.model.{{$id}},'/'));
                    @elseif(in_array($id,$model->getPasswords()))
                    $('#{{$id}}_id').val('');
                    @else
                    $('#{{$id}}').val(response.model.{{$id}});
                    @endif
                    @endif
                    @endforeach
                    $('#other_model').html('');
                    $('#operner-modal').click();

                }else{
                    swal("Erreur!", "Erreur lors de l'opération.", "warning");
                }
            });
            req.fail(function (jqXHR, textStatus) {
                swal("Erreur!", "Erreur lors de l'opération.", "warning");
            });
        }
    </script>
@endsection