{{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="classify">块分类</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/version/blockinfo?vid={{.vid}}">分块配置</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-striped table-bordered table-hover">
		              <thead>
		              <tr>
						<th>优先级</th>
						<th>分块正则</th>
						<th>块标题正则</th>
						<th>操作</th>
		              </tr>
		              </thead>
		            </table>
					<p class="text-danger text-right">注：可拖拽调整优先级顺序</p>
		        </div>
	          <!-- /.box-body -->
	        </div>
        <!-- /.box -->
		</div>
	</div>
  </section>
</div>
	
<!-- footer -->
{{template "dialog"}}
{{template "footer"}}
<link rel="stylesheet" href="https://cdn.datatables.net/rowreorder/1.2.5/css/rowReorder.bootstrap.min.css">
<script src="https://cdn.datatables.net/rowreorder/1.2.5/js/dataTables.rowReorder.min.js"></script>
<script>
menuActive("version")
$(function () {
	ttable=$('#dataTable').DataTable({
		"columnDefs": [
			{
				"targets": 0,
				visible:true
			},
	        {
	            "orderable": false,
	            "targets": "_all"
	        }
		],
		rowReorder: {
			dataSrc: 'index',
            selector: 'tr'
        },
		"order": [[ 0, 'desc' ]],
		"paging"      : false,
		"lengthChange": false,
		"searching"   : false,
		"info"        : true,
		"autoWidth"   : false,
		"language": {
            "url": "/res/dist/js/dataTables.chinese.lang"
        },
		"ajax": {
			"url": "/admin/version/blockinfo_list",
			"type": "post",
			"data":{"vid":{{.vid}}}
		},
		"columns": [
			{"data": "index","orderable": false},
			{"data": "block_reg","width":"50%"},
			{"data": "title_reg","width":"30%"},
			{"data":"_id","width":"12%",render:function(val,a,row){
				return '<a class="btn btn-sm btn-primary opr" opr="edit">编辑</a>&nbsp;<a class="btn btn-sm btn-danger" href="#" onclick="del(\''+val+'\')">删除</a>';
			}}
       	]
	});
	ttable.on('init.dt', function () {
		$(".opr").click(function(){
			var n=$(this).attr("opr")
			var _tit="",htmlObj={},obj,tag=[]
			switch(n){
			case "classify":
				window.location.href = "/admin/version/blockclassify?vid={{.vid}}";
				break
			case "edit":	
                obj=ttable.row($(this).closest("tr")).data();
			case "new":
                tag=[
						{label:"分块正则",s_label:"block_reg",type:"tpl_input",placeholder:"分块正则",must:true},
						{label:"块标题正则",s_label:"title_reg",type:"tpl_input",placeholder:"块标题正则",must:true},
                        {s_label:"_id",type:"tpl_hidden"},
					]
				
				if(n=="new"){
					_tit="新增规则"
					obj={}
				}else{
					_tit="编辑规则"
				}
				htmlObj={
					title:_tit,
					tag:tag,
					bts:[
						{label:"保存",class:"btn-primary",
							fun:function(){
								var block_reg = $.trim($("#block_reg").val());
								var title_reg = $.trim($("#title_reg").val());
								var bcon=true;
								if(block_reg==""||title_reg==""){
									bcon=false;
								}
								if (bcon){
									var obj={
										_id:$("#_id").val(),
										block_reg:block_reg,
										title_reg:title_reg,
										vid:{{.vid}}
									}
                                    //console.log(obj)							
									$.post("/admin/version/blockinfo_save",obj,function(data){
										if(data.status){
											window.location.href="/admin/version/blockinfo?vid={{.vid}}";	
										}else{
											showTip("保存失败！",1000)
										}
									},'json')
								}else{
									alert("红色标签的表单不能为空！")
								}
							}
						}
					]
				}
			OpenDialog(htmlObj,obj)
			break;
			}
		});
	});
	ttable.on( 'order.dt search.dt', function () {
        ttable.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
            cell.innerHTML = i+1;
        } );
    } ).draw();
	ttable.on( 'row-reordered', function ( e, diff, edit ) {
		var _ids = [],indexs=[];
		for(var i=0;i<diff.length;i++){
			var rowData = ttable.row( diff[i].node ).data();
			_ids.push(rowData._id);
			indexs.push(rowData.index);
		}
		ttable.rowReorder.disable();
		$.ajax({
			type: "POST",
			url: "/admin/version/blockinfo_updateindex",
			data: {_ids:_ids,indexs:indexs},
			dataType: "json",
			traditional: true,
			success: function(r){
				ttable.rowReorder.enable();
			}
		});
    });
})
function del(_id){
	showConfirm("确定删除?", function() {
		$.ajax({
			url:"/admin/version/blockinfo_delete",
			type:"post",
			data:{"_id":_id},
			success:function(r){
				if(r.status){				
					ttable.ajax.reload();
				}else{
					showTip("删除失败", 1000, function() {});
				}
			}
		})
	});
}
</script>