/**

视频通道
 */


layui.define(['table', 'form'], function (exports) {
    var $ = layui.$
        , admin = layui.admin
        , view = layui.view
        , table = layui.table
        , form = layui.form;

    //视频管理
    table.render({
        elem: '#LAY-manage'
        , url: '/api/search'
        , cols: [[
            { type: 'checkbox', fixed: 'left' }
            , { field: 'rtsp_name', title: '通道名称', width: 150 }// sort: true,templet: '#imgTpl'
            , { field: 'rtsp_url', title: 'rtsp地址', minWidth: 100 }
            , { title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-toolbar' }
        ]]
        , page: true
        , limit: 15
        , limits: [10, 15, 20, 25, 30, 50]
        , text: { none: '暂无数据!' }
        , request: {
            pageName: 'page' //页码的参数名称，默认：page
            , limitName: 'limit' //每页数据量的参数名，默认：limit
        }
        , response: {
            statusName: 'code' //规定数据状态的字段名称，默认：code
            , statusCode: 0 //规定成功的状态码，默认：0
        }
        , parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.message, //解析提示文本
                "count": res.count, //解析数据长度,区分大小写
                "data": res.data //解析数据列表,区分大小写
            };
        }
    });
    //监听搜索
    form.on('submit(LAY-front-search)', function (data) {
        var field = data.field;

        //执行重载
        table.reload('LAY-manage', {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: field
        });
    });
    //监听视频通道
    table.on('tool(LAY-manage)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除选中的视频通道?', function (index) {
                $.ajax(
                    {
                        type: "POST",
                        url: '/api/delete',
                        cache: false,//禁止缓存
                        data: {
                            'rtsp_name': data.rtsp_name
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            var res = XMLHttpRequest.responseJSON;
                            if (res != undefined)
                                layer.msg(res.message, { icon: 0, time: 2000 });
                        },
                        success: function (data) {
                            //obj.del();
                            layer.close(index);
                            layui.table.reload('LAY-manage'); //重载表格
                        }
                    });
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑通道'
                , area: ['600px', '350px']
                , id: 'LAY-popup-user-add'
                , shadeClose: false
                , success: function (layero, index) {
                    view(this.id).render('video/videoform', data).done(function () {
                        form.render(null, 'layuiadmin-form');
                        var container = $(view(this.id).container);
                        var _rtsp_name = container.find('input[name="rtsp_name"]');
                        var _rtsp_url = container.find('textarea[name="rtsp_url"]');
                        if (_rtsp_name != undefined)
                            _rtsp_name.val(data.rtsp_name);
                        if (_rtsp_url != undefined)
                            _rtsp_url.val(data.rtsp_url);
                        _rtsp_name.attr("disabled", "disabled");
                        //监听提交
                        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) {
                                        layer.close(index); //执行关闭 
                                        layui.table.reload('LAY-manage'); //重载表格
                                    }
                                });

                        });
                    });
                }
            });
        }
    });

    exports('video', {})
});