layui.define(['form', 'laytpl', 'restajax', 'dialog', 'consts'], function (exports) {
    var form = layui.form;
    var laytpl = layui.laytpl;
    var restAjax = layui.restajax;
    var dialog = layui.dialog;
    var consts = layui.consts;
    var expressions = consts.expressions;
    var datetypes = consts.datetypes;
    var dateformats = consts.dateformats;
    var anims = consts.anims;
    var arrows = consts.arrows;

    var lang = consts.lang;

    /**
     * 刷新属性
     * @param json
     * @returns {string}
     */
    var renderCommonProperty = function (json) {
        var _html = '';
        for (var key in json) {
            if (key === 'index') {
                continue;
            }
            if (key === 'tag' || key === 'id' || key === 'uploadUrl' || key === 'document' || key === 'interval' || key === 'cronUrl') { //只读字段
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                if (key === 'tag') {
                    _html += '    <input type="text" readonly id="{0}" name="{0}" value="{1}" required lay-verify="required" placeholder="请输入内容" autocomplete="off" class="layui-input">'.format(key, json[key] == undefined ? '' : json[key]);
                } else {
                    _html += '    <input type="text" id="{0}" name="{0}" value="{1}" required lay-verify="required" placeholder="请输入内容" autocomplete="off" class="layui-input">'.format(key, json[key] == undefined ? '' : json[key]);
                }
                _html += '  </div>';
                _html += '</div>';
            } else if (key === 'readonly' || key === 'disabled' || key === 'required' || key === 'listShow' || key === 'half' || key === "text" || key === "autoplay" || key === "full" || key === "verification" || key === 'autoplay' || key === 'isInput' || key === 'expression' || key === 'iconPickerSearch' || key === 'iconPickerPage' || key === 'isEnter' || key === 'isLabel') {
                var yes = "是";
                var no = "否";
                if (key === 'isInput') {
                    _html += '<div class="layui-form-item" >';
                    _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                    _html += '  <div class="layui-input-block">';
                    _html += '    <input type="checkbox" id="{1}" {0} name="{1}" lay-skin="switch" lay-text="{2}|{3}">'.format(json[key] ? 'checked' : '', key, yes, no);
                    _html += '  </div>';
                    _html += '</div>';
                }
                if (key === 'autoplay' || key === 'iconPickerSearch' || key === 'iconPickerPage') {
                    _html += '<div class="layui-form-item" >';
                    _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                    _html += '  <div class="layui-input-block">';
                    _html += '    <input type="checkbox" id="{1}" {0} name="{1}" lay-skin="switch" lay-text="{2}|{3}">'.format(json[key] ? 'checked' : '', key, yes, no);
                    _html += '  </div>';
                    _html += '</div>';
                }
                if (key === 'readonly') {
                    yes = "只读";
                    no = "可写";
                    _html += '<div class="layui-form-item" >';
                    _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                    _html += '  <div class="layui-input-block">';
                    _html += '    <input type="checkbox" id="{1}" {0} name="{1}" lay-skin="switch" lay-text="{2}|{3}">'.format(json[key] ? 'checked' : '', key, yes, no);
                    _html += '  </div>';
                    _html += '</div>';
                }
                if (key === 'isEnter' || key === 'isLabel') {
                    _html += '<div class="layui-form-item" >';
                    _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                    _html += '  <div class="layui-input-block">';
                    _html += '    <input type="checkbox" id="{1}" {0} name="{1}" lay-skin="switch" lay-text="{2}|{3}">'.format(json[key] ? 'checked' : '', key, yes, no);
                    _html += '  </div>';
                    _html += '</div>';
                }
                if (key === 'disabled') {
                    yes = "隐藏";
                    no = "显示";
                    _html += '<div class="layui-form-item" >';
                    _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                    _html += '  <div class="layui-input-block">';
                    _html += '    <input type="checkbox" id="{1}" {0} name="{1}" lay-skin="switch" lay-text="{2}|{3}">'.format(json[key] ? 'checked' : '', key, yes, no);
                    _html += '  </div>';
                    _html += '</div>';
                }
                if (key === 'required') {
                    yes = "必填";
                    no = "可选";
                    _html += '<div class="layui-form-item" >';
                    _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                    _html += '  <div class="layui-input-block">';
                    _html += '    <input type="checkbox" id="{1}" {0} name="{1}" lay-skin="switch" lay-text="{2}|{3}">'.format(json[key] ? 'checked' : '', key, yes, no);
                    _html += '  </div>';
                    _html += '</div>';
                }
                if (key === 'listShow') {
                    yes = "显示";
                    no = "不显示";
                    _html += '<div class="layui-form-item" >';
                    _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                    _html += '  <div class="layui-input-block">';
                    _html += '    <input type="checkbox" id="{1}" {0} name="{1}" lay-skin="switch" lay-text="{2}|{3}">'.format(json[key] ? 'checked' : '', key, yes, no);
                    _html += '  </div>';
                    _html += '</div>';
                }
                if (key === 'expression') {
                    _html += '<div class="layui-form-item" >';
                    _html += '  <label class="layui-form-label">验证</label>';
                    _html += '  <div class="layui-input-block">';
                    _html += '<select name="{0}" lay-verify="required">'.format(key);
                    for (var i = 0; i < expressions.length; i++) {
                        if (expressions[i].value === json.expression) {
                            _html += '<option value="{0}" selected="">{1}</option>'.format(expressions[i].value, expressions[i].text);
                        } else {
                            _html += '<option value="{0}">{1}</option>'.format(expressions[i].value, expressions[i].text);
                        }
                    }
                    _html += '</select>'
                    _html += '  </div>';
                    _html += '</div>';
                }
            } else if (key === 'defaultValue' || key === 'label' || key === 'height' || key === 'placeholder' || key === 'document' || key === 'minValue' || key === 'maxValue' || key === 'rateLength' || key === 'iconPickerLimit' || key === 'iconPickerCellWidth' || key === 'buttonVlaue') {
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                _html += '    <input type="text" id="{0}" name="{0}" value="{1}" required lay-verify="required" placeholder="请输入{2}" autocomplete="off" class="layui-input">'.format(key, json[key] == undefined ? '' : json[key], lang[key]);
                _html += '  </div>';
                _html += '</div>';
            } else if (key === 'stepValue') {
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                _html += '    <input type="number" id="{0}" name="{0}" value="{1}" required lay-verify="required" placeholder="请输入{2}" autocomplete="off" class="layui-input">'.format(key, json[key] == undefined ? '' : json[key], lang[key]);
                _html += '  </div>';
                _html += '</div>';
            } else if (key === 'count') {
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                _html += '    <input type="number" id="{0}" name="{0}" value="{1}" required lay-verify="required" placeholder="请输入{2}" autocomplete="off" class="layui-input">'.format(key, json[key] == undefined ? '' : json[key], lang[key]);
                _html += '  </div>';
                _html += '</div>';
            } else if (key === 'width' || key === 'labelWidth') {
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                _html += '<div id="{0}" class="widget-slider" style="top: 16px;"></div>'.format(key);
                _html += '    <input type="hidden" id="{0}"  name="{0}" value="{1}" readonly="readonly" style="background:#eeeeee!important;width: 100%;" placeholder="请选择{2}" autocomplete="off" class="layui-input">'.format(key + "-value", '', lang[key]);
                _html += '  </div>';
                _html += '</div>';
            } else if (key === 'menu') {
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                _html += '    <input type="text" id="{0}" name="{0}" value="{1}" readonly="readonly" style="background:#eeeeee!important;width: 100%;" placeholder="请选择{2}" autocomplete="off" class="layui-input">'.format(key, '', lang[key]);
                _html += '  </div>';
                _html += '</div>';
            } else if (key === 'switchValue') {
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                _html += '     <input type="checkbox" name="{0}" {1} lay-skin="switch" lay-text="ON|OFF">'.format(key, json[key] ? 'checked' : '');
                _html += '  </div>';
                _html += '</div>';
            } else if (key === 'datetype') {
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                _html += '<select name="{0}" lay-verify="required">'.format(key);
                for (var i = 0; i < datetypes.length; i++) {
                    if (datetypes[i].value === json.datetype) {
                        _html += '<option value="{0}" selected="">{1}</option>'.format(datetypes[i].value, datetypes[i].text);
                    } else {
                        _html += '<option value="{0}">{1}</option>'.format(datetypes[i].value, datetypes[i].text);
                    }
                }
                _html += '</select>'
                _html += '  </div>';
                _html += '</div>';
            } else if (key === 'dateformat') {
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                _html += '<select name="{0}" lay-verify="required">'.format(key);
                for (var i = 0; i < dateformats.length; i++) {
                    if (dateformats[i] === json.dateformat) {
                        _html += '<option value="{0}" selected="">{0}</option>'.format(dateformats[i]);
                    } else {
                        _html += '<option value="{0}">{0}</option>'.format(dateformats[i]);
                    }
                }
                _html += '</select>'
                _html += '  </div>';
                _html += '</div>';
            } else if (key === 'contents') {
                //处理
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                _html += '    <button style="margin: 5px 0px 0px 30px;" type="button" id="select-option-add" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon layui-icon-addition"></i>增加选项</button>'
                //_html += '    <button type="button" id="select-option-datasource" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon layui-icon-addition"></i>设置数据源</button>'
                _html += '  </div>';
                _html += '</div>';
                _html += '<div id="{0}">'.format(json.tag);
                //选项
                for (var i = 0; i < json.contents.length; i++) {
                    _html += '<div class="layui-form-item option contents-options" data-index="{0}">'.format(i);
                    _html += '  <div class="layui-inline" style="margin-right: 0px;width:220px; margin-left:30px;">';
                    _html += '   <input type="text" name="{0}-text"  autocomplete="off" value="{1}" class="layui-input">'.format(json.tag, json.contents[i]);
                    _html += '  </div>';
                    _html += '  <div class="layui-inline" style="margin-right: 0px;">';
                    _html += '   <i class="layui-icon layui-icon-slider contents-option-drag" style="color:blue;font-size:20px;"></i>';
                    _html += '   <i class="layui-icon layui-icon-delete contents-option-delete" style="color:red;font-size:20px;"></i>';
                    _html += '  </div>';
                    _html += '</div>';
                    //向 .option .layui-inline 添加drag事件并且必须设在 contents-option-drag 中才能拖动
                }
                _html += '</div>';
            } else if (key === 'options') {
                //处理
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                _html += '    <button style="margin: 5px 0px 0px 30px;" type="button" id="select-option-add" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon layui-icon-addition"></i>增加选项</button>'
                //_html += '    <button type="button" id="select-option-datasource" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon layui-icon-addition"></i>设置数据源</button>'
                _html += '  </div>';
                _html += '</div>';
                _html += '<div id="{0}">'.format(json.tag);
                //选项
                for (var i = 0; i < json.options.length; i++) {
                    _html += '<div class="layui-form-item option select-options" data-index="{0}">'.format(i);
                    _html += '  <div class="layui-inline" style="width: 30px; margin-right: 0px;">';
                    if (json.tag === 'checkbox') {
                        if (json.options[i].checked) {
                            _html += '    <input type="checkbox" name="{0}" lay-skin="primary" title="" checked="">'.format(json.tag);
                        } else {
                            _html += '    <input type="checkbox" name="{0}" lay-skin="primary" title="">'.format(json.tag);
                        }
                    } else {
                        if (json.options[i].checked) {
                            _html += '   <input type="radio" name="{0}"  checked="">'.format(json.tag);
                        } else {
                            _html += '   <input type="radio" name="{0}">'.format(json.tag);
                        }
                    }
                    _html += '  </div>';
                    _html += '  <div class="layui-inline" style="margin-right: 0px;width:110px;">';
                    _html += '   <input type="text" name="{0}-text"  autocomplete="off" value="{1}" class="layui-input">'.format(json.tag, json.options[i].text);
                    _html += '  </div>';
                    _html += '  <div class="layui-inline" style="margin-right: 0px;width:110px;">';
                    _html += '   <input type="text" name="{0}-value"  autocomplete="off" value="{1}" class="layui-input">'.format(json.tag, json.options[i].value);
                    _html += '  </div>';
                    _html += '  <div class="layui-inline" style="margin-right: 0px;">';
                    _html += '   <i class="layui-icon layui-icon-slider select-option-drag" style="color:blue;font-size:20px;"></i>';
                    _html += '   <i class="layui-icon layui-icon-delete select-option-delete" style="color:red;font-size:20px;"></i>';
                    _html += '  </div>';
                    _html += '</div>';
                    //向 .option .layui-inline 添加drag事件并且必须设在 select-option-drag 中才能拖动
                }
                _html += '</div>';
            } else if (key === 'columns') {
                var columnCount = 2;
                columnCount = json[key].length;
                //处理
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                _html += '<select name="{0}" lay-verify="required">'.format(key);
                for (var i = 2; i <= 12; i++) {
                    if (i === columnCount) {
                        _html += '<option value="{0}" selected="">{0}</option>'.format(i);
                    } else {
                        _html += '<option value="{0}">{0}</option>'.format(i);
                    }
                }
                _html += '</select>'
                _html += '</div>';
                _html += '</div>';
            } else if (key === 'anim') {
                //处理
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                _html += '<select name="{0}" lay-verify="required">'.format(key);
                for (var i = 0; i < anims.length; i++) {
                    if (anims[i].value === json.anim) {
                        _html += ' <option value="{1}" selected="">{0}</option>'.format(anims[i].text, anims[i].value);
                    } else {
                        _html += ' <option value="{1}">{0}</option>'.format(anims[i].text, anims[i].value);
                    }
                }
                _html += '</select>'
                _html += '</div>';
                _html += '</div>';
            } else if (key === 'arrow') {
                //处理
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                _html += '<select name="{0}" lay-verify="required">'.format(key);
                for (var i = 0; i < arrows.length; i++) {
                    if (arrows[i].value === json.arrow) {
                        _html += ' <option value="{1}" selected="">{0}</option>'.format(arrows[i].text, arrows[i].value);
                    } else {
                        _html += ' <option value="{1}">{0}</option>'.format(arrows[i].text, arrows[i].value);
                    }
                }
                _html += '</select>'
                _html += '</div>';
                _html += '</div>';
            } else if (key === 'buttonType') {
                //处理
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                _html += '<select name="{0}" lay-verify="required">'.format(key);
                for (var i = 0; i < buttonTypes.length; i++) {
                    if (buttonTypes[i].value === json.buttonType) {
                        if (buttonTypes[i].value === "") {
                            _html += ' <option selected="">{0}</option>'.format(buttonTypes[i].text);
                        } else {
                            _html += ' <option value="{1}" selected="">{0}</option>'.format(buttonTypes[i].text, buttonTypes[i].value);
                        }
                    } else {
                        _html += ' <option value="{1}">{0}</option>'.format(buttonTypes[i].text, buttonTypes[i].value);
                    }
                }
                _html += '</select>'
                _html += '</div>';
                _html += '</div>';
            } else if (key === 'buttonSize') {
                //处理
                _html += '<div class="layui-form-item" >';
                _html += '  <label class="layui-form-label">{0}</label>'.format(lang[key]);
                _html += '  <div class="layui-input-block">';
                _html += '<select name="{0}" lay-verify="required">'.format(key);
                for (var i = 0; i < buttonSizes.length; i++) {
                    if (buttonSizes[i].value === json.buttonSize) {
                        if (buttonSizes[i].value === "") {
                            _html += ' <option selected="">{0}</option>'.format(buttonSizes[i].text);
                        } else {
                            _html += ' <option value="{1}" selected="">{0}</option>'.format(buttonSizes[i].text, buttonSizes[i].value);
                        }
                    } else {
                        _html += ' <option value="{1}">{0}</option>'.format(buttonSizes[i].text, buttonSizes[i].value);
                    }
                }
                _html += '</select>'
                _html += '</div>';
                _html += '</div>';
            } else if (key === 'dataMaxValue' || key === 'dataMinValue') {
                _html += '<div id="{0}" class="layui-form-item">'.format(key + json.id);
                _html += '<label class="layui-form-label">{0}:</label>'.format(lang[key]);
                _html += '<div class="layui-input-block"">';
                _html += '<div id="{0}" class="layui-input icon-date widget-date" style="line-height: 40px;"></div>'.format(key + json.tag + json.id);
                _html += '</div>';
                _html += '</div>';
            } else if (key === 'dateDefaultValue') {
                _html += '<div id="{0}" class="layui-form-item">'.format(key + json.id);
                _html += '<label class="layui-form-label">{0}:</label>'.format(lang[key]);
                _html += '<div class="layui-input-block"">';
                _html += '<div id="{0}" class="layui-input icon-date widget-date" style="line-height: 40px;"></div>'.format(key + json.tag + json.id);
                _html += '</div>';
                _html += '</div>';
            } else if (key === 'dateRangeDefaultValue') {
                _html += '<div id="{0}" class="layui-form-item">'.format(key + json.id);
                _html += '<label class="layui-form-label">{0}:</label>'.format(lang[key]);
                _html += '<div class="layui-input-block"">';
                _html += '<div id="{0}" class="layui-input icon-date widget-date" style="line-height: 40px;"></div>'.format(key + json.tag + json.id);
                _html += '</div>';
                _html += '</div>';
            } else if (key === 'buttonIcon') {
                _html += '<div id="{0}" class="layui-form-item" ">'.format(key + json.id);
                _html += '<label class="layui-form-label">{0}:</label>'.format(lang[key]);
                _html += '<div class="layui-input-block"">';
                _html += '<div id="{0}" class="layui-input icon-date widget-date" style="line-height: 40px;"></div>'.format(json.tag + json.id + "property");
                _html += '</div>';
                _html += '</div>';
            } else if(key === 'docNoOptions') {
                _html += '<div id="{0}" class="layui-form-item">'.format(key + json.id);
                _html += '<label class="layui-form-label">添加选项</label>';
                _html += '<div class="layui-input-block">';
                _html += '<button type="button" class="layui-btn select-doc-no-btn" data-id="{0}">添加</button>'.format(key + json.id);
                _html += '</div>';
                _html += '<div id="{0}OptionsBox" class="layui-form-item option select-options">'.format(key + json.id);
                for(var i = 0, item; item = json.docNoOptions[i++];) {
                    _html += '<div class="layui-form-item option select-options" data-index="{0}">'.format(i - 1);
                    _html += '  <div class="layui-inline" style="margin-right: 0px;width:314px;">';
                    _html += '   <input type="text" name="{0}-text"  autocomplete="off" value="{1}" class="layui-input" readonly>'.format(json.tag, item.text + item.template);
                    _html += '  </div>';
                    _html += '</div>';
                }
                _html += '</div>';
            }
        }
        return _html;
    }

    var refreshDownloadTemplet = function(fileName, file) {
        var dataRander = {};
        dataRander[fileName] = file;
        laytpl(document.getElementById(fileName +'FileDownload').innerHTML).render(dataRander, function(html) {
            document.getElementById(fileName +'FileBox').innerHTML = html;
        });
    }
    // 初始化文件列表
    var initFileList = function(fileName, ids, callback) {
        var dataForm = {};
        dataForm[fileName] = ids;
        form.val('dataForm', dataForm);

        if(!ids) {
            refreshDownloadTemplet(fileName, []);
            if(callback) {
                callback(fileName, []);
            }
            return;
        }

        restAjax.get(restAjax.path('api/file/list', []), {
            ids: ids
        }, null, function(code, data) {
            refreshDownloadTemplet(fileName, data);
            if(callback) {
                callback(fileName, data);
            }
        }, function(code, data) {
            dialog.msg(data.msg);
        });
    }

    String.prototype.format = function (args) {
        var result = this;
        if (arguments.length > 0) {
            if (arguments.length == 1 && typeof (args) == "object") {
                for (var key in args) {
                    if (args[key] != undefined) {
                        var reg = new RegExp("({" + key + "})", "g");
                        result = result.replace(reg, args[key]);
                    }
                }
            } else {
                for (var i = 0; i < arguments.length; i++) {
                    if (arguments[i] != undefined) {
                        var reg = new RegExp("({[" + i + "]})", "g");
                        result = result.replace(reg, arguments[i]);
                    }
                }
            }
        }
        return result;
    }

    String.prototype.format = function (args) {
        var result = this;
        if (arguments.length > 0) {
            if (arguments.length == 1 && typeof (args) == "object") {
                for (var key in args) {
                    if (args[key] != undefined) {
                        var reg = new RegExp("({" + key + "})", "g");
                        result = result.replace(reg, args[key]);
                    }
                }
            } else {
                for (var i = 0; i < arguments.length; i++) {
                    if (arguments[i] != undefined) {
                        var reg = new RegExp("({[" + i + "]})", "g");
                        result = result.replace(reg, arguments[i]);
                    }
                }
            }
        }
        return result;
    }

    var autoId = function(tag) {
        return tag +'_'+ new Date().getTime();
    }

    var guid = function () {
        var d = new Date().getTime();
        if (window.performance && typeof window.performance.now === "function") {
            d += performance.now(); //use high-precision timer if available
        }
        var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
        return uuid;
    }

    exports('formUtils', {
        renderCommonProperty: renderCommonProperty,
        autoId: autoId,
        guid: guid,
        initFileList: initFileList
    })
});