<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE HTML>
<html>
<head>
    <title>能辉</title>
    <meta name="decorator" content="base" />
    <link rel="stylesheet" href="${ctxPlugin}/static/css/reset.css" media="all">
    <style>
        .layui-card{box-shadow: none;}
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-form layui-card">
        <div class="layui-tab  layui-tab-brief">
            <ul class="layui-tab-title">
                <c:if test="${user.userType eq '6' || user.userType eq '7'}">
                    <sfTags:pagePermission authFlag="XTSZFWS_YGSZ_JSGL_T" html='<li class="layui-this sf-tab"><a href="${ctx}/member/setRole">角色管理</a></li>'/>
                    <sfTags:pagePermission authFlag="XTSZFWS_YGSZ_QXKZ_T" html='<li class="sf-tab"><a href="${ctx}/member/roleIndex">权限设置</a></li>'/>
                    <sfTags:pagePermission authFlag="SIT_XTSZ_YGSZ_YGXX_T" html='<li class="sf-tab"><a href="${ctx}/traderEmploye/traderEmployeeList">员工信息</a></li>'/>
                </c:if>

                <c:if test="${user.userType ne '6' && user.userType ne '7'}">
                    <sfTags:pagePermission authFlag="XTSZ_YGSZ_JSGL_T" html='<li class="layui-this sf-tab"><a href="${ctx}/member/setRole">角色管理</a></li>'/>
                    <sfTags:pagePermission authFlag="XTSZ_YGSZ_QXKZ_T" html='<li class="sf-tab"><a href="${ctx}/member/roleIndex">权限设置</a></li>'/>
                    <sfTags:pagePermission authFlag="XTSZ_YGSZ_YGSZ_T" html='<li class="sf-tab"><a href="${ctx}/member/index">人员管理</a></li>'/>
                </c:if>

               <%-- <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " id="searchBtn" data-type="reload">
                    <i class="layui-icon fs-12">&#xe615;</i>
                    查询
                </button>--%>
            </ul>
        </div>

    </div>

    <div class='layui-card'>
        <div class="layui-card-body">
            <table class="layui-hide" lay-skin="row" id="test-table-reload" lay-filter="table-toolbar"></table>
            <script type="text/html" id="toolbarDemo">
                <c:if test="${user.userType ne '6' && user.userType ne '7'}">
                    <sfTags:pagePermission authFlag="YGSZ_JSGL_TJJS_B" html='<button class="layui-btn layui-btn-sm" lay-event="addEmployeeBtn" id="addEmployeeBtn"><i class="layui-icon layui-icon-add-1"></i>添加角色</button>'/>
                </c:if>
                <c:if test="${user.userType eq '6' || user.userType eq '7'}">
                    <sfTags:pagePermission authFlag="DLSYGSZ_JSGL_TJJS_B" html='<button class="layui-btn layui-btn-sm" lay-event="addEmployeeBtn" id="addEmployeeBtn"><i class="layui-icon layui-icon-add-1"></i>添加角色</button>'/>
                </c:if>
            </script>
        </div>
    </div>

</div>


<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script type="text/html" id="table-toolbar">
    <c:if test="${user.userType eq '6' || user.userType eq '7'}">
        <sfTags:pagePermission authFlag="DLSYGSZ_JSGL_BJ_B" html='<a href="#" class="layui-table-link" lay-event="edit" >编辑</a>'/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <sfTags:pagePermission authFlag="YGSZDLS_JSGL_SC_B" html='<a href="#" class="layui-table-link" lay-event="del">删除</a>'/>
    </c:if>
    <c:if test="${user.userType ne '6' && user.userType ne '7'}">
        <sfTags:pagePermission authFlag="YGSZ_JSGL_BJ_B" html='<a href="#" class="layui-table-link" lay-event="edit" >编辑</a>'/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <sfTags:pagePermission authFlag="YGSZ_JSGL_SC_B" html='<a href="#" class="layui-table-link" lay-event="del">删除</a>'/>
    </c:if>
</script>

<script type="text/javascript">
    var $;
    var table;
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tableExt', 'jquery', 'layer'], function(){
        var tableExt = layui.tableExt;
        var layer = layui.layer;
         $ = layui.jquery;

        tableExt.render({
            elem: '#test-table-reload'
            ,toolbar : '#toolbarDemo'
            , height: 'full-120'
            ,defaultToolbar : false
            ,url: '${ctx}/member/getRoleList'
            , limits : [ 20, 50, 100 ]
            , limit:20
            , page : true //开启分页
            ,cols: [[
                {type: 'numbers',title:'序号'}
                ,{field:'id', title: '操作',align: 'center',toolbar: '#table-toolbar'}
                // ,{field:'sort', title: '排序',align: 'center'}
                ,{field:'name', title: '角色名称',align: 'center'}
                ,{field:'number', title: '员工数量',align: 'center'}
             /*   ,{field:'mobile', title: '手机号'}
                ,{field:'hiredate', title: '入职时间'}*/
            ]]
            ,done: function(res, curr, count){
             //   console.log("customer done", res);
            },exportOpt:{                           //导出选项配置
                ele:'#exportBtn',                   //导出的Dom元素
                headerTitle: '员工列表',             //excel表格的标题
                pageData:false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                maxAmount: 1000,                    //当pageData为false生效
                fileName: '员工导出',
                where: function (){                 //当pageData为false时生效，过滤的条件
                    var ret = {};
                    ret.name= $("#form-name").val();
                    return ret;
                }
            }
        });

         table = tableExt.getLayTable();

        //头工具栏事件
        table.on('toolbar(table-toolbar)', function (obj) {
            if (obj.event === 'addEmployeeBtn') {
                layer.open({
                    type: 2
                    , title: '添加角色'
                    , area: ['470px', '280px']
                    , content: '${ctx}/member/addPlatemRole'
                })
            }
        });

        //监听行工具事件
        table.on('tool(table-toolbar)', function(obj){
            var data = obj.data;
            var roleId = data.id;
            var type= data.type;
            var traderId = data.trader_id;
            if(type=='1' || type =='2' || type =='4'|| type =='5'|| type =='6'|| type =='7' || type =='8'|| type =='9'|| type =='a' || type=='b'|| type=='c'|| type=='d'){
                layer.msg("该角色为系统默认，不能操作！！",{icon:2});
                return
            }
            if(obj.event === 'del'){
                layer.confirm('真的删除所选角色吗',{
                    btn: ['确定', '取消'],
                    success: function (layero, index) {
                        this.enterEsc = function (event) {
                            if (event.keyCode == 13 ) {
                                //确认  执行的代码
                                deleteRole(roleId, traderId);
                                //关闭弹窗
                                layer.close(index);
                                console.log("回车确认时事件")
                                //取消连续回车键，导致不断弹出询问框
                                $(':focus').blur();
                            }
                        };
                        //监听键盘事件
                        $(document).on('keydown', this.enterEsc);
                        //esc键触发取消
                        this.escQuit = (ev) => {
                            let code_val = ev.which;
                            if(code_val == 0x1B){
                                $('.layui-layer-btn1').click();
                                return false;
                            }
                        };
                        $(document).on('keydown', this.escQuit);
                    },
                    end: function () {
                        //取消键盘监听事件
                        $(document).off('keydown', this.enterEsc);
                        $(document).off('keydown', this.escQuit);
                    },
                    yes: function () {
                        //确认执行的代码
                        deleteRole(roleId, traderId);
                    },
                    no: function () {

                    }
                });
            } else if(obj.event === 'edit'){

              /*  goMemberform('编辑员工信息', '${ctx}/member/memberFormNew?id=' + memberId);*/
                layer.open({
                    type: 2
                    , title: '编辑角色'
                    , area: ['440px', '260px']
                    , content: '${ctx}/member/editPlatemRole?id='+roleId
                })
            }
        });

        var active = {
            reload: function(){
                table.reload('test-table-reload', {
                    where: {
                        name: $('#form-name').val(),
                        mobile: $("#form-mobile").val()
                    }
                });
            }
        };

        $('#searchBtn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });

    function deleteRole(roleId, traderId){
        $.post('${ctx}/member/deleteRole', {id: roleId, traderId: traderId}, function(result){
            if("200" == result){
                layer.msg('操作成功', {time: 2000}, function () {
                    reload();
                });
            }else if("202"==result){
                layer.msg("该角色下有员工，不能删除！！");
            }else{
                layer.msg("删除失败");
            }
        });
    }

    function reload(){
        table.reload('test-table-reload', {
            page:{
                curr:1
            }
        });
    }
</script>
</body>
</html>
