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

<style>
	#infos{
		height: 666px;
		overflow-y: scroll;
	}
</style>

<div class="content-wrapper">
	<section class="content-header">
		<h1>
			<small>
				<button class="btn btn-primary" onclick="allAudit()">批量审核</button>
				<button class="btn btn-primary" onclick="allDel({{.coll}},'','all')">批量删除</button>
			</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><input type="checkbox" id="selrow" onclick="selectrow(this)"/></th>
						<th>编号</th>
						<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="info_data">
    <div class="modal-dialog">
        <form id="update-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="info_title"></h4>
                </div>
                <div class="modal-body" id="infos">
                </div>
            </div>
            <!-- /.modal-content -->
        </form>
    </div>
    <!-- /.modal-dialog -->
</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>
	    </form>
  	</div>
</div>-->
<!-- /.modal -->
{{template "dialog"}}
{{template "footer"}}
<script>
menuActive("audit/recogfield")
var field = {{.name}};
var id = "";
$(function () {
	ttable=$('#dataauditTable').DataTable({
		"paging"      : true,
		"lengthChange": false,
		"searching"   : true,
		"ordering"    : false,
		"info"        : true,
		"autoWidth"   : true,
		"serverSide"  : true,
		"ajax": {
			"url": "/admin/audit/auditonefield",
			"type": "post",
			"data": {"field":{{.name}},"coll":{{.coll}}}
		},
		"language": {
            	"url": "/res/dist/js/dataTables.chinese.lang"
       	},
		"columns": [
			{ "data": "_id",width:"1%",render:function(val,a,row){
				return "<input type='checkbox' value='"+val+"' name='"+row.text+"'/>"
			}},
			{ "data": "_id",width:"5%",render:function(val,a,row){
				return row.num
			}},
			{ "data": {{.name}}},
			{ "data": "topscopeclass"},
			{ "data": "_id",render:function(val,a,row){
				return '<a class="btn btn-sm btn-success" href="#" onclick="audit(\''+row.text+'\',\''+val+'\')">审核</a>'+
					'&nbsp;&nbsp;<a class="btn btn-sm btn-danger" onclick="allDel(\''+{{.coll}}+'\',\''+val+'\',\'one\')">删除</a>'
			}}
       	],
		"fnServerParams": function (e) {  
			var auditattr=$("#auditattr").val();
			if(auditattr){
				e.auditattr=auditattr;
			}else{
				e.auditattr="-1";
			}
      	}
	});
	ttable.on('init.dt', function () {
		var opt="<option value='-1'>全部</option>"+
				"<option value='1'>正确</option>"+
				"<option value='0'>异常</option>";
		var select="<div class='form-group'><label for='name'>数据类型：</label>"+
			"<select id='auditattr' onchange='checkclick(this.value)' class='form-control input-sm'>"+
			opt+
			"</select></div>"
		$("#dataauditTable_filter").prepend("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		$("#dataauditTable_filter").prepend(select);
		
	})
})

function checkclick(){
	ttable.ajax.reload();
}

function allDel(coll,_id,type){
	var ids=[];
	if(type=="one"){//单个删除
		ids.push(_id)
	}else{//批量删除
		$("#dataauditTable td input[type=checkbox]").each(function(){
			if($(this).prop("checked")){
				ids.push($(this).val());
			}
		});
	}
	showConfirm("确定删除？", function() {
		$.ajax({
			url:"/admin/audit/alldel",
			type:"post",
			data:{"ids":ids.join(","),"coll":coll},
			success:function(r){
				if(r.rep){	
					ttable.ajax.reload();			
					//window.location.href="/admin/rulemanager/getrulelist?id="+fid+"&fname="+fname;
				}else{
					showTip("删除失败", 1000, function() {});
				}
			}
		})
	});
}

function audit(text,_id){//单条信息审核
  id = _id;
	AddQYKHTML(text)
}
function selectrow(me){
		var sel=$(me);
		var isSelected=sel.prop('checked');
		if(isSelected){
			$("#dataauditTable td input[type=checkbox]").prop("checked",true);
		}else{
			$("#dataauditTable td input[type=checkbox]").prop("checked",false);
		}
	}

var vastrjson = [{
    "infoid": "招标信息id",
    "contact_person": "联系人",
    "contact_type": "联系人类型",
    "phone": "联系人电话",
    "topscopeclass": "项目类型"
}];
/*新增企业库html*/
function AddQYKHTML(text) {
  $("#infos").html("");
  $("#info_title").html("新增企业库信息");
  var base1Html = "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\" style=\"color: red;\">名称：</label>" +
    "<div class=\"col-sm-9\"><input type=\"text\" class=\"form-control\" id=\"name\" must=\"true\" value='" + text + "'></div></div>"
  
  var winner1Html = "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\">别名：</label>" +
    "<div class=\"col-sm-9\"><input type=\"text\" class=\"form-control\" id=\"alias\" ></div></div>"
      
  var base2Html = "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\">历史名称：</label>" +
    "<div class=\"col-sm-9\"><input type=\"text\" class=\"form-control\" id=\"history_name\" placeholder=\"多个;隔开\" ></div></div>" +
    "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\">行政区划代码：</label>" +
    "<div class=\"col-sm-9\"><input type=\"text\" class=\"form-control\" id=\"area_code\" ></div></div>" +
    "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\">省份：</label>" +
    "<div class=\"col-sm-9\"><input type=\"text\" class=\"form-control\" id=\"province\"></div></div>" +
    "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\">城市：</label>" +
    "<div class=\"col-sm-9\"><input type=\"text\" class=\"form-control\" id=\"city\" ></div></div>" +
    "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\">区县：</label>" +
    "<div class=\"col-sm-9\"><input type=\"text\" class=\"form-control\" id=\"district\"></div></div>" 

	var winner2Html = "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\">注册资本：</label>" +
		"<div class=\"col-sm-9\"><input type=\"text\" class=\"form-control\" id=\"capital\"  placeholder=\"示例：45678.98元；456.78万元；1234.25；\"></div></div>" +
		"<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\">经营范围：</label>" +
    "<div class=\"col-sm-9\"><input type=\"text\" class=\"form-control\" id=\"business_scope\"></div></div>" 
   
	var buyerHtml = "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\">采购单位类型：</label>" +
        "<div class=\"col-sm-9\"><input type=\"text\" class=\"form-control\" id=\"buyerclass\"></div></div>" 

 	var agencyHtml = "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\">行政级别：</label>" +
    "<div class=\"col-sm-9\"><input type=\"text\" class=\"form-control\" id=\"ranks\"></div></div>"+
		"<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\">单位类型：</label>" +
    "<div class=\"col-sm-9\"><input type=\"text\" class=\"form-control\" id=\"type\"></div></div>" 
               
  var base3Html = "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\">地址：</label>" +
  "<div class=\"col-sm-9\"><input type=\"text\" class=\"form-control\" id=\"address\"></div></div>" +
  "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\">微信公众号：</label>" +
  "<div class=\"col-sm-9\"><input type=\"text\" class=\"form-control\" id=\"wechat_accounts\" placeholder=\"多个;隔开\"></div></div>" +
  "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\">网址：</label>" +
  "<div class=\"col-sm-9\"><input type=\"text\" class=\"form-control\" id=\"website\"></div></div>" +
  "<div class=\"row form-group\"><label for=\"lastname\" class=\"col-sm-3 control-label\">联系方式：</label>" +
  "<div class=\"col-sm-9\" id='contact' style=\"height:250px\"></div>" +
  "<div class=\"row form-group\">" +
  "<button type='button' class='btn btn-sm btn-default col-md-offset-5 col-md-2'  data-dismiss='modal' aria-label='Close'>取消</button>" +
  "<button type='button' class='btn btn-sm btn-danger col-md-2' onclick='Add()'>新增企业</button></div>";
  
  var com = "";
	{{if eq .name "winner"}}
		com = base1Html+winner1Html+base2Html+winner2Html+base3Html;
	{{else if eq .name "agency"}} 
		com = base1Html+base2Html+agencyHtml+base3Html;
	{{else if eq .name "buyer"}}
		com = base1Html+base2Html+agencyHtml+buyerHtml+base3Html;
	{{end}}
	$("#infos").html(com);
    $("#contact").html('');
    var container = document.getElementById('contact');
   	showJson("add", vastrjson, container);
    $("#info_data").modal("show");
}

function showJson(ptype, content, container) {
    var options = {
        mode: 'code',
        modes: ['code', 'tree'], // allowed modes
        onError: function (err) {
            alert(err.toString());
        }
    };
    if (ptype == "add") {
        editor_add = new JSONEditor(container, options, content);
    } else if (ptype == "update") {
        editor_update = new JSONEditor(container, options, content);
    }
}

/*保存企业库*/
function Add() {
    var contact = editor_add.getText();//联系人
	  if (typeof contact == 'string') {
        try {
            var obj=JSON.parse(contact);
            if(typeof obj == 'object' && obj ){
                // alert("格式正确");
            }else{
                alert("联系人的表单json格式不正确");
                return
            }
        } catch(e) {
            alert("联系人的表单json格式不正确");
            return
        }
    }
    if (contact === JSON.stringify(vastrjson, null, 2)) {
        alert("联系人的表单未填写！");
        return
    }
    //新增企业库api
	var obj={}
	var bcon=true
	$("#infos").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
		}
		if(el.id!= ""){
			obj[el.id]=$(el).val()
		}
	})
	obj["contact"]= contact;
	obj["coll"] = {{.coll}};
	obj["_id"] = id;
  obj["field"] = {{.name}};
  obj["stancoll"] = {{.stancoll}};
	$.post("/admin/audit/datasave",obj,function(data){
		if(data&&data.rep){
      $("#info_data").modal("hide");
      ttable.ajax.reload();
		}else{
			showTip(data.msg,1000)
		}
	},'json')
    
}

//批量审核
function allAudit(){
  var ids=[];
  var names=[];
	$("#dataauditTable td input[type=checkbox]").each(function(){
		if($(this).prop("checked")){
			ids.push($(this).val());
      names.push($(this).attr("name"));
		}
	});

	showConfirm("确定审核通过?", function() {
		$.ajax({
			url:"/admin/audit/allaudit",
			type:"post",
			data:{"ids":ids.join(","),"names":names.join(","),"coll":{{.coll}},"stancoll":{{.stancoll}},"field":{{.name}}},
			success:function(r){
				if(r.rep){	
					ttable.ajax.reload();			
					//window.location.href="/admin/rulemanager/getrulelist?id="+fid+"&fname="+fname;
				}else{
					showTip(r.msg, 1000, function() {});
				}
			}
		})
	});
}

</script>