let tip = require('plugin/tip');
let dialog = require('plugin/dialog');

$('.show-explain').on('click', function() {
    $('.show-explain').hide();
    $('.explain-main').css({height: 'auto'});
});

$('.hide-explain').on('click', function() {
    $('.show-explain').show();
    $('.explain-main').css({height: 0});
});

// 埋点
function point(type, trend) {
    let pointJosn = {
        TYPE: type
    };

    if (trend) {
        pointJosn.TREND = trend;
    }

    if ($('.yoho-page').hasClass('my-page')) {
        if (window._yas && window._yas.sendCustomInfo) {
            window._yas.sendCustomInfo({
                op: 'YB_HOMEPOP_TREND_SET_C',
                param: JSON.stringify(pointJosn)
            }, true);
        }
    } else if ($('.yoho-page').hasClass('my-promotion-page') && trend) {
        if (window._yas && window._yas.sendCustomInfo) {
            window._yas.sendCustomInfo({
                op: 'YB_MYPROMOTION_TREND_SET_C',
                appop: 'YB_H5_MYPROMOTION_TREND_SET_C',
                param: JSON.stringify({
                    TREND: trend
                })
            }, true);
        }
    }
}

$('.set-world').click(function() {
    let trendWord = $('.trend-world-area').val();

    $.ajax({
        type: 'GET',
        url: '/activity/set-trend-world',
        data: {
            trendWord: trendWord
        },
        success: function(result) {
            tip.show(result.message);

            if (result.code === 200) {
                let copyUrl = `//m.yohobuy.com/?openby:yohobuy={"action":"go.copy","params":{"text":"${trendWord}","message":"复制成功"}}`; // eslint-disable-line

                point(1, $('.trend-world-area').val());

                if ($('.yoho-page').hasClass('my-page')) {
                    $('.trend-code').html('# ' + $('.trend-world-area').val() + ' #');
                } else {
                    $('.trend-code').html($('.trend-world-area').val());
                }

                $('.copy.button').eq(0).attr('href', copyUrl);

                setTimeout(function() {
                    $('.set-trend-world').hide();
                }, 2500);
            }
        }
    });
});

$('.withhold').click(function() {

    if ((!window.cookie('trend-path') || parseInt(window.cookie('trend-path'), 10) !== 1) &&
        /^\/home/.test(location.pathname)) {
        dialog.showDialog({
            dialogText: '您还可以在“个人中心-基本资料-我的潮流口令”中进行设置',
            hasFooter: {
                centerBtnText: '我知道了'
            }
        });

        window.setCookie('trend-path', 1);
    }
    point(2);
    $('.set-trend-world').hide();
});

$('.set-trend-world').on('mousewheel touchmove', function(e) {
    e.preventDefault();
    return false;
});