{{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>
		</h1>
		<ol class="breadcrumb">
			<li><a href="/admin/version"><i class="fa fa-dashboard"></i>抽取版本</a></li>
			<li><a href="/admin/site_management?vid={{.vid}}">站点列表</a></li>
			<li><a href="/admin/site_management/info?vid={{.vid}}&pid={{.pzid}}">属性配置</a></li>
		  	<li><a href="/admin/site_management/rulelogic?vid={{.vid}}&pid={{.pid}}">抽取逻辑</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/site_management/rulelogic/data",
			"type": "post",
			"data":{"vid":{{ .vid}} ,"pid":{{ .pid}} }
		 },
		"language": {
            "url": "/res/dist/js/dataTables.chinese.lang"
        },
		"columns": [
			{ "data": "s_name"},
			{ "data": "s_username"},
			{ "data": "s_descript"},
			{ "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","width":"25%",render:function(val,a,row){
				tmp = '<div class="btn-group">'+
						'<a class="btn btn-sm btn-primary" href="/admin/site_management/logicpre?vid={{.vid}}&pid={{.pid}}&sid='+row._id+'">前置规则</a>'+
						'<a class="btn btn-sm btn-success" href="/admin/site_management/logicore?vid={{.vid}}&pid={{.pid}}&sid='+row._id+'">抽取规则</a>'+
						'<a class="btn btn-sm btn-info" href="/admin/site_management/logicback?vid={{.vid}}&pid={{.pid}}&sid='+row._id+'">后置规则</a>'+
						'<a class="btn btn-sm btn-warning" href="/admin/site_management/logickv?vid={{.vid}}&pid={{.pid}}&sid='+row._id+'">kv规则</a>'+
						'</div>';
				return  tmp
			}},
			{"data":"s_version","width":"15%",render:function(val,a,row,pos){
				return '<a class="btn btn-sm btn-primary opr" opr="edit" row="'+pos.row+'" >编辑</a>'+
					'&nbsp;&nbsp;<a class="btn btn-sm btn-warning" onclick="del(\''+row._id+'\')">删除</a>';
			}}
       	]
	});
	ttable.on('init.dt', function () {
		$(".opr").click(function(){
			var n=$(this).attr("opr")
			var htmlObj={},obj,tag=[]
			var _tit="" 
			switch(n){
			case "edit":			
				obj=ttable.row($(this).closest("tr")).data()
			case "editlua":
				obj=ttable.row($(this).closest("tr")).data()
			case "new":
				tag=[{label:"名称",s_label:"s_name",placeholder:"",must:true},
					{label:"描述",s_label:"s_descript",type:"tpl_text"},
					{label:"启用",s_label:"isuse",type:"tpl_list_local",list:[{"s_name":"是","_id":true},{"s_name":"否","_id":false}],default:true},
					{label:"标题抽取",s_label:"extfrom",type:"tpl_list_local",list:[{"s_name":"是","_id":true},{"s_name":"否","_id":false}],default:false},
					{label:"是否适用",s_label:"s_luascript",type:"tpl_text",must:true},
					{s_label:"_id",type:"tpl_hidden"},
					{s_label:"vid",type:"tpl_hidden"},{s_label:"pid",type:"tpl_hidden"}]
				if(n=="edit"){
					_tit="编辑-"+obj.s_name
				}else{
					_tit="新增逻辑"
					obj={"s_luascript":"function logic(doc)\n\treturn true\nend","vid":"{{.vid}}","pid":"{{.pid}}","s_type":"0"}
				}
				
				htmlObj={
					title:_tit,
					tag:tag,
					lua:true,
					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/site_management/rulelogic/save",obj,function(data){
										if(data&&data.rep){
											window.location.href="/admin/site_management/rulelogic?vid={{.vid}}&pid={{.pid}}"
										}else{
											showTip(data.msg,1000)
										}
									},'json')
								}else{
									alert("红色标签的表单不能为空！")
								}
							}
						}
					]
				}
			OpenDialog(htmlObj,obj)
			break;
			}
		});
	})
})
function del(_id){
	showConfirm("确定删除？", function() {
		$.ajax({
			url:"/admin/site_management/rulelogic/del",
			type:"post",
			data:{"_id":_id},
			success:function(r){
				if(r.rep){				
					window.location.href="/admin/site_management/rulelogic?vid={{.vid}}&pid={{.pid}}";
				}else{
					showTip("删除失败", 1000);
				}
			}
		})
	});
}
function use(_id,utype){
	smg=""
	if(utype){
		smg="确定启用？"
	}else{
		smg="确定停用？"
	}
	showConfirm(smg, function() {
		$.ajax({
			url:"/admin/site_management/rulelogic/use",
			type:"post",
			data:{"_id":_id,"isuse":utype},
			success:function(r){
				if(r.rep){				
					window.location.href="/admin/site_management/rulelogic?vid={{.vid}}&pid={{.pid}}"
				}else{
					showTip("启用失败", 1000, function() {});
				}
			}
		})
	});
}
</script>