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

    var HTML_ARRAY = [
        '<div id="{0}Box" class="layui-form-item {2} docno" data-id="{0}" data-tag="{1}" data-index="{3}">',
        '   <label class="layui-form-label" style="width: {1}px;"><span style="color:red;">*</span>{0}:</label>',
        '   <div id="{0}Block" class="layui-input-block layui-form" lay-filter="{0}" style="margin-left: {1}px">',
        '       <div class="layui-col-xs6 select">',
        '           <select id="{0}" name="{0}" lay-filter="{0}Filter">',
        '           </select>',
        '       </div>',
        '       <div class="layui-col-xs6">',
        '           <input id="{0}Text" name="{0}_text" class="layui-input" readonly lay-verify="required" placeholder="请选择或输入文号">',
        '       </div>',
        '   </div>',
        '</div>'
    ];

    var docno = {
        /**
         * 根据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, json.labelWidth);
            _html += HTML_ARRAY[2].format(json.id, json.labelWidth);
            _html += HTML_ARRAY[3];
            _html += HTML_ARRAY[4].format(json.id);
            _html += '<option value="">请选择</option>';
            // 渲染列表
            for (var i = 0, item; item = json.docNoOptions[i++];) {
                _html += '<option value="{0}" data-type="{1}" data-template="{2}" {4}>{3} {2}</option>'.format(item.value, item.type, item.template, item.text, item.checked ? 'checked="checked"' : '');
            }
            _html += HTML_ARRAY[5];
            _html += HTML_ARRAY[6];
            _html += HTML_ARRAY[7];
            _html += HTML_ARRAY[8].format(json.id);
            _html += HTML_ARRAY[9];
            _html += HTML_ARRAY[10];
            _html += HTML_ARRAY[11];
            return _html;
        },
        update: function (json) {
            var $select = $('#' + json.id + 'Box .layui-input-block .select');
            var $label = $('#' + json.id + 'Box .layui-form-label');
            $select.empty();
            $label.empty();
            var _html = '';
            _html += '<select name="{0}">'.format(json.id);
            _html += '<option value="">请选择</option>';
            for (var i = 0, item; item = json.docNoOptions[i++];) {
                _html += '<option value="{0}" data-type="{1}" data-template="{2}" {4}>{3} {2}</option>'.format(item.value, item.type, item.template, item.text, item.checked ? 'checked="checked"' : '');
            }
            _html += '</select>';
            $select.append(_html);
            $('#'+ json.id +'Block').css("margin-left", json.labelWidth);
            $label.css("width", json.labelWidth);
            $label.append(json.label + ":");
            form.render('select', json.id);
            $('#' + json.id + 'Box .layui-input-block div.layui-unselect.layui-form-select').css({
                width: '{0}'.format(json.width)
            });
        },
        /* 获取对象 */
        jsonData: function (id, index, columncount) {
            //分配一个新的ID
            var _json = JSON.parse(JSON.stringify(formField.docno));
            _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('docno', docno);
});