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

    var laySwitch = {
        /**
         * 根据json对象生成html对象
         * @param {object} json
         * @param {boolean} selected true 表示选择当前
         * */
        render: function (json, selected) {
            if (selected === undefined) {
                selected = false;
            }
            var _disabled = json.disabled ? 'disabled=""' : '';
            var _disabledClass = json.disabled ? ' layui-disabled' : '';
            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" style="width: {1}px;">{0}:</label>'.format(json.label, json.labelWidth);
            _html += '<div class="layui-input-block" style="width:calc({0} - {1}px);border: 1px solid #d2d2d2;border-left: 0px;margin-left: {1}px">'.format(json.width, json.labelWidth);
            _html += '<input type="checkbox" name="{0}" lay-skin="switch" lay-text="ON|OFF" {1} class="{2}" {3}>'.format(json.id, _disabled, _disabledClass, json.switchValue ? 'checked' : '');
            _html += '</div>';
            _html += '</div>';
            return _html;
        },
        update: function (json) {
            var _disabled = json.disabled ? 'disabled=""' : '';
            var _disabledClass = json.disabled ? ' layui-disabled' : '';
            var $block = $('#' + json.id + 'Box .layui-input-block');
            var $label = $('#' + json.id + 'Box .layui-form-label');
            $block.empty();
            $label.empty();
            var _html = '';
            _html += '<input type="checkbox" name="{0}" lay-skin="switch" lay-text="ON|OFF" {1} class="{2}" {3}>'.format(json.tag, _disabled, _disabledClass, json.switchValue ? 'checked' : '');
            $block.append(_html);
            $label.append(json.label + ":");
            $block.css({
                width: 'calc({0} - {1}px)'.format(json.width, json.labelWidth)
            });
            $block.css("margin-left", json.labelWidth);
            $label.css("width", json.labelWidth);
            form.render('checkbox');
        },
        /* 获取对象 */
        jsonData: function (id, index, columncount) {
            //分配一个新的ID
            var _json = JSON.parse(JSON.stringify(formField.switch));
            _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);
        }
    };

    exports('laySwitch', laySwitch);
});