"use strict";
layui.extend({
    common: 'js/common',
    dropdown    :   'layui/lay/modules/dropdown'
}).use(['jquery', 'layer', 'common', 'table', 'form', 'dropdown'], function () {
    //  弹窗插件
    var layer = layui.layer;
    //  Jquery插件
    var $ = layui.jquery;
    //  引入公共模块
    var common = layui.common;
    //引入表单
    var form = layui.form;
    //  引入下拉组件
    var dropdown = layui.dropdown;

    var $node = {
        deviceNameInput: $('#device_name'),
        deviceNumInput: $('#device_num'),
        onLineStatusSelect: $('#online_status'),
        pusherStatusSelect: $('#pusher_status'),
        accountNameInput: $('#account_name'),
    };

    //  数据
    var Data = {
        //  当前用户机构Id
        myOrgId: parseInt($('#user_org_id').val()),
        //  表格Id
        tableId: 'data_table'
    };

    //  初始化表单
    var layuiTable = common.layuiTable.render(layui.table, {
        id: Data.tableId
        , elem: '#data_entity_table'
        , url: '/videoCloud/device/pagingQuery'
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，
        ,autoSort: false
        , cols: [[
            {field: 'deviceName', title: '设备名称', align: 'center'}
            , {field: 'deviceNum', title: '设备号', align: 'center', width: 180}
            , {field: 'address', title: '安装地址', align: 'center'}
            , {field: 'isOnline', title: '是否在线', templet: '#is_online', align: 'center', width: 80}
            , {field: 'longitude', title: '经维度', align: 'center'}
            , {field: 'latitude', title: '纬度', align: 'center'}
            , {field: 'rtspUrl', title: 'rtsp地址', align: 'center', width: 280}
            , {field: 'updateStateTimeString', title: '设备最后状态更新时间', align: 'center'}
            , {field: 'updateDate', title: '最新同步时间', align: 'center'}
            , {field: 'pusherStatus', title: '推流状态', templet: '#is_pusher_status', align: 'center'}
            , {title: '操作', width: 180, align: 'center', toolbar: '#table-operate'}
        ]]
        , page: true
        , done: function (result) {

        }
    });

    //监听排序
    layui.table.on('sort(table_event)',function (obj) {
        reloadTable(obj);
    });

    //监听行工具事件 lay-filter="table_event"
    layui.table.on('tool(table_event)', function(obj){
        var data = obj.data; //获得当前行数据
        switch (obj.event) {
            //  详情
            case 'info':
                common.openDefaultIframe({
                    title: '视频云设备详情',
                    content: '/videoCloud/device/detail/' + obj.data.id
                });
                break;
            case 'syncDevice'://同步设备信息
                common.sendAsyncRequest("/videoCloud/device/sync/list",'POST',{
                    deviceNums  :  data.deviceNum,
                    accountName :  data.accountName
                },function (result){
                    if(result.data){
                        //  刷新表格
                        layuiTable.reload();
                        layer.msg("同步成功");
                        layer.close(index);
                    }else{
                        layer.msg("同步失败");
                    }
                });
            break;
            case 'getRtspUrl'://同步设备信息
                common.sendAsyncRequest("/videoCloud/device/sync/rtspUrl",'POST',{
                    deviceNum  :  data.deviceNum,
                    accountName :  data.accountName
                },function (result){
                    if(result.data){
                        //  刷新表格
                        layuiTable.reload();
                        layer.msg(result.msg);
                    }else{
                        layer.msg(result.msg);
                    }
                });
                break;
            case 'pusher'://推流
                common.sendAsyncRequest("/videoCloud/device/pusher",'POST',{
                    deviceNum  :  data.deviceNum,
                    rtspUrl    :  data.rtspUrl
                },function (result){
                    if(result.code == '200'){
                        //  刷新表格
                        layuiTable.reload();
                        layer.msg(result.data);
                    }else{
                        layer.msg(result.data);
                    }
                });
                break;
            case 'pusherStatus'://推流
                common.sendAsyncRequest("/videoCloud/device/pusherStatus",'POST',{
                    deviceNum  :  data.deviceNum,
                },function (result){
                    if(result.code == '200'){
                        //  刷新表格
                        layuiTable.reload();
                        layer.msg(result.data);
                    }else{
                        layer.msg(result.data);
                    }
                });
                break;
            case 'pusherDelete'://推流停止
                common.sendAsyncRequest("/videoCloud/device/pusherDelete",'POST',{
                    deviceNum  :  data.deviceNum
                },function (result){
                    if(result.code == '200'){
                        //  刷新表格
                        layuiTable.reload();
                        layer.msg(result.data);
                    }else{
                        layer.msg(result.data);
                    }
                });
                break;
            case 'zlmStreamOnline'://zml流在线查询
                common.sendAsyncRequest("/videoCloud/device/zlmStreamOnline",'POST',{
                    deviceNum  :  data.deviceNum
                },function (result){
                    if(result.code == '200'){
                        //  刷新表格
                        layuiTable.reload();
                        layer.msg(result.data);
                    }else{
                        layer.msg(result.data);
                    }
                });
                break;
            case 'zlmStreamPush'://zml推流
                common.sendAsyncRequest("/videoCloud/device/zlmStreamPush",'POST',{
                    deviceNum  :  data.deviceNum,
                    rtspUrl    :  data.rtspUrl
                },function (result){
                    if(result.code == '200'){
                        //  刷新表格
                        layuiTable.reload();
                        layer.msg(result.data);
                    }else{
                        layer.msg(result.data);
                    }
                });
                break;
            case 'zlmStreamClose'://zml推流
                layer.confirm('确定要关闭推流吗',{icon: 7, title:'提示'}, function(index) {
                    common.sendAsyncRequest("/videoCloud/device/zlmStreamClose", 'POST', {
                        deviceNum: data.deviceNum,
                        key: data.key
                    }, function (result) {
                        if (result.code == '200') {
                            //  刷新表格
                            layuiTable.reload();
                            layer.msg(result.data);
                        } else {
                            layer.msg(result.data);
                        }
                    });
                });
                break;
        }
    });

    var reloadTable = function (obj) {
        //  重新加载表格
        layuiTable.reload({
            //  设定异步数据接口的额外参数
            where: {
                deviceName: $node.deviceNameInput.val(),
                deviceNum: $node.deviceNumInput.val(),
                isOnline: $node.onLineStatusSelect.val(),
                pusherStatus: $node.pusherStatusSelect.val(),
                accountName: $node.accountNameInput.val(),
                //排序
                sortField   :   obj?obj.field:'',
                sortOrder   :   obj?obj.type:'',
            }
            //  重新从第1页开始
            , page: {
                curr: 1
            },
            initSort: obj,
        });
    };

    //全量同步设备播放地址
    form.on('submit(syncRtspUrlAll)', function () {
        layer.open({
            title: '全量同步设备播放地址',
            type: 1,
            icon: 3,
            trigger: 'click',
            area: ['400px', '200px'],
            content: '' +
                '   <style type="text/css">' +
                '       input::-webkit-outer-spin-button,' +
                '       input::-webkit-inner-spin-button {-webkit-appearance: none;}' +
                '   </style>' +
                '   <div class="layui-card-body" style="margin-top:17px;margin-left: 10px">' +
                '       <div class="layui-form">' +
                '           <div class="layui-form-item">' +
                '               <label class="layui-form-label"><span class="must">*</span><span>账号:</span></label>' +
                '               <div class="layui-inline">' +
                '                   <div class="layui-input-inline">' +
                '                       <input name="accountName" type="text" autocomplete="off" class="layui-input">' +
                '                   </div>' +
                '               </div>' +
                '           </div>' +
                '       </div>' +
                '</div>',
            btn: ['确定', '取消'],
            yes: function (index) {
                var accountName = $('input[name="accountName"]').val();
                if (accountName.length > 0) {
                    var data = {};
                    data.accountName = accountName;
                    $.ajax({
                        url: '/videoCloud/device/sync/rtspUrl/all',
                        data: data,
                        type: 'POST',
                        success: function () {
                            layer.close(index);
                            layer.msg('同步开始，请刷新列表查看结果');
                        }
                    });
                } else {
                    layer.close(index);
                }
            },
            btn2: function (index) {
                layer.close(index);
            }

        });
    });

    //同步设备列表
    form.on('submit(syncDeviceList)', function () {
        layer.open({
            title: '同步设备列表',
            type: 1,
            icon: 3,
            trigger: 'click',
            area: ['400px', '200px'],
            content: '' +
                '   <style type="text/css">' +
                '       input::-webkit-outer-spin-button,' +
                '       input::-webkit-inner-spin-button {-webkit-appearance: none;}' +
                '   </style>' +
                '   <div class="layui-card-body" style="margin-top:17px;margin-left: 10px">' +
                '       <div class="layui-form">' +
                '           <div class="layui-form-item">' +
                '               <label class="layui-form-label"><span class="must">*</span><span>账号:</span></label>' +
                '               <div class="layui-inline">' +
                '                   <div class="layui-input-inline">' +
                '                       <input name="accountName" type="text" autocomplete="off" class="layui-input">' +
                '                   </div>' +
                '               </div>' +
                '           </div>' +
                '       </div>' +
                '</div>',
            btn: ['确定', '取消'],
            yes: function (index) {
                var accountName = $('input[name="accountName"]').val();
                if (accountName.length > 0) {
                    var data = {};
                    data.accountName = accountName;
                    $.ajax({
                        url: '/videoCloud/device/sync/list',
                        data: data,
                        type: 'POST',
                        success: function () {
                            layer.close(index);
                            layer.msg('同步开始，请刷新列表查看结果');
                        }
                    });
                } else {
                    layer.close(index);
                }
            },
            btn2: function (index) {
                layer.close(index);
            }

        });
    });

    //监听查询
    form.on('submit(query)', function () {
        reloadTable();
    });

    //监听重置
    form.on('submit(reset)', function () {
        $node.deviceNumInput.val("");
        $node.deviceNameInput.val("");
        $node.onLineStatusSelect.val("");
        $node.pusherStatusSelect.val("");
        $node.accountNameInput.val("");
        // 刷新select标签
        form.render('select');
        reloadTable();
    });

});
