{{include "com/inc.html"}}
<!-- Main Header -->
{{include "com/header.html"}}
<!-- Left side column. 权限菜单 -->
{{include "com/menu.html"}}

<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="/front/logic"><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>
{{include "com/dialog.html"}}
{{include "com/footer.html"}}
<script>
menuActive("logic/list")
$(function () {
	ttable=$('#dataTable').DataTable({
		"paging"      : false,
		"lengthChange": false,
		"searching"   : false,
		"ordering"    : false,
		"info"        : true,
		"autoWidth"   : false,
		"ajax": {
			"url": "/logic/list",
			"type": "post",
			"data":{}
		 },
		"language": {
            "url": "/dist/js/dataTables.chinese.lang"
        },
		"columns": [
            { "data": "s_taskname"},
			{ "data": "l_comeintime",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":"23%"},
			{ "data": "_id","width":"21%",render:function(row){
				add_logic = '<div>'+
						'<a class="btn btn-sm btn-primary" href="/front/logic/pre">预处理</a>　'+
						'<a class="btn btn-sm btn-success" href="/front/logic/match">匹配</a>　'+
						'<a class="btn btn-sm btn-info" href="/front/logic/clean">清洗</a>　'+
						'</div>'
				return add_logic
			}},
			{ "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-danger opr" opr="del" onclick="del(\''+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 "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/dataprocess/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},
					{label:"预处理逻辑",s_label:"s_preprocesslogic",type:"tpl_list_local",url:""},
					{label:"匹配逻辑",s_label:"s_class",type:"tpl_list_ajax",url:"",fun:function(){
						var ids=$("#s_class").data("ids")
						var parentDiv=$("#s_class").closest("div")
						ids=ids||{}
						var tid=$(this).attr("_id")
						if(!ids[tid]){
							ids[tid]=true
							$("#s_class").data("ids",ids)
							var tpl1=$('<div class="alert alert-dismissible alert-success" style="min-width:50px;max-width:250px;font-size:10px;padding:3px;margin:5px;display:inline-block"><button type="button" class="close" data-dismiss="alert" style="right:0px;">&times;</button><span></span></div>')
							tpl1.find("span").text($(this).text())							
							tpl1.attr("tid",tid)
							tpl1.find("button").click(function(){
								var ttid=$(this).closest(".alert").attr("tid");
								var iids=$("#s_class").data("ids")
								delete iids[ttid]
								$("#s_class").val(function(){
									var strid=[]
									for(var k in iids){
										strid.push(k)
									}
									return strid.join(",")
								}())
							})
							$("#s_show",parentDiv).append(tpl1)
							$("#s_class").val(function(){
								var strid=[]
								for(var k in ids){
									strid.push(k)
								}
								return strid.join(",")
							}())
						}
					}},
					//{label:"匹配逻辑",s_label:"s_matchlogic",type:"tpl_list_local",url:""},
					{label:"字段",s_label:"s_field",placeholder:"title"},
					{label:"起始id",s_label:"s_extlastid",must:true},
					{s_label:"_id",type:"tpl_hidden"},
				];
				/*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("/task/save",obj,function(data){
									if(data&&data.rep){
										//window.location.href="/task/list"
										$('#myModal').modal('hide');
										ttable.ajax.reload();
									}else{
										alert(data.msg)
									}
								},'json')
							}else{
								alert("红色标签的表单不能为空！")
							}
						}
					}
				];
				if(n == "new"){
					_tit="新增抽取任务";
					tag = com.pushArry(tag,addtask);
					bts = com.pushArry(bts,addtaskbtn);
				}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:"/task/start",
			type:"post",
			data:{"_id":_id},
			success:function(r){
				//window.location.reload();
				ttable.ajax.reload();
			}
		})
	});
}
function stop(_id){
	showConfirm("确定停止？", function() {
		$.ajax({
			url:"/task/stop",
			type:"post",
			data:{"_id":_id},
			success:function(r){
				//window.location.reload()
				ttable.ajax.reload();
			}
		})
	});
}
function del(_id){
	showConfirm("确定启动？", function() {
		$.ajax({
			url:"/task/del",
			type:"post",
			data:{"_id":_id},
			success:function(r){
				ttable.ajax.reload();
			}
		})
	});
}
</script>
