$(function () {
    // 获取元素属性
    function getStyle(el, name, isSend) {
        if (isSend) {
            return el.style[name]
        } else {
            if (el.currentStyle) {
                return el.currentStyle[name];
            } else {
                return getComputedStyle(el, null)[name];
            }
        }
    }
    window.getEleData = function () {
        var Data = {}   // 交互的数据
        // 通过手机内的元素 构建数据
        console.log(document.querySelectorAll('.tmp-editable'))
        for (var i = 0, aTmpEdit = document.querySelectorAll('.tmp-editable'); i < aTmpEdit.length; i++) {
            var region = aTmpEdit[i].getAttribute('data-region');
            Data[region] || (Data[region] = []);

            var obj = {
                query: aTmpEdit[i].getAttribute('data-query'),
                editType: aTmpEdit[i].getAttribute('data-edit-type'),
                content: aTmpEdit[i].getAttribute('data-edit-content'),
                size: aTmpEdit[i].getAttribute('data-size')
            }
            if (aTmpEdit[i].getAttribute('data-putonid') != null) {
                obj.putonId = aTmpEdit[i].getAttribute('data-putonid')
            }
            if (aTmpEdit[i].getAttribute('stroke') != null) {
                obj.strokeColor = aTmpEdit[i].getAttribute('stroke')
            }
            if (aTmpEdit[i].getAttribute('data-activityid') != null) {
                obj.activityId = aTmpEdit[i].getAttribute('data-activityid')
            }

            aTmpEdit[i].getAttribute('data-index') && (obj.index = aTmpEdit[i].getAttribute('data-index'))
            var types = aTmpEdit[i].getAttribute('data-edit-type');
            switch (types) {
                case 'img':
                    obj[types] = aTmpEdit[i].src;
                    break;
                case 'bgImg':
                    var str = getStyle(aTmpEdit[i], 'backgroundImage')
                    var str1 = str.replace('url(', '')
                    var str2 = str1.replace(')', '')
                    obj[types] = str2.replace(/"/g, '');
                    break;
                case 'bgC':
                    if (getStyle(aTmpEdit[i], 'backgroundImage') !== 'none') {
                        obj[types] = getStyle(aTmpEdit[i], 'backgroundImage');
                    } else {
                        obj[types] = getStyle(aTmpEdit[i], 'backgroundColor');
                    }
                    break;
                case 'bdC':
                    obj[types] = getStyle(aTmpEdit[i], 'borderColor');
                    break;
                case 'text':
                    obj[types] = aTmpEdit[i].innerHTML;
                    break;
                case 'color':
                    obj[types] = getStyle(aTmpEdit[i], 'color');
                    break;
                case 'strokeColor':
                    obj[types] = getStyle(aTmpEdit[i], 'stroke');
                    break;
            }
            Data[region].push(obj);
        }
        delete Data.null
        return {
            data: Data,
            version: '1.0'
        }
    }
    window.getEleDataToSend = function () {
        var Data = {}   // 交互的数据
        // 通过手机内的元素 构建数据
        console.log(document.querySelectorAll('.tmp-editable'))
        for (var i = 0, aTmpEdit = document.querySelectorAll('.tmp-editable'); i < aTmpEdit.length; i++) {
            var region = aTmpEdit[i].getAttribute('data-region');
            Data[region] || (Data[region] = []);

            var obj = {
                query: aTmpEdit[i].getAttribute('data-query'),
                editType: aTmpEdit[i].getAttribute('data-edit-type'),
            }
            if (aTmpEdit[i].getAttribute('stroke') != null) {
                obj.strokeColor = aTmpEdit[i].getAttribute('stroke')
            }
            if (aTmpEdit[i].getAttribute('data-putonid') != null) {
                var activeQuery = $(aTmpEdit[i]).attr('data-query')
                var activeIndex = $(aTmpEdit[i]).attr('data-index')

                if (activeIndex !== undefined) {
                    for (var j = 0; j < probabilityDrawConfig.length; j++) {
                        var pItem = probabilityDrawConfig[j]
                        if (pItem.index === activeIndex) {
                            obj.putonId = pItem.PutOnId
                            break
                        }
                    }
                } else {
                    for (var j = 0; j < probabilityDrawConfig.length; j++) {
                        var pItem = probabilityDrawConfig[j]
                        if (pItem.query === activeQuery) {
                            obj.putonId = pItem.PutOnId
                            break
                        }
                    }
                }
            }

            aTmpEdit[i].getAttribute('data-index') && (obj.index = aTmpEdit[i].getAttribute('data-index'))

            var types = aTmpEdit[i].getAttribute('data-edit-type')

            switch (types) {
                case 'img':
                    obj[types] = aTmpEdit[i].src.replace('http:', '')
                    break;
                case 'bgImg':
                    var str = getStyle(aTmpEdit[i], 'backgroundImage')
                    var str1 = str.replace('url(', '')
                    var str2 = str1.replace(')', '')
                    var str3 = str2.replace(/"/g, '')
                    obj[types] = str3.replace('http:', '')
                    break;
                case 'bgC':
                    obj[types] = getStyle(aTmpEdit[i], 'backgroundImage', true);
                    break;
                case 'bdC':
                    obj[types] = getStyle(aTmpEdit[i], 'borderColor', true);
                    break;
                case 'text':
                    obj[types] = aTmpEdit[i].innerHTML;
                    break;
                case 'color':
                    obj[types] = getStyle(aTmpEdit[i], 'color', true);
                    break;
                case 'strokeColor':
                    obj[types] = getStyle(aTmpEdit[i], 'stroke');
                    break;
            }
            Data[region].push(obj);
        }
        delete Data.null
        return {
            data: Data,
            version: '1.0'
        }
    }
})
