import { tpl, listTpl } from './frequentLinkTpl';
import { Messager } from '@core/component/msgDistribute';
import {fetch} from '@core/component/httpClient';
import Message from '@core/component/message';
import domain from '@core/util/domain';
import LinkTool from '../linkTool';
import {IsYaoB2B, ShopType} from '@core/util/channelUtil'
const FrequentLink = (($) => {
    const DefaultConfig = {
        tpl,
        listTpl,
    };
    class FrequentLink {
        constructor(config) {
            this._config = this._getConfig(config);
            this.el = this._config.node;
            this.$el = $(this.el);
            this._renderDom();
            this._addEvent();
            this._renderList();
        }
        
        _addEvent() {
            let _this = this;
            
            this.linkChangeKey = Messager.on('linkChange', (req, res) => {
                this.$el.find('[' + _this._config.linkAttr + ']').each(function() {
                    let val = $(this).attr(_this._config.linkAttr);
                    if (val === res.link) {
                        this.checked = true;
                    } else {
                        this.checked = false;
                    }
                })
            })
            
            this.$el.on('click.productLink', '.J_linkItem', function() {
                $(this).find('input[type=radio]').trigger('change');
            })
            
            this.$el.on('change.frequentLink', '[' + _this._config.linkAttr + ']', function() {
                let link = $(this).attr(_this._config.linkAttr);
                let originalData = $(this).attr(_this._config.originalDataAttr);
                let linkType = $(this).attr('data-type');
                linkType = parseInt(linkType);
                originalData = link;
                Messager.trigger('linkChange', { link, linkType, originalData });
            })
            
            this.$el.on('click.frequentLink', '.J_ok', function() {
                Messager.trigger('confirm');
            })
            
            this.$el.on('dblclick.frequentLink', '.J_linkItem', function() {
                Messager.trigger('confirm');
            })
            
            this.$el.on('click.frequentLink', '.J_cancel', function() {
                Messager.trigger('cancel');
            })
        }
        
        _getConfig(config) {
            return $.extend(true, {}, DefaultConfig, config);
        }
        
        _renderDom() {
            let config = this._config;
            let html = template.render(config.tpl, config);
            this.$el.html(html);
        }
        
        async _renderList() {
            let data;
            let { platformId,shopType, shopId, venderId ,venderType} = this._config.env;
            let { tab } = this._config;
            if (platformId === 0) {
                data = await this._getBasic(); 
                if (data.result === true) {
                    if (tab.includes('frequent') &&
                        !tab.includes('index') &&
                        !tab.includes('productList') &&
                        !tab.includes('summary') &&
                        !tab.includes('member') &&
                        !IsYaoB2B()
                    ) {
                        data.list = data.list; 
                    } else {
                        let temp = [];
                        if (tab.includes('index')) { 
                            const result = data.list.filter(v => { return v.type === LinkTool.Type.Index })[0]
                            result && temp.push(result)
                        }
                        if (tab.includes('productList')) { 
                            const result = data.list.filter(v => { return v.type === LinkTool.Type.List })[0]
                            result && temp.push(result)
                        }
                        if (tab.includes('summary')) { 
                            if(shopType != 5){
                                const result = data.list.filter(v => { return v.type === LinkTool.Type.Summary })[0]
                                result && temp.push(result)
                            }
                        }
                        if (tab.includes('member')) {  
                            if(shopType == 0){
                                const result = data.list.filter(v => { return v.type === LinkTool.Type.MemberCenter })[0]
                                result && temp.push(result)
                            }
                        }
                        if (tab.includes('search')) {  
                                const result = data.list.filter(v => { return v.type === LinkTool.Type.SearchResult })[0]
                                result && temp.push(result)
                        }
                        if(IsYaoB2B()){
                            let t = [];
                            let result = data.list.filter(v => { return v.type === LinkTool.Type.Index })[0]
                            result && t.push(result)
                            result = data.list.filter(v => { return v.type === LinkTool.Type.SearchResult })[0]
                            result && t.push(result)
                            data.list = t;
                        }else{
                            data.list = temp;
                        }
                    }
                    let html = template.render(this._config.listTpl, data.list);
                    this.$el.find('.J_linkList').html(html);
                    this._config.onDomUpdate();
                } else {
                    Message.error(data.msg);
                }
            } else if (platformId === 1) { 
                let list = [];
                if (tab.includes('frequent') &&
                    !tab.includes('index') &&
                    !tab.includes('productList') &&
                    !tab.includes('summary') &&
                    !tab.includes('member')
                ) {
                    list = [{
                        link: shopType == ShopType.YHD?`//shop.m.yhd.com/?shopId=${shopId}`:`//conecli.com/?shopId=${shopId}`,
                        name: '店铺首页',
                        type: LinkTool.Type.Index,
                    },{
                        link: shopType == ShopType.YHD?`//shop.m.yhd.com/customer/customerCenter?vendorId=${venderId}&shopId=${shopId}`:`//conecli.com/shopcard/?venderId=${venderId}&shopId=${shopId}&venderType=${venderType}&channel=4402`,
                        name: '会员中心页',
                        type: LinkTool.Type.MemberCenter,
                    }];
                } else {
                    if (tab.includes('index')) { 
                        list.push({
                            link: shopType == ShopType.YHD?`//shop.m.yhd.com/?shopId=${shopId}`:`//conecli.com/?shopId=${shopId}`,
                            name: '店铺首页',
                            type: LinkTool.Type.Index,
                        })
                    }
                    if (tab.includes('member')) { 
                        list.push({
                            link: shopType == ShopType.YHD?`//shop.m.yhd.com/customer/customerCenter?vendorId=${venderId}&shopId=${shopId}`:`//conecli.com/shopcard/?venderId=${venderId}&shopId=${shopId}&venderType=${venderType}&channel=4402`,
                            name: '会员中心页',
                            type: LinkTool.Type.MemberCenter,
                        })
                    }
                }
                let html = template.render(this._config.listTpl, list);
                this.$el.find('.J_linkList').html(html);
                this._config.onDomUpdate();
            } else {
                console.warn('未传入platformId');
            }
        }
        
        _getBasic() {
            let { shopType } = this._config.env;
            (shopType === 3 || shopType === 4) && (shopType = 0); 
            return fetch({
                url: `${domain.backend}/shop/commonLink/getBasicUrl.html`,
                data: {
                    shopType,
                },
                dataType: 'jsonp',
            })
        }
        
        _getCustom() {
            let { appId } = this._config.env;
            return fetch({
                url: `${domain.backend}/shop/commonLink/getCustomUrl.html`,
                data: {
                    appId,
                },
                dataType: 'jsonp',
            })
        }
        
        dispose() {
            Messager.off(this.linkChangeKey);
            this.$el.off('click.frequentLink').off('linkChange.frequentLink').off('change.frequentLink').off('dblclick.frequentLink');
        }
    }
    return FrequentLink;
})($);
export default FrequentLink;