

<title>RtspLiveServer</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a><cite>视频管理</cite></a>
        <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="rtsp_name" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">rtsp地址</label>
                    <div class="layui-input-block">
                        <input type="text" name="rtsp_url" placeholder="请输入 如ip" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <!--<div class="layui-inline">
                  <label class="layui-form-label">性别</label>
                  <div class="layui-input-block">
                    <select name="sex">
                      <option value="0">不限</option>
                      <option value="1">男</option>
                      <option value="2">女</option>
                    </select>
                  </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="imgTpl">
                <img style="display: inline-block; width: 50%; height: 100%;" src={{ d.avatar }}>
            </script>       
            <script type="text/html" id="table-toolbar">
                <!-- <a class="layui-btn layui-btn-primary layui-btn-radius layui-btn-xs" lay-event="push"><i class="fa fa-play-circle"></i>启动推流</a> -->
                <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>
    //.use('video', layui.factory('video'))必需加否则影响部分表格加载失败
    layui.use('video', layui.factory('video')).use(['admin', 'video', '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.rtsp_name);
                    });
                    var names = arr.join(',');
                    $.ajax(
                        {
                            type: "POST",
                            url: '/api/delete',
                            cache: false,//禁止缓存
                            data: {
                                'rtsp_name': 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', '500px']
                    , id: 'LAY-popup-user-add'
                    , shadeClose: false
                    , success: function (layero, index) {
                        view(this.id).render('video/videoform').done(function () {
                            form.render(null, 'layuiadmin-form');

                            //监听提交
                            form.on('submit(LAY-front-submit)', function (data) {
                                var field = data.field; //获取提交的字段
                                $.ajax(
                                    {
                                        type: "POST",
                                        url: '/api/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>