/*
 * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：研发部
 * 日 期：2017.03.22
 * 描 述：人员管理	
 */
var selectedRow;
var refreshGirdData;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGrid();
            page.bind();
            $('#jfgrid_body_gridtable').lrscrollSet('moveBottom');
        },
        bind: function () {
            $('#Organization').lrselect({
                // 字段
                value: "F_ItemDetailId",
                text: "F_ItemName",
                title: "F_ItemName",
                //type: 'multiple',
                // 展开最大高度
                maxHeight: 200,
                // 是否允许搜索
                allowSearch: true,
                // 访问数据接口地址
                url: top.$.rootUrl + '/LR_SystemModule/DataItem/GetCodeList',
                // 访问数据接口参数
                param: { itemCode: 'BIMCD_Organization' }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({ Organization: $('#Organization').lrselectGet(), keyword: keyword });
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            /*模板下载*/
            $('#lr_down_file_btn').on('click', function () {
                learun.download({ url: top.$.rootUrl + '/LR_OrganizationModule/User/DownTemplateFile', param: { folderName: "Template" }, method: 'POST' });
            });
            // 重置
            $('#lr_reset').on('click', function () {
                var format = 'xlsx';
                var uploadKeyValue = Guid.NewGuid().toString();
                learun.layerForm({
                    id: 'importfile',
                    title: '重置用户信息',
                    url: top.$.rootUrl + '/LR_SystemModule/Annexes/UploadForm?keyVaule=' + uploadKeyValue + "&multiple=false" + "&format=" + format,
                    width: 600,
                    height: 400,
                    maxmin: true,
                    btn: ['确认'],
                    end: function () {
                        updateData(uploadKeyValue);
                    }
                });
            });
            // 导入
            $('#lr_import').on('click', function () {
                var format = 'xlsx';
                var uploadKeyValue = Guid.NewGuid().toString();
                learun.layerForm({
                    id: 'importfile',
                    title: '附件管理',
                    url: top.$.rootUrl + '/LR_SystemModule/Annexes/UploadForm?keyVaule=' + uploadKeyValue + "&multiple=false" + "&format=" + format,
                    width: 600,
                    height: 400,
                    maxmin: true,
                    btn: ['确认'],
                    end: function () {
                        LoadEXCEL(uploadKeyValue);
                    }
                });
            });

            // 新增
            $('#lr_add').on('click', function () {
                selectedRow = null;
                learun.layerForm({
                    id: 'form',
                    title: '添加账号',
                    url: top.$.rootUrl + '/LR_OrganizationModule/User/Form',
                    width: 750,
                    height: 390,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('F_UserId');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '编辑账号',
                        url: top.$.rootUrl + '/LR_OrganizationModule/User/Form',
                        width: 750,
                        height: 390,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#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 + '/ZSJT_BIMCD/ProjectUser/DeleteProjectUser', { keyValue: keyValue }, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 重置账号
            $('#lr_resetpassword').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('F_UserId');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认要【重置密码】！', function (res) {
                        if (res) {
                            learun.postForm(top.$.rootUrl + '/LR_OrganizationModule/User/ResetPassword', { keyValue: keyValue }, function () {
                            });
                        }
                    });
                }
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/LR_OrganizationModule/User/GetPageListByProject',
                headData: [
                    { label: "组织", name: "OrganizationName", index: "OrganizationName", width: 100, align: "left", frozen: true },
                    { label: '账号', name: 'F_Account', width: 150, align: 'center', frozen: true },
                    { label: '姓名', name: 'F_RealName', width: 100, align: 'center', frozen: true  },
                    { label: '手机号码', name: 'F_Mobile', width: 120, align: 'center', frozen: true },
                    { label: "岗位", name: "RoleName", index: "RoleName", width: 500, align: "left" },
                    { label: "单位", name: "F_CompanyId", index: "F_CompanyId", width: 200, align: "left" },
                ],
                isPage: true,
                reloadSelected: true,
                rows: 50,
                mainId: 'F_UserId'
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            $('#gridtable').jfGridSet('reload', { queryJson: JSON.stringify(param) });
        }
    };

    refreshGirdData = function () {
        $('#gridtable').jfGridSet('reload');
    };

    page.init();
}
// 读取EXCEL数据
function LoadEXCEL(fileId) {
    var learun = top.learun;
    learun.loading(true, "读取EXCEL数据中...");
    $.ajax({
        type: 'POST',
        url: top.$.rootUrl + "/LR_OrganizationModule/User/LoadEXCEL?rdo=" + Math.random(),
        data: { fileId: fileId },
        traditional: true,
        success: function (data) {
            refreshGirdData();
        },
        error: function (msg) {
            learun.loading(false);
        },
        complete: function (data) {
            learun.loading(false);
        }
    });
}

// 读取EXCEL数据
function updateData(fileId) {
    var learun = top.learun;
    learun.loading(true, "读取EXCEL数据中...");
    $.ajax({
        type: 'POST',
        url: top.$.rootUrl + "/LR_OrganizationModule/User/UpdateData?rdo=" + Math.random(),
        data: { fileId: fileId },
        traditional: true,
        success: function (data) {
            refreshGirdData();
        },
        error: function (msg) {
            learun.loading(false);
        }, complete: function (data) {
            learun.loading(false);
        }
    });
}