;
layui.config({
    base: "../../../admin/js/layuiPlugins/"
}).use(['form', 'layer', 'jquery', 'laypage', 'element', 'table', 'laydate', 'cascade', 'step', 'eleTree', 'laytpl'], function () {
    var form = layui.form,
        layer = layui.layer,
        laypage = layui.laypage,
        $ = layui.jquery,
        table = layui.table,
        element = layui.element,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        eleTree = layui.eleTree;
    cascade = layui.cascade;
    //自定义验证
    form.verify({
        namelength: [
            /^.{0,18}$/,
            "长度限制在0-18个字符噢"
        ]
    });

    var obj = {
        roleId: ''
    }

    obj.main = function () {
        var _this = this;
        _this.getRole();
		if(!layui.data('auth').data.add){
			$('#addRole').hide();
		}
    }

    obj.getRole = function () {
        var _this = this;
        $.ajax({
            type: "get",
            url: 'roleListUrl',
            success: function (data) {
                if (data.code === 0) {
                    var getTpl = roleTpl.innerHTML, view = document.getElementById('roleInfo');
                    laytpl(getTpl).render(data, function (html) {
                        view.innerHTML = html;
                        
                    });
                } else {
                    parent.layer.msg(res.msg, {
                        icon: 1
                    });
                }
            }
        });
    }

    function getMenu(id, data, open) {
        eleTree.render({
            elem: '#system' + id,
            data: data,
            showCheckbox: true,
            drag: false,
            accordion: false,
            showIcon: true,
            menuShow: true,
            open: open,
        });
    }

    function getSystem(roleId) {
        $.ajax({
            type: "get",
            url: 'menuListUrl?roleId=' + roleId,
            success: function (data) {
                if (data.code === 0) {
                    var getTpl = systemTpl.innerHTML, view = document.getElementById('systemInfo');
                    laytpl(getTpl).render(data, function (html) {
                        view.innerHTML = html;
                    });
                    for (let i = 0; i < data.data.length; i++) {
                        getMenu(data.data[i].sysId, data.data[i].list, data.data[i].open)
                        form.render();
                    }
                } else {
                    parent.layer.msg(res.msg, {
                        icon: 1
                    });
                }
            }
        });
    }

    // 点击角色获取菜单权限
    $('#roleInfo').on('click', '.li-item', function () {
        let roleId = $(this).attr('roleId');
        $(this).addClass('active').siblings().removeClass('active');
        obj.roleId = roleId
        if ($(this).children('.role-forbid').length > 0) {
            $('#systemInfo').empty();
            return
        }
        getSystem(obj.roleId);
    })

    // 编辑角色
    $('#roleInfo').on('click', '#roleEdit', function (e) {
        e.stopPropagation();
        let roleId = $(this).parents('.li-item').attr('roleId');
        layer.open({
            type: 2,
            anim: 0,
            title: "添加角色",
            area: ['700px', '350px'],
            fixed: true,
            scrollbar: true,
            maxmin: true,
            content: "add_role.html?id=" + roleId,
            end: function () {
                obj.getRole(obj.regionId)
            }
        });
    })

    // 删除角色
    $('#roleInfo').on('click', '#roleDel', function (e) {
        e.stopPropagation();
        let roleId = $(this).parents('.li-item').attr('roleId');
        layer.confirm('真的删除吗？', function (index) {
            layer.close(index);
            var index1 = layer.msg('删除中，请稍候', {
                icon: 16,
                time: false,
                shade: 0.8
            });
            $.ajax({
                type: "DELETE",
                url: 'roleFormUrl?roleId=' + roleId,
                contentType: 'application/json',
                success: function (res) {
                    
                    if (!(res.code - 0)) {
                        layer.close(index1);
                        layer.msg(res.msg, {
                            icon: 1
                        });
                        obj.getRole(obj.regionId)
                    } else {
                        layer.msg(res.msg, {
                            icon: 2
                        });
                    }
                }
            });
        });
    })

    // 禁用角色
    $('#roleInfo').on('click', '#roleForbid', function (e) {
        e.stopPropagation();
        
        let roleId = $(this).parents('.li-item').attr('roleId')
        let forbidId = {
            id: roleId,
            status: '2'
        }
        layer.confirm('确定禁用吗？', function (index) {
            layer.close(index);
            $.ajax({
                type: "PUT",
                url: 'roleFormUrl',
                data: JSON.stringify(forbidId),
                contentType: 'application/json',
                success: function (res) {
                    
                    if (!(res.code - 0)) {
                        layer.msg('禁用成功', {
                            icon: 1
                        });
                        obj.getRole(obj.regionId)
                    } else {
                        layer.msg(res.msg, {
                            icon: 2
                        });
                    }
                }
            });
        });
    })

    // 启用角色
    $('#roleInfo').on('click', '#roleEnable', function (e) {
        e.stopPropagation();
        
        let roleId = $(this).parents('.li-item').attr('roleId')
        let forbidId = {
            id: roleId,
            status: '0'
        }
        layer.confirm('确定启用吗？', function (index) {
            layer.close(index);
            $.ajax({
                type: "PUT",
                url: 'roleFormUrl',
                data: JSON.stringify(forbidId),
                contentType: 'application/json',
                success: function (res) {
                    
                    if (!(res.code - 0)) {
                        layer.msg('启用成功', {
                            icon: 1
                        });
                        obj.getRole(obj.regionId)
                    } else {
                        layer.msg(res.msg, {
                            icon: 2
                        });
                    }
                }
            });
        });
    })

    // 保存菜单权限
    $("#saveMenus").on("click", function () {
        let arr1 = [], sysReqs = [], tempArray = [], parentNode = [];
        $('#systemInfo').find('li').each(function () {
            if ($(this).attr('opened') == 1) {
                sysReqs.push($(this).attr('id'));
            }
        });
        for (let i = 0; i < sysReqs.length; i++) {
            this['ids' + i] = [];
        }
        for (let i = 0; i < sysReqs.length; i++) {
            this['ids' + i] = eleTree.checkedData("#system" + sysReqs[i]);
            $("#system" + sysReqs[i]).find("input[eletree-status='2']").each(function () {
                parentNode.push($(this).val())
            });
            if (this['ids' + i].length == 0) {
                let title = $("#system" + sysReqs[i]).prev().find('h4').text();
                layer.msg(title + '下必须有选中菜单')
                return false;
            }
            for (let j = 0; j < this['ids' + i].length; j++) {
                arr1.push(this['ids' + i][j].id)
            }
        }

        // for (let i = 0; i < arr1.length; i++) {
        //     if (tempArray.indexOf(arr1[i]) == -1) {
        //         tempArray.push(arr1[i]);
        //     }
        // }

        let temp = {
            roleId: obj.roleId,
            menuReq: arr1.concat(parentNode),
            sysReq: sysReqs
        }
        
        $.ajax({
            type: "post",
            url: 'menuFormUrl',
            data: JSON.stringify(temp),
            contentType: 'application/json',
            success: function (res) {
                
                if (!(res.code - 0)) {
                    layer.msg(res.msg, {
                        icon: 1
                    });
                    getSystem(obj.roleId);
                } else {
                    layer.msg(res.msg, {
                        icon: 2
                    });
                }
            }
        });
    })

    // 点击开关控制菜单权限 是否可选
    form.on('switch(switchTest)', function (data) {
        if (data.elem.checked == true) {
            data.othis.parents('.group').find('.eleTree-checkbox').removeClass('eleTree-checkbox-disabled')
            data.othis.parents('.group').attr('opened', '1')
        } else {
            data.othis.parents('.group').find('.eleTree-checkbox').addClass('eleTree-checkbox-disabled')
            data.othis.parents('.group').attr('opened', '0')
        }
    });

    // 添加角色
    $("#addRole").on("click", function () {
        var id = this.getAttribute('layId');
        layer.open({
            type: 2,
            anim: 0,
            title: "添加角色",
            area: ['700px', '350px'],
            fixed: true,
            scrollbar: true,
            maxmin: true,
            content: id + ".html",
            end: function () {
                obj.getRole()
            }
        });
    });
    obj.main();
});

