import BaseFormControl from '../../baseFormControl';
import JDImage from '../../formType/image/image';
import JDLink from '../../formType/link';
import ImageSize from '../../../../util/ImageSize';
import { LinkImageTpl } from './tpl'
class LinkImage extends BaseFormControl {
    constructor(config) {
        super(config);
        const { domData } = this.config;
        if(domData.extConfig){
            this.blindbox = domData.extConfig.blindbox == '1' ? true : false;
        }
        this.render();
    }
    
    _getConfig(config) {
        return $.extend(true, {}, {
            domData: {},
            moduleData: {}
        }, config);
    }
    
     render() {
        const { moduleData, domData } = this.config;
        let image = moduleData.image || {};
        let validate = domData.validate || {};
        let _w = validate.width || '';
        let _h = validate.height || '';
        let imgSize = _w + ',' + _h;
        let size = ImageSize.getTip(imgSize);
        let tips = '支持' + size + '，大小不超过800KB的JPG、PNG图片';
        let domLinkImg = template.render(LinkImageTpl)
        this.appendFormControl(domLinkImg)
        this.img = new JDImage({
            domData: {
                node: this.element.parents('.cd-data-content'),
                name: "value",
                formType: {
                    type: "image"
                },
                tips
            },
            format: imgSize,
            chooseSizeLimit: 800,
            defaultImage: image.value || ''
        });
        let config = {
            domData: {
                node: this.element.parents('.cd-data-content'),
                name: 'linkInfo',
            },
            linkName: false,
            addLink: false,
            operation: false,
            tab: ['custom', 'frequent', 'shopCategory'],
            moduleData: moduleData.linkInfo || {},
            tabValueForbidIncrease: true 
        };
        if (this.blindbox) {
            return;
        }
        this.link = new JDLink(config);
    }
    setData(value){
    }
    
    getData() {
        let key = this.config.domData.name;
        let result = {};
        let data;
        if(this.blindbox){
            let link = `openapp.jdmobile://virtual?params={ "category": "jump","des": "jdmp","appId":"449A83F937B8E11410FDC32AB0ACA706", "vapptype": "1","param":{"venderId":"${page_data.venderId}","shopId":"${page_data.shopId}"}}`;
            let linkInfo = {
                link:link,
                linkType:1,
                originalData:link
            };
            data = $.extend(true, {}, {image: this.img.getData()}, {linkInfo:linkInfo});
        }else{
            data = $.extend(true, {}, {image: this.img.getData()}, this.link.getData());
        }
        result[key] = data;
        return result;
    }
}
export default LinkImage;