import { tpl } from './customLinkTpl'
import { Messager } from '@core/component/msgDistribute'
import {fetch} from '@core/component/httpClient';
import LinkTool from '../../linkTool'
import SLD from '@core/util/domain'
const CustomLink = (($) => {
    const DefaultConfig = {
        tpl
    }
    class CustomLink {
        constructor(config) {
            this._config = this._getConfig(config)
            this.el = this._config.node
            this.$el = $(this.el)
            this._renderDom()
            this._config.onDomUpdate()
            this._addEvent()
        }
        
        _addEvent() {
            
            this.linkChangeKey = Messager.on('linkChange', (req, res) => {
                this.$el.find('.J_customLink').val(res.link)
            })
            
            this.$el.on(
                'input.customLink change.customLink',
                '.J_customLink',
                function () {
                    let input = $(this)
                    let link = input.val()
                    let linkType = LinkTool.Type.CustomLink
                    let originalData = link
                    Messager.trigger('linkChange', {
                        link,
                        linkType,
                        originalData,
                    })
                },
            )
            
            this.$el.on('click.customLink', '.J_ok', async () => {
                let { platformId, shopType } = this._config.env
                ;(shopType === 3 || shopType === 4) && (shopType = 0) 
                let url = $('.J_linkInput').val()
                if (url === '') {
                    errorHint('链接不能为空')
                    return false
                }
                if (url) {
                    url =
                        '//' +
                        url.replace(/https:\/\/|\/\/|http:\/\//g, '').trim()
                }
                try {
                    if (
                        /^\/\/coupon.m.conecli.com\/coupons\/show.action/.test(url)
                    ) {
                        if (/to=(https:|http:)?\/\//.test(url)) {
                            url = url.replace(/to=(https:|http:)?\/\//, 'to=')
                        }
                    }
                } catch (e) {
                    console.log(e)
                }
                ;(shopType === 3 || shopType === 4) && (shopType = 0) 
                if (page_data.venderType === 3) {
                    shopType = 8
                }
                let data = await fetch({
                    url: `${SLD.backend}/shop/commonLink/checkCustomUrl.html`,
                    data: {
                        shopType,
                        platformId,
                        url,
                    },
                    errorHandler(data) {
                        errorHint(data.message)
                    },
                })
                if (data.result) {
                    this.$el.find('.J_customLink').val(url).trigger('keyup')
                    if (
                        /^\/\/coupon.m.conecli.com\/coupons\/show.action/.test(url)
                    ) {
                        $('.J_linkInput').val(url)
                    }
                    Messager.trigger('confirm')
                }
                function errorHint(msg) {
                    $('.J_customLink').addClass('cd-error')
                    $('.J_errorMsg').text(msg)
                }
            })
            this.$el.on('focus', '.J_customLink', function () {
                $(this).removeClass('cd-error')
            })
            
            this.$el.on('click.customLink', '.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)
        }
        
        dispose() {
            Messager.off(this.linkChangeKey)
            this.$el
                .off('click.customLink')
                .off('linkChange.customLink')
                .off('change.customLink')
        }
    }
    return CustomLink
})($)
export default CustomLink