(function ($, B) {

    // 选中模式
    var g_SelectMode = {
        // 单选
        SINGLE: "single",
        // 多选
        MULTIPE: "multiple"
    };

    var g_FilterArgmentName = {
        // 用户过滤器
        excludedUsers: "excludedUsers",
        // 组织机构过滤器
        excludedOrgs: "excludedOrgs"
    };

    var platform = "";
    var useWebSolution = true;
    BM.ready(function () {
        BM.appointment.driver.getLaster(function (driver) {
            platform = driver.platform;
            useWebSolution = platform != "android" && platform != "ios";
        })
    });

    /**
      * 对象选择器命名空间
      * @module Beyondbit.OPicker
	  * @submodule OPicker
      *    
      */
    var opicker = B.registerNameSpace("OPicker");

    opicker.regional = [];

    opicker.regional[""] = opicker.regional["zh-CN"] = {
        service: {
            notAllowRegisterCodeEmpty: "注册Code不能为空",
            repeatRegistered: "重复注册服务标识:{0}",
            useNotRegisteredCode: "使用无注册服务标识:{0}"
        },
        title: "对象选择器",
        notSetObjectCode: "没有填写选择对象Codes",
        defaultNamesLengthNotEqualValuesLength: "默认值的name和value的长度不一致!",
        buttons: {
            ok: "确定",
            clear: "清除",
            cancel: "取消"
        },
        search: {
            title: "搜索",
            placeholder: "输入关键字检索"
        },
        select: {
            selected: "已选择",
            up: "向上",
            down: "向下"
        }
    }

    var OPICKER_DATA_KEY = "Opicker_data";

    function getRegional() {
        return opicker.regional[B.CultureInfo.Current.get()];
    }

    /**
     * @class Opicker
     * 
     */

    /**
	  * 选择
	  * @method select
	  * @param {json} options 选项请参考属性值
	  * @static
	  * @example
	  *     
	  *     <html>
	  *          <div>
	  *               <button class="btn" type="button" id="btnSelectUser">
	  *                   <i class="fa fa-male"></i>
	  *                   选择用户
	  *               </button>
	  *
	  *               <input type="text" name="name" value="" id="txtName" />
	  *               <input type="text" name="name" value="" id="txtValue" />
	  *           </div>
	  *
	  *
	  *           <div>
	  *               <button class="btn" type="button" id="btnSelectUser2">
	  *                   <i class="fa fa-male"></i>
	  *                   选择用户2
	  *               </button>
	  *
	  *               <input type="text" name="name" value="" id="txtName2" />
	  *               <input type="text" name="name" value="" id="txtValue2" />
	  *           </div>
	  *     </html>
	  * 
	  *     <script>
	  *          $(function () {
	  *               $("#btnSelectUser2").click(function () {
	  *                   B.OPicker.select({
	  *                       title: "<i class='fa fa-male'></i> 选择用户",
	  *                       objectCodes: ["orguser"],
	  *                       associatedNameControl: $("#txtName2"),
	  *                       associatedValueControl: $("#txtValue2")
	  *                   });
	  *               });
	  *
	  *
	  *               $("#btnSelectUser").click(function () {
	  *                   B.OPicker.select({
	  *                       title: "<i class='fa fa-male'></i> 选择用户",
	  *                       objectCodes: ["orguser", "roleuser"],
	  *                       defaultValue: {
	  *                           name: $("#txtName").val(),
	  *                           value: $("#txtValue").val()
	  *                       },
	  *                       onSelectedValue: function (data) {
	  *                           $("#txtName").val(data.name);
	  *                           $("#txtValue").val(data.value);
	  *                       }
	  *                   });
	  *               });
	  *           })
	  *     </script>
	  */
    opicker.select = function (options) {
        if (useWebSolution) {
            opicker.init(options);
        }
        else {
            openPopupOPicker(options);
        }
    };


    /*
     * 
     */
    var OPICKER_CONFIG = {
        registerServiceUrls: []
    };

    /**
     * 对象选择器全局配置
     * @method config
     * @param {json} options 配置
     */
    opicker.config = function (options) {
        var options = $.extend(true, {}, {
            registerServiceUrls: []
        }, options);

        if (options.registerServiceUrls.length > 0) {
            OPICKER_CONFIG.registerServiceUrls = OPICKER_CONFIG.registerServiceUrls.concat(options.registerServiceUrls)
        }
    };
     
    opicker.init = function (options) {
        new OPicker(options);
    }

    function openPopupOPicker(options) {
        getDefaultSelect(options);

        var remoteOptions = getRemoteOptions(options);
        remoteOptions.registerServiceUrls = remoteOptions.registerServiceUrls || OPICKER_CONFIG.registerServiceUrls;

        var popupOptions = {
            url: "/Select/OPicker",
            title: options.title,
            data: remoteOptions,
            isNeedProgressBar: false,
            onClose: function (result) {
                if (result) {
                    onOpickerOkHandle(options, result);
                }
            }
        };
        BM.appointment.webview.popup(popupOptions);
    }

    /**
     * 格式化字符串到选择数据数组
     */
    opicker.formatData = function (textString, valueString, splitChar) {
        splitChar = splitChar || ',';
        var result = new Array();
        if (textString && valueString) {
            var textArray = textString.split(splitChar);
            var valueArray = valueString.split(splitChar);
            for (var i = 0; i < textArray.length; i++) {
                result.push({
                    data: {
                        name: textArray[i],
                        value: valueArray[i]
                    }
                });
            }
        }
        return result;
    }

    // #region 服务

    // 保存所有注册的services
    var _services = {};

    /**
      * 注册服务
      * @method registerService
      * @static
      * @param {String} code 服务标识,不能注册重复标识,重复抛异常
      * @param {String} name 显示名称
      * @param {String} serviceUrl 服务地址
      * @param {Fun} [dataFilter ] 调用成功后,过滤数据
      * @example
      *
      *    B.OPicker.registerService("user", http://service.beyondbit.com/user/get");
      *    
      */

    opicker.registerService = function (code, title, serviceUrl, dataFilter) {
        var serviceCode = code;
        if (!arguments[0]) {
            throw Error(getRegional().service.notAllowRegisterCodeEmpty);
        }
        var service;
        var firstArgumentType = $.type(arguments[0]);
        if (firstArgumentType === "string") {
            service = {
                code: serviceCode,
                title: title,
                url: serviceUrl
            };
        } else if ($.isFunction(arguments[0])) {
            //TODO: 延迟加载注册功能
        }
        else {
            service = arguments[0];
            serviceCode = service.code;
        }

        if (_services[serviceCode]) {
            throw Error(getRegional().service.repeatRegistered.bformat(serviceCode));
        }


        _services[serviceCode] = $.extend(true, {},
            {
                /*
                 * 数据过滤事件
                 * @event dataFilter 
                 * @param {object} 过滤的数据项
                 */
                dataFilter: function (item) {
                    return item;
                },
                /*
                 * 获取静态数据数据源
                 * @event getStaticDataSource
                 */
                getStaticDataSource: function () { return null; },
                getSearchUrl: function (config) {
                    if (this.searchUrl) {
                        return this.getUrl(config, this.searchUrl);
                    }
                },
                /*
                 * 获取服务Url事件
                 * @event getUrl
                 * @param {json} config
                 * @param {string} url[default=this.url] 要包装的Url默认为当前url属性
                 */
                getUrl: function (config, url) {
                    return url || this.url;
                },
                /*
                 * 是否显示图标
                 * @event showIcon
                 * @param {object} 数据项
                 */
                showIcon: function (item) {
                    return true;
                },
                nodeDataConvert: function (data) {
                    return {
                        id: data.id,
                        text: data.name,
                        value: data.value,
                        opened: data.open || false,
                        canSelected: !data.nocheck,
                        hasChildren: data.isParent,
                        childrens: data.nodes,
                        data: data
                    };
                },
                convert: function (data) {
                    if ($.isArray(data)) {
                        var list = new Array();
                        for (var i = 0; i < data.length; i++) {
                            list.push(this.nodeDataConvert(data[i]));
                        }
                        return list;
                    }
                    else {
                        return [this.nodeDataConvert(data)];
                    }
                },
                setParam: function (data, container) {
                    container.code = data.value;
                }
            },
            service
         );
    };

    /**
      * 获取一个服务
      * @static
      * @private
      * @method getService
      * @param {String} code 服务标识,标识未注册抛异常
      * @example
      *
      *    B.OPicker.getService("user");
      *    
      */

    opicker.getService = function (code) {
        var service = _services[code];
        if (!service) {
            throw Error(getRegional().service.useNotRegisteredCode.bformat(code));
        }
        return service;
    }

    // #endregion

    var getDefaultOptions = function () {
        return {
            id: "opicker",
            /**
              * 选择器标题
              * @property title
              * @type String
              * @default "对象选择器"
              */
            title: getRegional().title,
            /**
             * 选择对象Code集合, 目前只注册orguser,roleuser
             * @property objectCodes
             * @type Array(String)
             * @default [];
             * @requires
             */
            objectCodes: [],

            objects: [],
            /**
              * 多选值的分隔符
              * @property splitChar
              * @type {String}
              * @default ","
              */
            splitChar: ",",
            /**
              * 选择模式,单选还是多选. 0为多选, 1为单选
              * @property selectMode
              * @type Number
              * @default 0
              */
            selectMode: 0,
            /**
             * 默认值 .格式{name:"dd,dd1", value:"22,33"};
             * @property defaultValue 
             * @type {json}
             */
            defaultValue: null,
            /**
             * 联动名称控件,提供默认选中文本和选中完成后放置文本的控件
             * @property associatedNameControl
             * @type jQuery
             */
            associatedNameControl: null,
            /**
             * 联动值控件,提供默认选中值和选中完成后放置值的控件
             * @property associatedValueControl
             * @type jQuery
             */
            associatedValueControl: null,
            /**
              * 选择完成事件
              * @event onSelectedValue
              * @param {Json} datas 返回选择的数据对象
              * @param {String} datas.name 名称
              * @param {String} datas.value 值
              * @param {Array[String]} datas.names 名称数组
              * @param {Array[String]} datas.values 值数组
              * @param {Array[json]} datas.nodes 完整数据对象数组
              */
            onSelectedValue: null,
            /**
			  * 关闭选择事件
			  * @event onClose
			  */
            onClose: null,
            /**
			  * 用户过滤器
			  * @param String 需要过滤的UserUID字符串(以,分割)
			  */
            excludedUsers: null,
            /**
			  * 组织机构过滤器
			  * @param String 需要过滤的OrgCode字符串(以,分割)
			  */
            excludedOrgs: null
        }
    }

    var getRemoteOptions = function (options) {
        var remoteOptions = jQuery.extend(true, {}, options);
        remoteOptions.associatedNameControl = null;
        remoteOptions.associatedValueControl = null;
        return remoteOptions;
    }

    var getDefaultSelect = function (options) {
        if (!options.defaultSelect) {
            var defaultName, defaultValue;
            if (options.defaultValue) {
                defaultName = options.defaultValue.name,
                defaultValue = options.defaultValue.value
            }
            else {
                if (options.associatedValueControl) {
                    defaultValue = options.associatedValueControl.val();
                }
                if (options.associatedNameControl) {
                    defaultName = options.associatedNameControl.val();
                }
            }

            options.defaultSelect = convertSelectedDataToInnerData(opicker.formatData(defaultName, defaultValue, options.splitChar));
        }
        return options.defaultSelect;
    }

    function OPicker(options) {
        options = $.extend(true, getDefaultOptions(), options);

        var renderHtml = function (options) {
            var titleHTML = "";
            var bodyClass = "";
            if (useWebSolution) {
                titleHTML = '<div class="TreeList-head">\
                            <div class="TreeList-btn-close"><i class="fc fc-close"></i></div>\
                            <h2 class="TreeList-title"></h2>\
                            <div class="TreeList-btn-ok">\
                                <span class="TreeList-btn-ok-text">确定</span>\
                                (<span class="TreeList-btn-ok-total">0</span>)\
                            </div>\
                        </div>';
            }
            else {
                bodyClass = "TreeList-nohead";
            }
            return $('<div id="{0}" style="z-index:{2};position:relative">\
                    <div class="TreeList-container">\
                        {3}\
                        <div class="TreeList-body {1}"></div>\
                        <div class="TreeList-selected">\
                            <div class="pull-top">\
                                <i class="fc fc-direction-up"></i>\
                            </div>\
                            <div class="TreeList-selected-container">\
                                <ul></ul>\
                            </div>\
                        </div>\
                     </div>\
                  </div>'.bformat(options.id, bodyClass, BM.Web.get_zIndex(), titleHTML));
        }

        var setTitle = function (title) {
            if (useWebSolution) {
                $(".TreeList-title", container).html(title);
            }
            else {
                BM.appointment.webview.setTitle(title);
            }
        }

        var addSelected = function (data, single) {
            var selectedContainer = $(".TreeList-selected-container > ul", container);
            if (single) {
                selectedContainer.children().each(function () {
                    var li = $(this);
                    var itemData = { text: li.attr("text"), value: li.attr("value") };
                    removeSelected(itemData);
                });
            }
            var html = $('<li text="{0}" value={1}>{0}</li>'.bformat(data.text, data.value));
            html.click(function () {
                var item = $(this);
                var data = { text: item.attr("text"), value: item.attr("value") };
                removeSelected(data);
            });
            html.data("node", data);
            selectedContainer.append(html);
            updateSelectedTotal();

            if ($(".TreeList-selected", container).is(".show-all")) {
                selectedContainer.scrollTop(selectedContainer[0].scrollHeight);
            }
            else {
                selectedContainer.scrollLeft(selectedContainer[0].scrollWidth);
            }
        }

        var removeSelected = function (data) {
            var checkbox = $(".node-check[text='{0}'][value='{1}']".bformat(data.text, data.value), container);
            toggleCheckbox(checkbox, false);
            $('.TreeList-selected-container > ul > li[text="{0}"][value="{1}"]'.bformat(data.text, data.value), container).remove();
            updateSelectedTotal();
        }

        var updateSelectedTotal = function () {
            var selectedList = $(".TreeList-selected-container > ul > li", container);
            if (useWebSolution) {
                $(".TreeList-btn-ok-total", container).html(selectedList.length);
            }
            else {
                BM.appointment.webview.changeTextToolbar("TreeList-btn-ok", "确定({0})".bformat(selectedList.length));
            }
        }

        var bindShowAllResult = function () {
            var selectedContainer = $(".TreeList-selected", container);
            var hammer = new Hammer(selectedContainer[0]);
            hammer.get('swipe').set({ direction: Hammer.DIRECTION_ALL });
            hammer.on("swipeup swipedown", function (ev) {
                if (ev.type == "swipeup") {
                    showSelectedInFullSreen.call(selectedContainer);
                }
                else {
                    showSelectedInNormal.call(selectedContainer);
                }
            });
            $(".TreeList-selected > .pull-top", container).click(function () {
                var that = $(this);
                var TreeListSelected = that.parent();
                if ($("> i ", that).is(".fc-direction-up")) {
                    showSelectedInFullSreen.call(TreeListSelected);
                }
                else {
                    showSelectedInNormal.call(TreeListSelected);
                }
            });

            function showSelectedInFullSreen() {
                var that = $(this);
                that.addClass("show-all");
                $(".pull-top > i", that).removeClass("fc-direction-up").addClass("fc-direction-down");
                var height = $(window).height() - 70;
                that.animate({ height: height }, 300);
            }
            function showSelectedInNormal() {
                var that = $(this);
                that.removeClass("show-all");
                $(".pull-top > i", that).removeClass("fc-direction-down").addClass("fc-direction-up");
                that.animate({ height: 54 }, 300);
            }
        }

        var bindEvent = function () {
            if (useWebSolution) {
                bindHTMLButtonEvent()
            }
            else {
                bindAppButtonEvent();
            }
            bindShowAllResult();
        }

        var bindHTMLButtonEvent = function () {
            $(".TreeList-btn-ok", container).click(function () {
                onOpickerOkHandle(options, getOKResult());
                that.empty();
                that.hide();
            });
            $(".TreeList-btn-close", container).click(function () {
                that.empty();
                that.hide();
            });
        }

        var bindAppButtonEvent = function () {
            BM.appointment.webview.addTextToolbar("TreeList-btn-ok", "确定(0)", function () {
                BM.appointment.webview.closePopup(getOKResult());
            })

        }

        var toggleCheckbox = function (checkbox, checked) {
            if (checked) {
                checkbox.removeClass("fc-circle").addClass("fc-circle-right-filled").addClass("blue");
            }
            else {
                checkbox.removeClass("fc-circle-right-filled").removeClass("blue").addClass("fc-circle");
            }
        }

        var getSelectedData = function () {
            var selectedList = $(".TreeList-selected-container > ul > li", container);
            var result = selectedList.map(function () {
                return {
                    text: $(this).attr("text"),
                    value: $(this).attr("value")
                };
            }).toArray();
            return result;
        }

        var getOKResult = function () {
            var selectedList = $(".TreeList-selected-container > ul > li", container);
            var names = selectedList.map(function () {
                return $(this).attr("text");
            }).toArray();
            var values = selectedList.map(function () {
                return $(this).attr("value");
            }).toArray();
            var nodeData = selectedList.map(function () {
                return $(this).data("node").data;
            }).toArray();
            return {
                name: names.join(options.splitChar),
                value: values.join(options.splitChar),
                names: names,
                values: values,
                nodes: nodeData
            };
        }

        var that = this;

        var defaultSelect = getDefaultSelect(options);

        options.objects = options.objects || [];
        var objects = options.objects;
        if (options.objectCodes) {
            $.each(options.objectCodes, function (i, code) {
                objects.push({ code: code });
            });
        }

        if (objects.length == 0) {
            throw new Error(getRegional().notSetObjectCode);
        }
        var single = options.selectMode == 1;
        var services = opicker.getService(options.objects[0].code);
        var serviceObject = options.objects[0];
        serviceObject.config = $.extend(true, {}, services.config, serviceObject.config);
        var dataSource = convertZTreeDataToData(services.getStaticDataSource(serviceObject.config, serviceObject));

        var container = $("#" + options.id);
        if (container.length <= 0) {
            container = renderHtml(options);
            $("body").append(container);
            bindEvent();
        }
        setTitle(options.title);

        that.hide = function () {
            container.remove();
        }

        that.empty = function () {
            $(".TreeList-body", container).empty();
            $(".TreeList-selected-container > ul", container).empty();
            updateSelectedTotal();
        }

        that.setTitle = function (title) {
            setTitle(title);
        };

        //初始化选中数据
        for (var i = 0; i < defaultSelect.length; i++) {
            addSelected(defaultSelect[i], single);
        }


        var treeOptions = {
            target: $(".TreeList-body", container),
            url: services.getUrl(serviceObject.config, services.url),//获取数据的URL
            data: {},//获取数据时的参数
            dataType: "jsonp",
            rootCode: services.config.rootOrgCode,
            source: dataSource,
            event: {
                onLoadData: function (data, optionData) {
                    var result = {};
                    result[B.Tenant.ACCESS_TOKEN_IDENTITY] = B.Tenant.getCurrentAccessToken();
                    if (options.excludedUsers) {
                        result[g_FilterArgmentName.excludedUsers] = options.excludedUsers;
                    }
                    if (options.excludedOrgs) {
                        result[g_FilterArgmentName.excludedOrgs] = options.excludedOrgs;
                    }
                    if (data) {
                        services.setParam(data, result);
                    }
                    return result
                },
                onBindItem: function (item) {
                    var newItem = services.dataFilter(item, serviceObject.config);
                    return services.nodeDataConvert(newItem);
                },                
                onAppendNodeComplete: function (itemHtml, data) {
                    if (!data.canSelected) {
                        return;
                    }
                    var inSelectedData = false;
                    var selectedData = getSelectedData();
                    for (var i = 0; i < selectedData.length; i++) {
                        var item = selectedData[i];
                        if (item.text == data.text && item.value == data.value) {
                            inSelectedData = true;
                            break;
                        }
                    }
                    if (inSelectedData) {
                        toggleCheckbox($(".node-check", itemHtml.eq(0)), true);
                    }
                },
                onItemClick: function (data, tree) {
                    if (data.hasChildren && !data.canSelected) {
                        BM.Controls.Tree.toggleNode.call(tree, data);
                        return;
                    }
                    var checkbox = $(".node-check", this);
                    if (checkbox.length < 0) {
                        return;
                    }

                    var checked = checkbox.is(".fc-circle");
                    toggleCheckbox(checkbox, checked);
                    if (checked) {
                        addSelected(data, single);
                    }
                    else {
                        removeSelected(data);
                    }
                }
            },
            handle: {
                renderItemHtml: function (data) {
                    var checkHtml = "";
                    if (data.canSelected) {
                        checkHtml = '<span class="pull-right">\
                                            <i class="node-check fc fc-circle" text="{0}" value="{1}"></i>\
                                    </span>'.bformat(data.text, data.value);
                    }
                    return '<h4>\
                                <span class="text-ellipsis span-container">{0}</span>{1}\
                            </h4>'.bformat(data.text, checkHtml);
                }
            }
        };
        BM.Controls.Tree.create(treeOptions).show();
        container.show();
    }

    var onOpickerOkHandle = function (options, result) {
        if (options.associatedNameControl) {
            options.associatedNameControl.val(result.name);
        }
        if (options.associatedValueControl) {
            options.associatedValueControl.val(result.value);
        }
        if (options.onSelectedValue) {
            options.onSelectedValue(result);
        }
    }

    var convertSelectedDataToInnerData = function (selectedData) {
        var result = new Array();
        for (var i = 0; i < selectedData.length; i++) {
            result.push({
                text: selectedData[i].data.name,
                value: selectedData[i].data.value
            });
        }
        return result;
    }

    function convertZTreeDataToData(ztreeData) {
        var data = [];
        if (!ztreeData) {
            return data;
        }
        
        $.each(ztreeData, function (index, item) {
            var newItem = convertZTreeDataItemToDataItem(item);
            var parentId = item.parentId;
            var parentData = findParentData(parentId, data);
            if (parentData) {
                parentData.isParent = true;
                parentData.nodes.push(newItem);
            } else {
                data.push(newItem);
            }
        });

        return data;
    };

    function convertZTreeDataItemToDataItem(item) {
        item.nodes = [];
        return item;
    };

    function findParentData(parentId, data) {
        var resultData = null;

        $.each(data, function (index, item) {
            if (item.id == parentId) {
                resultData = item;
            }
            else {
                if (item.nodes && item.nodes.length > 0) {
                    resultData = findParentData(parentId, item.nodes);
                }
            }

            if (resultData) {
                return false
            }
        });

        return resultData;
    }

})(jQuery, BM);

(function ($, B) {


    var _defaultServiceHost = "http://192.168.40.106/SharedService";
    // _defaultServiceHost = "http://localhost:2109";

    function getEntireServiceUrl(url) {
        if (url.indexOf("http://") == 0) {
            return url;
        }
        return (window.SharedServiceHost || _defaultServiceHost) + url;
    }


    var opicker = B.registerNameSpace("OPicker");

    opicker.regional["zh-CN"].services = {
        orgUser: {
            title: "按部门"
        },
        roleUser: {
            title: "按角色"
        },
        org: {
            title: "部门"
        },
        role: {
            title: "角色"
        },
        source: {
            title: "选择"
        },
        orgDeputy: {
            title: "选择机构代表",
            notConfigHierarchyCode: "没有配置hierarchyCode参数"
        }
    };

    function getRegional() {
        return opicker.regional[B.CultureInfo.Current.get()];
    }

    // #region 注册系统默认提供服务

    /**
     * 按部门选择用户服务
     * @class OrgUserService
     * 
     */
    opicker.registerService({
        code: "orguser",
        /**
		  * 标题
		  * @property title 
		  * @type String
		  * @default "按部门"
		  */
        title: getRegional().services.orgUser.title,
        /**
         * 配置
         * @property config
         * @type json
         */
        config: {
            /**
             * 是否显示根元素
             * @property config.isShowRoot
             * @type Boolean
             * @default false
             */
            isShowRoot: false,
            /**
             * 根部门Code
             * @property config.rootOrgCode
             * @type String
             */
            rootOrgCode: null
        },
        searchUrl: getEntireServiceUrl("/api/OrgAndUserQuery"),
        url: getEntireServiceUrl("/api/OrgAndUser"),
        dataFilter: function (item, config) {
            if (config.isShowRoot == true) {
                item.open = true;
            }
            if (item.isParent || (item.nodes != null && item.nodes.length > 0)) {
                item.nocheck = true;
            };
            return item;
        },
        getUrl: function (config, url) {
            var url = new B.Url(url || this.url);

            if (config.rootOrgCode) {
                url.appendKey("rootCode", config.rootOrgCode);
            }
            if (config.isShowRoot == true) {
                url.appendKey("isIncludeRoot", config.isShowRoot);
            }

            return url.toString();
        }
    });

    /**
     * 按角色选择用户服务
     * @class RoleUserService
     * 
     */
    opicker.registerService({
        code: "roleuser",
        /**
		  * 标题
		  * @property title 
		  * @type String
		  * @default "按角色"
		  */
        title: getRegional().services.roleUser.title,
        /**
         * 配置
         * @property config
         * @type json
         */
        config: {
            /*
             * 是否显示根元素
             * @property config.isShowRoot
             * @type Boolean
             * @default false
             */
            isShowRoot: false,
            /**
             * 指定的角色Code
             * @property config.code
             * @type String
             */
            code: null,
            /**
             * 指定的App Code
             * @property config.appCode
             * @type String
             */
            appCode: null
        },
        url: getEntireServiceUrl("/api/RoleAndUser"),
        dataFilter: function (item, config) {
            if (item.isParent || (item.nodes != null && item.nodes.length > 0)) {
                item.nocheck = true;
            };
            return item;
        },
        getUrl: function (config, url) {
            var url = new B.Url(url || this.url);
            if (config.code) {
                url.appendKey("code", config.code);
            }
            if (config.appCode) {
                url.appendKey("appCode", config.appCode);
            }
            return url.toString();
        }
    });

    /**
     * 选择部门服务
     * @class OrgService
     * 
     */
    opicker.registerService({
        code: "org",
        /**
        * 标题
        * @property title 
        * @type String
        * @default "部门"
        */
        title: getRegional().services.org.title,
        /**
         * 配置
         * @property config
         * @type json
         */
        config: {
            /*
             * 是否显示根元素
             * @property config.isShowRoot
             * @type Boolean
             * @default false
             */
            isShowRoot: false,
            /**
             * 根部门Code
             * @property config.rootOrgCode
             * @type String
             */
            rootOrgCode: null
        },
        url: getEntireServiceUrl("/api/Org"),
        showIcon: function (item) {
            return false;
        },
        dataFilter: function (item, config) {
            if (config.isShowRoot == true) {
                item.open = true;
            }
            return item;
        },
        getUrl: function (config, url) {
            var url = new B.Url(url || this.url);

            if (config.rootOrgCode) {
                url.appendKey("rootCode", config.rootOrgCode);
            }
            if (config.isShowRoot == true) {
                url.appendKey("isIncludeRoot", config.isShowRoot);
            }

            return url.toString();
        }
    });


    /**
     * 选择角色服务
     * @class RoleService
     * 
     */
    opicker.registerService({
        code: "role",
        /**
          * 标题
          * @property title 
          * @type String
          * @default "按角色"
          */
        title: getRegional().services.role.title,
        /**
         * 配置
         * @property config
         * @type json
         */
        config: {
            /**
             * 指定的App Code
             * @property config.appCode
             * @type String
             */
            appCode: null
        },
        url: getEntireServiceUrl("/api/Role"),
        getUrl: function (config, url) {
            var url = new B.Url(url || this.url);

            if (config.appCode) {
                url.appendKey("appCode", config.appCode);
            }

            return url.toString();
        }
    });

    /**
	  * 自定义数据源选择服务
	  * @class SourceService
	  * 
	  */
    opicker.registerService({
        code: "source",
        title: getRegional().services.source.title,
        /**
          * 数据源
          * @property source
          * @type Array[Node]
          * @default []
          */
        source: [],
        /*
          * 配置
          * @property config
          * @type json
          */
        config: {

        },
        getStaticDataSource: function (config, object) {
            return object.source;
        }
    });

    // #endregion


    // #region 机构代表服务

    opicker.registerService({
        code: "orgdeputy",
        title: getRegional().services.orgDeputy.title,
        url: getEntireServiceUrl("/OrgDeputy/Org"),
        config: {
            hierarchyCode: null
        },
        showIcon: function (item) {
            return false;
        },
        dataFilter: function (item, config) {
            if (config.isShowRoot == true) {
                item.open = true;
            }
            return item;
        },
        getUrl: function (config) {
            var url = new B.Url(this.url);

            if (!config.hierarchyCode) {
                throw Error(getRegional().services.orgDeputy.notConfigHierarchyCode);
            }
            url.appendKey("hierarchyCode", config.hierarchyCode);

            if (config.rootOrgCode) {
                url.appendKey("rootCode", config.rootOrgCode);
            }
            if (config.isShowRoot == true) {
                url.appendKey("isIncludeRoot", config.isShowRoot);
            }

            return url.toString();
        }
    })

    // #endregion

})(jQuery, BM);
    
Top