<?php
// Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.
?>
<a href="#" id="provide-rating-button" class="s-btn" onclick="Smb.Views.RatingDialog.showVote('manual'); return false;">
  <i class="icon-opinions"></i> <?php echo lmsg('provide_rating'); ?>
</a>

<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function() {
<?php if ('manual' !== $this->dialogCause): ?>
    Smb.Views.RatingDialog.showVote(<?php echo $this->jsEscape($this->dialogCause); ?>);
<?php endif; ?>
});

Jsw.namespace('Smb.Views.RatingDialog');
Smb.Views.RatingDialog = {
    _dialog: null,
    _dialogCause: null,
    _voteButtonOk: null,
    _voteTextareaLike: null,
    _voteTextareaDislike: null,
    _provideButton: null,
    _voted: false,
    _time: null,
    showVote: function(dialogCause) {
        var self = this;
        this._voted = false;
        this._dialogCause = dialogCause;
        this._dialog = new Jsw.Popup({
            title: <?php echo $this->jsLmsg("{$this->localeSection}.voteTitle"); ?>,
            id: 'rating-dialog',
            closeButtonEnabled: true,
            hideOnEscape: true,
            content:    '<p>' +
                <?php echo $this->jsLmsg("{$this->localeSection}.voteHint"); ?> +
                '</p><hr/>' +
                '<div class="form-box form--vertical">' +
                    '<div class="box-area">' +
                        '<div class="content">' +
                            '<div class="content-area">' +
                                '<div class="nps-survey">' +
                                    '<p class="nps-survey__question">' + <?php echo $this->jsLmsg("{$this->localeSection}.voteQuestion"); ?> + '</p>' +
                                    '<div class="nps-survey__container">' +
                                        '<div id="nps-rating" class="nps-survey__scores"></div>' +
                                        '<div class="nps-survey__legend">' +
                                            '<div class="nps-survey__legend-left">' + <?php echo $this->jsLmsg("{$this->localeSection}.voteLegendBad"); ?> + '</div>' +
                                            '<div class="nps-survey__legend-right">' + <?php echo $this->jsLmsg("{$this->localeSection}.voteLegendGood"); ?> + '</div>' +
                                        '</div>' +
                                    '</div>' +
                                '</div><hr/>' +
                                '<div class="form-row">' +
                                    '<div class="field-name">' +
                                        <?php echo $this->jsLmsg("{$this->localeSection}.voteLikeHint"); ?> +
                                    '</div>' +
                                    '<div class="field-value"><textarea id="rating-like" class="f-max-size" maxlength="1000"></textarea></div>' +
                                '</div>' +
                                '<div class="form-row">' +
                                    '<div class="field-name">' +
                                        <?php echo $this->jsLmsg("{$this->localeSection}.voteDislikeHint"); ?> +
                                    '</div>' +
                                    '<div class="field-value"><textarea id="rating-dislike" class="f-max-size" maxlength="1000"></textarea></div>' +
                                '</div>' +
                            '</div>' +
                        '</div>' +
                    '</div>' +
                '</div>',
            buttons: [{
                title: <?php echo $this->jsLmsg("{$this->localeSection}.voteButtonOk"); ?>,
                id: 'rating-vote-button-ok',
                class: 'action',
                handler: function(event, popup) {
                    if (this.checkPossibleSendVote()) {
                        this._voted = true;
                        this.sendVote();
                        popup.hide();
                        this.showThank();
                    }
                }.bind(this)
            }, {
                title: <?php echo $this->jsLmsg("{$this->localeSection}.voteButtonCancel"); ?>,
                id: 'rating-vote-button-cancel',
                handler: function(event, popup) {
                    popup.hide();
                }
            }],
            onShow: function() {
                Smb.Views.Rating.init(this.getId() + '-form');
                self._time = Date.now();
            },
            onHide: function() {
                if (this._voted) {
                    return;
                }
                if (this.checkPossibleSendVote() || this._voteTextareaLike.value.length || this._voteTextareaDislike.value.length) {
                    this.sendVote();
                }
<?php if ('timeout' === $this->dialogCause): ?>
                this.sendPostpone();
<?php endif; ?>
            }.bind(this)
        });
        this._voteButtonOk = $('rating-vote-button-ok');
        this._voteTextareaLike = $('rating-like');
        this._voteTextareaDislike = $('rating-dislike');
    },
    showThank: function() {
        var content = '<p>' + <?php echo $this->jsLmsg("{$this->localeSection}.thankHint"); ?> + '</p>';
        var introduction = <?php echo $this->jsLmsg("{$this->localeSection}.introductionText"); ?>;
        var buttons = [{
            title: <?php echo $this->jsLmsg("{$this->localeSection}.thankButtonClose"); ?>,
            'class': 'action',
            handler: function(event, popup) {
                if (this._provideButton) {
                    this._provideButton.hide();
                }
                popup.hide();
            }.bind(this)
        }];
        if (this._voteTextareaDislike.value.trim().length) {
            content += this.supportCenter();
        } else if (this._voteTextareaLike.value.trim().length) {
            content += this.socialShare();
            buttons = [];
        }
        this._dialog = new Jsw.Popup({
            title: <?php echo $this->jsLmsg("{$this->localeSection}.thankTitle"); ?>,
            closeButtonEnabled: true,
            hideOnEscape: true,
            content: content,
            buttons: buttons
        });
        if ($('share-text')) {
            $('share-text').value = introduction.replace('%%message%%', this._voteTextareaLike.value.trim());
        }
    },
    checkPossibleSendVote: function() {
        var errorField, survey = $('nps-rating').up('.nps-survey');
        if (Smb.Views.Rating.getRate()) {
            errorField = survey.removeClassName('error').down('.field-errors');
            if (errorField) {
                errorField.remove();
            }
        } else {
            errorField = survey.addClassName('error').down('.field-errors');
            if (!errorField) {
                survey.insert(
                    '<span class="field-errors"><span class="error-hint">' +
                    <?php echo $this->jsLmsg("{$this->localeSection}.voteRequire"); ?> +
                    '</span></span>'
                );
            }
        }
        return Smb.Views.Rating.getRate();
    },
    sendPostpone: function() {
        new Ajax.Request(
            Jsw.prepareUrl(<?php echo $this->jsEscape($this->baseUrl('/admin/rating/postpone')); ?>),
            { method: 'post' }
        );
    },
    sendVote: function() {
        new Ajax.Request(
            Jsw.prepareUrl(<?php echo $this->jsEscape($this->baseUrl('/admin/rating/vote')); ?>),
            {
                method: 'post',
                parameters : {
                    voted: this._voted,
                    rate: Smb.Views.Rating.getRate(),
                    feedbackLike: this._voteTextareaLike.value,
                    feedbackDislike: this._voteTextareaDislike.value,
                    dialogCause: this._dialogCause,
                    dialogDuration: Math.round((Date.now() - this._time) / 1000),
                    dialogAtPage: <?php echo $this->jsEscape(Plesk_Base_Utils_Http::getServerValue('PHP_SELF')); ?>
                }
            }
        );
    },
    socialShare: function() {
        return '<hr/><p>' + <?php echo $this->jsLmsg("{$this->localeSection}.thankSocialChoose"); ?> + '</p>' +
            '<textarea id="share-text" class="f-max-size"></textarea>' +
            '<div class="social-links">' +
            '<a class="twitter-share-button" href="#" onclick="Smb.Views.RatingDialog.sendSocialShare(event);">Tweet</a>' +
            '<a class="ui-link--muted" href="#" onclick="Smb.Views.RatingDialog.closeDialog(event);">' + <?php echo $this->jsLmsg("{$this->localeSection}.inviteButtonClose"); ?> + '</a>' +
            '</div>';
    },
    sendSocialShare: function (event) {
        event.preventDefault();
        window.open(<?php echo $this->jsEscape($this->baseUrl('/admin/rating/social')); ?> + '?button=twitter&text=' + encodeURIComponent($('share-text').value), 'social-share', 'width=600,height=460');
        this._dialog.hide();
    },
    supportCenter: function() {
        return '<p>' + <?php echo $this->jsLmsg("{$this->localeSection}.thankSupportCenter", [
                'link' => '<a id="rating-dialog-support-link" href="' . $this->escape($this->configProduct->supportUrl) . '" target="_blank" onclick="Smb.Views.RatingDialog.sendSupportCenter();">' . $this->lmsg("{$this->localeSection}.supportCenter") . '</a>'
            ]); ?> + '</p>';
    },
    sendSupportCenter: function () {
        new Ajax.Request(
            Jsw.prepareUrl(<?php echo $this->jsEscape($this->baseUrl('/admin/rating/support-center')); ?>),
            { method: 'post' }
        );
    },
    closeDialog: function (event) {
        event.preventDefault();
        this._dialog.hide();
    }
};

Jsw.namespace('Smb.Views.Rating');
Smb.Views.Rating = {
    _formId: null,
    init: function(formId) {
        this._formId = formId;
        Jsw.render($('nps-rating'), $R(0, 10).map(function (i) {
            return Jsw.createElement('label', {
                'class': 'nps-survey__score nps-survey__score--' + i,
                'onclick': function () {
                    Smb.Views.RatingDialog.checkPossibleSendVote();
                }.bind(this)
            }, [
                Jsw.createElement('input', {'type': 'radio', 'name': 'nps-survey-option', 'value': i}),
                Jsw.createElement('span', {'class': 'nps-survey__score-label'})
            ]);
        }));
    },
    getRate: function() {
        return $(this._formId)['nps-survey-option'].value;
    }
};
//]]>
</script>
