const AppointmentSelectTable = {
    templateUrl : 'js/app/components/appointments/appointment-select-table.html',
    create : function( appointments )
    {
        var app = this.app,
            $container = $( this.container );

        this.render( { apps: appointments.getAll(), activeGroup : this.app.user.getActiveGroup() } );

        $container.find( '.datatable' ).DataTable( {
            pageLength: 50,
            responsive: true,
            dom: '<"html5buttons"B>lTfgitp',
            language : {
                search      : 'Suche',
                emptyTable  : 'Keine Einträge vorhanden.',
                zeroRecords : 'Keine Einträge vorhanden.',
                info        : 'Zeige _START_ bis _END_ von _TOTAL_ Einträgen',
                lengthMenu : 'Zeige _MENU_ Einträge',
                paginate : {
                    first : 'Erste',
                    previous : 'Vorherige',
                    next : 'Nächste',
                    last : 'Letzte'
                },
            },
            /*
            columnDefs : [
                {
                    width : 180, targets : 10
                }
            ],*/
            buttons: [
                { extend: 'copy'},
                {extend: 'csv'},
                {extend: 'excel', title: 'Termine'},
                {extend: 'pdf', title: 'Termine'},

                {extend: 'print',
                    customize: function (win){
                        $(win.document.body).addClass('white-bg');
                        $(win.document.body).css('font-size', '10px');

                        $(win.document.body).find('table')
                            .addClass('compact')
                            .css('font-size', 'inherit');
                    }
                }
            ]
        });

        $container.on( 'click', 'tr[data-type="appointment"]', function()
        {
            $( this ).toggleClass( 'tr-selected' );
            //self.dispatchEvent( { type : 'selectionChanged', data : { selectedIds : self.getSelectedIds() } } );
        });

        $container.on( 'click', '[data-type="btn-edit-appointment"]', function( e )
        {
            app.openInApp( '/#/appointment/edit/' + $( this ).attr( 'data-appointment-id' ) );
            e.stopPropagation();
        });

        $container.on( 'click', '[data-type="btn-detail-appointment"]', function( e )
        {
            app.openInApp( '/#/appointment/' + $( this ).attr( 'data-appointment-id' ) );
            e.stopPropagation();
        });

        $container.on( 'click', '[data-type="btn-attendees-appointment"]', function( e )
        {
            app.openInApp( '/#/appointment/' + $( this ).attr( 'data-appointment-id' ) + '/attendee' );
            e.stopPropagation();
        });

        $container.on( 'click', '[data-type="btn-chat-appointment"]', function( e )
        {
            app.openInApp( '/#/appointment/' + $( this ).attr( 'data-appointment-id' ) + '/chat' );
            e.stopPropagation();
        });
    },

    getSelectedIds : function()
    {
        let $container = $( this.container ),
            ids = [];

        $container.find( 'tr.tr-selected' ).each( function( i, v )
        {
            ids.push( $( this ).attr( 'data-appointment-id' ) );
        });

        return ids;
    },

    destroy : function()
    {

    }
};

export { AppointmentSelectTable };