{{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/getclasslist?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>
		              </tr>
		              </thead>
		            </table>
		        </div>
	          <!-- /.box-body -->
	        </div>
        <!-- /.box -->
		</div>
	</div>
  </section>
</div>

{{template "dialog"}}
{{template "footer"}}
<script>
menuActive("recogfield")
var fname={{.fname}};
var fid={{.fid}};
$(function () {
	ttablerulemanager=$('#rulemanagerTable').DataTable({
		"paging"      : true,
		"lengthChange": false,
		"searching"   : true,
		"ordering"    : false,
		"info"        : true,
		"autoWidth"   : false,
		"ajax": {
			"url": "/admin/rulemanager/getclass",
			"type": "post",
			"data":{"fid":fid}
		},
		"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){
				return '<a class="btn btn-sm btn-info opr" opr="edit">编辑</a>'+
					'&nbsp;&nbsp;<a class="btn btn-sm btn-warning" href="/admin/rulemanager/getrulelist?id='+val+'&cname='+row["s_recogfield"]+'&fid='+fid+'">编辑规则</a>'+
					'&nbsp;&nbsp;<a class="btn btn-sm btn-danger" onclick="del(\''+val+'\')">删除</a>'
			}}
       	]
	});
	ttablerulemanager.on('init.dt', function () {
		$("#showbtn").on('click','a.opr',function(){
			var n=$(this).attr("opr");
			var _tit="",htmlObj={},obj,tag=[];
			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,type:"tpl_list_local",list:[{"s_name":"通过","_id":"通过"},{"s_name":"不通过","_id":"不通过"}],default:"通过"},
					{label:"保存字段",s_label:"s_savefield",type:"tpl_hidden"},
					/*{label:"父类",s_label:"s_pid",type:"tpl_list_ajax",url:"/admin/rulemanager/getclist?nid="+(obj?obj._id:""),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(",")
								}())
							}
						}},
					*/
					//{label:"默认值",s_label:"s_default"},
					{label:"前置过滤",s_label:"s_class_prerule",type:"tpl_text"},
					{s_label:"_id",type:"tpl_hidden"},
					{s_label:"s_fid",type:"tpl_hidden",val:{{.fid}}},
					{s_label:"s_recogfield",type:"tpl_hidden",val:{{.fname}}}
				]
				if(n == "new"){
					_tit="新增_"+fname+"分类";
				}else{
					_tit="编辑_"+fname+"分类";
					tag[0]={label:"名称",s_label:"s_name",disabled:true}
				}
				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(obj.s_name == "通过"){
									obj.s_savefield = "ok";
								}else{
									obj.s_savefield = "err";
								}
								if (bcon){								
									$.post("/admin/rulemanager/saveclass",obj,function(data){
										if(data&&data.rep){
											window.location.href="/admin/rulemanager/getclasslist?id="+fid+"&fname="+fname;	
										}else{
											showTip("已存在！",1000)
										}
									},'json')
								}else{
									alert("红色标签的表单不能为空！")
								}
							}
						}
					]
				}
				
			OpenDialog(htmlObj,obj)
			break;
			}
		});
	})
})

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

</script>
