const ContractAppointmentAssignDualList = {
    templateUrl : 'js/app/components/contract/contract-appointment-assign-dual-list.html',
    create : function( props )
    {
        const app = this.app,
              self = this,
              profile = props.get( 'profile' ),
              groupId = props.get( 'groupId' ),
              $container = $( this.container );

        self.profileId = profile.id;
        self.contracts = props.get( 'contracts');
        self.attendedAppointments = props.get( 'attendedAppointments' );

        this.render(
            {
                profile : profile,
                contracts : self.contracts,
                attendedAppointments : self.attendedAppointments,
                groupId : groupId,
                preselectedContractId : props.get( 'preselectedContractId' )
            }
        );

        self.$dlb = $container.find( '[data-id="select-contract-appointments"]' ).first().bootstrapDualListbox(
            {
                nonSelectedListLabel : 'Unzugewiesene Terminteilnahmen',
                selectedListLabel : 'Zum Vertrag zugewiesene Teilnahmen',
                filterTextClear : 'Zeige alle',
                removeSelectedLabel : 'Ausgewählte entfernen',
                removeAllLabel : 'Alle entfernen',
                infoText : 'Alle {0}',
                infoTextFiltered : '<span class="label label-warning">Filterresultat</span> {0} von {1}',
                infoTextEmpty : 'Keine Einträge',
                selectorMinimalHeight : '340'

            }
        );

        $container.find( '[name="selectedContractId"]' ).change( function()
        {
            self.update();
        });

        this.update();
    },

    update : function()
    {
        const self = this,
              $container = $( this.container ),
              selectedIds = [],
              $select = $container.find( '[data-id="select-contract-appointments"]' ).first();

        self.selectedContractId = $container.find( '[name="selectedContractId"]' ).first().val();

        let selectedContract = null,
            selectedContractAdminNote = '---',
            selectedContractQuota = '∞',
            selectedContractTimespan = '∞';

        for ( let ci = 0; ci < self.contracts.length; ci++ )
        {
            if ( self.contracts[ ci ].id == self.selectedContractId )
            {
                selectedContract = self.contracts[ ci ];
                break;
            }
        }

        if ( self.selectedContractId != "-1" && selectedContract )
        {
            selectedContractQuota = selectedContract.getCurrentPriceFactor() + ' / ';
            if ( selectedContract.isFlatrate() )
            {
                selectedContractQuota += '∞ (flatrate)';
            }
            else
            {
                selectedContractQuota += selectedContract.getMaxPriceFactor();
            }

            selectedContractTimespan = selectedContract.getMomentStartDate().format( 'DD.MM.YYYY' ) + ' - ' + selectedContract.getMomentEndDate().format( 'DD.MM.YYYY' );

            if ( selectedContract.admin_note )
            {
                selectedContractAdminNote = ('' + selectedContract.admin_note);
            }
        }

        $container.find( '[name="contractQuota"]' ).first().val( selectedContractQuota );
        $container.find( '[name="contractTimespan"]' ).first().val( selectedContractTimespan );
        $container.find( '[data-id="contractAdminNote"]' ).first().val( selectedContractAdminNote );

        $select.find( 'option' ).remove();
        self.$dlb.bootstrapDualListbox( 'refresh', true );

        let a = null,
            aCategories = null,
            activeGroup = app.user.getActiveGroup(),
            aSubject = '';

        for ( let aai = 0; aai < self.attendedAppointments.length; aai++ )
        {
            a = self.attendedAppointments[ aai ].getAppointment();
            aSubject = '';
            aCategories = null;
            if ( a.category_ids_js && a.category_ids_js.length && "string" === typeof a.category_ids_js )
            {
                try
                {
                    aCategories = JSON.parse( a.category_ids_js )
                }
                catch(e)
                {
                    aCategories = null;
                }
            }
            if ( a && aCategories.length > 0 )
            {
                aSubject = '[';
                for ( let ci = 0; ci < aCategories.length; ci++ )
                {
                    aSubject += activeGroup.getCourseCategoryNameById( aCategories[ ci ] )
                    if ( (ci + 1 ) < aCategories.length )
                    {
                        aSubject += ',';
                    }
                }
                aSubject += '] ';
            }
            aSubject += a.subject + ', ' + moment( a.start_dt ).format( 'DD.MM.YYYY' );

            if ( self.selectedContractId && self.attendedAppointments[ aai ].contractId == self.selectedContractId )
            {
                $select.append(
                    $( '<option>',
                        {
                            value: self.attendedAppointments[ aai ].attendee_id,
                            text : aSubject,
                            selected : "selected"
                        }
                    )
                );
            }
            else if ( null === self.attendedAppointments[ aai ].contractId )
            {
                $select.append(
                    $( '<option>',
                        {
                            value: self.attendedAppointments[ aai ].attendee_id,
                            text : aSubject
                        }
                    )
                );
            }
        }

        self.$dlb.bootstrapDualListbox( 'refresh', true );
    },

    getSelectedIds : function()
    {
        return {
            profileId : this.profileId,
            contractId : this.selectedContractId,
            contractAttendeeIds : this.$dlb.val()
        }
    },

    destroy : function()
    {
        this.$dlb.bootstrapDualListbox( 'destroy' );
    }
};

export { ContractAppointmentAssignDualList };