layui.define(['jquery', 'upload', 'dialog', 'restajax', 'formUtils'], function(exports) {
    var $ = layui.jquery;
    var upload = layui.upload;
    var utils = layui.formUtils;
    var dialog = layui.dialog;
    var restAjax = layui.restajax;
    var formField = layui.formField;

    var viewerObj = {};

    var HTML_ARRAY = [
        '<div id="{0}Box" class="layui-form-item {2}" data-id="{0}" data-tag="{1}" data-index="{3}">',
        '   <label class="layui-form-label">{0}:</label>',
        '   <button type="button" class="layui-btn layui-upload-file-btn" id="{0}UploadBtn">',
        '       上传图片',
        '   </button>',
        '   <div class="upload-image-box">',
        '       <input type="hidden" id="{0}" name="{0}">',
        '       <div class="layui-btn-container" id="{0}FileBox"></div>',
        '   </div>',
        '</div>'
    ];

    var uploadImage = {
        /**
         * 根据json对象生成html对象
         * @param {object} json
         * @param {boolean} selected true 表示选择当前
         * */
        render: function (json, selected) {
            if (selected === undefined) {
                selected = false;
            }

            var _html = HTML_ARRAY[0].format(json.id, json.tag, selected ? 'active' : '', json.index);
            _html += HTML_ARRAY[1].format(json.label);
            _html += HTML_ARRAY[2].format(json.id);
            _html += HTML_ARRAY[3];
            _html += HTML_ARRAY[4];
            _html += HTML_ARRAY[5];
            _html += HTML_ARRAY[6].format(json.id);
            _html += HTML_ARRAY[7].format(json.id);
            _html += HTML_ARRAY[8];
            _html += HTML_ARRAY[9];
            return _html;
        },
        update: function (json) {
            $('#'+ json.id +'Box .layui-form-label').text(json.label);
        },
        /* 获取对象 */
        jsonData: function (id, index, columncount) {
            //分配一个新的ID
            var _json = JSON.parse(JSON.stringify(formField.uploadImage));
            _json.id = id == undefined ? utils.guid() : id;
            _json.index = index;
            return _json;
        },
        /* 根据 json 对象显示对应的属性 */
        property: function (json) {
            $('#columnProperty').empty();
            var _html = '';
            _html = utils.renderCommonProperty(json); //获取通用属性HTML字符串
            //处理特殊字符串
            for (var key in json) {
                if (key === 'index') {
                    continue;
                }
            }
            $('#columnProperty').append(_html);
        },
        componentRender: function(item) {
        }
    };

    exports('uploadImage', uploadImage);
});