import { tpl, tbodyTpl } from './fansMiniTpl'
import { Messager } from '@core/component/msgDistribute'
import Message from '@core/component/message';
import ScrollList from '@core/component/scrollList';
import LinkTool from '../../linkTool'
import Domain from '@core/util/domain'
const fansMini = (($) => {
    const DefaultConfig = {
        tpl,
        tbodyTpl,
    }
    class FansMini {
        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.ScrollList = new ScrollList({
                node: this.$el.find('.J_table')[0],
                className: 'cd-mytable',
                itemRenderer: this._config.tbodyTpl,
                loading: true,
                needStringify: true,
                pageSize: 10,
                queryOptions: {
                    url: `${Domain.mc}/project/queryOnlineProject`,
                    data: {
                        type: 0,
                        subType: 0,
                        isLinkTool: true,
                    },
                    contentType: 'application/json; charset=UTF-8',
                },
                renderContentBefore: (data) => {
                    const venderId = window.page_data ? window.page_data.venderId : ''
                    let shopId = window.page_data ? window.page_data.shopId : "";
                    data &&
                        data.map((item) => {
                            item.paramStr = encodeURIComponent(`{"projectId":"${item.projectId}","venderId":"${venderId}","shopId":"${shopId}"}`)
                            item.customPath = `${item.type == 1 ? encodeURIComponent('pages/promotions/index.html') : encodeURIComponent('pages/index/index.html')}`
                            const url = `${window.location.protocol}//conecli.com/apps/mpshare/index.html?category=jump&des=jdmp&vapptype=1&path=${item.type == 1 ? encodeURIComponent('pages/promotions/index.html') : encodeURIComponent('pages/index/index.html')}&appId=${item.appId}&param=${encodeURIComponent(item.paramStr)}`
                            item.qrcode =
                                '//conecli.com/public/qrimg?url=' +
                                encodeURIComponent(url) +
                                '-200-1-4-2.png'
                            item.venderId = venderId;
                            item.shopId = shopId;
                            return item
                        })
                },
                onDomUpdate: this._config.onDomUpdate,
            })
            this._addEvent()
        }
        
        _addEvent() {
            let _this = this
            
            this.linkChangeKey = Messager.on('linkChange', (req, res) => {
                if (!res.link) {
                    return
                }
                const chooseAppid = JSON.parse(decodeURIComponent(res.link.match(/param=([^&]+)/)[1]))['projectId']
                this.$el.find('.J_rowItem').each(function () {
                    const { appid } = this.dataset
                    if (appid === chooseAppid) {
                        this.querySelector('input').checked = true
                        $(this).trigger('click', chooseAppid)
                    }
                })
            })
            
            
            
            
            this.$el.on('click.fansMini', '.J_rowItem', async function (e) {
                selectRow(this)
            })
            
            
            this.$el.on(
                'change.fansMini',
                '[' + _this._config.linkAttr + ']',
                function (e) {
                    let $this = $(this)
                    let link = $this.attr(_this._config.linkAttr)
                    let originalData = $this.attr(
                        _this._config.originalDataAttr,
                    )
                    let linkType = LinkTool.Type.FansMini
                    Messager.trigger('linkChange', {
                        link,
                        linkType,
                        originalData,
                    })
                    $(e.currentTarget)
                        .closest('.J_rowItem')
                        .find('.cd-qr-code')
                        .attr(
                            'src',
                            '//conecli.com/public/qrimg?url=' +
                                encodeURIComponent(
                                    `${window.location.protocol}${link}`,
                                ) +
                                '-200-1-4-2.png',
                        )
                },
            )
            
            this.$el.on('click.fansMini', '.J_ok', function () {
                Messager.trigger('confirm')
            })
            
            this.$el.on('click.fansMini', '.J_cancel', function () {
                Messager.trigger('cancel')
            })
            function selectRow(row) {
                let $row = $(row)
                $row.siblings().removeClass('active')
                $row.addClass('active')
            }
        }
        
        _getConfig(config) {
            return $.extend(true, {}, DefaultConfig, config)
        }
        
        dispose() {
            Messager.off(this.linkChangeKey)
            this.$el
                .off('click.fansMini')
                .off('linkChange.fansMini')
                .off('change.fansMini')
        }
    }
    return FansMini
})($)
export default fansMini