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

    var HTML_ARRAY = [
        '<div id="{0}Box" class="layui-form-item layui-form-text {2}"  data-id="{0}" data-tag="{1}" data-index="{3}">',
        '   <label class="layui-form-label {0}" style="width: {3};"><span style="color:red;">{2}</span>{1}:</label>',
        '   <div id="{1}Block" class="layui-input-block"  style="width: {0}">',
        '       <textarea name="{0}" placeholder="{3}" width="{2}" class="layui-textarea{6}" {4} {5} {7}>{1}</textarea>',
        '   </div>',
        '</div>'
    ]

    var textarea = {
        /**
         * 根据json对象生成html对象
         * @param {object} json
         * @param {boolean} selected true 表示选择当前
         * */
        render: function (json, selected) {
            if (selected === undefined) {
                selected = false;
            }
            var _disabled = json.disabled ? 'disabled=""' : '';
            var _required = json.required ? 'lay-verify="required"' : '';
            var _readonly = json.readonly ? 'readonly=""' : '';
            var _disabledClass = json.disabled ? ' layui-disabled' : '';
            var _html = HTML_ARRAY[0].format(json.id, json.tag, selected ? 'active' : '', json.index);
            _html += HTML_ARRAY[1].format(json.required ? 'layui-form-required' : '', json.label, json.required ? '*' : '', json.width);
            _html += HTML_ARRAY[2].format(json.width, json.id);
            _html += HTML_ARRAY[3].format(json.id, json.defaultValue ? json.defaultValue : '', json.width, json.placeholder, _disabled, _required, _disabledClass, _readonly);
            _html += HTML_ARRAY[4];
            _html += HTML_ARRAY[5];
            return _html;
        },
        update: function (json) {
            var _disabled = json.disabled ? 'disabled=""' : '';
            var _required = json.required ? 'lay-verify="required"' : '';
            var _disabledClass = json.disabled ? ' layui-disabled' : '';
            var _readonly = json.readonly ? 'readonly=""' : '';
            var $block = $('#' + json.id + 'Box .layui-input-block');
            var $label = $('#' + json.id + 'Box .layui-form-label');
            $block.empty();
            $label.empty();
            var _html = '';
            _html += HTML_ARRAY[3].format(json.id, json.defaultValue ? json.defaultValue : '', json.width, json.placeholder, _disabled, _required, _disabledClass, _readonly);
            $('#' + json.id + 'Box .layui-input-block').append(_html);
            $label.css({
                width: '{0}'.format(json.width)
            });
            $block.css({
                width: '{0}'.format(json.width)
            });
            if (json.required) {
                $label.append('<span style="color:red;">*</span>');
            }
            $label.append(json.label + ":");
        },
        /* 获取对象 */
        jsonData: function (id, index, columncount) {
            //分配一个新的ID
            var _json = JSON.parse(JSON.stringify(formField.textarea));
            _json.id = id == undefined ? utils.autoId(_json.tag) : 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);
        }
    };

    exports('textarea', textarea);
});