var refreshGirdData;
var SelectData;
var userData;
var acceptClick;
var auditorIds;
var bootstrap = function ($, learun) {
    "use strict";
    var companyId = '';
    var departmentId = '';
    userData = [];
    auditorIds = "";
   

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

            //公司选择

            $('#company_select').lrselect({
                placeholder: '请选择公司',
                // 是否允许搜索
                allowSearch: true,
                url: top.$.rootUrl + '/LR_OrganizationModule/Company/GetTree',
                // 访问数据接口参数
                param: { parentId: '0' },
                select: function (item) {
                    if (!item || item.value == '-1') {
                        companyId = '';
                    }
                    else {
                        companyId = item.value;
                        $('#titleinfo').text(item.text);
                        //刷新部门下拉列表数据
                        $('#department_select').lrselectRefresh({
                            url: top.$.rootUrl + '/LR_OrganizationModule/Department/GetTree',
                            // 访问数据接口参数
                            param: { companyId: companyId, parentId: '0' },
                        });
                        page.search();
                    }
                    
                }
            });
            // 部门选择
            $('#department_select').lrselect({
                placeholder: '请选择部门',
                // 是否允许搜索
                allowSearch: true,
                select: function (item) {
                    if (!item || item.value == '-1') {
                        departmentId = '';
                    }
                    else {
                        departmentId = item.value;
                        page.search();
                    }
                }
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/LR_OrganizationModule/User/GetPageList?IsAll=true',
                headData: [
                    {
                        label: "选择", name: "F_UserId", index: "IsCheck", width: 60, align: "center",
                        formatter: function (cellvalue, row, op, $cell) {
                            var rowindex = parseInt($cell.attr('rowindex'));
                            return "<input value='" + row.F_RealName + "'  id='"+row.F_UserId+"' name='myname' type='checkbox' style='cursor: pointer;'   onclick='SelectData(\"" + rowindex + "\",\"" + row.F_UserId + "\",\"" + row.F_RealName + "\")'>";
                        }
                    },
                    { label: '账户', name: 'F_Account', width: 150, align: 'center' },
                    { label: '姓名', name: 'F_RealName', width: 100, align: 'center' },
                    {
                        label: '部门', name: 'F_DepartmentId', width: 100, align: 'left',
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('department', {
                                key: value,
                                callback: function (item) {
                                    callback(item.name);
                                }
                            });
                        }
                    }
                ],
                isPage: true,
                reloadSelected: true,
                mainId: 'F_UserId',
                onRenderComplete: function (data) {
                    var obj = document.getElementsByName("myname");
                    if (userData.length > 0) {
                        for (var j = 0; j < userData.length; j++) {
                            for (var i = 0; i < obj.length; i++) {

                                if (userData[j].text == obj[i].value) {
                                    obj[i].checked = true;

                                }
                            }
                        }
                    }
                   
                },
            });
        },
        search: function (param) {
            param = param || {};
            param.companyId = companyId;
            param.departmentId = departmentId;
            $('#gridtable').jfGridSet('reload', param);

        }
    };
    SelectData = function (rowindex,userId, userName)
    {
        var tempdata = { 
            id:userId,
            text:userName,
            title: userName,
            parentId:0,   
            showcheck:false,    
            ChildNodes: null
        };
        var obj = document.getElementsByName("myname");
        for (var i = 0; i < obj.length; i++) {
            if (userName == obj[i].value&&obj[i].checked) {
                userData.push(tempdata);
             
            } else if (userName == obj[i].value && obj[i].checked == false)
            {
                for (var j = 0; j < userData.length; j++) {
                    if (userData[j].text== userName) {
                        userData.splice(j, 1);
                    }
                }
            }
        }



        //var obj = document.getElementsByName("myname");
        //for (var i = 0; i < obj.length; i++) {

        //    for (var j = 0; j < userData.length; j++) {
        //        if (userData[j].text == obj[i].value) {
        //            userData.splice(j, 1);
        //        }
        //    }


        //    if (obj[i].checked) {
        //        userData.push(tempdata);
        //    }
        //}
        $('#userTree').lrtree({
            data: userData,
        });
        console.log(userName);
    };
    refreshGirdData = function () {
        $('#gridtable').jfGridSet('reload');
    };
    // 保存数据
    acceptClick = function (callBack) {
        for (var j = 0; j < userData.length; j++) {
            auditorIds += userData[j].id + ",";
        }
        auditorIds = auditorIds.substring(0, auditorIds.length - 1);
        $('#auditorIds').val(auditorIds);
        if (!$('#lr_layout').lrValidform()) {
            return false;
        }
        var formData = $('#lr_layout').lrGetFormData();
        callBack(formData);
        return true;
    };
    page.init();
}
