/*
 * 版 本 Learun-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：研发部
 * 日 期：2017.03.22
 * 描 述：查看成员	
 */

var objectId = request('objectId');
var type = request('type');
var category = request('category');
var ProjectId = request('ProjectId');
var selectedRow;
var refreshGirdData;
var userList = [];//已有角色的用户id
var userlistselected = [];//添加角色成员或者修改角色成员 勾选的人员
var treeData = [];
var Select;
var bootstrap = function ($, learun) {
    "use strict";
    var companyId = '';
    var roleId = '';
    var page = {
        init: function () {
            page.initGrid();
            page.bind();
            page.search();
            //$('#jfgrid_body_gridtable').lrscrollSet('moveBottom');
        },
        bind: function () {
            if (type == "look")//查看成员
            {
                $('.lr-layout-left').hide();
                $('.lr-layout-center').css("margin-left", " -198px");
                roleId = objectId;
            } else {
                $('.lr-layout-tool-right').show();
            }
            // 获取角色用户列表
            $.ajax({
                type: 'GET',
                url: top.$.rootUrl + "/LR_AuthorizeModule/UserRelation/GetUserIdList?rdo=" + Math.random() + '&ProjectId=' + ProjectId,
                data: { objectId: objectId },
                traditional: true,
                async: false,
                success: function (data) {
                    var data = JSON.parse(data);
                    if (data.code == '200') {
                        var dataList = data["data"].userIds;
                        if (dataList.indexOf(',') != -1) {
                            dataList = dataList.split(',');
                            for (var i in dataList) {
                                userList.push({ userId: dataList[i] });
                            }
                        }
                        else {
                            userList.push({ userId: dataList });
                        }
                        if (data["data"].userInfoList != null && data["data"].userInfoList != "") {
                            for (var i = 0; i < data["data"].userInfoList.length; i++) {
                                var tempdata = {
                                    id: data["data"].userInfoList[i].F_UserId,
                                    text: data["data"].userInfoList[i].F_RealName,
                                    title: data["data"].userInfoList[i].F_RealName,
                                    parentId: 0,
                                    showcheck: false,
                                    ChildNodes: null,
                                    isSelectUser: true
                                };
                                treeData.push(tempdata);
                            }
                        }
                        $('#userTree').lrtree({
                            data: treeData
                        });
                    }

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

            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({ keyword: keyword });
            });

            //全部保存
            $('#lr_saveALL').on('click', function () {
                userlistselected = [];
                for (var j = 0; j < treeData.length; j++) {
                    userlistselected.push(treeData[j].id);
                }
                $.ajax({
                    type: 'POST',
                    url: top.$.rootUrl + "/LR_AuthorizeModule/UserRelation/SaveForm?rdo=" + Math.random(),
                    data: { objectId: objectId, category: category, userIds: String(userlistselected) },
                    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);
                            });
                        }
                    },
                    error: function (msg) {
                    }, complete: function (data) {

                    }
                });

            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/LR_OrganizationModule/User/GetProjectUserList',
                headData: [
                    {
                        label: "选择", name: "F_UserId", index: "IsCheck", width: 60, align: "center", frozen: true,
                        formatter: function (cellvalue, row, op, $cell) {
                            var rowindex = parseInt($cell.attr('rowindex'));
                            return "<input value='" + row.F_RealName + "' title='" + row.F_RealName + "'  id='" + row.F_UserId + "' name='myname' type='checkbox' style='cursor: pointer;margin-top: 8px;' onclick='Select(\"" + rowindex + "\",\"" + row.F_UserId + "\",\"" + row.F_RealName + "\")'>";
                        }
                    },
                    { label: '账户', name: 'F_Account', width: 150, align: 'center', frozen: true },
                    { label: '姓名', name: 'F_RealName', width: 100, align: 'center', frozen: true },

                ],
                //isPage: true,
                reloadSelected: true,
                mainId: 'F_UserId',
                onRenderComplete: function (data) {
                    var obj = document.getElementsByName("myname");
                    for (var i = 0; i < obj.length; i++) {
                        if (type == "look") {
                            obj[i].disabled = true;
                        }
                        if (treeData.length > 0) {
                            var temp = treeData.find(function (x) {
                                return x.id === obj[i].id
                            });
                            if (temp != undefined && temp != "") {
                                obj[i].checked = true;
                            } else {
                                obj[i].checked = false;
                            }
                        }
                    }
                },
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            param.companyId = companyId;
            param.roleId = roleId;
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    refreshGirdData = function () {
        $('#gridtable').jfGridSet('reload');
    };
    Select = function (rowindex, userId, userName) {
        var tempdata = {
            id: userId,
            text: userName,
            title: userName,
            parentId: 0,
            showcheck: false,
            ChildNodes: null,
            isSelectUser: true
        };
        var obj = $('#gridtable').context.getElementsByName("myname");
        for (var i = 0; i < obj.length; i++) {
            if (userId == obj[i].id && obj[i].checked) {
                treeData.push(tempdata);
            } else if (userId == obj[i].id && obj[i].checked == false) {
                for (var j = 0; j < treeData.length; j++) {
                    if (treeData[j].id == userId) {
                        treeData.splice(j, 1);
                    }
                }
            }
        }
        $('#userTree').lrtree({
            data: treeData
        });


    };
    page.init();
}

function Del(id) {
    for (var j = 0; j < treeData.length; j++) {
        if (treeData[j].id == id) {
            treeData.splice(j, 1);
        }
    }
    var obj = $('#gridtable').context.getElementsByName("myname");
    for (var i = 0; i < obj.length; i++) {
        if (id == obj[i].id && obj[i].checked) {
            obj[i].checked = false;
        }
    }
    $('#userTree').lrtree({
        data: treeData
    });
}
