import BaseFormControl from '../../baseFormControl'
import ImageDom from '../../../imageDom/ImageDom'
import { imageTpl } from './imageTpl'
class JDImage extends BaseFormControl {
    constructor(config) {
        super(config)
        this.render()
    }
    
    _getConfig(config) {
        return $.extend(
            true,
            {},
            {
                domData: {},
                defaultImage: '',
                format: '1-9999,1-9999',
                useSquare: false,
            },
            config,
        )
    }
    
    update(config) {
        this.config = $.extend(true, this.config, config)
        this.$formTips.html(this.config.domData.tips)
        this.imageDom.refreshFormat(this.config.format)
        if (config.hasOwnProperty('defaultImage')) {
            this.imageDom.updateImage(this.config.defaultImage)
        }
    }
    
    render() {
        let _this = this
        let dom = template.render(imageTpl, {
            tips: _this.config.domData.tips,
            imageUrl: _this.config.defaultImage,
        })
        this.appendFormControl(dom)
        const isCustom =
            (this.config.moduleConfg &&
                this.config.moduleConfg.module.moduleFlag === 'newAndOld') ||
            false
        const isCustom_new =
            (isCustom &&
                this.config.moduleConfg.domData.customType == 'custom_new') ||
            false
        const isCustom_old =
            (isCustom &&
                this.config.moduleConfg.domData.customType == 'custom_old') ||
            false
        this.imageDom = new ImageDom({
            node: this.element.find('.J_uploadImage'),
            format: this.config.format,
            useSquare: this.config.useSquare,
            chooseSizeLimit: this.config.chooseSizeLimit,
            typeLimit: this.config.typeLimit,
            delImg: this.config.delImg || false,
            delImgCallback: this.config.delImgCallback,
            hasMakeImage: this.config.hasMakeImage || false, 
            moduleConfg: this.config.moduleConfg || null, 
            moduleItemData: isCustom_new
                ? (this.config.moduleConfg.moduleData &&
                    this.config.moduleConfg.moduleData.newCustomer &&
                    this.config.moduleConfg.moduleData.newCustomer.image) ||
                null
                : isCustom_old
                    ? (this.config.moduleConfg.moduleData &&
                        this.config.moduleConfg.moduleData.oldCustomer &&
                        this.config.moduleConfg.moduleData.oldCustomer.image) ||
                    null
                    : null, 
        })
        this.$formTips = this.element.find('.J_formTips')
    }
    
    getData() {
        let key = this.config.domData.name
        const $imageNode = this.element.find('.cd-image-panel')
        let picUrl = $imageNode.attr('data-url')
        const linglongTemplateId = $imageNode.attr('data-linglongtemplateId')
        const linglongBannerId = $imageNode.attr('data-linglongbannerId')
        const lingLongNewLink = $imageNode.attr('data-lingLongNewLink')
        let $imgSize = this.element.find('.J_imgSize')
        let picWidth = $imgSize.attr('picWidth')
        let picHeight = $imgSize.attr('picHeight')
        let result = {}
        result[key] = picUrl
        result['picWidth'] = picWidth
        result['picHeight'] = picHeight
        linglongTemplateId &&
            (result['linglongTemplateId'] = linglongTemplateId)
        linglongBannerId && (result['linglongBannerId'] = linglongBannerId)
        lingLongNewLink && (result['lingLongNewLink'] = lingLongNewLink)
        return result
    }
}
export default JDImage