{{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>
			<li><a href="/front/logic/pre"><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":"25%"},
			{ "data": "i_runstate",render:function(val){
					if(val==1){
						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>　'+
					'</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_logicname",placeholder:"数据清洗",must:true},
					{label:"描述",s_label:"s_descript",type:"tpl_text",must:true},
					{s_label:"_id",type:"tpl_hidden"},
				];
				/*按钮*/
				//新增保存按钮
				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){
								console.log(obj)
								$.post("/front/logic/pre/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>
