import {tpl,tbodyTpl} from './tpl'
import './style.scss'
import PageTable from '@core/component/pageTable';
import {fetch} from '@core/component/httpClient';
import ShortPagination from '@core/component/shortPagination';
import Message from '@core/component/message';
import Domain from '@core/util/domain';
import { Messager } from '@core/component/msgDistribute';
import LinkTool from '../index';
class FinanceCouponLink {
    constructor(config) {
        this.config = this.getConfig(config);
        this.$el = $(this.config.node);
        this.render();
        this.pageTable = new PageTable({
            node: this.$el.find('.J_table')[0],
            className: "cd-mytable",
            theadRenderer: ['活动类型', '活动名称' , '链接'],
            itemRenderer: tbodyTpl,
            pageSize: 20,
            loading: true,
            columnWidth: ["20%", "33%", "47%"],
            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();
    }
    getConfig(config){
        return $.extend(true, {}, config);
    }
    
    _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.FinanceCouponLink;
            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');
        })
    }
    render(){
        this.$el.html(template.render(tpl));
    }
    
    async queryData() {
        let data;
        data = await fetch({
            url: Domain.backend + '/linktool/listFinanceCoupon',
            data: {
            },
            dataType: 'jsonp',
        });
        if (data.result === true) {
            this.pageTable.update({
                loading: false,
                dataProvider: data.data.list,
            })
        } else {
            Message.error(data.msg);
        }
    }
    dispose() {
    }
}
export default FinanceCouponLink