<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>uniRobot - 故障注入</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="task_scheduler" class="easyui-datagrid" style="width:100%;height:auto;"
                    data-options="singleSelect:true,
                        fitColumns:true,
                        url:'/api/v1/task_list/?method=query',
                        method:'post',
                        toolbar: toolbar"
                    >
                <thead>
                    <tr>
                        <th data-options="field:'status',align:'center' , halign: 'center',formatter:do_view_status"></th>
                        <th data-options="field:'name'">故障名称</th>
                        <th data-options="field:'next_time',align:'center' , halign: 'center'">描述</th>
                        <th data-options="field:'cron',align:'center' , halign: 'center'">脚本</th>
                        <th data-options="field:'task',align:'center' , halign: 'center',formatter:do_view_task">列表：任务名称/实施对象</th>
                    </tr>
                </thead>
            </table>
        </div>
        <!-- edit project cron -->
        <div id="cron_win" class="easyui-window" title="Edit Cron"
             data-options="iconCls:'icon-edit',collapsible:false,minimizable:false,maximizable:false"
             style="width:400px;height:140px;padding:10px;">
            <form id="cron_ff" method="post">
                <div style="margin-bottom:20px">
                    <select class="easyui-combobox" name="cron" id="cron" label="Cron Config" style="width:100%">
                        <option value="* * * * * *">Default(* * * * * *)</option>
                        <option value="0 0 1 * * *">Once at 1:00 per day(0 0 1 * * *)</option>
                        <option value="0 0 12 * * *">Once at 12:00 per day(0 0 12 * * *)</option>
                        <option value="0 0 23 * * *">Once at 23:00 per day(0 0 23 * * *)</option>
                        <option value="0 0/30 * * * *">Once per 30 Min(0 0/5 * * * *)</option>
                        <option value="0 0 0/1 0 0 6/7">Once each hour at weekend(0 0 0/1 0 0 6/7)</option>
                        <option value="0 0 0/1 0 0 1/5">Once each hour at weekday(0 0 0/1 0 0 1/5)</option>
                    </select>
                </div>
                <div style="text-align:right;margin-bottom:10px">
                    <a href="#" class="easyui-linkbutton" style="width:80px;height:32px" onclick="do_edit();">刷新</a>
                </div>
            </form>
        </div>
        <!-- -->
        <!-- edit project notifys -->
        <div id="notify_win" class="easyui-window" title="Email Report"
             data-options="iconCls:'icon-notification',collapsible:false,minimizable:false,maximizable:false"
             style="width:400px;height:210px;padding:10px;">
            <form id="notify_ff" method="post">

                <div style="margin-bottom:10px">
                    <input class="easyui-textbox" id="success_list" name="success_list" label="Pass List" labelPosition="left" style="width:100%;height:50px;" data-options="multiline:true">
                </div>
                <div style="margin-bottom:10px">
                    <input class="easyui-textbox" id="fail_list" name="fail_list" label="Fail List" labelPosition="left" style="width:100%;height:50px;" data-options="multiline:true">
                </div>

                <div style="text-align:right;margin-bottom:10px">
                    <a href="#" class="easyui-linkbutton" style="width:80px;height:32px" onclick="do_notify_edit();">Save</a>
                </div>
            </form>
        </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">
            $(document).ready(function(){
               $('#task_scheduler').datagrid('getPanel').removeClass('lines-both lines-no lines-right lines-bottom').addClass('lines-no');
               $("#cron_win").window('close');
               $("#notify_win").window('close');
            });
            var toolbar = [
                {
                    text:'新增故障场景',
                    iconCls:'icon-refresh',
                    handler:function(){
                        $("#task_scheduler").datagrid('reload');
                    }
			    }, '-',
			    {
			        text:'删除故障场景',
                    iconCls:'icon-run',
                    handler:function(){
                        var row = $('#task_scheduler').datagrid('getSelected');
                        if(row){
                            var data ={"method": "start", "name": "{0}".lym_format(row.name), "cron": "{0}".lym_format(row.cron) };
                            do_ajax('post',
                                '/api/v1/task_list/',
                                data,
                                do_msg);

                            $("#task_scheduler").datagrid('reload');
                        }
                        else{
                            show_msg("Alert", "Please select one record！");
                        }
                    }
			    }, '-',
			    {
			        text:'Git导入场景',
                    iconCls:'icon-stop',
                    handler:function(){
                        $.messager.confirm('Alert', 'Wont generate report，still stop ?', function(r){
                            if (r){
                                var row = $('#task_scheduler').datagrid('getSelected');
                                if(row){
                                    var data ={"method": "stop", "name": "{0}".lym_format(row.name) };
                                    do_ajax('post',
                                        '/api/v1/task_list/',
                                        data,
                                        do_msg);
                                    $("#task_scheduler").datagrid('reload');
                                }
                                else{
                                    show_msg("Alert", "Please select one record!");
                                }
                            }
                        });

                    }
			    }, '-',
			    {
			        text:'修改脚本/手动注入',
                    iconCls:'icon-edit',
                    handler:function(){
                        var row = $('#task_scheduler').datagrid('getSelected');
                        if(row){
                            open_win('cron_win');
                        }
                        else{
                            show_msg("Alert", "Please select one record！");
                        }
                    }
			    }, '-',
			    {
			        text:'列表：停止/恢复，任务名称，注入对象',
                    iconCls:'icon-notification',
                    handler:function(){
                        var row = $('#task_scheduler').datagrid('getSelected');
                        if(row){
                            init_email_ff(row["name"]);
                            open_win("notify_win");
                        }
                        else{
                            show_msg("Alert", "Please select one record！");
                        }
                    }
			    }
			 ];

			function do_edit(){
			    var row = $('#task_scheduler').datagrid('getSelected');
			    var data = {"name": row["name"], "method": "edit", "cron": $("#cron").combobox("getValue")};
			    do_ajax('post',
                    '/api/v1/task_list/',
                    data,
                    do_msg);
                $("#task_scheduler").datagrid('reload');
                close_win("cron_win");
			}

			function do_notify_edit(){
			    var row = $('#task_scheduler').datagrid('getSelected');
			    var data = {
			        "method": "email",
			        "project": row["name"],
			        "success_list": $("#notify_ff input#success_list").textbox('getValue'),
			        "fail_list": $("#notify_ff input#fail_list").textbox('getValue'),
			    };
			    do_ajax('post',
                    '/api/v1/settings/',
                    data,
                    do_msg);
                close_win("notify_win");
			}

			function do_view_task(value, row, index){
                return '<a href="#" onclick="parent.addTab(\'{0}\', \'/task_list/{1}\', \'icon-task\');">查看</a>'.lym_format(row.name, row.name);
            }

            function do_view_status(value, row, index){
                return '<img width="24px" height="24px" border="0" src="{0}"/>'.lym_format(row.status) ;
            }
        </SCRIPT>
    </body>
</html>
