const ContractUnchargedMemberDataTable = {
    templateUrl : 'js/app/components/contract/contract-uncharged-member-data-table.html',
    create : function( props )
    {
        var app = this.app,
            groupId = props.get( 'groupId' ),
            items = props.get( 'unchargedItems' ),
            $container = $( this.container );

        this.render( { ui: items, activeGroup : groupId } );


        let table = $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'
                },
            },
            order : [[ 5, "desc" ]],
            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="memberChargeItem"]', function( e )
        {
            app.redirect( '/contract/charge-appointment/' + groupId + '/' + $( e.currentTarget ).attr( 'data-member-id' ) );
        });

        $('input.filterContract').on('change', function() {
            table.draw();
        });

        $('input.filterOpenClaim').on('change', function() {
            table.draw();
        });

        $.fn.dataTable.ext.search.push(
            function(settings, searchData, index, rowData, counter) {
                const contractChecked = $('.filterContract:checked').length === 1;
                const openClaimChecked = $('.filterOpenClaim:checked').length === 1;
                const item = items[index];
                if (contractChecked && openClaimChecked) {
                    return item['hasActiveContract'] === false && parseInt(item['count']) !== 0;
                }
                if (contractChecked) {
                    return item['hasActiveContract'] === false;
                }
                if (openClaimChecked) {
                    return parseInt(item['count']) !== 0;
                }
                return true;
            }
        );
    },

    destroy : function()
    {

    }
};

export { ContractUnchargedMemberDataTable };