<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>uniRobot - Schdule info</title>

        <link rel="stylesheet" href="{{ url_for('static', filename='lib/easyui/themes/icon.css') }}">
        <link rel="stylesheet" href="{{ url_for('static', filename='lib/easyui/themes/bootstrap/easyui.css') }}">
        <link rel="stylesheet" href="{{ url_for('static', filename='css/auto.css') }}">

        <link rel="icon" type="image/x-icon" href="{{ url_for('static', filename='favicon.ico') }}" />
        <style type="text/css">
            .lines-no .datagrid-body td{
                border-right:1px dotted transparent;
                border-bottom:1px dotted transparent;
            }
        </style>
    </head>
    <body class="easyui-layout" style="padding:10">

        <div data-options="region:'center'" style="padding: 5px 5px 5px 5px">
            <table id="schedulejob_list" class="easyui-datagrid" style="width:100%;height:auto;"
                    data-options="fit:true, singleSelect:true,
                        fitColumns:true,
                        url:'/api/v1/task_list?method=get_schedulejoblist&project=all',
                        method:'get',
                        toolbar: toolbar"
                    >
                <thead>
                    <tr>
                        <th data-options="field:'task_no',align:'center' , halign: 'center'">任务ID</th>
                        <th data-options="field:'user',align:'center' , halign: 'center'">用户</th>
                        <th data-options="field:'project',align:'center' , halign: 'center'">项目</th>
                        <th data-options="field:'task_name',align:'center' , halign: 'center'">任务名称</th>
                        <th data-options="field:'status',align:'center' , halign: 'center',formatter:show_img">状态</th>
                        <th data-options="field:'next_time',align:'center' , halign: 'center'">下次调度</th>
                        <th data-options="field:'schedule_type',align:'center' , halign: 'center'">调度类型</th>
                        <th data-options="field:'year',align:'center' , halign: 'center'">年</th>
                        <th data-options="field:'mon',align:'center' , halign: 'center'">月</th>
                        <th data-options="field:'day',align:'center' , halign: 'center'">日</th>
                        <th data-options="field:'hour',align:'center' , halign: 'center'">时</th>
                        <th data-options="field:'min',align:'center' , halign: 'center'">分</th>
                        <th data-options="field:'sec',align:'center' , halign: 'center'">秒</th>
                        <th data-options="field:'week',align:'center' , halign: 'center'">第几周</th>
                        <th data-options="field:'day_of_week',align:'center' , halign: 'center'">每周几</th>
                        <th data-options="field:'start_date',align:'center' , halign: 'center'">开始时间</th>
                        <th data-options="field:'end_date',align:'center' , halign: 'center'">结束时间</th>
                        <th data-options="field:'sponsor',align:'center' , halign: 'center'">发起方</th>
                    </tr>
                </thead>
            </table>
        </div>

        <div id="edit_schedulejob" class="easyui-window" title="修改调度任务"
                 data-options="modal:true,closed:true,minimizable:false,maximizable:false,collapsible:false"
                 style="width:530px;height:450px;padding:10px;">
                <form id="edit_schedulejob_ff" method="post">
                    <div style="margin-bottom:10px">
                        <input id="task_name", name="task_name" class="easyui-textbox" data-options="label:'任务名称:',labelPosition:'left',editable:false " style="width:80%;">
                    </div>
                    <div style="margin-bottom:10px">
                    <p>选择调度类型: 配置参见<a href="https://apscheduler.readthedocs.io/en/stable/userguide.html" target="_blank">APScheduler</a></p>
                        <select id="schedule_type" class="easyui-combobox" name="schedule_type" style="width:200px;" >
                            <option value="interval">间隔调度(interval)</option>
                            <option value="cron">周期调度(corn)</option>
                            <option value="date">定时调度(date)</option>
                        </select>
                    </div>

                    <div id="p" class="easyui-panel" style="width:450px;height:100px;padding:10px;">
                        <table border="0">
                          <tr>
                            <th align="center">年</th><th align="center">月</th><th align="center">日</th><th align="center">时</th><th align="center">分</th><th align="center">秒</th>
                          </tr>
                          <tr>
                            <td><input id="year" name="year" class="easyui-textbox" style="width:80%;"></td>
                            <td><input id="mon" name="mon" class="easyui-textbox" style="width:80%;"></td>
                              <td><input id="day" name="day" class="easyui-textbox" style="width:80%;"></td>
                            <td><input id="hour" name="hour" class="easyui-textbox" style="width:80%;"></td>
                              <td><input id="min" name="min" class="easyui-textbox" style="width:80%;"></td>
                            <td><input id="sec" name="sec" class="easyui-textbox" style="width:80%;"></td>
                          </tr>
                        </table>
                    </div>

                    <div id="p2" class="easyui-panel" style="width:450px;height:100px;padding:10px;margin-top:10px">
                        <table border="0">
                          <tr>
                            <th align="center">第几周</th><th align="center">每周几</th><th align="center">开始日期</th><th align="center">结束日期</th>
                          </tr>
                          <tr>
                            <td><input id="week" name="week" class="easyui-textbox" style="width:90%;"></td>
                            <td><input id="day_of_week" name="day_of_week" class="easyui-textbox" style="width:90%;"></td>
                              <td><input id="start_date" name="start_date" class="easyui-textbox" style="width:90%;"></td>
                            <td><input id="end_date" name="end_date" class="easyui-textbox" style="width:90%;"></td>

                          </tr>
                        </table>
                    </div>

                </form>
                <div style="text-align:right;padding:5px 0">
                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="close_win('edit_schedulejob')" style="width:60px">Cancle</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="do_edit_schedulejob('edit_schedulejob', 'edit_schedulejob_ff')" style="width:60px">Modify</a>
                </div>
            </div>

        <script src="{{ url_for('static', filename='lib/easyui/jquery.min.js') }}"></script>
        <script src="{{ url_for('static', filename='lib/easyui/jquery.easyui.min.js') }}"></script>
        <!-- 自定义js库 -->
        <script type="text/javascript" src="{{ url_for('static', filename='lib/auto.js') }}"></script>

        <SCRIPT type="text/javascript">
            function edit_schedulejob(win_id, ff_id, method) {
                if (method == "edit_schedulejob") {
                    clear_form(ff_id);
                    var row = $('#schedulejob_list').datagrid('getSelected');
                    $("#task_name").textbox("setValue", row.task_no+'_#'+row.user+'_#'+row.project);
                    $('#schedule_type').combobox('setValue', 'interval');
                }
                open_win(win_id);
            }

            function do_edit_schedulejob(win_id, ff_id) {
                var data = $("#{0}".lym_format(ff_id)).serializeObject();
                data["method"] = "edit_schedulejob";

                do_ajax('post', '/api/v1/task_list/', data, function(m){ $("#schedulejob_list").datagrid('reload'); do_msg(m)});

                close_win(win_id);
            }
        </SCRIPT>

        <SCRIPT type="text/javascript">
            $(document).ready(function(){
               $('#task_scheduler').datagrid('getPanel').removeClass('lines-both lines-no lines-right lines-bottom').addClass('lines-no');
            });
            var toolbar = [
                {
                    text:'刷新',
                    iconCls:'icon-refresh',
                    handler:function(){
                        $("#schedulejob_list").datagrid('reload');
                    }
			    }, '-',
			    {
			        text:'冻结任务',
                    iconCls:'icon-stop',
                    handler:function(){
                        var row = $('#schedulejob_list').datagrid('getSelected');
                        if(!row){
                            show_msg("Alert", "请先选择一个任务！");
                        }
                        else{
                            $.messager.confirm('Alert', '确定要冻结该任务吗?', function(r){
                                if (r){
                                    var data ={"method": "pause", "task_no": row.task_no, "user": row.user, "project": row.project, "task_name": row.task_name };
                                    do_ajax('post',
                                    '/api/v1/task_list/',
                                    data,
                                    function(r){do_msg(r); $("#schedulejob_list").datagrid('reload');});
                                }
                            });
                        }
                    }
			    }, '-',
			    {
			        text:'恢复任务',
                    iconCls:'icon-resume',
                    handler:function(){
                        var row = $('#schedulejob_list').datagrid('getSelected');
                        if(!row){
                            show_msg("Alert", "请先选择一个任务！");
                        }
                        else{
                            $.messager.confirm('Alert', '确定要恢复该调度任务吗?', function(r){
                                if (r){
                                    var data ={"method": "resume", "task_no": row.task_no, "user": row.user, "project": row.project, "task_name": row.task_name };
                                    do_ajax('post',
                                    '/api/v1/task_list/',
                                    data,
                                    function(r){do_msg(r); $("#schedulejob_list").datagrid('reload');});
                                }
                            });
                        }
                    }
			    }, '-',
			    {
			        text:'加入调度',
                    iconCls:'icon-expand',
                    handler:function(){
                        var row = $('#schedulejob_list').datagrid('getSelected');
                        if(!row){
                            show_msg("Alert", "请先选择一个任务！");
                        }
                        else{
                            $.messager.confirm('Alert', '确定要加入该调度任务吗?', function(r){
                                if (r){
                                    var data ={"method": "add_job2schedule", "task_no": row.task_no, "user": row.user, "project": row.project, "task_name": row.task_name };
                                    do_ajax('post',
                                    '/api/v1/task_list/',
                                    data,
                                    function(r){do_msg(r); $("#schedulejob_list").datagrid('reload');});
                                }
                            });
                        }

                    }
			    }, '-',
			    {
			        text:'修改调度参数',
                    iconCls:'icon-edit',
                    handler:function(){
                        var row = $('#schedulejob_list').datagrid('getSelected');
                        if(row){
                            edit_schedulejob("edit_schedulejob", "edit_schedulejob_ff", "edit_schedulejob");
                        }
                        else{
                            show_msg("Alert", "请先选择一个任务！");
                        }
                    }
			    }, '-',
			    {
			        text:'删除任务',
                    iconCls:'icon-remove',
                    handler:function(){
                        var row = $('#schedulejob_list').datagrid('getSelected');
                        if(!row){
                            show_msg("Alert", "请先选择一个任务！");
                        }
                        else{
                            $.messager.confirm('Alert', '确定要删除该调度任务吗?', function(r){
                                if (r){
                                    var data ={"method": "remove_schedulejob", "task_no": row.task_no, "user": row.user, "project": row.project, "task_name": row.task_name };
                                    do_ajax('post',
                                    '/api/v1/task_list/',
                                    data,
                                    function(r){do_msg(r); $("#schedulejob_list").datagrid('reload');});
                                }
                            });
                        }
                    }
			    }, '-',
			    {
			        text:'删除所有自己的调度任务',
                    iconCls:'icon-remove',
                    handler:function(){
                        $.messager.confirm('Alert', '<br>确认要删除当前用户的调度任务吗?', function (r) {
                            if (r) {
                                var data = {
                                    "method": "remove_myschedulejobs"
                                };
                                do_ajax('post', "/api/v1/task_list/", data, function(r){$("#schedulejob_list").datagrid('reload'); do_msg(r);});
                            }
                        });
                    }
			    }
			 ];

        </SCRIPT>
    </body>
</html>
