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

    var layRate = {
        /**
         * 根据json对象生成html对象
         * @param {object} json
         * @param {boolean} selected true 表示选择当前
         * */
        render: function (json, selected) {
            if (selected === undefined) {
                selected = false;
            }
            var _readonly = json.readonly ? 'readonly=""' : '';
            var _required = json.required ? 'required=""' : '';
            var _html = '<div id="{0}Box" class="layui-form-item {2}"  data-id="{0}" data-tag="{1}" data-index="{3}">'.format(json.id, json.tag, selected ? 'active' : '', json.index);
            _html += '<label class="layui-form-label {0}" style="width:{2}px;">{1}:</label>'.format(json.required ? 'layui-form-required' : '', json.label, json.labelWidth);
            _html += '<div class="layui-input-block" style="margin-left: {0}px">'.format(json.labelWidth);
            _html += '<div id="{0}" class="widget-rate"></div>'.format(json.tag + json.id);
            _html += '<input name="{0}" type="hidden" value="{1}"></input>'.format(json.id, json.defaultValue);
            _html += '</div>';
            _html += '</div>';
            return _html;
        },
        update: function (json) {
            var $block = $('#' + json.id + 'Box .layui-input-block');
            var $label = $('#' + json.id + 'Box .layui-form-label');
            $label.empty();
            $block.css("margin-left", json.labelWidth);
            $label.css("width", json.labelWidth);
            $label.append(json.label + ":");
            rate.render({
                elem: '#' + json.tag + json.id,
                value: json.defaultValue,
                text: json.text,
                length: json.rateLength,
                half: json.half,
                readonly: json.readonly
            });
        },
        /* 获取对象 */
        jsonData: function (id, index, columncount) {
            //分配一个新的ID
            var _json = JSON.parse(JSON.stringify(formField.rate));
            _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) {
            rate.render({
                elem: '#' + item.tag + item.id,
                value: item.defaultValue,
                text: item.text,
                half: item.half,
                length: item.rateLength,
                readonly: item.readonly,
                choose: function (value) {
                    $("#" + item.id).find("input[name=" + item.id + "]").val(value);
                }
            });
        }
    };

    exports('layRate', layRate);
});