const Modal = require('plugin/modal2');
const yohoApp = require('yoho-app');

const getImgHost = function(url, bucket = 'goodsimg') {
    let urlArr = url.split('/'),
        num = urlArr[urlArr.length - 1].substr(1, 1),
        domain = `static.yhbimg.com/${bucket}`;

    url = domain + url;
    if (num === '1') {
        return '//img11.' + url;
    } else {
        return '//img12.' + url;
    }
};

const getImgUrl = function(src, width = 300, height = 300, mode = 2) {
    return src ? src.replace(/(\{width}|\{height}|\{mode})/g, function($0) {
        const dict = {
            '{width}': width,
            '{height}': height,
            '{mode}': mode || 2
        };

        return dict[$0];
    }).replace(/https?:/, '') + '/interlace/1' : '';
};

// 退换货 申请 成功, 打开 modal
const applySuccuss = function(type, applyId) {
    // 取消 Mars APP 头部的提交按钮
    if (yohoApp && yohoApp.isMarsApp) {
        yohoApp.ready(function() {
            yohoApp.invokeMethod('set.removeTopRightButton');
        });
    }

    const config = {
        exchange: {
            name: '换货',
            detailUrl: `/home/return/exchange/detail/${applyId}`
        },
        refund: {
            name: '退货',
            detailUrl: `/home/return/refund/detail/${applyId}`
        }
    };
    const kind = config[type];

    const modal = new Modal({
        styleClass: 'return-success-modal',
        title: `${kind.name}申请已提交,请等待审核.....`,
        buttons: [{
            text: '查看进度',
            handler: function() {
                this.hide();
                document.location.href = kind.detailUrl;
            }
        }]
    });

    modal.show();
};

module.exports = {
    getImgHost,
    getImgUrl,
    applySuccuss
};