<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script>
    var reResourceAuthorize = {
        $layoutDivWrapper: $("#div_wrapper"),//layout对象
        $btnBack: $("#btn_back"),//返回按钮
        $btnSearch: $("#div_wrapper_left_inp_search"),//返回按钮
        $btnAllStop: $("#btn_all_stop"),//全部禁止按钮
        $btnAllAllow: $("#btn_all_allow"),//全部允许按钮
        $btnSelectStop: $("#btn_select_stop"),//选中禁止按钮
        $btnSelectAllow: $("#btn_select_allow"),//选中允许按钮
        $btnAuthorizeApp: $("#btn_authorize_app"),//应用授权APP
        $leftDatagrid: $("#left_grid"),//左边的datagrid
        $centerDatagrid: $("#center_grid"),//左边的datagrid
        $title: $("#title"),//title
        $appInfoDialog: null,
        selectAppId: null,
        resourceId: '${resourceModel.id}',//当前资源ID

        //给按钮添加触发事件
        initEvent: function () {
            this.$btnBack.click(function () {
                indexPage.openMenu({
                    data: {
                        id: "31",
                        pid: "3",
                        text: "资源注册",
                        treedataindex: "10",
                        url: "${contextRoot}/resource/resource/initial"
                    }
                });
            });
            //全部禁止
            this.$btnAllStop.click(function () {
                if (!reResourceAuthorize.selectAppId || reResourceAuthorize.selectAppId == "") {
                    $.ligerDialog.error("请选择应用后再进行操作");
                    return;
                }
                $.ligerDialog.confirm('确认全部禁止？', function (yes) {
                    if (yes) {
                        reResourceAuthorize.changeAppResource("1");
                    }
                });
            });
            //全部允许
            this.$btnAllAllow.click(function () {
                if (!reResourceAuthorize.selectAppId || reResourceAuthorize.selectAppId == "") {
                    $.ligerDialog.error("请选择应用后再进行操作");
                    return;
                }
                $.ligerDialog.confirm('确认全部允许？<br>如果是请点击确认按钮，否则请点击取消。', function (yes) {
                    if (yes) {
                        reResourceAuthorize.changeAppResource("2");
                    }
                });
            });
            //选中禁止
            this.$btnSelectStop.click(function () {
                if (!reResourceAuthorize.selectAppId || reResourceAuthorize.selectAppId == "") {
                    $.ligerDialog.error("请选择应用后再进行操作");
                    return;
                }
                $.ligerDialog.confirm('确认禁止选中的数据？', function (yes) {
                    if (yes) {

                        var ids = "";
                        var selectRows = reResourceAuthorize.$centerDatagrid.getSelectedRows();
                        if (!selectRows || selectRows.length == 0) {
                            $.ligerDialog.error("未选中数据");
                            return;
                        }
                        for (var i = 0; i < selectRows.length; i++) {
                            if (selectRows[i].checked != "true") {
                                $.ligerDialog.error("部分数据已禁止!");
                                return;
                            }
                            ids += selectRows[i].appResourceDeatilId;
                            if (i != selectRows.length - 1) {
                                ids += ",";
                            }
                        }
                        reResourceAuthorize.changeAppResource("3", ids);
                    }
                });
            });
            //选中允许
            this.$btnSelectAllow.click(function () {
                if (!reResourceAuthorize.selectAppId || reResourceAuthorize.selectAppId == "") {
                    $.ligerDialog.error("请选择应用后再进行操作");
                    return;
                }
                $.ligerDialog.confirm('确认允许选中的数据？<br>如果是请点击确认按钮，否则请点击取消。', function (yes) {
                    if (yes) {
                        var ids = "";
                        var selectRows = reResourceAuthorize.$centerDatagrid.getSelectedRows();
                        if (!selectRows || selectRows.length == 0) {
                            $.ligerDialog.error("未选中数据");
                            return;
                        }
                        for (var i = 0; i < selectRows.length; i++) {
                            if (selectRows[i].checked == "true") {
                                $.ligerDialog.error("部分数据已允许!");
                                return;
                            }
                            ids += selectRows[i].metaDataId;
                            if (i != selectRows.length - 1) {
                                ids += ",";
                            }
                        }
                        reResourceAuthorize.changeAppResource("4", ids);
                    }
                });
            });
        },
        init: function () {
            this.$title.css("margin-right", document.body.scrollWidth / 2 - 200);
            this.initEvent();
            this.initlayout();
            this.initleft();
            this.initRight();
        },
        initleft: function () {
            this.$btnSearch.ligerSearch({
                width: 190, onClick: function (value) {
                    reResourceAuthorize.$leftDatagrid.queryByText("name", value);
                }
            });
            this.$leftDatagrid = this.$leftDatagrid.ligerGrid({
                url: '${contextRoot}/resource/resource/getResourceAppList',
                columns: [
                    {display: '全流程', name: 'name', width: '180', isAllowHide: false, isSort: false}
                ],
                validate: true,
                rownumbers: false,
                allowAdjustColWidth: false,
                width: 190,
                parms: {rows: 10000, resourceId: reResourceAuthorize.resourceId},
                fitColumn: false,
                height: "97%",
                usePager: false,//不显示分页
                onSelectRow: function (rowdata, rowid, rowobj) {
                    reResourceAuthorize.selectAppId = rowdata.id;
                    reResourceAuthorize.$centerDatagrid.loadServerData({resourceId: reResourceAuthorize.resourceId, appId: rowdata.id});
                }
            });
            $("#left_grid").css("margin-top","70px");
        },
        initRight: function () {
            //应用授权
            this.$btnAuthorizeApp.click(function () {
                reResourceAuthorize.$appInfoDialog = $.ligerDialog.open({
                    height: 540,
                    width: 610,
                    title: "资源应用授权",
                    url: '${contextRoot}/resource/resource/rsResourceAuthorize',
                    urlParms: {
                        id: reResourceAuthorize.resourceId,
                    },
                    isHidden: false,
                    opener: true
                });
            });
            this.$centerDatagrid = this.$centerDatagrid.ligerGrid({
                url: '${contextRoot}/resource/resource/getResourceAuthorize',
                columns: [
                    {display: '字段名称', name: 'metaDataName', width: '30%', isAllowHide: false},
                    {
                        display: '字段授权',
                        name: 'checked',
                        width: '20%',
                        isAllowHide: false,
                        render: function (row) {
                            if (row.checked == "true") {
                                return "允许";
                            } else {
                                return "禁止";
                            }
                        }
                    },
                    {display: '维度授权', name: 'dimensionMessage', width: '30%', isAllowHide: false},
                    {
                        display: '操作', name: 'operator', width: '16%', render: function (row) {
                        var html = '<div class="m-inline-buttons" style="width:80px;">';
                        if (row.checked == "true") {
                            html += "<a class=\"m-btn-edit\" onclick=\"reResourceAuthorize.config('"+row.appResourceDeatilId+"')\"></a>";
                        }
                        html += '</div>';
                        return html;
                    }
                    }
                ],
                validate: true,
                rownumbers: true,
                parms: {rows: 10000, resourceId: reResourceAuthorize.resourceId, appId: reResourceAuthorize.selectAppId},
                allowAdjustColWidth: false,
                width: document.body.scrollWidth - 410,
                fitColumn: false,
                checkbox: true,
                height: "97%",
                usePager: false//不显示分页

            });
        },
        config:function(id){
            reResourceAuthorize.$appInfoDialog = $.ligerDialog.open({
                height: 340,
                width: 550,
                title: "维度配置",
                url: '${contextRoot}/resource/resource/rsResourceDimension',
                urlParms: {
                    id: id,
                    resourceId: reResourceAuthorize.resourceId
                },
                isHidden: false,
                opener: true
            });
        },
        changeAppResource: function (flag, ids) {
            $.ajax({
                type: "POST",
                url: "${contextRoot}/resource/resource/changeAppResource",
                data: {"resourceId": reResourceAuthorize.resourceId, appId: reResourceAuthorize.selectAppId, ids: ids, flag: flag},
                success: function (data) {
                    if (data.successFlg) {
                        reResourceAuthorize.$centerDatagrid.loadServerData({
                            resourceId: reResourceAuthorize.resourceId,
                            appId: reResourceAuthorize.selectAppId
                        });
                        $.ligerDialog.success(data.message)
                    }
                    else {
                        $.ligerDialog.error(data.message);
                    }
                }
            });
        },
        dialogSuccess: function (message, type) {
            $.ligerDialog.success(message);
            if (type == "1") {
                reResourceAuthorize.$leftDatagrid.reload();
            } else {
                reResourceAuthorize.$centerDatagrid.loadServerData({
                    resourceId:reResourceAuthorize.resourceId,
                    appId: reResourceAuthorize.selectAppId
                });
            }
            reResourceAuthorize.$appInfoDialog.close();
        },
        initlayout: function () {
            this.$layoutDivWrapper.ligerLayout({
                height:"99%",
                topHeight: 50,
                leftWidth: 200,
                isLeftCollapse: false,//左边区域初始化不可以隐藏
                isRightCollapse: false,//左边区域初始化不可以隐藏
                allowLeftCollapse: false,//左边区域不可以隐藏
                allowRightCollapse: false,//左边区域不可以隐藏
                allowRightResize: false,
                allowLeftResize: false,
                allowBottomResize: false,
                allowTopResize: false
            });
            //隐藏layout左右两边的标题
            $("#div_wrapper .l-layout-left .l-layout-header").hide();
            $("#div_wrapper .l-layout-right .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"});
        }
    }

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