<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-12">
        <h2>Repeat任务</h2>
        <ol class="breadcrumb">
            <li><a>任务队列管理</a></li>
            <li class="active"><b>Repeat任务</b></li>
        </ol>
    </div>
</div>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox">
                <div class="ibox-title">
                    <h3><span class="text-navy">Repeat任务</span></h3>
                </div>
                <div class="ibox-content">
                    <form method="post" id="form" class="form-horizontal" onsubmit="return false">
                        <div class="form-group">
                            <label class="col-sm-2 control-label" style="width:10%">任务ID</label>

                            <div class="col-sm-3">
                                <input type="text" class="form-control" name="taskId"
                                       placeholder="请输入任务ID(TaskId)">
                            </div>

                            <label class="col-sm-2 control-label" style="width:10%">提交节点组</label>

                            <div class="col-sm-3">
                                <select name="submitNodeGroup" class="form-control">
                                    <option value="">-- 不限 --</option>
                                    #foreach($nodeGroup in $jobClientNodeGroups)
                                        <option value="$nodeGroup.name">$nodeGroup.name</option>
                                    #end
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" style="width:10%">执行节点组</label>

                            <div class="col-sm-3">
                                <select name="taskTrackerNodeGroup" class="form-control">
                                    <option value="">-- 不限 --</option>
                                    #foreach($nodeGroup in $taskTrackerNodeGroups)
                                        <option value="$nodeGroup.name">$nodeGroup.name</option>
                                    #end
                                </select>
                            </div>
                            <label class="col-sm-2 control-label" style="width:10%">反馈客户端</label>

                            <div class="col-sm-2">
                                <select name="needFeedback" class="form-control">
                                    <option value="">-- 不限 --</option>
                                    <option value="true">需要</option>
                                    <option value="false">不需要</option>
                                </select>
                            </div>
                            <div class="col-sm-1" style="width:70px;">
                                <button class="btn btn-primary" type="button" id="searchBtn">
                                    搜索
                                </button>
                            </div>
                            <div class="col-sm-1">
                                <button class="btn btn-warning" type="reset" id="resetBtn">
                                    重置
                                </button>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-12">
            <div class="ibox">
                <div class="ibox-content" id="ltstableContainer">
                </div>
            </div>
        </div>
    </div>
    `
</div>

<div class="modal inmodal" id="job-edit-modal" role="dialog" aria-hidden="true" style="display: none;">
    <div class="modal-dialog" style="width: 600px;">
        <div class="modal-content animated rubberBand">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span
                        class="sr-only">关闭</span></button>
                <h2 class="modal-title">编辑任务信息</h2>
            </div>
            <div class="modal-body">
                <div class="row">
                    <form method="post" id="editForm" class="form-horizontal" onsubmit="return false">
                        <input type="hidden" class="form-control" name="jobId"/>
                        <input type="hidden" class="form-control" name="taskId"/>

                        <div class="form-group">
                            <label class="col-sm-2 control-label w_120">重复时间间隔</label>

                            <div class="col-sm-3 w_250">
                                <input type="text" class="form-control" name="repeatInterval" value="0"
                                       placeholder="毫秒数">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label w_120">重复次数</label>

                            <div class="col-sm-3 w_250">
                                <input type="text" class="form-control" name="repeatCount" value="-1"
                                       placeholder="-1表示无限重复, 必须大于等于 -1">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label w_120">反馈客户端</label>

                            <div class="col-sm-3 w_250">
                                <select name="needFeedback" class="form-control">
                                    <option value="true">需要</option>
                                    <option value="false" selected>不需要</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label w_120">优先级</label>

                            <div class="col-sm-3 w_250">
                                <input type="text" class="form-control" name="priority" value="100"
                                       placeholder="必须为数字，数值越小，优先级越大【必填】">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label w_120">最大重试次数</label>

                            <div class="col-sm-3 w_250">
                                <input type="text" class="form-control" name="maxRetryTimes" value="0"
                                       placeholder="最大重试次数, 必须为非负数【必填】">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label w_120">提交节点组</label>

                            <div class="col-sm-3 w_250">
                                <select name="submitNodeGroup" class="form-control">
                                    <option value="">-- 请选择提交节点组 --</option>
                                    #foreach($nodeGroup in $jobClientNodeGroups)
                                        <option value="$nodeGroup.name">$nodeGroup.name</option>
                                    #end
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label w_120">执行节点组</label>

                            <div class="col-sm-3 w_250">
                                <select name="taskTrackerNodeGroup" class="form-control">
                                    <option value="">-- 请选择执行节点组 --</option>
                                    #foreach($nodeGroup in $taskTrackerNodeGroups)
                                        <option value="$nodeGroup.name">$nodeGroup.name</option>
                                    #end
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label w_120">用户参数</label>

                            <div class="col-sm-4 w_340">
                                <textarea type="text" class="form-control" name="extParams"
                                          placeholder="请输入用户参数 JSON格式【非必填】"></textarea>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-1 col-sm-offset-3" style="width:70px;">
                                <button class="btn btn-primary" type="button" id="editBtn">
                                    修改
                                </button>
                            </div>
                            <div class="col-sm-1">
                                <button class="btn btn-warning" data-dismiss="modal">关闭</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script id="ltstable" type="text/html">
    <table class="table table-stripped toggle-arrow-tiny footable" data-page-size="10">
        <thead>
        <tr>
            <th data-toggle="true">任务ID</th>
            <th data-hide="all">提交节点组</th>
            <th>执行节点组</th>
            <th>重复时间间隔</th>
            <th>重复次数</th>
            <th data-hide="all">优先级</th>
            <th data-hide="all">最大重试次数</th>
            <th>反馈客户端</th>
            <th data-hide="all">用户参数</th>
            <th data-hide="phone,tablet">创建时间</th>
            <th data-hide="all">修改时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {{each rows as row index}}
        <tr>
            <td>{{row.taskId}}</td>
            <td>{{row.submitNodeGroup}}</td>
            <td>{{row.taskTrackerNodeGroup}}</td>
            <td>{{row.repeatInterval | format: 'repeatIntervalFormat', row}}</td>
            <td>{{row.repeatCount | format:'repeatCountFormat', row}}</td>
            <td>{{row.priority}}</td>
            <td>{{row.maxRetryTimes}}</td>
            <td>{{row.needFeedback | format:'needFeedbackLabel',row}}</td>
            <td>{{row.extParams | format:'stringifyJSON'}}</td>
            <td>{{row.gmtCreated | dateFormat:'yyyy-MM-dd HH:mm:ss'}}</td>
            <td>{{row.gmtModified | dateFormat:'yyyy-MM-dd HH:mm:ss'}}</td>
            <td>{{row.opt | format:'optFormat',row}}</td>
        </tr>
        {{/each}}
        {{if results == 0}}
        <tr>
            <td colspan="15">暂无数据</td>
        </tr>
        {{/if}}
        </tbody>
        <tfoot>
        <tr>
            <td colspan="9">
                <span>共{{results}}条记录，每页展示{{pageSize}}条</span>
                <ul class="pagination-sm pull-right"></ul>
            </td>
        </tr>
        </tfoot>
    </table>
</script>

<script>
    $(document).ready(function () {

        LTS.colFormatter.optFormat = function (v, row) {
            var logUrl = "job-logger.htm?taskId=" + row['taskId'] + "&taskTrackerNodeGroup=" + row['taskTrackerNodeGroup'];
            return '<a target="_blank" href="' + logUrl + '"><span class="label label-info"><i class="fa fa-file-code-o"></i> 日志</span></a>&nbsp;' +
                    '<a href="javascript:;" class="job-edit-btn"><span class="label label-success"><i class="fa fa-edit"></i> 编辑</span><span class="hidden lts-data">' + JSON.stringify(row) + '</span></a>&nbsp;' +
                    '<a href="javascript:;" class="job-suspend-btn" jobId="' + row['jobId'] + '" taskTrackerNodeGroup="' + row['taskTrackerNodeGroup'] + '"><span class="label label-success"><i class="fa fa-lock"></i> 暂停</span><span class="hidden lts-data">' + JSON.stringify(row) + '</span></a>&nbsp;' +
                    '<a href="javascript:;" class="job-del-btn" jobId="' + row['jobId'] + '" taskTrackerNodeGroup="' + row['taskTrackerNodeGroup'] + '"><span class="label label-primary" style="background-color: #DD6B55;"><i class="fa fa-trash-o"></i> 删除</span></a>';
        };

        $(document).on("click", ".job-del-btn", function () {
            var that = $(this);

            var jobId = that.attr("jobId");
            var taskTrackerNodeGroup = that.attr("taskTrackerNodeGroup");

            swal({
                title: "确认要删除该任务吗？",
                text: "在等待执行中的该条任务也将会被删除，请谨慎操作 !",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认删除",
                closeOnConfirm: false
            }, function (isConfirm) {
                if (isConfirm) {
                    $.ajax({
                        url: 'api/job-queue/repeat-job-delete',
                        type: 'POST',
                        dataType: 'json',
                        data: {jobId: jobId, taskTrackerNodeGroup: taskTrackerNodeGroup},
                        success: function (json) {
                            if (json && json.success) {
                                swal("删除成功!", "恭喜你", "success");
                                that.parents("tr").remove();
                            } else {
                                json ? swal(json['msg']) : {};
                            }
                        }
                    });
                }
            });
        });

        $(document).on("click", ".job-edit-btn", function () {
            var jobText = $(this).children("span.lts-data").text();
            var job = JSON.parse(jobText);

            $.each($('#editForm').parent().find(".form-control"), function () {
                var name = $(this).attr("name");
                var value = job[name];
                if (name == 'extParams') {
                    if (value == null) {
                        value = ''
                    } else {
                        value = JSON.stringify(value);
                    }
                } else {
                    value = value + '';
                }
                if ($(this)[0].tagName.toUpperCase() == 'SELECT') {
                    if (!value) {
                        value = "";
                    }
                    $(this).selectpicker('val', value);
                } else {
                    $(this).val(value);
                }
            });
            $("#job-edit-modal").modal("show");
        });

        $(document).on("click", "#editBtn", function () {
            var params = {};
            $.each($('#editForm').parent().find(".form-control"), function () {
                var name = $(this).attr("name");
                var value = $(this).val();
                params[name] = value;
            });
            var priority = params['priority'];
            if (!priority) {
                sweetAlert("请输入优先级", "必须为数字，数值越小，优先级越大【必填】", "error");
                return;
            }
            if (!LTS.ReExp.number.test(priority)) {
                sweetAlert("优先级格式错误", "必须为数字，数值越小，优先级越大【必填】", "error");
                return;
            }
            var repeatInterval = params['repeatInterval'];
            if (!repeatInterval) {
                sweetAlert("请输入重复时间间隔", "必须为数字，毫秒数", "error");
                return;
            }
            if (!LTS.ReExp.number.test(repeatInterval)) {
                sweetAlert("重复时间间隔", "必须为数字，毫秒数", "error");
                return;
            }
            var repeatCount = params['repeatCount'];
            if (!repeatCount) {
                sweetAlert("请输入重复次数", "-1表示无限重复, 必须大于等于 -1", "error");
                return;
            }
            if (repeatCount != -1 && !/^\d+$/.test(repeatCount)) {
                sweetAlert("重复次数格式错误", "-1表示无限重复, 必须大于等于 -1", "error");
                return;
            }
            if (!params['submitNodeGroup'] && params['needFeedback'] == 'true') {
                sweetAlert("请选择提交节点组", "需要反馈客户端必须选择提交节点组,如果列表中没有，请在节点组管理中添加，并启动改节点。", "error");
                return;
            }
            if (!params['taskTrackerNodeGroup']) {
                sweetAlert("请选择执行节点组", "如果列表中没有，请在节点组管理中添加，并启动改节点。", "error");
                return;
            }
            if (!params['maxRetryTimes'] || !/^\d+$/.test(params['maxRetryTimes'])) {
                sweetAlert("最大重试次数必须为非负整数", "最大重试次数必须为非负整数", "error");
                return;
            }
            params['cronExpression'] = null;
            var extParams = params['extParams'];
            if (extParams) {
                try {
                    JSON.parse(extParams)
                } catch (e) {
                    sweetAlert("用户参数格式错误", "必须为JSON格式", "error");
                    return;
                }
            }
            // 请求修改数据
            $.ajax({
                url: 'api/job-queue/repeat-job-update',
                type: 'POST',
                dataType: 'json',
                data: params,
                success: function (json) {
                    if (json && json.success) {
                        swal("修改成功!", "恭喜你", "success");
                        $("#form")[0].reset();
                        $("#form").find('select').selectpicker('render');
                        $("input[name='taskId']").val(params['taskId']);
                        $("#job-edit-modal").modal("hide");
                        $("#searchBtn").trigger("click");
                    } else {
                        json ? swal(json['msg']) : {};
                    }
                }
            });
        });

        $(document).on("click", ".job-suspend-btn", function () {
            var that = $(this);

            var jobId = that.attr("jobId");
            var taskTrackerNodeGroup = that.attr("taskTrackerNodeGroup");

            swal({
                title: "确认要暂停该任务吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认暂停",
                closeOnConfirm: false
            }, function (isConfirm) {
                if (isConfirm) {
                    $.ajax({
                        url: 'api/job-queue/repeat-job-suspend',
                        type: 'POST',
                        dataType: 'json',
                        data: {jobId: jobId, taskTrackerNodeGroup: taskTrackerNodeGroup},
                        success: function (json) {
                            if (json && json.success) {
                                swal("暂停成功!", "恭喜你", "success");
                                that.parents("tr").remove();
                            } else {
                                json ? swal(json['msg']) : {};
                            }
                        }
                    });
                }
            });
        });

        var ltsTable = $("#ltstableContainer").ltsTable({
            url: 'api/job-queue/repeat-job-get',
            templateId: 'ltstable'
        });

        $(document).on("click", "#searchBtn", function () {
            var params = {};
            $.each($('#form').parent().find(".form-control"), function () {
                var name = $(this).attr("name");
                var value = $(this).val();
                params[name] = value;
            });
            ltsTable.post(params, 1);
        });
        $("#searchBtn").trigger("click");
    });
</script>