define(['jquery', 'bootstrap', 'upload', 'validator'], function ($, undefined, Upload, Validator) {
    var Form = {
        config: {
            fieldlisttpl: '<dd class="form-inline"><input type="text" name="<%=name%>[<%=index%>][key]" class="form-control" value="<%=key%>" placeholder="<%=options.keyPlaceholder||\'\'%>" size="10" /> <input type="text" name="<%=name%>[<%=index%>][value]" class="form-control" value="<%=value%>" placeholder="<%=options.valuePlaceholder||\'\'%>" /> <span class="btn btn-sm btn-danger btn-remove"><i class="mdi mdi-window-close"></i></span> <span class="btn btn-sm btn-primary btn-dragsort"><i class="mdi mdi-drag"></i></span></dd>'
        },
        events: {
            validator: function (form, _success, _error, _submit) {
                if (!form.is("form"))
                    return;                
                //绑定表单事件
                var options = $.extend({
                    rules: {
                        username: [/^\w{3,30}$/, __('Username must be 3 to 30 characters')],
                        password: [/^[\S]{6,30}$/, __('Password must be 6 to 30 characters')],
                        email:[/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,__('Invalid email')],
                        phone:[/^1[356789][0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$/,__('Invalid phone number')],
                    },
                    validClass: 'has-success',
                    invalidClass: 'has-error',
                    bindClassTo: '.form-group',
                    formClass: 'n-default n-bootstrap',
                    msgClass: 'n-right',
                    stopOnError: true,
                    display: function (elem) {
                        return $(elem).closest('.form-group').find(".control-label").text().replace(/\:/, '');
                    },
                    dataFilter: function (data) {
                        if (data.code === 0) {
                            return data.msg ? {"ok": data.msg} : '';
                        } else {
                            return data.msg;
                        }
                    },
                    target: function (input) {
                        var target = $(input).data("target");
                        if (target && $(target).length > 0) {
                            return $(target);
                        }
                        var $formitem = $(input).closest('.form-group'),
                            $msgbox = $formitem.find('span.msg-box');
                        if (!$msgbox.length) {
                            return [];
                        }
                        return $msgbox;
                    },
                    valid: function (ret) {
                        var that = this, submitBtn = $(".layer-footer [type=submit]", form);
                        that.holdSubmit(true);
                        submitBtn.addClass("disabled");
                        //验证通过提交表单
                        var submitResult = Form.api.submit($(ret), function (data, ret) {
                            that.holdSubmit(false);
                            submitBtn.removeClass("disabled");
                            if (false === $(this).triggerHandler("success.form", [data, ret])) {
                                return false;
                            }
                            if (typeof _success === 'function') {
                                if (false === _success.call($(this), data, ret)) {
                                    return false;
                                }
                            }
                            //提示及关闭当前窗口
                            var msg = ret.hasOwnProperty("msg") && ret.msg !== "" ? ret.msg : __('Operation completed');
                            //parent.Toastr.success(msg);
                            // parent.$(".btn-refresh").trigger("click");
                            // if (window.name) {
                            //     var index = parent.Layer.getFrameIndex(window.name);
                            //     parent.Layer.close(index);
                            // }
                            Fast.api.success(msg,function(){
                                if(Fast.isPopIframe()){
                                    parent.$('#table').bootstrapTable('refresh');
                                    parent.Fast.api.closeTopPopWindow();
                                }
                            });
                            return false;
                        }, function (data, ret) {
                            that.holdSubmit(false);
                            if (false === $(this).triggerHandler("error.form", [data, ret])) {
                                return false;
                            }
                            submitBtn.removeClass("disabled");
                            if (typeof _error === 'function') {
                                if (false === _error.call($(this), data, ret)) {
                                    return false;
                                }
                            }
                        }, _submit);
                        //如果提交失败则释放锁定
                        if (!submitResult) {
                            that.holdSubmit(false);
                            submitBtn.removeClass("disabled");
                        }
                        return false;
                    }
                }, form.data("validator-options") || {});
                form.validator(options);

                //移除提交按钮的disabled类
                $(".layer-footer [type=submit],.fixed-footer [type=submit],.normal-footer [type=submit]", form).removeClass("disabled");
                //自定义关闭按钮事件
                form.on("click", ".layer-close", function () {
                    if (window.name) {
                        var index = parent.Layer.getFrameIndex(window.name);
                        parent.Layer.close(index);
                    }
                    return false;
                });
            },
            selectpicker: function (form) {
                //绑定select元素事件
                if ($(".selectpicker", form).length > 0) {
                    require(['bootstrap-select', 'bootstrap-select-lang'], function () {
                        $('.selectpicker', form).selectpicker();
                        $(form).on("reset", function () {
                            setTimeout(function () {
                                $('.selectpicker').selectpicker('refresh').trigger("change");
                            }, 1);
                        });
                    });
                }
            },
            selectpage: function (form) {
                //绑定selectpage元素事件
                if ($(".selectpage", form).length > 0) {
                    require(['selectpage'], function () {
                        $('.selectpage', form).selectPage({
                            eAjaxSuccess: function (res) {
                                var data = {};
                                data.list = typeof res.data.data !== 'undefined' ? res.data.data : (typeof res.data.list !== 'undefined' ? res.data.list : []);
                                data.totalRow = typeof res.data.total !== 'undefined' ? res.data.total : (typeof res.data.totalRow !== 'undefined' ? res.data.totalRow : res.data.list.length);
                                return data;
                            }
                        });
                    });
                    //给隐藏的元素添加上validate验证触发事件
                    $(document).on("change", ".sp_hidden", function () {
                        $(this).trigger("validate");
                    });
                    $(document).on("change", ".sp_input", function () {
                        $(this).closest(".sp_container").find(".sp_hidden").trigger("change");
                    });
                    $(form).on("reset", function () {
                        setTimeout(function () {
                            $(".selectpage", form).each(function () {
                                var selectpage = $(this).data("selectPageObject");
                                selectpage.elem.hidden.val($(this).val());
                                $(this).selectPageRefresh();
                            });
                        }, 1);
                    });
                }
            },
            cxselect: function (form) {
                //绑定cxselect元素事件
                if ($("[data-toggle='cxselect']", form).length > 0) {
                    require(['cxselect'], function () {
                        $.cxSelect.defaults.jsonName = 'name';
                        $.cxSelect.defaults.jsonValue = 'value';
                        $.cxSelect.defaults.jsonSpace = 'data';
                        $("[data-toggle='cxselect']", form).cxSelect();
                    });
                }
            },
            datetimepicker: function (form) {
                //绑定日期时间元素事件
                if ($(".datepicker,.datetimepicker", form).length > 0) {
                    require(['bootstrap-datetimepicker'], function () {
                        $(".datepicker,.datetimepicker", form).each(function () {
                            var $input = $(this);
                            var options = {
                                showTodayButton: true,
                                showClose: true,
                                format:$input.data('format') ? $input.data('format') : 'YYYY-MM-DD HH:mm:ss'
                            };
                            $input.datetimepicker(options).on('dp.change', function (e) {
                                $(this, document).trigger("changed");
                            });
                        });
                        $('.datetimepicker', form).parent().css('position', 'relative');
                    });
                }
            },
            daterangepicker: function (form) {
                //绑定日期时间元素事件
                if ($(".datetimerange", form).length > 0) {
                    require(['bootstrap-daterangepicker','moment'], function () {
                        var ranges = {};
                        ranges[__('Today')] = [Moment().startOf('day'), Moment().endOf('day')];
                        ranges[__('Yesterday')] = [Moment().subtract(1, 'days').startOf('day'), Moment().subtract(1, 'days').endOf('day')];
                        ranges[__('Last 7 Days')] = [Moment().subtract(6, 'days').startOf('day'), Moment().endOf('day')];
                        ranges[__('Last 30 Days')] = [Moment().subtract(29, 'days').startOf('day'), Moment().endOf('day')];
                        ranges[__('This Month')] = [Moment().startOf('month'), Moment().endOf('month')];
                        ranges[__('Last Month')] = [Moment().subtract(1, 'month').startOf('month'), Moment().subtract(1, 'month').endOf('month')];
                        var options = {
                            timePicker: false,
                            autoUpdateInput: false,
                            timePickerSeconds: true,
                            timePicker24Hour: true,
                            autoApply: true,
                            locale: {
                                format: 'YYYY-MM-DD HH:mm:ss',
                                customRangeLabel: __("Custom Range"),
                                applyLabel: __("Apply"),
                                cancelLabel: __("Clear"),
                            },
                            ranges: ranges,
                        };
                        var origincallback = function (start, end) {
                            $(this.element).val(start.format(this.locale.format) + " - " + end.format(this.locale.format));
                            $(this.element).trigger('blur');
                        };
                        $(".datetimerange", form).each(function () {
                            var callback = typeof $(this).data('callback') == 'function' ? $(this).data('callback') : origincallback;
                            $(this).on('apply.daterangepicker', function (ev, picker) {
                                callback.call(picker, picker.startDate, picker.endDate);
                            });
                            $(this).on('cancel.daterangepicker', function (ev, picker) {
                                $(this).val('').trigger('blur');
                            });
                            $(this).daterangepicker($.extend(true, options, $(this).data() || {}, $(this).data("daterangepicker-options") || {}));
                        });
                    });
                }
            },
            /**
             * 绑定上传事件
             * @param form
             */
            upload: function (form) {
                //绑定上传元素事件
                if ($(".plupload,.faupload", form).length > 0) {
                    Upload.api.upload($(".plupload,.faupload", form));
                }
            },
            faselect: function (form) {
                //绑定fachoose选择附件事件
                if ($(".faselect,.fachoose", form).length > 0) {
                    $(".faselect,.fachoose", form).off('click').on('click', function () {
                        var that = this;
                        var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
                        var mimetype = $(this).data("mimetype") ? $(this).data("mimetype") : '';
                        var admin_id = $(this).data("admin-id") ? $(this).data("admin-id") : '';
                        var user_id = $(this).data("user-id") ? $(this).data("user-id") : '';
                        mimetype = mimetype.replace(/\/\*/ig, '/');
                        var url = $(this).data("url") ? $(this).data("url") : Config.admin_path+"/files/list";
                        parent.Fast.api.open(url + "?element_id=" + $(this).attr("id") + "&multiple=" + multiple + "&mimetype=" + mimetype + "&admin_id=" + admin_id + "&user_id=" + user_id, __('Choose'), {
                            callback: function (data) {
                                var button = $("#" + $(that).attr("id"));
                                var maxcount = $(button).data("maxcount");
                                var input_id = $(button).data("input-id") ? $(button).data("input-id") : "";
                                maxcount = typeof maxcount !== "undefined" ? maxcount : 0;
                                if (input_id && data.multiple) {
                                    var urlArr = [];
                                    var inputObj = $("#" + input_id);
                                    var value = $.trim(inputObj.val());
                                    if (value !== "") {
                                        urlArr.push(inputObj.val());
                                    }
                                    var nums = value === '' ? 0 : value.split(/\,/).length;
                                    var files = data.url !== "" ? data.url.split(/\,/) : [];
                                    $.each(files, function (i, j) {
                                        var url = Config.fullmode ? Fast.api.cdnurl(j) : j;
                                        urlArr.push(url);
                                    });
                                    if (maxcount > 0) {
                                        var remains = maxcount - nums;
                                        if (files.length > remains) {
                                            Fast.api.error(__('You can choose up to %d file%s', remains));
                                            return false;
                                        }
                                    }
                                    var result = urlArr.join(",");
                                    inputObj.val(result).trigger("change").trigger("validate");
                                } else {
                                    var url = Config.fullmode ? Fast.api.cdnurl(data.url) : data.url;
                                    $("#" + input_id).val(url).trigger("change").trigger("validate");
                                }
                            }
                        });
                        return false;
                    });
                }
            },
            fieldlist: function (form) {
                //绑定fieldlist
                if ($(".fieldlist", form).length > 0) {
                    require(['dragsort', 'template'], function (undefined, Template) {
                        //刷新隐藏textarea的值
                        var refresh = function (container) {
                            var data = {};
                            var name = container.data("name");
                            var textarea = $("textarea[name='" + name + "']", form);
                            var template = container.data("template");
                            $.each($("input,select,textarea", container).serializeArray(), function (i, j) {
                                var reg = /\[(\w+)\]\[(\w+)\]$/g;
                                var match = reg.exec(j.name);
                                if (!match)
                                    return true;
                                match[1] = "x" + parseInt(match[1]);
                                if (typeof data[match[1]] == 'undefined') {
                                    data[match[1]] = {};
                                }
                                data[match[1]][match[2]] = j.value;
                            });
                            var result = template ? [] : {};
                            $.each(data, function (i, j) {
                                if (j) {
                                    if (!template) {
                                        if (j.key != '') {
                                            result[j.key] = j.value;
                                        }
                                    } else {
                                        result.push(j);
                                    }
                                }
                            });
                            textarea.val(JSON.stringify(result));
                        };
                        //追加一行数据
                        var append = function (container, row, initial) {
                            var tagName = container.data("tag") || (container.is("table") ? "tr" : "dd");
                            var index = container.data("index");
                            var name = container.data("name");
                            var template = container.data("template");
                            var data = container.data();
                            index = index ? parseInt(index) : 0;
                            container.data("index", index + 1);
                            row = row ? row : {};
                            row = typeof row.key === 'undefined' || typeof row.value === 'undefined' ? {key: '', value: row} : row;
                            var options = container.data("fieldlist-options") || {};
                            var vars = {index: index, name: name, data: data, options: options, key: row.key, value: row.value, row: row.value};
                            var html = template ? Template(template, vars) : Template.render(Form.config.fieldlisttpl, vars);
                            var obj = $(html);
                            if ((options.deleteBtn === false || options.removeBtn === false) && initial)
                                obj.find(".btn-remove").remove();
                            if (options.dragsortBtn === false && initial)
                                obj.find(".btn-dragsort").remove();
                            if ((options.readonlyKey === true || options.disableKey === true) && initial) {
                                obj.find("input[name$='[key]']").prop("readonly", true);
                            }
                            obj.attr("fieldlist-item", true);
                            obj.insertAfter($(tagName + "[fieldlist-item]", container).length > 0 ? $(tagName + "[fieldlist-item]:last", container) : $(tagName + ":first", container));
                            if ($(".btn-append,.append", container).length > 0) {
                                //兼容旧版本事件
                                $(".btn-append,.append", container).trigger("fa.event.appendfieldlist", obj);
                            } else {
                                //新版本事件
                                container.trigger("fa.event.appendfieldlist", obj);
                            }
                            return obj;
                        };
                        var fieldlist = $(".fieldlist", form);
                        //表单重置
                        form.on("reset", function () {
                            setTimeout(function () {
                                fieldlist.trigger("fa.event.refreshfieldlist");
                            });
                        });
                        //监听文本框改变事件
                        $(document).on('change keyup changed', ".fieldlist input,.fieldlist textarea,.fieldlist select", function () {
                            var container = $(this).closest(".fieldlist");
                            refresh(container);
                        });
                        //追加控制(点击按钮)
                        fieldlist.on("click", ".btn-append,.append", function (e, row) {
                            var container = $(this).closest(".fieldlist");
                            append(container, row);
                            // refresh(container);
                        });
                        //移除控制(点击按钮)
                        fieldlist.on("click", ".btn-remove", function () {
                            var container = $(this).closest(".fieldlist");
                            var tagName = container.data("tag") || (container.is("table") ? "tr" : "dd");
                            $(this).closest(tagName).remove();
                            refresh(container);
                        });
                        //追加控制(通过事件)
                        fieldlist.on("fa.event.appendtofieldlist", function (e, row) {
                            var container = $(this);
                            append(container, row);
                            refresh(container);
                        });
                        //根据textarea内容重新渲染
                        fieldlist.on("fa.event.refreshfieldlist", function () {
                            var container = $(this);
                            var textarea = $("textarea[name='" + container.data("name") + "']", form);
                            //先清空已有的数据
                            $("[fieldlist-item]", container).remove();
                            var json = {};
                            try {
                                json = JSON.parse(textarea.val());
                            } catch (e) {
                            }
                            $.each(json, function (i, j) {
                                append(container, {key: i, value: j}, true);
                            });
                        });
                        //拖拽排序
                        fieldlist.each(function () {
                            var container = $(this);
                            var tagName = container.data("tag") || (container.is("table") ? "tr" : "dd");
                            container.dragsort({
                                itemSelector: tagName,
                                dragSelector: ".btn-dragsort",
                                dragEnd: function () {
                                    refresh(container);
                                },
                                placeHolderTemplate: $("<" + tagName + "/>")
                            });
                            if (typeof container.data("options") === 'object' && container.data("options").appendBtn === false) {
                                $(".btn-append,.append", container).hide();
                            }
                            $("textarea[name='" + container.data("name") + "']", form).on("fa.event.refreshfieldlist", function () {
                                //兼容旧版本事件
                                $(this).closest(".fieldlist").trigger("fa.event.refreshfieldlist");
                            });
                        });
                        fieldlist.trigger("fa.event.refreshfieldlist");
                    });
                }
            },
            switcher: function (form) {
                form.on("click", "[data-role=switcher]", function () {
                    if ($(this).hasClass("disabled"))  {
                        return false;
                    }
                    var switcher = $.proxy(function () {
                        var input = $(this).find(":checkbox");
                        var hidden = $(this).find("[type=hidden]");
                        if (input.length > 0) {
                            var yes = input.data("yes") || 1;
                            var no = input.data("no") || 0;
                            if (input.prop('checked')) {
                                input.prop('checked',false);
                                hidden.val(no);
                            } else {
                                input.prop('checked',true);
                                hidden.val(yes);
                            }
                            input.trigger('change');
                        }
                    }, this);
                    if (typeof $(this).data("confirm") !== 'undefined') {
                        Layer.confirm($(this).data("confirm"), function (index) {
                            switcher();
                            Layer.close(index);
                        });
                    } else {
                        switcher();
                    }
                    return false;
                });
            },
            bindevent: function (form) {

            },
            tagsinput: function (form) {
                if ($("[data-role='tagsinput']", form).length > 0) {
                    require(['tagsinput', 'autocomplete'], function () {
                        $("[data-role='tagsinput']").tagsinput();
                        form.on("reset", function () {
                            setTimeout(function () {
                                $("[data-role='tagsinput']").tagsinput('reset');
                            }, 0);
                        });
                    });
                }
            },
            autocomplete: function (form) {
                if ($("[data-role='autocomplete']", form).length > 0) {
                    require(['autocomplete'], function () {
                        $("[data-role='autocomplete']").autocomplete();
                    });
                }
            },
            favisible: function (form) {
                if ($("[data-favisible]", form).length == 0) {
                    return;
                }
                
                // 获取表单所有字段的值（支持各种输入类型）
                function getFormValues() {
                    const values = {};
                    form.find('input, select, textarea').each(function() {
                        const $field = $(this);
                        const name = $field.attr('name');
                        if (!name) return;
                        
        
                        // 处理多选框（name以[]结尾的情况）
                        if (name.endsWith('[]')) {
                            const baseName = name.slice(0, -2); // 去掉[]
                            
                            // 如果是第一次遇到这个多选框组，初始化数组
                            if (values[baseName] === undefined) {
                                values[baseName] = [];
                            }
                            
                            // 如果当前多选框被选中，添加它的值
                            if ($field.is(':checked')) {
                                values[baseName].push($field.val());
                            }
                            
                            // 跳过后续处理
                            return;
                        }
                        if ($field.is(':checkbox, :radio')) {
                            values[name] = $field.is(':checked') ? $field.val() || true : false;
                        } else {
                            values[name] = $field.val();
                        }
                    });
                    console.log(values);
                    
                    return values;
                }
                
                // 创建安全的沙箱环境
                function createSandbox(formValues) {
                    // 基本数学和逻辑运算
                    const sandbox = {
                        // 表单值
                        ...formValues,
                        
                        // 常用比较函数
                        Math: Math,
                        Array: Array,
                        String: String,
                        Number: Number,
                        Date: Date,
                        
                        // 逻辑运算符
                        and: (a, b) => a && b,
                        or: (a, b) => a || b,
                        not: a => !a,
                        
                        // 类型转换
                        parseInt: parseInt,
                        parseFloat: parseFloat,
                        
                        // 安全限制
                        eval: undefined,
                        Function: undefined,
                        window: undefined,
                        document: undefined,
                        console: undefined
                    };
                    
                    return sandbox;
                }
                
                // 在沙箱中评估表达式
                function evaluateInSandbox(expression, sandbox) {
                    // 使用Function构造函数比eval更安全
                    try {
                        const func = new Function(
                            'sandbox',
                            `with(sandbox) { return ${expression}; }`
                        );
                        
                        // 执行并捕获可能的错误
                        const result = func(sandbox);
                        return !!result; // 确保返回布尔值
                    } catch (e) {
                        console.error(`表达式评估错误: "${expression}"`, e);
                        return false;
                    }
                }
                form.on("keyup change click configchange", "input,select", function () {
                    $("[data-favisible][data-favisible!='']").each(function() {
                        const $element = $(this);
                        const condition = $element.data('favisible');
                        
                        var target = $(this).data("target");
                        if (!target) {
                            target = $(this);
                        } else {
                            target = $(this).parents('.'+target);
                        }
                        try {
                            // 获取当前表单的所有值
                            const formValues = getFormValues();
                            
                            // 创建安全的评估环境
                            const sandbox = createSandbox(formValues);
                            
                            // 评估条件
                            const isVisible = evaluateInSandbox(condition, sandbox);
                            
                            // 显示/隐藏元素
                            target.toggle(isVisible);
                        } catch (e) {
                            console.warn('条件评估错误:', e);
                            target.hide();
                        }
                    });
                });

                //追加上忽略元素
                setTimeout(function () {
                    form.data('validator').options.ignore += ((form.data('validator').options.ignore ? ',' : '') + '[data-favisible] :hidden,[data-favisible]:hidden');
                }, 0);

                $("input,select", form).trigger("configchange");
            },
            editor:function(form){
                if ($("[data-role='editor']", form).length > 0) {
                    require(['froala-editor','froala_lang/zh_cn'], function (FroalaEditor) {
                        var uploadUrl = Config.admin_path+'/files/feupload';
                        new FroalaEditor('[data-role=editor]',{
                            height:500,
                            language: 'zh_cn',
                            // Set the file upload parameter.
                            fileUploadParam: 'file',

                            // Set the file upload URL.
                            fileUploadURL: uploadUrl,

                            // Additional upload params.
                            fileUploadParams: {from: 'editor'},

                            // Set request type.
                            fileUploadMethod: 'POST',

                            // Set max file size to 20MB.
                            fileMaxSize: 20 * 1024 * 1024,

                            // Allow to upload any file.
                            fileAllowedTypes: ['*'],

                            events: {
                                'file.beforeUpload': function (files) {
                                    // Return false if you want to stop the file upload.
                                },
                                'file.uploaded': function (response) {
                                    // File was uploaded to the server.
                                },
                                'file.inserted': function ($file, response) {
                                    // File was inserted in the editor.
                                },
                                'file.error': function (error, response) {
                                    // Bad link.
                                    if (error.code == 1) { }

                                    // No link in upload response.
                                    else if (error.code == 2) {}

                                    // Error during file upload.
                                    else if (error.code == 3) { }

                                    // Parsing response failed.
                                    else if (error.code == 4) { }

                                    // File too text-large.
                                    else if (error.code == 5) {}

                                    // Invalid file type.
                                    else if (error.code == 6) {}

                                    // File can be uploaded only to same domain in IE 8 and IE 9.
                                    else if (error.code == 7) {}

                                    // Response contains the original server response to the request if available.
                                }
                            },
                            
                            // Set the image upload parameter.
                            imageUploadParam: 'file',

                            // Set the image upload URL.
                            imageUploadURL: uploadUrl,

                            // Additional upload params.
                            imageUploadParams: {from: 'editor'},

                            // Set request type.
                            imageUploadMethod: 'POST',

                            // Set max image size to 5MB.
                            imageMaxSize: 5 * 1024 * 1024,

                            // Allow to upload PNG and JPG.
                            imageAllowedTypes: ['jpeg', 'jpg', 'png'],

                            events: {
                                'image.beforeUpload': function (images) {
                                    // Return false if you want to stop the image upload.
                                },
                                'image.uploaded': function (response) {
                                    // Image was uploaded to the server.
                                },
                                'image.inserted': function ($img, response) {
                                    // Image was inserted in the editor.
                                },
                                'image.replaced': function ($img, response) {
                                    // Image was replaced in the editor.
                                },
                                'image.error': function (error, response) {
                                    // Bad link.
                                    if (error.code == 1) {  }

                                    // No link in upload response.
                                    else if (error.code == 2) { }

                                    // Error during image upload.
                                    else if (error.code == 3) { }

                                    // Parsing response failed.
                                    else if (error.code == 4) {}

                                    // Image too text-large.
                                    else if (error.code == 5) { }

                                    // Invalid image type.
                                    else if (error.code == 6) {  }

                                    // Image can be uploaded only to same domain in IE 8 and IE 9.
                                    else if (error.code == 7) { }

                                    // Response contains the original server response to the request if available.
                                }
                            },
                            
                            // Set a preloader.
                            imageManagerPreloader: "/images/loader.gif",

                            // Set page size.
                            imageManagerPageSize: 20,

                            // Set a scroll offset (value in pixels).
                            imageManagerScrollOffset: 10,

                            // Set the load images request URL.
                            imageManagerLoadURL: Config.admin_path+"/files/list",

                            // Set the load images request type.
                            imageManagerLoadMethod: "GET",

                            // Additional load params.
                            imageManagerLoadParams: {user_id: 4219762},

                            // Set the delete image request URL.
                            imageManagerDeleteURL: Config.admin_path+"/files/delete",

                            // Set the delete image request type.
                            imageManagerDeleteMethod: "DELETE",

                            // Additional delete params.
                            imageManagerDeleteParams: {param: 'value'},
                        });
                    });
                }
            }
        },
        api: {
            submit: function (form, success, error, submit) {
                if (form.length === 0) {
                    Fast.api.error("表单未初始化完成,无法提交");
                    return false;
                }
                if (typeof submit === 'function') {
                    if (false === submit.call(form, success, error)) {
                        return false;
                    }
                }
                var type = form.attr("method") ? form.attr("method").toUpperCase() : 'GET';
                type = type && (type === 'GET' || type === 'POST') ? type : 'GET';
                url = form.attr("action");
                url = url ? url : location.href;
                //修复当存在多选项元素时提交的BUG
                var params = {};
                var multipleList = $("[name$='[]']", form);
                if (multipleList.length > 0) {
                    var postFields = form.serializeArray().map(function (obj) {
                        return $(obj).prop("name");
                    });
                    $.each(multipleList, function (i, j) {
                        if (postFields.indexOf($(this).prop("name")) < 0) {
                            params[$(this).prop("name")] = '';
                        }
                    });
                }
                //调用Ajax请求方法
                Fast.api.ajax({
                    type: type,
                    url: url,
                    data: form.serialize() + (Object.keys(params).length > 0 ? '&' + $.param(params) : ''),
                    dataType: 'json',
                    complete: function (xhr) {
                        var token = xhr.getResponseHeader('__token__');
                        if (token) {
                            $("input[name='__token__']").val(token);
                        }
                    }
                }, function (data, ret) {
                    $('.form-group', form).removeClass('has-feedback has-success has-error');
                    if (data && typeof data === 'object') {
                        //刷新客户端token
                        if (typeof data.token !== 'undefined') {
                            $("input[name='__token__']").val(data.token);
                        }
                        //调用客户端事件
                        if (typeof data.callback !== 'undefined' && typeof data.callback === 'function') {
                            data.callback.call(form, data);
                        }
                    }
                    if (typeof success === 'function') {
                        if (false === success.call(form, data, ret)) {
                            return false;
                        }
                    }
                }, function (data, ret) {
                    if (data && typeof data === 'object' && typeof data.token !== 'undefined') {
                        $("input[name='__token__']").val(data.token);
                    }
                    if (typeof error === 'function') {
                        if (false === error.call(form, data, ret)) {
                            return false;
                        }
                    }
                });
                return true;
            },
            bindevent: function (form, success, error, submit) {

                form = typeof form === 'object' ? form : $(form);

                var events = Form.events;

                events.bindevent(form);

                events.validator(form, success, error, submit);

                events.selectpicker(form);

                events.daterangepicker(form);

                events.selectpage(form);

                events.cxselect(form);

                events.datetimepicker(form);

                events.upload(form);

                events.faselect(form);

                events.fieldlist(form);

                events.switcher(form);

                //events.tagsinput(form);

                events.autocomplete(form);

                events.favisible(form);

                events.editor(form);
            },
            custom: {} 
        },
    };
    return Form;
});
