<title>执行规则</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>采集管理</cite></a>
        <a><cite>采集管理</cite></a>
        <a><cite>热门书更新</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">热门书更新</div>
                <div class="layui-card-body" pad15>
                    <form class="layui-form" wid100 lay-filter="LAY-info-form">
                        <div class="layui-fields">
                        </div>
                        <div class="layui-submit">
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn" lay-submit lay-filter="submit">确认执行</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var config, checkbox = [];
    layui.admin.req({
        url: '/manage/data/response.json',
        method: 'POST',
        data: {rank: 'kvselect'},
        success: function (res) {
            config = {
                submit: function (obj) {
                    if (obj.field.mode === 'page') {
                        layui.layer.open({
                            type: 2,
                            title: '执行结果',
                            shadeClose: false,
                            shade: false,
                            maxmin: true,
                            area: ['893px', '600px'],
                            content: '/manage/collect/collect/top.html?type=' + obj.field.type + '&num=' + obj.field.num + '&access_token=' + encodeURIComponent(layui.data('layuiAdmin').access_token)
                        });
                    } else {
                        var t = Math.round(new Date().getTime() / 1000), interval = null;
                        layui.admin.req({
                            url: '/manage/collect/collect/top.json',
                            data: {
                                type: obj.field.type,
                                num: obj.field.num,
                                t: t
                            },
                            method: 'POST',
                            success: function () {
                                layui.layer.open({
                                    type: 1,
                                    title: '执行脚本',
                                    shadeClose: false,
                                    shade: false,
                                    maxmin: true,
                                    area: ['893px', '600px'],
                                    content: '',
                                    success: function (layero, index) {
                                        interval = setInterval(function () {
                                            layui.admin.req({
                                                url: '/manage/collect/collect/log.json',
                                                data: {
                                                    type: 'top',
                                                    t: t
                                                },
                                                success: function (res) {
                                                    if (res.data.content.search('<!-- end -->') >= 0) {
                                                        clearInterval(interval);
                                                        interval = null;
                                                    }
                                                    layero.find('.layui-layer-content').html(res.data.content)[0].scroll(0, 999999);
                                                }
                                            });
                                        }, 1000);
                                        if (parseInt(obj.field.interval) > 0) {
                                            setTimeout(function () {
                                                clearInterval(interval);
                                                interval = null;
                                            }, parseInt(obj.field.interval) * 1000)
                                        }
                                    },
                                    end: function () {
                                        if (interval !== null) clearInterval(interval);
                                    }
                                });
                            }
                        });
                    }
                    return false;
                },
                done: function (layui) {
                },
                fields: [
                    {
                        name: '采集模式',
                        key: 'mode',
                        type: 'radio',
                        extra: 'page:前台页面采集 shell:后台离线采集',
                        require: true,
                        value: 'page',
                        intro: "后台采集可以关闭浏览器运行，在手动采集日志中查看进度；前台页面采集必须保证浏览器是打开状态"
                    },
                    {
                        name: '选用列表',
                        key: 'type',
                        type: 'select',
                        extra: res.data.rank.kvselect,
                        require: true,
                        value: 'allvisit'
                    },
                    {
                        name: '更新数量',
                        key: 'num',
                        type: 'num',
                        require: true,
                        value: 100,
                        intro: '请输入当前列表方式更新的数量',
                    }
                ],
            };
            layui.use('curd/addOrEdit', layui.factory('curd/addOrEdit'));
        }
    });
</script>