{{template "inc"}}
<!-- Main Header -->
{{template "header"}}
<!-- Left side column. 权限菜单 -->
{{template "memu"}}

<div class="content-wrapper" id="showbtn">
	<section class="content-header">
		<h1>
			<small><a class="btn btn-primary opr" opr="new">新增任务</a></small>
		</h1>
		<ol class="breadcrumb">
		  <li><a href="/admin/task/list"><i class="fa fa-dashboard"></i> 任务管理</a></li>		  
		</ol>
    </section>
  <!-- Main content -->
  <section class="content">
      <div class="row">
	      <div class="col-xs-12">
	        <div class="box">
		        <div class="box-body">
		            <table id="dataTable" class="table table-bordered table-hover">
		              <thead>
		              <tr>
		                <th>任务名称</th> <th>创建时间</th> <th>创建人</th> <th>描述</th><th>运行状态</th> <th>操作</th>
		              </tr>
		              </thead>
		            </table>
		        </div>
	          <!-- /.box-body -->
	        </div>
        <!-- /.box -->
		</div>
	</div>
  </section>
</div>
{{template "dialog"}}
{{template "footer"}}
<script>
menuActive("task")
$(function () {
	ttable=$('#dataTable').DataTable({
		"paging"      : false,
		"lengthChange": false,
		"searching"   : false,
		"ordering"    : false,
		"info"        : true,
		"autoWidth"   : false,
		"ajax": {
			"url": "/admin/task/list",
			"type": "post",
			"data":{}
		 },
		"language": {
            "url": "/res/dist/js/dataTables.chinese.lang"
        },
		"columns": [
            { "data": "s_taskname"},
			{ "data": "l_createtime",render:function(val){
				var dt=new Date()
				dt.setTime(parseInt(val)*1000)
				return dt.format("yyyy-MM-dd hh:mm:ss")
			}},
			{ "data": "s_username"},
			{ "data": "s_descript","width":"25%"},
			{ "data": "runstatus",render:function(val){
				if(val){
					return "<i class='fa fa-fw fa-circle text-green'></i>运行中"
				}else{
					return "<i class='fa fa-fw fa-circle text-danger'></i>未启动"
				}
			}},
			{ "data":"_id","width":"25%",render:function(val,a,row,pos){
				tmp = '<div>'+
					'<a class="btn btn-sm btn-primary opr" opr="edit" row="'+pos.row+'" >编辑</a>　'+
					'<a class="btn btn-sm btn-success opr" opr="start" onclick="start(\''+row._id+'\')">启动</a>　'+
					'<a class="btn btn-sm btn-info opr" opr="stop" onclick="stop(\''+row._id+'\')">停止</a>　'+
					//'<a class="btn btn-sm btn-warning opr" opr="test" taskid="'+val+'">测试</a>'+
					'</div>';
				return  tmp
			}}
       	]
	});
	ttable.on('init.dt', function () {
		$("#showbtn").on('click','a.opr',function(){
			var n=$(this).attr("opr");
			var taskid=$(this).attr("taskid");
			var htmlObj={},obj,tag=[],bts=[];
			var _tit="";
			switch(n){
			case "edit":			
				obj=ttable.row($(this).closest("tr")).data();	
			case "test":
				obj=ttable.row($(this).closest("tr")).data();
			case "new":
				/*表单*/
				addtask=[
					{label:"任务名称",s_label:"s_taskname",placeholder:"剑鱼抽取",must:true},
					{label:"源库连接",s_label:"s_mgoaddr",must:true},
					{label:"源数据库",s_label:"s_mgodb",must:true},
					{label:"源表",s_label:"s_mgocoll",must:true},
					{label:"保存表",s_label:"s_mgosavecoll",placeholder:"127.0.0.1:27080/extract/bidding(数据库地址/数据库/表)",must:true},
					{label:"描述",s_label:"s_descript",type:"tpl_text"},
					/*
					{label:"是否追踪",s_label:"i_track",type:"tpl_list_local",must:true,list:[{"s_name":"是","_id":1},{"s_name":"否","_id":0}],default:0,fun:function(){
						var to=$("#i_track")					
						to.val($(this).attr("_id"))
						$("#s_show",to.closest("div")).val($(this).text())
						//追踪表样式
						$("#s_trackcoll").attr("must",$(this).attr("_id")==1)
						$("#s_trackcoll").closest("div.row").find("label").css("color",$(this).attr("_id")==1?"red":"")
					}},
					{label:"追踪记录表",s_label:"s_trackcoll",must:function(){
						return  obj&&obj.i_track
					}()},
					{label:"是否统计",s_label:"i_count",type:"tpl_list_local",must:true,list:[{"s_name":"是","_id":1},{"s_name":"否","_id":0}],default:0},
					*/
					{label:"使用版本",s_label:"s_version",type:"tpl_list_local",must:true,url:"/admin/task/getversion"},
					{label:"并发数量",s_label:"i_process",placeholder:"5",must:true},
					{s_label:"_id",type:"tpl_hidden"},
					{label:"起始id",s_label:"s_extlastid",must:true}
				];
				/*testtask=[
					{label:"起始id",s_label:"s_startid",must:true},
					{label:"数据数量",s_label:"s_datanum",placeholder:"5",must:true}
				];*/
				/*按钮*/
				//新增保存按钮
				addtaskbtn=[
					{label:"保存",class:"btn-primary",
						fun:function(){
							var obj={}
							var bcon=true
							$("#_con").find("input[id!=s_show],textarea").each(function(i,el){
								var val=$(el).val();
								if(el.id!="_id"&&$(el).attr("must")&&!val){
									bcon=false
									return false
								}
								obj[el.id]=$(el).val()
							})
							if (bcon){								
								$.post("/admin/task/save",obj,function(data){
									if(data&&data.rep){
										window.location.href="/admin/task"								
									}else{
										alert(data.msg)
									}
								},'json')
							}else{
								alert("红色标签的表单不能为空！")
							}
						}
					}
				];
				//测试启动按钮
				/*testtaskbtn=[
					{label:"Run",class:"btn-danger",
						fun:function(){
							var reg = /^[0-9]*$/;
							var id = $("#s_startid").val();
							var num = $("#s_datanum").val();
							var bcon = true;
							if(id == ""){
								bcon = false;
								alert("红色标签的表单不能为空！");
								return
							}
							if(num != ""){//验证数字
								if(!reg.test(num)){
									bcon = false;
									alert("数据数量填写错误！");
									return
								}
							}else{
								num = "5";
							}
							if(bcon){								
								$.post("/admin/task/test",{"startid":id,"num":num,"taskid":taskid},function(data){
									if(data&&data.rep){
										$("#myModal").modal("hide");
										alert("测试完成!");								
									}else{
										alert("起始id输入错误!");	
									}
								},'json')
								
							}else{
								alert("红色标签的表单不能为空！")
							}
						}
					}
				];*/
				
				if(n == "new"){
					_tit="新增抽取任务";
					tag = com.pushArry(tag,addtask);
					bts = com.pushArry(bts,addtaskbtn);
				//}else if(n == "test"){
				//	_tit="测试任务";
				//	tag = com.pushArry(tag,testtask);
				//	bts = com.pushArry(bts,testtaskbtn);
				}else if(n == "edit"){
					_tit="编辑-"+obj.s_taskname;
					tag = com.pushArry(tag,addtask);
					bts = com.pushArry(bts,addtaskbtn);
				}
				htmlObj={
					title:_tit,
					tag:tag,
					bts:bts
				}
			OpenDialog(htmlObj,obj)
			break;
			}
		});
	})
	
})
function start(_id){
	showConfirm("确定启动？", function() {
		$.ajax({
			url:"/admin/task/start",
			type:"post",
			data:{"_id":_id},
			success:function(r){
				window.location.reload()
			}
		})
	});
}
function stop(_id){
	showConfirm("确定停止？", function() {
		$.ajax({
			url:"/admin/task/stop",
			type:"post",
			data:{"_id":_id},
			success:function(r){
				window.location.reload()
			}
		})
	});
}
</script>
