{{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">新增{{.cname}}规则</a></small>
		</h1>
		<ol class="breadcrumb">
			<li><a href="/admin/audit/recogfield"><i class="fa fa-dashboard"></i> 识别字段</a></li>
			<li class="active"><a href="/admin/rulemanager/getclasslist?id={{.fid}}&fname={{.cname}}">分类列表</a></li>
		  	<li class="active"><a href="/admin/rulemanager/getrulelist?id={{.cid}}&cname={{.cname}}&fid={{.fid}}">规则列表</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="rulemanagerTable" class="table table-bordered table-hover">
		              <thead>
		              <tr>
		                <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("recogfield")
var cid = {{.cid}};
var cname = {{.cname}};
var fid = {{.fid}};
$(function () {
	ttablerulemanager=$('#rulemanagerTable').DataTable({
		"paging"      : true,
		"lengthChange": false,
		"searching"   : true,
		"ordering"    : false,
		"info"        : true,
		"autoWidth"   : false,
		"ajax": {
			"url": "/admin/rulemanager/getrule",
			"type": "post",
			"data":{"cid":cid}
		},
		"language": {
            "url": "/res/dist/js/dataTables.chinese.lang"
        },
		"columns": [
            { "data": "s_name"},
			{ "data": "l_createtime"},
			{ "data": "s_user"},
			{ "data": "_id",render:function(val,a,row,meta){
				var udhtml = '&nbsp;&nbsp;<a class="btn btn-sm btn-success opr" opr="moveup" num="'+meta.row+'">上移</a>'+
					'&nbsp;&nbsp;<a class="btn btn-sm btn-success opr" opr="movedown" num="'+meta.row+'">下移</a>';
				if({{.data.rule}}.length ==1){//一条数据
					udhtml = '&nbsp;&nbsp;<a class="btn btn-sm btn-success btn-default opr" opr="moveup" num="'+meta.row+'" disabled>上移</a>'+
					'&nbsp;&nbsp;<a class="btn btn-sm btn-success btn-default opr" opr="movedown" num="'+meta.row+'" disabled>下移</a>';
				}else if(meta.row == 0){//第一行
					udhtml = '&nbsp;&nbsp;<a class="btn btn-sm btn-success btn-default opr" opr="moveup" num="'+meta.row+'" disabled>上移</a>'+
					'&nbsp;&nbsp;<a class="btn btn-sm btn-success opr" opr="movedown" num="'+meta.row+'">下移</a>';
				}else if(meta.row+1 == {{.data.rule}}.length){//最后一行
					udhtml = '&nbsp;&nbsp;<a class="btn btn-sm btn-success opr" opr="moveup" num="'+meta.row+'">上移</a>'+
					'&nbsp;&nbsp;<a class="btn btn-sm btn-success btn-default opr" opr="movedown" num="'+meta.row+'" disabled>下移</a>';
				}
				return '<a class="btn btn-sm btn-info opr" opr="edit" num="'+meta.row+'">编辑</a>'+
					'&nbsp;&nbsp;<a class="btn btn-sm btn-warning opr" opr="ruletest" num="'+meta.row+'">测试</a>'+
					'&nbsp;&nbsp;<a class="btn btn-sm btn-danger" onclick="del(\''+val+'\')">删除</a>'+udhtml
					
			}}
       	]
	});
	ttablerulemanager.on('init.dt', function () {
		$("#showbtn").on('click','a.opr',function(){
			var n=$(this).attr("opr");
			var rownum=parseInt($(this).attr("num"));
			var tobj = {{.data.rule}}[rownum];//本行数据
			var _tit="",htmlObj={},obj,tag=[];
			var delid=$(this).attr("value");
			switch(n){
			//测试规则
			case "ruletest":
				htmlObj={
					title:"测试_规则:"+tobj["s_name"],
					content:"<div>测试规则...</div>",
					tag:[
						{s_label:function(){var str="";for(var i in tobj["s_rule"]){str+="<p>"+(parseInt(i)+1)+"、"+tobj["s_rule"][i]+"</p>";}return str
								}(),type:"tpl_small"},
						{s_label:"s_con",type:"tpl_text",rows:18},
						{s_label:"_id",type:"tpl_hidden",val:tobj["_id"]}
					],
					bts:[{label:"RUN",class:"btn-danger",
							fun:function(){
								var form = $('<form/>').appendTo("body");  
							    // 设置属性  
							    form.attr('action','/admin/rulemanager/runruletest').attr('method', 'post').attr('target', '_blank');  
							    // 创建Input  
							    var my_input = $('<input type="text" name="_id" />'); 
							    my_input.attr('value', $("#_id").val()); 
								var mytext = $('<textarea name="s_con" />') 
								mytext.val($("#s_con").val())
							    // 附加到Form  
							    form.append(my_input).append(mytext); 
							    // 提交表单  
							    form.submit();  
								form.remove();
							}
						}]
				}
			OpenDialog(htmlObj,obj);
			break;
			case "edit":			
				obj=ttablerulemanager.row($(this).closest("tr")).data();
			case "new":
				tag=[
					{label:"名称",s_label:"s_name",must:true},
					{label:"代码",s_label:"s_code",must:true},
					//{label:"标识",s_label:"s_isok",must:true,type:"tpl_list_local",list:[{"s_name":"wrong","_id":"wrong"},{"s_name":"right","_id":"right"}],default:"wrong"},
					{label:"前置过滤",s_label:"s_rule_prerule",type:"tpl_text",placeholder:"XXX__YYY"},
					{label:"规则",s_label:"s_rule",type:"tpl_text",rows:8,must:true},
					{s_label:"_id",type:"tpl_hidden"},
					{s_label:"s_classid",type:"tpl_hidden",val:cid},
					{s_label:"s_pfield",type:"tpl_hidden",val:cname}
				]
				if(n == "new"){
					_tit="新增_"+cname+"规则";
				}else{
					_tit="编辑_"+cname+"规则:"+tobj["s_name"];
				}
				htmlObj={
					mutilfield:"s_pid",
					title:_tit,
					tag:tag,
					bts:[
						{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/rulemanager/saverule",obj,function(data){
										if(data&&data.rep){
											window.location.href="/admin/rulemanager/getrulelist?id="+cid+"&cname="+cname+"&fid="+fid;	
										}else{
											showTip(data.msg,1000)
										}
									},'json')
								}else{
									alert("红色标签的表单不能为空！")
								}
							}
						}
					]
				}
			//if({{.data.class.s_pid}}){
			//	htmlObj.tag.push({label:"父规则",s_label:"s_pid",type:"tpl_list_ajax",url:"/admin/rulemanager/getrlist?id="+{{.data.class.s_pid}},fun:function(){
			//		var ids=$("#s_pid").data("ids")
			//		ids=ids||{}
			//		var tid=$(this).attr("_id")
			//		if(!ids[tid]){
			//			ids[tid]=true
			//			$("#s_pid").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_pid").data("ids")
			//				delete iids[ttid]
			//				$("#s_pid").val(function(){
			//					var strid=[]
			//					for(var k in iids){
			//						strid.push(k)
			//					}
			//					return strid.join(",")
			//				}())
			//			})
			//			$("#s_pid").prev().append(tpl1)
			//			$("#s_pid").val(function(){
			//				var strid=[]
			//				for(var k in ids){
			//					strid.push(k)
			//				}
			//				return strid.join(",")
			//			}())
			//		}
			//	}})
			//}
			OpenDialog(htmlObj,obj)
			break;
			//上移下移
			case "moveup":
			case "movedown":
				if($(this).attr("disabled")=="disabled"){
					return
				}
				var poststr=""
				if(n == "moveup"){//上移
					var uobj = {{.data.rule}}[rownum-1];//上一行数据
					poststr=tobj["_id"]+","+tobj["i_order"]+","+uobj["_id"]+","+uobj["i_order"]
				}else{//下移
					var uobj={{.data.rule}}[rownum+1];//下一行数据
					poststr=uobj["_id"]+","+uobj["i_order"]+","+tobj["_id"]+","+tobj["i_order"]
				}
				$.post("/admin/rulemanager/shift",{str:poststr},function(data){
					if(data&&data.rep){
						window.location.href="/admin/rulemanager/getrulelist?id="+cid+"&cname="+cname+"&fid="+fid;						
					}else{
						showTip("移动失败", 1000, function() {});
					}
				})
				break;
			}
		});
	})
})

function del(_id){
	showConfirm("确定删除？", function() {
		$.ajax({
			url:"/admin/rulemanager/delrule",
			type:"post",
			data:{"_id":_id},
			success:function(r){
				if(r.rep){				
					window.location.href="/admin/rulemanager/getrulelist?id="+cid+"&cname="+cname+"&fid="+fid;
				}else{
					showTip("删除失败", 1000, function() {});
				}
			}
		})
	});
}

</script>
