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

<div class="content-wrapper">
	<section class="content-header">
		<h1>
			<small>
				{{if eq .name "buyer" "winner"}}
				<button class="btn btn-primary" onclick="addaudit()">新增{{.name}}</button>
				{{end}}
			</small>
		</h1>
		<ol class="breadcrumb">
		<li><a href="/admin/audit/recogfield"><i class="fa fa-dashboard"></i> 识别字段</a></li>
			<li class="active"><a href="#">数据审核</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="dataauditTable" class="table table-bordered table-hover">
		              <thead>
		              <tr>
		                <th>字段名称</th>
						<th>字段值</th>
						<th>ExtractID</th>
						<th>结果表</th>
						<th>分包</th>
						<th>时间</th>
						<th>操作</th>
		              </tr>
		              </thead>
		            </table>
		        </div>
	          <!-- /.box-body -->
	        </div>
        <!-- /.box -->
		</div>
	</div>
  </section>
</div>

<!--新增标签--> 
<div class="modal fade" id="modal-info-auditdata">
  	<div class="modal-dialog">
	    <form id="add-dataform" class="form-horizontal" role="form">
		<div class="modal-content">
		    <div class="modal-header">
		        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
		          <span aria-hidden="true">&times;</span></button>
		        <h4 class="modal-title" id="audittitleinfo"></h4>
		    </div>
		    <div class="modal-body">
				<div class="form-group">
				    <label for="code" id="labelname" class="col-sm-2 control-label">名称:</label>
				    <div class="col-sm-10">
					   	<input class="form-control" id="auditname"></input>
					</div>
				</div>
				{{if eq .name "buyer"}}
				<div class="form-group">
				    <label for="code" id="labelname" class="col-sm-2 control-label">类别:</label>
				    <div class="col-sm-10" style="display:flex;">
					   	<select class="form-control" id="parentclass" style="margin-right:15px;"></select>
						<select class="form-control" id="childclass"></select>
					</div>
				</div>
				{{end}}
		    </div>
		    <div class="modal-footer">
		        <button type="button" class="btn btn-default" data-dismiss="modal" onclick="reset()">取消</button>
		        <button type="button" class="btn btn-primary" onclick="saveaudit()">保存</button>
	    	</div>
		</div>
	    <!-- /.modal-content -->
	    </form>
  	</div>
  <!-- /.modal-dialog -->
</div>
<!-- /.modal -->
{{template "dialog"}}
{{template "footer"}}
<script>
menuActive("recogfield")
var id = "";
var eid = "";
var classifymap = {};
var bid = 1;
var coll = "";
var field = {{.name}}
$(function () {
	ttable=$('#dataauditTable').DataTable({
		"paging"      : true,
		"lengthChange": false,
		"searching"   : true,
		"ordering"    : false,
		"info"        : true,
		"autoWidth"   : false,
		"serverSide"  : true,
		"ajax": {
			"url": "/admin/audit/auditonefield",
			"type": "post",
			"data": {"field":{{.name}}}
		},
		"language": {
            "url": "/res/dist/js/dataTables.chinese.lang"
        },
		"columns": [
            { "data": "field"},
			{ "data": "val"},
			{ "data": "extractid"},
			{ "data": "extractcoll"},
			{ "data": "ispackage",render:function(val,a,row){
				if(val){
					return "是"
				}else{
					return "否"
				}
			}},
			{ "data": "l_createtime"},
			{ "data": "_id",render:function(val,a,row){
					return '<a class="btn btn-sm btn-success" href="#" onclick="audit(\''+row.val+'\',\''+val+'\',\''+row.extractid+'\',\''+row.extractcoll+'\')">审核</a>'
			}}
       	]
	});
})

function addaudit(){
	//查询分类数据
	bid = 1;
	$("#audittitleinfo").html("新增");
	if(field == "buyer"){
		getbuyerclass();
	}
	$("#modal-info-auditdata").modal("show");
	$("#auditname").val("");
}
//获取buyer分类
function getbuyerclass(){
	$("#parentclass").empty();
	$("#childclass").empty();
	$("#parentclass").append("<option value=''>--请选择--</option>");
	$("#childclass").append("<option value=''>--请选择--</option>");
	$.ajax({
		url:"/admin/audit/buyerclass",
		type:"post",
		success:function(data){
			var classify = data.data;
			for(var i in classify){
				var name = classify[i]["s_name"];
				classifymap[name] = classify[i]["child"];
				$("#parentclass").append("<option value='"+name+"'>"+name+"</option>");
			}
		}
	})
}

function saveaudit(){
	var parentclass = "";
	var childclass = "";
	var auditname = $("#auditname").val();
	if(field == "buyer"){
		parentclass = $("#parentclass").val();
		childclass = $("#childclass").val();
	}
	if(auditname == ""){
		alert("表单填写不完整！");
		return ;
	}else if(field == "buyer" && (parentclass == "" || childclass == "")){
		alert("表单填写不完整！");
		return ;
	}
	if(bid == 1){//新增保存
		addsave(auditname,parentclass,childclass);
	}else{//审核保存确认
		auditsave(parentclass,childclass);
	}
}

//新增保存
function addsave(auditname,parentclass,childclass){
	$.ajax({
		url:"/admin/audit/addsave",
		type:"post",
		data:{"field":field,"auditname":auditname,"parentclass":parentclass,"childclass":childclass},
		success:function(r){
			if(r.rep){
				$("#modal-info-auditdata").modal("hide");
				showTip("保存成功", 1000, function() {});
			}else{
				showTip("保存失败", 1000, function() {});
			}
		}
	})
}

//审核
function audit(val,_id,extractid,extractcoll){
	bid = 2;
	getbuyerclass();
	id = _id;
	eid = extractid;
	coll = extractcoll;
	$("#auditname").val(val);
	$("#audittitleinfo").html("审核");
	$("#modal-info-auditdata").modal("show");
}
//审核确认
function auditsave(parentclass,childclass){
	var val = $("#auditname").val();
	$("#modal-info-auditdata").modal("hide");
	showConfirm("确定通过？", function() {
		$.ajax({
			url:"/admin/audit/auditsave",
			type:"post",
			data:{"field":field,"val":val,"parentclass":parentclass,"childclass":childclass,"id":id,"eid":eid,"coll":coll},
			success:function(r){
				if(r.rep){				
					ttable.ajax.reload();
				}else{
					showTip("审核失败", 1000, function() {});
				}
			}
		})
	});
}

$(document).ready(function(){
	$("#parentclass").change(function(){
	    var parentname = $(this).val();
		var child = classifymap[parentname];
		$("#childclass").empty();
		if(child == undefined || child.length == 0 ){
			$("#childclass").append("<option value=''>--请选择--</option>");
		}else{
			for(var i in child){
				$("#childclass").append("<option value='"+child[i]+"'>"+child[i]+"</option>");
			}
		}
	});
});
</script>
