/*
 * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：研发部
 * 日 期：2017.03.22
 * 描 述：角色管理	
 */
var selectedRow;
var refreshGirdData;
var RoleId;
var isClose = false;
var ProjectId = request('ProjectId');
var defaultSelectUser = "";//临时选择的参与人员 供通用选人界面使用,多个用，分割
var saveRoleUser;//保存时回调

var bootstrap = function ($, learun) {
    "use strict";
    if (!ProjectId) {
        ProjectId = parent.request('ProjectId');
    }
    var page = {
        init: function () {
            page.initGrid();
            page.bind();
            //没有进入具体项目时项目角色管理界面隐藏【更多：添加用户，功能授权】的功能
            if (ProjectId == "00000000-0000-0000-0000-000000000001") {
                $("#lr_more").hide();
            } else {
                $("#lr_more").show();
            }
        },
        bind: function () {
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({ keyword: keyword });
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                selectedRow = null;
                learun.layerForm({
                    id: 'form',
                    title: '添加角色',
                    url: top.$.rootUrl + '/LR_OrganizationModule/Role/Form?ProjectId=' + ProjectId,
                    width: 500,
                    height: 290,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);

                    }
                });
            });

            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('F_RoleId');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '编辑角色',
                        url: top.$.rootUrl + '/LR_OrganizationModule/Role/Form?keyValue=' + keyValue + '&ProjectId=' + ProjectId,
                        width: 500,
                        height: 290,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });

            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('F_RoleId');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认删除该项！', function (res) {
                        if (res) {
                            learun.deleteForm(top.$.rootUrl + '/LR_OrganizationModule/Role/DeleteForm', { keyValue: keyValue }, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });

            // 添加成员
            $('#lr_memberadd').on('click', function () {
                RoleId = $('#gridtable').jfGridValue('F_RoleId');
                if (learun.checkrow(RoleId)) {
                    //初始化已经选定的人员
                    $.ajax({
                        type: 'GET',
                        url: top.$.rootUrl + "/UserRelation/GetRelationUser",
                        data: { KeyValue: RoleId, ProjectId: ProjectId },
                        traditional: true,
                        async: false,
                        success: function (data) {
                            defaultSelectUser = "";
                            var userList = [];
                            var data = JSON.parse(data);
                            if (data.code == '200') {
                                defaultSelectUser = data["data"].userIds;
                            }

                        },
                        error: function (msg) {
                        }, complete: function (data) {
                        }
                    });

                    //打开选择人员窗口
                    learun.layerForm({
                        id: 'selectuser',
                        title: '添加角色用户 - （注意：一个用户只会在一个角色下）',
                        url: top.$.rootUrl + '/LR_OrganizationModule/User/SelectUserForm?callType=1&textEleId=&valueEleId=&isMult=1&ProjectId=',
                        width: 850,
                        height: 650,
                        callBack: function (id) {
                            return top[id].acceptClick(saveRoleUser);
                        }
                    });
                }
            });

            // 功能授权
            $('#lr_authorize').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('F_RoleId');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                var parentId = '';
                if (!!ProjectId) {
                    parentId = ConstantZSJT.MODULETYPEID_PROJECT;
                } else {
                    parentId = ConstantZSJT.MODULETYPEID_UNIT;
                }
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'authorizeForm',
                        title: '功能授权 - 当前角色组为：' + selectedRow.F_FullName,
                        url: top.$.rootUrl + '/LR_AuthorizeModule/Authorize/Form?objectId=' + keyValue + '&objectType=3' + '&parentId=' + parentId,
                        width: 550,
                        height: 690,
                        btn: null
                    });
                }
            });
            
            // 插件端功能授权
            $('#lr_appRole').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('F_RoleId');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                var parentId = '';
                if (!!ProjectId) {
                    parentId = ConstantZSJT.MODULETYPEID_PROJECT;
                } else {
                    parentId = ConstantZSJT.MODULETYPEID_UNIT;
                }
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'authorizeForm',
                        title: '功能授权 - ' + selectedRow.F_FullName,
                        url: top.$.rootUrl + '/LR_AuthorizeModule/Authorize/Form?objectId=' + keyValue + '&objectType=3' + '&parentId=' + parentId,
                        width: 550,
                        height: 690,
                        btn: null
                    });
                }
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/LR_OrganizationModule/Role/GetPageList',
                headData: [
                    { label: "角色名称", name: "F_FullName", width: 200, align: "center" },
                    { label: "创建人", name: "F_CreateUserName", width: 100, align: "center" },
                    {
                        label: "创建时间", name: "F_CreateDate", width: 100, align: "center",
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd');
                        }
                    },
                    { label: "备注", name: "F_Description", width: 300, align: "left" },
                    { label: "角色用户", name: "MemberName", width: 200, align: "left" },
                    { name: "" }
                ],
                isPage: true,
                reloadSelected: true,
                mainId: 'F_RoleId'
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            param.ProjectId = ProjectId;
            $('#gridtable').jfGridSet('reload', { queryJson: JSON.stringify(param) });
        }
    };

    refreshGirdData = function () {
        $('#gridtable').jfGridSet('reload');
    };
    saveRoleUser = function () {
        if (defaultSelectUser == "") {
            learun.alert("请选择人员");
            return;
        }
        $.ajax({
            type: 'POST',
            url: top.$.rootUrl + "/LR_AuthorizeModule/UserRelation/SaveForm?rdo=" + Math.random(),
            data: { objectId: RoleId, category: "1", userIds: defaultSelectUser, ProjectId: ProjectId },
            traditional: true,
            async: false,
            success: function (data) {
                var data = JSON.parse(data);
                if (data.code == '200') {
                    top.learun.language.get('保存成功！', function (text) {
                        learun.alert.success(text);
                        learun.layerClose(window.name);
                        refreshGirdData();
                    });
                }
            },
            error: function (msg) {
            }, complete: function (data) {

            }
        });
    };

    page.init();
}

