<div class="table-primary">
    <table class="table table-striped table-bordered" id="subscribers-table" width="100%">
        <thead>
        <tr>
            <th>{{ trans('admin.fields.id') }}</th>
            <th>{{ trans('admin.fields.user') }}</th>
            <th>{{ trans('admin.capsule.created_at') }}</th>
            @if($managerPermissions->contains('value', 'users.full'))<th></th>@endif
        </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</div>

@section('scripts')
    @parent

    <script>
        var subscribers_table = $('#subscribers-table').DataTable({
            "processing": true,
            "serverSide": true,
            "pageLength": 10,
            "lengthMenu": [[15, 30], [15, 30]],
            "order": [[ 0, "desc" ]],
            "ajax": {
                "url": "{{ route('admin.capsule.capsule_subscriber.list', ['id' => $capsule->id]) }}"
            },
            "columns": [
                {data: 'id', name: 'id'},
                {data: 'user.nickname', name: 'user.nickname'},
                {data: 'created_at', name: 'created_at'},
                @if($managerPermissions->contains('value', 'users.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-subscriber' href='" + row.deleteLink + "'>{{ __('admin.buttons.delete')}}</a>";
                    },
                    orderable: false,
                    searchable: false
                }
                @endif
            ]
        });

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

            if (confirm('{{ __('admin.capsule.subscriber.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') {
                        subscribers_table.draw(false);
                    }
                });
            }
        });
        @endif
    </script>
@endsection