<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script>
    var rsResoureManege = {

        $searchresourceName: $('#inp_search'),//资源名称文本匡对象
        $catalogDDL: $('#ipt_catalog'),
        $statusDDL: $('#ipt_status'),
        $searchBtn: $('#btn_search'),//搜索按钮
        $addBtn: $('#btn_add'),//新增按钮
        $resourcelayout: $("#div_wrapper"),//layout对象
        $searchTreeBox: $("#div_wrapper_left_inp_search"),//树上面的搜索框
        $resourceTree: $("#div_wrapper_left_ul_resourcetree"),//树对象
        appInfoDialog: null,
        $resourceTreeId:null,
        grid: null,
        init: function () {
            this.$addBtn.click(function () {
                rsResoureManege.editor("", "new");
            });
            //初始化layout
            this.$resourcelayout.ligerLayout({
                height: "99%",
                leftWidth: 200,
                isLeftCollapse: false,//左边区域初始化不可以隐藏
                allowLeftCollapse: false//左边区域不可以隐藏
            });
            //隐藏左边的标题
            $("#div_wrapper .l-layout-left .l-layout-header").hide();
            //l-layout-left
            $("#div_wrapper .l-layout-left").css({"border": "0px"});
            $("#div_wrapper .l-layout-right").css({"border": "0px"});
            $("#div_wrapper .l-layout-center").css({"border": "0px"});
            $("#div_wrapper .l-layout-top").css({"border": "0px"});
            //初始化树
            var resourceTree = this.$resourceTree;
            $.ajax({
                type: "POST",
                url: "${contextRoot}/resource/resourceTreeList",
                success: function (msg) {
                    // 初始化树形菜单
                    resourceTree = resourceTree.ligerTree({
                        data: msg.data,
                        idFieldName: 'id',
                        nodeWidth: 200,
                        parentIDFieldName: 'pid',
                        isExpand: false,
                        onClick: function (obj) {
                            rsResoureManege.reloadGridTree(obj.data.id, rsResoureManege.$searchresourceName.val());
                            rsResoureManege.$resourceTreeId=obj.data.id;
                        },
                        onSuccess: function (data) {

                        }
                    });
                }
            });

            //初始化树上面的搜索框
            this.$searchTreeBox.ligerSearch({
                width: 180,onClick: function (value) {
                    if (value != "" && value != null && value != undefined && value.length > 0)
                        $.ajax({
                            type: "POST",
                            url: "${contextRoot}/resource/resourceTreeList",
                            data: {name:value},
                            success: function (msg) {
                                // 初始化树形菜单
                                resourceTree = $('#div_wrapper_left_ul_resourcetree').ligerTree({
                                    data: msg.data,
                                    idFieldName: 'id',
                                    nodeWidth: 200,
                                    parentIDFieldName: 'pid',
                                    isExpand: false,
                                    onClick: function (obj) {
                                        rsResoureManege.reloadGridTree(obj.data.id, rsResoureManege.$searchresourceName.val());
                                        rsResoureManege.$resourceTreeId=obj.data.id;
                                    },
                                    onSuccess: function (data) {

                                    }
                                });
                            }
                        });
                }
            });
            this.$searchresourceName.ligerSearch({onClick:function(value){
                rsResoureManege.reloadGridSearch();
            }});
            //初始化搜索按钮
            var this$searchresourceName = this.$searchresourceName;
            this.$searchBtn.click(function () {
                var searchNm = this$searchresourceName.val();
                var selectData = resourceTree.getSelected();
                rsResoureManege.reloadGrid(selectData ? selectData.data.id : "", searchNm);
            })


            this.initGrid();
        },
        initCategoryList: function (target) {
            var target = $(target);
            var dataModel = $.DataModel.init();

        },

        initGrid: function () {
            var catalog = $("#ipt_catalog").val();
            var status = $("#ipt_status").val();

            this.grid = $("#div_app_info_grid").ligerGrid({
                url: '${contextRoot}/resource/resource/searchRsResource',
                parms: {
                    catalog: catalog,
                    status: status
                },
                columns: [
                    {display: '资源名称', name: 'name', width: '15%', isAllowHide: false},
                    {display: '资源代码', name: 'code', width: '31.5%',dict:true,dictName: "RESOURCE_CODE"},
                    {
                        display: '资源类型', name: 'type', width: '15%', resizable: true, render: function (row) {
                        if (row.type == "1") {
                            return "事实表";
                        } else {
                            return "逻辑表";
                        }
                    }
                    },
                        /*
                    {
                        display: '服务频率',
                        name: 'serviceRate',
                        width: '12.5%',
                        resizable: true,
                        render: function (row) {
                            return row.serviceRate;
                        }
                    },*/
                    {
                        display: '提供方式',
                        name: 'dataSupportType',
                        width: '12.5%',
                        resizable: true,
                        render: function (row) {
                            if (row.dataSupportType == "2") {
                                return "拉模式";
                            } else {
                                return "推模式";
                            }
                        }
                    },
                    {
                        display: '操作', name: 'operator', width: '25%', render: function (row) {
                        var html ='<div class="m-inline-buttons" style="width:200px;">';
                        html += "<a style=\"width:55px\" onclick=\"javascript:rsResoureManege.config('" + row.id + "');\">配置</a> ";
                        html += "<a style=\"width:55px\" href=\"javascript:void(0)\" onclick=\"javascript:rsResoureManege.grant('" + row.id + "');\">授权</a> ";
                        html +=  "<a style=\"width:55px\" href=\"javascript:void(0)\" onclick=\"javascript:rsResoureManege.look('" + row.id + "');\">浏览</a>  ";
                        html += "<a class=\"m-btn-edit\" onclick=\"rsResoureManege.editor('"+row.id+"', 'modify')\"></a>";
                        html += "<a class=\"m-btn-delete\" onclick=\"rsResoureManege.delete('"+row.id+"')\"></a>";
                        html += '</div>';
                        return html;
                    }
                    }
                ],
                enabledEdit: true,
                validate: true,
                unSetValidateAttr: false,
                onDblClickRow: function (data, rowid, rowdata) {
                    rsResoureManege.editor(data.id, "modify");
                }
            });

        },
        reloadGridTree: function (categoryId, resourcename) {
            this.grid.loadServerData({
                "categoryId": categoryId,
                "resourceName": resourcename,
                "page": 1,
                "rows": 10
            });
        },
        reloadGridSearch: function () {
            this.grid.set({
                parms: {resourceName:$('#inp_search').val(),categoryId:rsResoureManege.$resourceTreeId}
            });
            this.grid.reload();
            this.grid.changePage("first");

        },
        editor: function (id, mode) {
            var title = '';
            var params=null;
            //只有new 跟 modify两种模式会到这个函数
            if (mode == 'modify') {
                title = '修改资源';
                params={
                    id: id,
                    mode: mode,
                    };
            }
            else {
                title = '注册/新增资源';
                params={
                    id: id,
                    mode: mode,
                    categoryId:rsResoureManege.$resourceTreeId
                }
            }
            this.appInfoDialog = $.ligerDialog.open({
                height: 440,
                width: 500,
                title: title,
                url: '${contextRoot}/resource/resource/rsResourceInfo',
                urlParms: params,
                isHidden: false,
                opener: true
            });
        },
        delete: function (id) {
            //删除
            var status = "delete";
            $.ligerDialog.confirm('确认删除该行信息？', function (yes) {
                if (yes) {
                    $.ajax({
                        type: "POST",
                        url: "${contextRoot}/resource/resource/deleteResource",
                        data: {"id": id},
                        success: function (msg) {
                            rsResoureManege.reloadGridSearch();
                        }
                    });
                }
            });
        },
        config: function (id) {
            //配置
            indexPage.openChildPage("资源配置", '${contextRoot}/resource/resource/configInitial?id=' + id + '');
        },
        grant: function (id) {
            //授权
            indexPage.openChildPage("资源授权", '${contextRoot}/resource/resource/authorizeInitial?id=' + id + '');
        },
        look: function (id) {
            //跳转到资源浏览界面
            indexPage.openChildPage("资源浏览", '${contextRoot}/resource/resourceBrowse?id=' + id + '');
        },
        dialogCallBack: function (message) {
            $.ligerDialog.success(message);
            rsResoureManege.grid.set({
                parms: {categoryId:rsResoureManege.$resourceTreeId}
            });
            rsResoureManege.grid.reload();
            rsResoureManege.appInfoDialog.close();
        }
    }

    $(function () {
        rsResoureManege.init();
    });
</script>