<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>UP主管理</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<div class="layui-layout layui-layout-admin">
    <div id="header"></div>
    <div class="layui-body" style="left: 0px;">
        <div style="width: 50%;margin-left: 25%; margin-top: 2%;">
            <blockquote class="layui-elem-quote">
                在这里添加需要监控的UP主，添加请填入直播地址或房间号
                <div id="temp"></div>
            </blockquote>
        </div>
        <div style="width: 80%;margin-left: 10%; margin-top: 2%;">
            <form class="layui-form" action="" lay-filter="myForm">

                <div class="layui-tab layui-tab-card" lay-filter="type" lay-allowclose="true">
                    <ul class="layui-tab-title">
                    </ul>
                    <div class="layui-tab-content">
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div id="footer"></div>
</div>
<script src="/layui/layui.js"></script>
<script src="/js/jquery-3.2.1.js"></script>
<script type="text/html" id="topTools">
    <a class="layui-btn layui-btn-xs" lay-event="addAddress">新增</a>
    <scan id="online">当前正在直播:0</scan>
</script>
<script type="text/html" id="listTools">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="upInfoCheckToOfflineListening">
    {{# if(d.offlinelistening==1){}}
    <div><input name="offlineListeningCheckbox" lay-filter="offlineListeningCheckbox" type="checkbox" lay-skin="switch"
                lay-text="开启|关闭" value="{{d.roomid}}" checked></div>
    {{# } else { }}
    <div><input name="offlineListeningCheckbox" lay-filter="offlineListeningCheckbox" type="checkbox" lay-skin="switch"
                lay-text="开启|关闭" value="{{d.roomid}}"></div>
    {{# }}}
</script>
<script type="text/html" id="upInfoCheckToEnable">
    {{# if(d.enable==1){}}
    <div><input name="enableCheckbox{{d.roomid}}" lay-filter="enableCheckbox" type="checkbox" lay-skin="switch"
                lay-text="启用|关闭"
                value="{{d.roomid}}" checked></div>
    {{# } else { }}
    <div><input name="enableCheckbox{{d.roomid}}" lay-filter="enableCheckbox" type="checkbox" lay-skin="switch"
                lay-text="启用|关闭"
                value="{{d.roomid}}"></div>
    {{# }}}
</script>
<script type="text/html" id="upInfoCheckToSaveLive">
    {{# if(d.savelive==1){}}
    <div><input name="saveLiveCheckbox{{d.roomid}}" lay-filter="saveLiveCheckbox" type="checkbox"
                lay-skin="switch"
                lay-text="启用|关闭"
                value="{{d.roomid}}" checked></div>
    {{# } else { }}
    <div><input name="saveLiveCheckbox{{d.roomid}}" lay-filter="saveLiveCheckbox" type="checkbox"
                lay-skin="switch"
                lay-text="启用|关闭"
                value="{{d.roomid}}"></div>
    {{# }}}

</script>
<script type="text/html" id="upInfoCheckTolive">
    <div><input id="live" name="liveCheckbox{{d.roomid}}" lay-filter="liveCheckbox" type="checkbox" lay-skin="switch"
                lay-text="直播|自闭"
                value="{{d.roomid}}"></div>
</script>
<script type="text/html" id="upInfoCheckToSaveDanmu">
    {{# if(d.savedanmu==1){}}
    <div><input name="saveDanmuCheckbox" lay-filter="saveDanmuCheckbox" type="checkbox" lay-skin="switch"
                lay-text="开启|关闭" value="{{d.roomid}}" checked></div>
    {{# } else { }}
    <div><input name="saveDanmuCheckbox" lay-filter="saveDanmuCheckbox" type="checkbox" lay-skin="switch"
                lay-text="开启|关闭" value="{{d.roomid}}"></div>
    {{# }}}
</script>
<script>
    let form = layui.form;
    $('#header').load("/html/header.html");
    $('#footer').load("/html/footer.html");
    layui.use(['layer', 'form', 'element', 'table'], function () {
        let layer = layui.layer
            , form = layui.form
            , table = layui.table
            , element = layui.element;
        element.tabAdd('type', {
            title: "BiliBili"
            , content: '<table id="bili_up" lay-filter="listTools"></table>'
            , id: 1
        })
        element.on('tab(type)', function (data) {

            table.render({
                elem: "#bili_up"
                , url: '/upinfo/get/list.do'
                , toolbar: '#topTools'
                , page: true
                , cols: [[
                    {field: "id", title: "id", width: 80, sort: true, fixed: 'left'}
                    , {
                        field: 'name',
                        title: '标题 <i class="layui-icon" id="htitle">&#xe60b;</i>',
                        width: 150,
                        templet: '<div><span id="{{d.roomid}}">{{d.name}}</span></div>'
                    }
                    , {field: 'roomid', title: 'roomid', width: 100}
                    , {
                        field: 'url',
                        title: '直播地址',
                        width: 250,
                        templet: '<div><a href="{{d.url}}" target="_blank">{{d.url}}</a></div>'
                    }
                    , {
                        field: 'offlinelistening',
                        title: '24小时监控 <i class="layui-icon" id="h24">&#xe60b;</i>   ',
                        width: 150,
                        templet: '#upInfoCheckToOfflineListening'
                    }
                    , {
                        field: 'enable',
                        title: '统计 <i class="layui-icon" id="hdata">&#xe60b;</i>',
                        width: 100,
                        templet: '#upInfoCheckToEnable'
                    }
                    , {
                        field: 'saveLive',
                        title: '录制 <i class="layui-icon" id="hsave">&#xe60b;</i>',
                        width: 120,
                        templet: '#upInfoCheckToSaveLive'
                    }
                    , {
                        field: 'live',
                        title: '直播状态 <i class="layui-icon" id="hlive">&#xe60b;</i>',
                        width: 120,
                        templet: '#upInfoCheckTolive'
                    }
                    , {
                        field: 'saveDanmu',
                        title: '保存弹幕 <i class="layui-icon" id="hdanmu">&#xe60b;</i>',
                        width: 120,
                        templet: '#upInfoCheckToSaveDanmu'
                    }
                    , {field: "right", width: 200, toolbar: '#listTools'}
                ]]
                , done: function (res, curr, count) {
                    let index=layer.load()
                    $('#htitle').on('click', function () {
                        var that = this;
                        layer.tips('当名字变成绿色则代表后台正在统计中，为黑色则代表未统计，如果需要统计请打开统计开关后刷新查看结果', that, {
                            tpis: 4
                        });
                    });
                    $('#h24').on('click', function () {
                        var that = this;
                        layer.tips('只有开启24小时监控才会自动统计数据或录制', that, {
                            tpis: 4
                        });
                    });
                    $('#hdata').on('click', function () {
                        var that = this;
                        layer.tips('启动统计数据，仅在主播正在直播时可用，如需统计未直播时段，请开启24小时监控', that, {
                            tpis: 4
                        });
                    });
                    $('#hsave').on('click', function () {
                        var that = this;
                        layer.tips('在主播直播时录制视频，在统计为开启时才可用，录像文件在UP管理-录像中查看', that, {
                            tpis: 4
                        });
                    });
                    $('#hlive').on('click', function () {
                        var that = this;
                        layer.tips('主播当前直播状态，显示直播时并不代表在统计和录制视频，该状态仅代表主播是否正在直播', that, {
                            tpis: 4
                        });
                    });
                    $('#hdanmu').on('click', function () {
                        var that = this;
                        layer.tips('在统计为启用时保存弹幕信息，该功能可能占用大量储存空间请注意，保存文件为sqlite', that, {
                            tpis: 4
                        });
                    });
                    $.post('/upinfo/get/liveInfo.do', function (json) {
                        if (json.code === 0) {
                            $('#online').text("当前正在直播:" + json.data.online)
                            for (let i in json.data.info) {
                                console.log('input[name="liveCheckbox' + json.data.info[i].roomid + '"]')
                                $('input[name="liveCheckbox' + json.data.info[i].roomid + '"]').prop('checked', true);
                            }
                            for (let i in json.data.live) {
                                $('#' + json.data.live[i].roomid).css('color', '#8FE18F')
                                console.log('绿了:'+json.data.live[i].roomid)
                            }

                            $.post('/user/get/test.do',function (json) {
                                console.log(json.data.power)
                                if(json.data.power.indexOf(-1)===-1&&json.data.power.indexOf(4)===-1){
                                    $('input[type="checkbox"]').prop('disabled',true)
                                }
                                layer.close(index);
                                form.render();

                            })
                        }else{
                            layer.msg("close")
                            layer.close();
                        }
                    })

                }
            });
        })
        /* $('#h24').on('click', function(){
             var that = this;
             layer.tips('只有开启24小时监控才会自动统计数据或录制', that);

         });*/

        table.on('toolbar(listTools)', function (obj) {
            if (obj.event === 'addAddress') {
                layer.prompt({
                    formType: 2,
                    value: '',
                    title: '请输入房间号或直播地址(直播地址请勿包含参数 如http://live.bilibili.com/110?abcd)',
                    area: ['350px', '30px'] //自定义文本域宽高
                }, function (value, index, elem) {
                    $.post("/user/up/set/add.do", {"url": value}, function (json) {
                        layer.msg(json.msg)
                        layer.close(index)
                    })
                });
            }
        })
        table.on('tool(listTools)', function (obj) {
            let event = obj.event;
            switch (event) {
                case "edit":
                    layer.prompt({
                        formType: 2,
                        value: '',
                        title: '请输入房间号或直播地址(直播地址请勿包含参数 如http://live.bilibili.com/110?abcd)',
                        area: ['350px', '30px'] //自定义文本域宽高
                    }, function (value, index, elem) {
                        let dialogIndex=layer.load();
                        queryUpInfo(obj.data.roomid, function (udata) {
                            udata.url = value;
                            udata.offlinelistening = udata.offlinelistening ? 1 : 0;
                            udata.live = udata.live ? 1 : 0;
                            udata.saveDanmu = udata.saveDanmu ? 1 : 0;
                            console.log(udata)
                            $.post("/upinfo/update.do", udata, function (json) {
                                layer.close(dialogIndex);
                                layer.msg(json.msg,function () {
                                    window.location.reload();
                                })
                                layer.close(index)

                            })
                        })

                    });
                    break;
                case "del":
                    layer.open({
                        title: '提示！'
                        , content: '确定是否删除' + obj.data.name + '直播间？删除后已保存数据并不会一并删除！'
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            $.post('/upinfo/set/delete.do', {'roomid': obj.data.roomid}, function (json) {
                                layer.msg(json.msg)
                            })
                            layer.close(index); //如果设定了yes回调，需进行手工关闭
                        }
                    });
                    break;
            }

        })
        form.on('switch(offlineListeningCheckbox)', function (data) {
            let roomid = data.value;
            let flag = data.elem.checked ? 1 : 0;
            queryUpInfo(roomid, function (udata) {
                udata.offlinelistening = flag;
                udata.live = udata.live ? 1 : 0;
                udata.saveDanmu = udata.saveDanmu ? 1 : 0;
                $.post('/upinfo/set/update.do', udata, function (json) {
                    layer.msg(json.msg)
                })
            })

        });
        form.on('switch(liveCheckbox)', function (data) {
            let roomid = data.value;
            let flag = data.elem.checked ? 1 : 0;
            queryUpInfo(roomid, function (udata) {
                udata.live = flag;
                udata.offlinelistening = udata.offlinelistening ? 1 : 0;
                udata.saveDanmu = udata.saveDanmu ? 1 : 0;
                $.post('/upinfo/set/update.do', udata, function (json) {
                    layer.msg(json.msg)
                })
            })
        });
        form.on('switch(enableCheckbox)', function (data) {
            let roomid = data.value;
            let flag = data.elem.checked ? 1 : 0;
            queryUpInfo(roomid, function (udata) {
                udata.enable = flag;
                udata.offlinelistening = udata.offlinelistening ? 1 : 0;
                udata.saveDanmu = udata.saveDanmu ? 1 : 0;
                console.log(udata)
                $.post('/upinfo/set/update.do', udata, function (json) {
                    layer.msg(json.msg)
                })
            })
        });
        form.on('switch(saveLiveCheckbox)', function (data) {
            let roomid = data.value;
            let flag = data.elem.checked ? 1 : 0;
            queryUpInfo(roomid, function (udata) {
                udata.savelive = flag;
                udata.offlinelistening = udata.offlinelistening ? 1 : 0;
                udata.saveDanmu = udata.saveDanmu ? 1 : 0;
                $.post('/upinfo/set/update.do', udata, function (json) {
                    layer.msg(json.msg)
                })
            })
        });
        form.on('switch(saveDanmuCheckbox)', function (data) {
            if (data.elem.checked) {
                layer.open({
                    title: '警告！'
                    , content: '保存弹幕可能会占用大量硬盘空间，也可能会降低数据库查询时间，请谨慎使用（建议手动控制时段开关）'
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        let roomid = data.value;
                        let flag = data.elem.checked ? 1 : 0;
                        queryUpInfo(roomid, function (udata) {
                            udata.savedanmu = flag;
                            $.post('/upinfo/set/update.do', udata, function (json) {
                                layer.msg(json.msg)
                            })
                        })
                        layer.close(index); //如果设定了yes回调，需进行手工关闭
                    }
                    , btn2: function (index, layero) {
                        console.log('off')
                        $('input[name="saveDanmuCheckbox"]').prop('checked', false)
                        form.render();
                        layer.close(index)
                    }
                    , cancel: function (index, layero) {
                        console.log('off')
                        $('input[name="saveDanmuCheckbox"]').prop('checked', false)
                        form.render();
                        layer.close(index)
                    }
                });

            } else {
                let roomid = data.value;
                let flag = data.elem.checked ? 1 : 0;
                queryUpInfo(roomid, function (udata) {
                    udata.savedanmu = flag;
                    $.post('/upinfo/set/update.do', udata, function (json) {
                        layer.msg(json.msg)
                    })
                })
            }


        })
        element.tabChange('type', '1');

    })
    let that;

    function queryUpInfo(roomid, fun) {
        $.post('/upinfo/get/query.do', {"roomid": roomid}, function (data) {
            fun(data)
        })
    }

</script>


</body>

</html>