<div class="row">
    <div class="col-md-12">

        <div class="panel">
            <div class="table-primary">
                <table class="table table-striped table-bordered" id="capsule-table" width="100%">
                    <thead>
                    <tr>
                        <th>{{ trans('admin.fields.id') }}</th>
                        <th>{{ trans('admin.fields.uuid') }}</th>
                        <th>{{ trans('admin.fields.caption') }}</th>
                        <th>{{ trans('admin.fields.opens') }}</th>
                        <th>{{ trans('admin.fields.created') }}</th>
                        @if($managerPermissions->contains('value', 'capsules.full'))<th></th>@endif
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

@section('scripts')
    @parent

    <script>
        var capsule_table = $('#capsule-table').DataTable({
            "processing": true,
            "serverSide": true,
            "pageLength": 20,
            "lengthMenu": [[15, 30], [15, 30]],
            "order": [[ 0, "desc" ]],
            "ajax": {
                "url": "{{ route('admin.user.user_capsules.list', ['id' => $user->id]) }}"
            },
            "columns": [
                {data: 'id', name: 'id'},
                {data: 'uuid', name: 'uuid'},
                {data: 'caption', name: 'caption'},
                {data: 'open_at', name: 'open_at'},
                {data: 'created_at', name: 'created_at'},
                @if($managerPermissions->contains('value', 'capsules.full'))
                {
                    data: 'id',
                    name: 'id',
                    render: function (data, type, row) {
                        return "<a class='btn btn-xs btn-info' href='" + row.showLink + "'>{{ __('admin.buttons.show') }}</a>" +
                            "<a class='btn btn-xs btn-primary' href='" + row.editLink + "'>{{ __('admin.buttons.edit')}}</a>" +
                            "<a class='btn btn-xs btn-danger delete-capsule' href='" + row.deleteLink + "'>{{ __('admin.buttons.delete')}}</a>";
                    },
                    orderable: false,
                    searchable: false
                }
                @endif
            ]
        });

        @if ( $managerPermissions->contains('value', 'capsules.full'))
        $(document).on('click', '.delete-capsule', function (e) {
            e.preventDefault();

            if (confirm('{{ __('admin.capsule.ask.delete')}}')) {
                $.ajax({
                    url: $(this).attr('href'),
                    type: 'DELETE',
                    dataType: 'json',
                    data: { method: '_DELETE' }
                }).always(function (result) {
                    if (typeof result.status !== 'undefined' && result.status === 'success') {
                        capsule_table.draw(false);
                    }
                });
            }
        });
        @endif
    </script>
@endsection