

<title>RtspLiveServer</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a><cite>用户管理</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-front-formlist">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" name="user" placeholder="请输入" maxlength="30" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="batchdel">删除</button>
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
            </div>

            <table id="LAY-manage" lay-filter="LAY-manage"></table>
            <script type="text/html" id="statusTemp">
                {{#  if(d.status==0){ }}
                <span class="layui-badge layui-bg-green">普通用户</span>
                {{#  } }}
                {{#  if(d.status==1){ }}
                <span class="layui-badge layui-bg-black">内置用户</span>
                {{#  } }}
            </script>
            <script type="text/html" id="table-toolbar">
                <a class="layui-btn layui-btn-normal layui-btn-radius layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-radius layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script>
    layui.use('user', layui.factory('user')).use(['admin', 'user', 'table'], function () {
        var $ = layui.$
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , form = layui.form;

        form.render(null, 'layadmin-front-formlist');

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('LAY-manage')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                layer.confirm('确定删除选中用户？', function (index) {
                    var arr = new Array();
                    $.each(checkData, function (i, e) {
                        arr.push(e.user);
                    });
                    var names = arr.join(',');
                    $.ajax(
                        {
                            type: "POST",
                            url: '/api/user_delete',
                            cache: false,//禁止缓存
                            data: {
                                'user': names
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                var res = XMLHttpRequest.responseJSON;
                                if (res != undefined)
                                    layer.msg(res.message, { icon: 0, time: 2000 });
                            },
                            success: function (data) {
                                layer.close(index); //执行关闭
                                table.reload('LAY-manage');
                            }
                        });

                });
            }
            , add: function () {
                admin.popup({
                    title: '添加用户'
                    , area: ['600px', '350px']
                    , id: 'LAY-popup-user-add'
                    , shadeClose: false
                    , success: function (layero, index) {
                        view(this.id).render('system/userform').done(function () {
                            form.render(null, 'layuiadmin-form');
                            //监听提交
                            form.on('submit(LAY-front-submit)', function (data) {
                                var field = data.field; //获取提交的字段
                                if (field.pwd != field.repwd) {
                                layer.msg('确认密码不一致', { icon: 0, time: 2000 });
                                return;
                                }
                                field.pwd = md5(field.pwd);
                                $.ajax(
                                    {
                                        type: "POST",
                                        url: '/api/user_save',
                                        cache: false,//禁止缓存
                                        data: field,
                                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                                            var res = XMLHttpRequest.responseJSON;
                                            if (res != undefined)
                                                layer.msg(res.message, { icon: 0, time: 2000 });
                                        },
                                        success: function (data) {
                                            layui.table.reload('LAY-manage'); //重载表格
                                            layer.close(index); //执行关闭
                                        }
                                    });
                            });
                        });
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>