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

var bootstrap = function ($, learun) {
    "use strict";
    if (!ProjectId) {
        ProjectId = parent.request('ProjectId');
    }
    var page = {
        init: function () {
            page.initGrid();
            page.bind();
        },
        bind: function () {
            if (openType == "look") {
                $("#lr_add").hide();
                $("#lr_delete").hide();
                $("#lr_edit").hide();
            }

            // 查询
            $('#btn_Search').on('click', function () {
                page.search();
            });
            // 初始化岗位树
            $('#PostTree').lrtree({
                url: top.$.rootUrl + '/LR_OrganizationModule/Post/GetOrganizationTree?ProjectId=' + ProjectId,
                param: {},
                nodeClick: function (item) {
                    PostId = item.value;
                    //$("#titleinfo").html(item.text);
                    page.search({ KeyValue: item.value });
                }
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });

            // 岗位管理
            $('#lr_edit').on('click', function () {
                learun.layerForm({
                    id: 'lr_layout',
                    title: '岗位管理',
                    url: top.$.rootUrl + '/LR_OrganizationModule/Post/PostForm?ProjectId=' + ProjectId,
                    width: 880,
                    height: 480,
                    callBack: function (id) {
                        return learun.layerClose('lr_layout');
                    }, end: function () {
                        if (isClose) {
                            location.reload();
                            isClose = false;
                        }

                    }
                });

            });

            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('F_UserId');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认删除该项！', function (res) {
                        if (res) {
                            learun.deleteForm(top.$.rootUrl + '/LR_AuthorizeModule/UserRelation/DeletRelationUser', { keyValue: keyValue, objectId: PostId, ProjectId: ProjectId }, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });

            // 添加岗位用户
            $('#lr_add').on('click', function () {
                if (learun.checkrow(PostId)) {
                    //update by chenkai 20210326 启用通用选人窗口
                    //初始化已经选定的岗位人员
                    $.ajax({
                        type: 'GET',
                        url: top.$.rootUrl + "/UserRelation/GetRelationUser",
                        data: { KeyValue: PostId, 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(savePostUser);
                        }
                    });
                }
            });

        },
        initGrid: function () {
            /*var tbalewidth = $('#gridtable').width() / 4;*/
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/LR_AuthorizeModule/UserRelation/GetRelationUserList',
                headData: [
                    { label: "姓名", name: "F_RealName", width: 100, align: "center" },
                    {
                        label: "性别", name: "F_Gender", width: 80, align: "center", formatter: function (value, row, op, $cell) {
                            if (row.F_Gender == 1) {
                                return "男";
                            } else if (row.F_Gender == 0) {
                                return "女"
                            } else {
                                return "";
                            }
                        }
                    },
                    { label: "手机号码", name: "F_Mobile", width: 120, align: "center" },
                    { label: "单位", name: "F_FullName", width: 250, align: "left" },
                    { name: ""}
                ],
                //isPage: true,
                reloadSelected: true,
                mainId: 'F_UserId',
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            param.KeyValue = PostId;
            param.ProjectId = ProjectId;
            param.keyword = $('#txt_Keyword').val();
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    refreshGirdData = function () {
        $('#gridtable').jfGridSet('reload');
    };
    savePostUser = function () {
        if (defaultSelectUser == "") {
            learun.alert("请选择人员");
            return;
        }
        $.ajax({
            type: 'POST',
            url: top.$.rootUrl + "/LR_AuthorizeModule/UserRelation/SaveForm?rdo=" + Math.random(),
            data: { objectId: PostId, category: "2", 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();
}

