import { tpl, tbodyTpl, tbodyTplYhd } from './activityPageTpl';
import PageTable from '@core/component/pageTable';
import sendHttp,{fetch} from '@core/component/httpClient';
import ShortPagination from '@core/component/shortPagination';
import { Messager } from '@core/component/msgDistribute';
import LinkTool from '../../linkTool';
import Domain, {INTERFACE} from '@core/util/domain';
import { ShopType } from "@core/util/channelUtil";
import DefaultDatasM from '@core/component/form/DefaultDatasM';
const ActivityPage = (($) => {
    const DefaultConfig = {
        tpl,
        tbodyTpl,
        tbodyTplYhd
    };
    class ActivityPage {
        constructor(config) {
            this._config = this._getConfig(config);
            this.el = this._config.node;
            this.$el = $(this.el);
            let html = template.render(this._config.tpl, this._config);
            this.$el.html(html);
            this.pageTable = new PageTable({
                node: this.$el.find('.J_table')[0],
                className: "cd-mytable",
                theadRenderer: ['页面名称', '页面地址'],
                itemRenderer: this._config.env && this._config.env.shopType == ShopType.YHD ? this._config.tbodyTplYhd : this._config.tbodyTpl,
                pageSize: 100,
                loading: true,
                columnWidth: ["50%", "50%"],
                fixedThead: true,
                frontEndPaging: false,
                pagination: ShortPagination,
                onDomReady: ($node) => {
                    console.log("onDomReady:");
                },
                onPageChange: (data) => {
                    this.queryData(data.selected);
                },
                onDomUpdate: this._config.onDomUpdate
            })
            this.queryData();
            this._addEvent();
        }
        
        async queryData() {
            let { platformId, shopType } = this._config.env;
            (shopType === 3 || shopType === 4) && (shopType = 0); 
            let data;
            if(window.page_data.isSDKM){
                data = DefaultDatasM.linkTool.activityList;
            }
            else{
                data = await fetch({
                    url: Domain.backend + '/mshop/activity/activityList.html',
                    data: {
                        platformId,
                        shopType,
                        pageNo: 1,
                        pageSize: 1000,
                    },
                    dataType: 'jsonp',
                });
            }
            if (data.result === true) {
                this.pageTable.update({
                    loading: false,
                    dataProvider: data.data.list,
                })
            } else {
                Message.error(data.msg);
            }
        }
        
        _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('change.activityPage', '[' + _this._config.linkAttr + ']', function() {
                let $this = $(this);
                let link = $this.attr(_this._config.linkAttr);
                let originalData = $this.attr(_this._config.originalDataAttr);
                let linkType = LinkTool.Type.ActivityPage;
                Messager.trigger('linkChange', {
                    link,
                    originalData,
                    linkType,
                });
            })
            
            this.$el.on('click.activityPage', 'table tbody tr', function() {
                $(this).find('input[type=radio]').trigger('change');
            })
            
            this.$el.on('dblclick.activityPage', 'table tbody tr', function() {
                Messager.trigger('confirm');
            })
            
            this.$el.on('click.activityPage', '.J_ok', function() {
                Messager.trigger('confirm');
            })
            
            this.$el.on('click.activityPage', '.J_cancel', function() {
                Messager.trigger('cancel');
            })
        }
        
        _getConfig(config) {
            return $.extend(true, {}, DefaultConfig, config);
        }
        
        dispose() {
            Messager.off(this.linkChangeKey);
            this.$el.off('linkChange.activityPage').off('change.activityPage').off('click.activityPage').off('dblclick.activityPage');
        }
    }
    return ActivityPage;
})($);
export default ActivityPage;