const EmployeeDataTable = {
    templateUrl : 'js/app/components/kkspecial/employee-data-table.html',
    create : function( params )
    {
        const app = this.app,
              $container = $( this.container ),
              employees = params.getAll();

        let dTable = null;

        this.render( { emps: employees } );


        $( this.container ).on( 'click', '[data-type="employee-row"]', function()
        {
            let employee = null,
                employeeRow = null,
                oldRowData = null,
                employeeId = $(this).attr( 'data-employee-id' );
            for ( let ei = 0; ei < employees.length; ei++ )
            {
                if( employeeId == employees[ ei ].id )
                {
                    employee = employees[ ei ];
                    break;
                }
            }

            if ( !employee )
            {
                return false;
            }

            employeeRow = dTable.row( '#employee-' + employeeId );
            if ( !employeeRow )
            {
                return false;
            }

            oldRowData = employeeRow.data();

            let $modalRoot = $container.find( '[data-id="modal-kkspecial-edit-employee-attributes"]' ).first();

            $modalRoot.find( '[data-id="input-special-hourlyrate"]' ).first().val( employee.kk_hourlyRate );
            $modalRoot.find( '[data-id="input-special-hourmax"]' ).first().val( employee.kk_hourMax );
            $modalRoot.find( '[data-id="input-special-notice"]' ).first().val( employee.kk_notice );
            $modalRoot.find( '[data-id="user-address"]' ).first().html( employee.getName() + '<br />' + employee.getAddress() );

            $modalRoot.find( '[data-id="section-attribute-form"]' ).first().show();
            $modalRoot.find( '[data-id="section-loader"]' ).first().hide();
            $modalRoot.on( 'shown.bs.modal', function()
            {
                $modalRoot.find( '[data-id="btnSaveEmployeeAttributes"]' ).first().off( "click" );
                $modalRoot.find( '[data-id="btnSaveEmployeeAttributes"]' ).first().click( function()
                {
                    $(this).off( "click" );

                    $modalRoot.find( '[data-id="section-attribute-form"]' ).first().hide();
                    $modalRoot.find( '[data-id="section-loader"]' ).first().show();

                    app.rpc.call(
                        'Special',
                        'updateKKProfileAttributes',
                        {
                            groupId : app.user.activeGroupId,
                            profileId : employeeId,
                            attributes : {
                                hourlyRate : $modalRoot.find( '[data-id="input-special-hourlyrate"]' ).first().val(),
                                hourMax : $modalRoot.find( '[data-id="input-special-hourmax"]' ).first().val(),
                                notice : $modalRoot.find( '[data-id="input-special-notice"]' ).first().val()
                            }
                        },
                        function( resp )
                        {
                            if ( resp && resp.hasOwnProperty( 'code' ) && resp.code == 200 )
                            {
                                app.UI.toastSuccess( 'Daten erfolgreich aktualisiert' );

                                if ( resp.hasOwnProperty( 'data' ) && resp.data.hasOwnProperty( 'profile_attributes' ) )
                                {
                                    oldRowData[ 5 ] = resp.data.profile_attributes.hourlyRate;
                                    oldRowData[ 6 ] = resp.data.profile_attributes.hourMax;
                                    oldRowData[ 7 ] = resp.data.profile_attributes.notice;
                                    employee.kk_hourlyRate = resp.data.profile_attributes.hourlyRate;
                                    employee.kk_hourMax = resp.data.profile_attributes.hourMax;
                                    employee.kk_notice = resp.data.profile_attributes.notice;
                                    employeeRow.data( oldRowData ).draw();

                                    $container.find( '[data-notice-cell-id="employee-notice-' + employeeId + '"]' ).first().attr(
                                        'title', employee.kk_notice ? employee.kk_notice : ''
                                    );
                                }
                            }
                            else
                            {
                                app.UI.toastError();
                            }

                            $modalRoot.modal( 'hide' );
                        }
                    );
                });
            });
            $modalRoot.on( 'hidden.bs.modal', function()
            {
                $modalRoot.modal( 'dispose' );
            });
            $modalRoot.modal( 'show' );

        });

        dTable = $( this.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'
                },
            },
            buttons: [
                { extend: 'copy'},
                {extend: 'csv'},
                {extend: 'excel', title: 'Mitgliederliste'},
                {extend: 'pdf', title: 'Mitgliederliste'},

                {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');
                    }
                }
            ]

        });
    },

    destroy : function()
    {

    }
};

export { EmployeeDataTable };