let radio, checkedUser = [], checkedUsers = {};
layui.use(['layer', 'gtable', 'orgTree', 'form'], function () {
    let orgTree = layui.orgTree;
    let gtable = layui.gtable;
    let userTable;

    let urlParams = layui.url().search, initData = urlParams.initData;
    radio = urlParams.radio == 'true';
    let searchAll = urlParams.all == 'true';

    let initDataArr = initData ? initData.split(',') : [];

    let userUrl = ctx + '/sys/sysUser/page';
    if(searchAll){
        userUrl = ctx + '/sys/sysUser/page/all';
    }

    /**
     * 加载用户数据列表
     */
    let opstions = {
        id: 'userTable',
        url: userUrl,
        toolbar: '#tableToolBar',
        title: '用户列表',
        limit: 20,
        //响应后数据处理
        parseData: function (res) { //res 即为原始返回的数据
            let data = res.data || {};
            let records = data.records || [];
            records = records.map(item => {
                if (initDataArr.includes(item.userId)) {
                    item.LAY_CHECKED = true;
                }
                return item;
            })
            return {
                "flag": res.flag, //解析接口状态
                "msg": res.msg, //解析提示文本
                "records": records, //解析数据列表
                "total": data.total || data.length //解析数据长度
            };
        },
        cols: [[
            {field: 'userId', title: 'ID', hide: true},
            {field: 'userId', type: radio ? 'radio' : 'checkbox', title: '选择', width: 60},
            {field: 'loginName', title: '用户名'},
            {field: 'userName', title: '姓名'},
            {field: 'orgName', title: '所属部门', templet: data => data.org ? data.org.orgName : ''},
            {field: 'valid', title: '是否允许登录系统', hide: true},
            {field: 'limitMultiLogin', title: '是否允许多人在线', hide: true},
            {field: 'limitedIp', title: '限制允许登录的IP集合', hide: true},
            {field: 'expiredTime', title: '账号失效时间', hide: true},
            {field: 'lastChangePwdTime', title: '最近修改密码时间', hide: true},
            {field: 'createTime', title: '创建时间', hide: true},
            {field: 'modifyTime', title: '更新时间'}
        ]],
        onToolBarTable: function (obj) {
            switch (obj.event) {
                case 'query':
                    let queryByLoginName = $("#queryByLoginName").val();
                    let query = {
                        page: {
                            current: 1 //重新从第 1 页开始
                        }
                    };
                    if (!queryByLoginName) {
                        queryByLoginName = "";
                    }
                    //设定异步数据接口的额外参数
                    query.where = {loginName: queryByLoginName};
                    userTable.reload(query);
                    $("#queryByLoginName").val(queryByLoginName);
                    break;
                case 'reload':
                    break;
            }
        },
        onRadio: function (obj) {
            checkedUser[0] = obj.data;
        },
        onChecked: function (obj) {
            let {data, checked} = obj;
            if (checked) {
                checkedUsers[data.userId] = data;
            } else {
                delete checkedUsers[data.userId];
            }

        },
        done: (res) => {
            // 删除全选复选框
            $('thead .laytable-cell-checkbox .layui-form-checkbox').remove();

            // 禁用已选中数据的复选框
            $('tbody .laytable-cell-checkbox .layui-form-checked')
                .addClass('layui-disabled')
                .off('click');

        }
    };
    userTable = gtable.init(opstions);


    /**
     * 加载部门树
     */
    function loadOrgTree() {
        orgTree.init({
            id: `orgTree`,
            all: true,
            done: function (nodes, elem) {
            },
            onClick: function (obj) {
                let query = {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                };
                //设定异步数据接口的额外参数
                query.where = {orgId: obj.param.id};
                gtable.reload(query);
            }
        })
    }

    loadOrgTree();
});

function getSelectedUser() {
    if (radio) {
        return checkedUser;
    }
    checkedUser = [];
    for (let key in checkedUsers) {
        checkedUser.push(checkedUsers[key]);
    }
    return checkedUser;
}