{{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">新增{{.fname}}规则</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/getrulelist?id={{.fid}}&fname={{.fname}}">规则列表</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>
                    <th>类型</th>
        						<th>是否启用</th>
        						<th>操作</th>
		              </tr>
		              </thead>
		            </table>
		        </div>
	          <!-- /.box-body -->
	        </div>
        <!-- /.box -->
		</div>
	</div>
  </section>
</div>

{{template "dialog"}}
{{template "footer"}}
<script>
menuActive("audit/recogfield")
var fname = {{.fname}};
var fid = {{.fid}};
$(function () {
	ttablerulemanager=$('#rulemanagerTable').DataTable({
		"paging"      : true,
		"lengthChange": false,
		"searching"   : true,
		"ordering"    : false,
		"info"        : true,
		"autoWidth"   : true,
		"serverSide": true,
		"ajax": {
			"url": "/admin/rulemanager/getrule",
			"type": "post",
			"data":{"fid":fid}
		},
		"language": {
            "url": "/res/dist/js/dataTables.chinese.lang"
        },
		"columns": [
           		{ "data": "s_name"},
			{ "data": "l_createtime"},
			{ "data": "s_username"},
			{ "data": "s_descript"},
      { "data": "s_type",render:function(val){
        if(val=="ok"){
					return "正确";
				}else if(val == "err"){
					return "异常";
				}
      }},
			{ "data": "isuse",render:function(val,a,row){
				tmp=""
				if(val){
					tmp="<a href='#' title='停用' onclick='use(\""+row._id+"\",false)'><i class='fa fa-fw fa-circle text-green'></i></a>已启用"
				}else{
					tmp="<a href='#' title='启用' onclick='use(\""+row._id+"\",true)'><i class='fa fa-fw fa-circle text-red'></i></a>未启用"
				}
				return tmp
			}},
			{ "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(row.count ==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 == row.count){//最后一行
					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>'
					
			}}
     ],
		"fnServerParams": function (e) {  
			var rule_type=$("#rule_type").val();
  			if(rule_type){
  				e.rule_type=rule_type;
  			}else{
  				e.rule_type="-1";
  			}
      }
	});
	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 btn =[
						{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="+fid+"&fname="+fname;	
										}else{
											showTip(data.msg,1000)
										}
									},'json')
								}else{
									alert("红色标签的表单不能为空！")
								}
							}
						}
					];
			var delid=$(this).attr("value");
			switch(n){
			case "edit":			
				obj=ttablerulemanager.row($(this).closest("tr")).data();
			case "new":
				tag=[
					{label:"名称",s_label:"s_name",must:true},
					{label:"描述",s_label:"s_descript"},
					{label:"启用",s_label:"isuse",type:"tpl_list_local",list:[{"s_name":"是","_id":true},{"s_name":"否","_id":false}],default:true},
					{label:"类型",s_label:"s_type",type:"tpl_list_local",must:true,list:[{"s_name":"正确","_id":"ok"},{"s_name":"异常","_id":"err"}],default:"0"},
          {label:"正则",s_label:"s_rule",type:"tpl_text",rows:2,must:true},
					{s_label:"_id",type:"tpl_hidden"},
					{s_label:"s_fid",type:"tpl_hidden",val:fid},
					{s_label:"s_field",type:"tpl_hidden",val:fname}
				]
				if(n == "new"){
					_tit="新增"+fname+"规则";
				}else{
					_tit="编辑"+fname+"规则";
					testcon=[{label:"测试内容",s_label:"s_testcon",type:"tpl_text"}];
					check=[{label:"测试",class:"btn-warning",
								fun:function(){
									var obj={}
									var bcon=true
									$("#_con").find("input[id!=s_show],textarea").each(function(i,el){
										var val=$(el).val(); 
										obj[el.id]=$(el).val()
										if(el.id!="_id"&&$(el).attr("must")&&!val){
											bcon=false
											return false
										}
									})
									if (bcon){								
										$.post("/admin/check/auditrule",obj,function(data){
											showMsg(JSON.stringify(data.rep))
										},'json')
									}else{
										alert("红色标签的表单不能为空！")
									}
								}
							}];
					tag = com.pushArry(tag,testcon)
					btn = com.pushArry(btn,check)
				}
				htmlObj={
					mutilfield:"s_pid",
					title:_tit,
					tag:tag,
					bts:btn
				}
			
			OpenDialog(htmlObj,obj)
			break;
			}
		});
		var opt="<option value='-1'>全部</option>"+
		"<option value='ok'>正确</option>"+
		"<option value='err'>异常</option>";
		var select="<div class='form-group'><label for='name'>类型：</label>"+
			"<select id='rule_type' onchange='checkclick(this.value)' class='form-control input-sm'>"+
			opt+
			"</select></div>"
		$("#rulemanagerTable_filter").prepend("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		$("#rulemanagerTable_filter").prepend(select);
	})
})

function checkclick(){
	ttablerulemanager.ajax.reload();
}

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="+fid+"&fname="+fname;
				}else{
					showTip("删除失败", 1000, function() {});
				}
			}
		})
	});
}
function use(_id,utype){
	smg=""
	if(utype){
		smg="确定启用？"
	}else{
		smg="确定停用？"
	}
	showConfirm(smg, function() {
		$.ajax({
			url:"/admin/rulemanager/ruleuse",
			type:"post",
			data:{"_id":_id,"isuse":utype},
			success:function(r){
				if(r.rep){				
					window.location.href="/admin/rulemanager/getrulelist?id="+fid+"&fname="+fname;
				}else{
					showTip("启用失败", 1000, function() {});
				}
			}
		})
	});
}
</script>
