{extend name='table'}

{block name="button"}
<!--{if isset($super) and $super}-->
<a data-table-id="QueueTable" class="layui-btn layui-btn-sm layui-btn-primary" data-queue="{:url('admin/api.plugs/optimize')}">优化数据库</a>
<!--{/if}-->

<!--{if isset($super) and $super and $iswin}-->
<button data-load='{:url("admin/api.queue/start")}' class='layui-btn layui-btn-sm layui-btn-primary'>开启服务</button>
<button data-load='{:url("admin/api.queue/stop")}' class='layui-btn layui-btn-sm layui-btn-primary'>关闭服务</button>
<!--{/if}-->

<!--{if auth("clean")}-->
<button data-table-id="QueueTable" data-queue='{:url("clean")}' class='layui-btn layui-btn-sm layui-btn-primary'>定时清理</button>
<!--{/if}-->

<!--{if auth("remove")}-->
<button data-table-id="QueueTable" data-action='{:url("remove")}' data-rule="id#{id}" data-confirm="确定批量删除记录吗？" class='layui-btn layui-btn-sm layui-btn-primary'>批量删除</button>
<!--{/if}-->
{/block}

{block name="content"}
<div class="think-box-notify">
    <!--{if isset($super) and $super}-->
    <b>服务状态：</b><b class="margin-right-5" data-queue-message><span class="color-desc">检查中</span></b>
    <b data-tips-text="点击可复制【服务启动指令】" class="layui-icon pointer margin-right-20" data-copy="{$command|default=''}">&#xe60e;</b>
    <script>$('[data-queue-message]').load('{:sysuri("admin/api.queue/status")}');</script>
    <!--{/if}-->
    <b>任务统计：</b>待处理 <b class="color-text" data-extra="pre">{$total.pre|default=0}</b> 个任务，处理中 <b class="color-blue" data-extra="dos">{$total.dos|default=0}</b> 个任务，已完成 <b class="color-green" data-extra="oks">{$total.oks|default=0}</b> 个任务，已失败 <b class="color-red" data-extra="ers">{$total.ers|default=0}</b> 个任务。
</div>
<div class="think-box-shadow">
    {include file='queue/index_search'}
    <table id="QueueTable" data-line="2" data-url="{:sysuri()}" data-target-search="form.form-search"></table>
</div>
{/block}

{block name='script'}
<script>
    $(function () {
        $('#QueueTable').layTable({
            even: true, height: 'full',
            sort: {field: 'loops_time desc,id', type: 'desc'},
            // 扩展数据处理，需要返回原 items 数据
            filter: function (items, result) {
                return result && result.extra && $('[data-extra]').map(function () {
                    this.innerHTML = result.extra[this.dataset.extra] || 0;
                }), items;
            },
            cols: [[
                {checkbox: true, fixed: 'left'},
                {
                    field: 'id', title: '任务名称', width: '25%', sort: true, templet: function (d) {
                        if (d.loops_time > 0) {
                            d.one = '<span class="layui-badge think-bg-blue">循</span>';
                        } else {
                            d.one = '<span class="layui-badge think-bg-red">次</span>';
                        }
                        if (d.rscript === 1) {
                            d.two = '<span class="layui-badge layui-bg-green">复</span>';
                        } else {
                            d.two = '<span class="layui-badge think-bg-violet">单</span>';
                        }
                        return laytpl('{{-d.one}}任务编号：<b>{{d.code}}</b><br>{{-d.two}}任务名称：{{d.title}}').render(d);
                    }
                },
                {
                    field: 'exec_time', title: '任务计划', width: '25%', templet: function (d) {
                        d.html = '执行指令：' + d.command + '<br>计划执行：' + d.exec_time;
                        if (d.loops_time > 0) {
                            return d.html + ' ( 每 <b class="color-blue">' + d.loops_time + '</b> 秒 ) ';
                        } else {
                            return d.html + ' <span class="color-desc">( 单次任务 )</span> ';
                        }
                    }
                },
                {
                    field: 'loops_time', title: '执行状态', width: '30%', templet: function (d) {
                        d.html = ([
                            '<span class="pull-left layui-badge layui-badge-middle layui-bg-gray">未知</span>',
                            '<span class="pull-left layui-badge layui-badge-middle layui-bg-black">等待</span>',
                            '<span class="pull-left layui-badge layui-badge-middle layui-bg-blue">执行</span>',
                            '<span class="pull-left layui-badge layui-badge-middle layui-bg-green">完成</span>',
                            '<span class="pull-left layui-badge layui-badge-middle layui-bg-red">失败</span>',
                        ][d.status] || '') + '执行时间：';
                        d.enter_time = d.enter_time || '', d.outer_time = d.outer_time || '0.0000';
                        if (d.enter_time.length > 12) {
                            d.html += d.enter_time.substring(12) + '<span class="color-desc"> ( 耗时 ' + d.outer_time + ' ) </span>';
                            d.html += ' 已执行 <b class="color-blue">' + (d.attempts || 0) + '</b> 次';
                        } else {
                            d.html += '<span class="color-desc">任务未执行</span>'
                        }
                        return d.html + '<br>执行结果：<span class="color-blue">' + (d.exec_desc || '<span class="color-desc">未获取到执行结果</span>') + '</span>';
                    }
                },
                {toolbar: '#toolbar', title: '操作面板', align: 'center', minWidth: 210, fixed: 'right'}
            ]]
        });
    });
</script>

<script type="text/html" id="toolbar">

    <!--{if auth('redo')}-->
    {{# if(d.status===4||d.status===3){ }}
    <a class="layui-btn layui-btn-sm" data-confirm="确定要重置该任务吗？" data-queue="{:url('redo')}?code={{d.code}}">重 置</a>
    {{# }else{ }}
    <a class="layui-btn layui-btn-sm layui-btn-disabled">重 置</a>
    {{# } }}
    <!--{/if}-->

    <!--{if auth('remove')}-->
    <a class='layui-btn layui-btn-sm layui-btn-danger' data-confirm="确定要删除该任务吗？" data-action='{:url("remove")}' data-value="id#{{d.id}}">删 除</a>
    <!--{/if}-->

    <a class='layui-btn layui-btn-sm layui-btn-normal' onclick="$.loadQueue('{{d.code}}',false,this)">日 志</a>
</script>
{/block}
