{{template "inc"}}
<!-- Main Header -->
{{template "header"}}
<!-- Left side column. 权限菜单 -->
{{template "memu"}}

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
	<section class="content-header">
		<h1>
			<small><a class="btn btn-primary opr" opr="new">新增正则</a></small>
			<small><a class="btn btn-primary opr" opr="newlua">新增脚本</a></small>
		</h1>
		<ol class="breadcrumb">
		  <li><a href="/admin/version"><i class="fa fa-dashboard"></i>抽取版本</a></li>
		  <li class="active"><a href="/admin/ruleback?version={{.version}}">后置逻辑</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>
{{template "luares"}}
{{template "dialog"}}
{{template "footer"}}

<script>
menuActive("version")
$(function () {
	ttable=$('#dataTable').DataTable({
		"paging"      : false,
		"lengthChange": false,
		"searching"   : true,
		"ordering"    : false,
		"info"        : true,
		"autoWidth"   : false,
		"ajax": {
			"url": "/admin/ruleback/data",
			"type": "post",
			"data":{"version":{{ .version}} }
		 },
		"language": {
            "url": "/res/dist/js/dataTables.chinese.lang"
        },
		"columns": [
			{ "data": "s_code"},
            { "data": "s_name"},
			{ "data": "s_username"},
			{ "data": "s_descript"},
			{ "data": "s_type",render:function(val,a,row){
				if(val=="0"){
					return "正则"
				}else{
					return "lua脚本"
				}
			}},
			{ "data": "_id",render:function(val,a,row,pos){
				tmp=""
				if(row.s_type=="0"){
					tmp = '<div>'+
						'<a class="btn btn-sm btn-primary opr" opr="edit" row="'+pos.row+'" >编辑</a>　'+
						'<a class="btn btn-sm btn-warning" onclick="del(\''+val+'\')">删除</a>　'+
						'</div>';
				}else{
					tmp = '<div>'+
						'<a class="btn btn-sm btn-primary opr" opr="editlua" row="'+pos.row+'" >编辑</a>　'+
						//'<a href="/admin/ruleback/luastr?_id='+val+'" class="btn btn-sm btn-primary">编辑</a>　'+
						'<a class="btn btn-sm btn-warning" onclick="del(\''+val+'\')">删除</a>　'+
						'</div>';
				}
				return tmp
			}}
       	]
	});
	ttable.on('init.dt', function () {
		$(".opr").click(function(){
			var n=$(this).attr("opr")
			var htmlObj={},obj,tag=[]
			var 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=="s_luascript"){
										val=editor_1.getValue()
										obj[el.id]=val
									}else{
										obj[el.id]=$(el).val()
									}
									if(el.id!="_id"&&$(el).attr("must")&&!val){
										bcon=false
										return false
									}
								})
								if (bcon){								
									$.post("/admin/ruleback/save",obj,function(data){
										if(data&&data.rep){
											window.location.href="/admin/ruleback?version={{.version}}"								
										}else{
											showTip(data.msg,1000)
										}
									},'json')
								}else{
									alert("红色标签的表单不能为空！")
								}
							}
						}
					]
			var _tit="" 
			switch(n){
			case "edit":			
				obj=ttable.row($(this).closest("tr")).data();
			case "editlua":			
				obj=ttable.row($(this).closest("tr")).data();
			case "newlua":
			case "new":
				comtag=[{label:"名称",s_label:"s_name",placeholder:"",must:true},{label:"描述",s_label:"s_descript",type:"tpl_text"}]
				regtag=[{label:"字段",s_label:"s_field",type:"tpl_list_local",url:"/admin/getfields"},{label:"正则",s_label:"s_rule",type:"tpl_text",must:true}]
				luatag=[{label:"脚本",s_label:"s_luascript",type:"tpl_text",must:true}]
				testcon=[{label:"测试内容",s_label:"s_testcon",type:"tpl_text",must:true}]
				hiddentag=[{s_label:"_id",type:"tpl_hidden"},{s_label:"s_version",type:"tpl_hidden"},{s_label:"s_type",type:"tpl_hidden"}]
				islua=false
				tag = com.pushArry(tag,comtag)
				if(n=="edit"){
					_tit="编辑-"+obj.s_name
					tag = com.pushArry(tag,regtag)
					tag = com.pushArry(tag,hiddentag)
					tag = com.pushArry(tag,testcon)
					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/backrule",obj,function(data){
											showMsg(data.rep)
										},'json')
									}else{
										alert("红色标签的表单不能为空！")
									}
								}
							}]
					bts = com.pushArry(bts,check)
				}else if(n=="editlua"){
					_tit="编辑-"+obj.s_name
					islua=true
					tag = com.pushArry(tag,luatag)
					tag = com.pushArry(tag,hiddentag)
					check=[{label:"测试",class:"btn-warning",
								fun:function(){
									window.location.href="/admin/check/lua/back?version={{.version}}&s_code="+obj.s_code
								}
							}]
					bts = com.pushArry(bts,check)
				}else{
					_tit="新增规则"
					if(n=="newlua"){
						_tit="新增脚本"
						obj={"s_luascript":"--code脚本代码，result 抽取结果，过滤后返回result，对象结构不可改变\nfunction main(code,result)\n\t--过滤操作\n\treturn result\nend","s_version":"{{.version}}","s_type":"1"}
						tag = com.pushArry(tag,luatag)
						tag = com.pushArry(tag,hiddentag)
						islua=true
					}else{
						obj={"s_version":"{{.version}}","s_type":"0"}
						tag = com.pushArry(tag,regtag)
						tag = com.pushArry(tag,hiddentag)
					}
				}
				htmlObj={
					title:_tit,
					tag:tag,
					lua:islua,
					bts:bts
				}
			OpenDialog(htmlObj,obj)
			break;
			}
		});
	})
})

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

</script>
