{{template "inc"}}
<!-- Main Header -->
{{template "header"}}
<!-- Left side column. 权限菜单 -->
{{template "memu"}}
<link href="../res/other/css/other.css" rel="stylesheet" type="text/css">
<style>
	#dataTable_filter div{
		padding:0px 10px;
	}
	.operate a{
		padding:5px 10px;
	}
</style>
<div class="content-wrapper">
	<section class="content-header" style="padding: 49px 15px 0 15px;">
		<h1>
			<small><a class="btn btn-primary opr" opr="test">抽取测试</a></small>
		</h1>
		<ol class="breadcrumb">
		  <li><a href="#"><i class="fa fa-dashboard"></i> 结果追踪</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>
						<th>招标机构</th>
						<th>操作</th> 
		              </tr>
		              </thead>
		            </table>
		        </div>
	          <!-- /.box-body -->
	        </div>
        <!-- /.box -->
		</div>
	</div>
  </section>

</div>
<!--新增标签--> 
<div class="modal fade" id="modal-info-resulttrack">
  	<div class="modal-dialog">
	    <form id="resulttrack-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">信息</h4>
		    </div>
		    <div class="modal-body" id="trackinfo">
		    </div>
		</div>
	    <!-- /.modal-content -->
	    </form>
  	</div>
  <!-- /.modal-dialog -->
</div>
{{template "dialog"}}
{{template "footer"}}
<script>
var rowdata ={};
var trackId = "";
menuActive("resulttrack")
$(function () {
	ttable=$('#dataTable').DataTable({
		"paging"      : true,
		"lengthChange": false,
		"searching"   : true,
		"ordering"    : false,
		"info"        : true,
		"autoWidth"   : false,
		"serverSide"  : true,
		"ajax": {
			"url": "/admin/resulttrack/list",
			"type": "post"
		 },
		"language": {
            "url": "/res/dist/js/dataTables.chinese.lang"
        },
		"columns": [
            { "data": "toptype",render:function(val,a,row){
                if(val){                    
    				return val
                }else{
                    return ""
                }
			}},
            { "data": "projectname","width":"16%",render:function(val,a,row){
				var tmpval = val;
				if(tmpval == undefined){
					tmpval = "";
				} 
				if(tmpval.length>10){
					tmpval = tmpval.substr(0,10)+"...";
				}
				return "<a style='color:#333' target='_blank' title='"+val+"'>"+tmpval+"</a>"
			}},
			{ "data": "budget",render:function(val,a,row){
				if(val == undefined){
					return "";
				}else{
					return val;
				}
				
			}},
			{ "data": "bidamount",render:function(val,a,row){
				if(val == undefined){
					return "";
				}else{
					return val;
				}
				
			}},
			{ "data": "buyer",render:function(val,a,row){
				var tmpval = val;
				if(tmpval == undefined){
					tmpval = "";
				} 
				if(tmpval.length>10){
					tmpval = tmpval.substr(0,10)+"...";
				}
				return "<a style='color:#333' target='_blank' title='"+val+"'>"+tmpval+"</a>"
			}},
			{ "data": "winner",render:function(val,a,row){
				var tmpval = val;
				if(tmpval == undefined){
					tmpval = "";
				} 
				if(tmpval.length>10){
					tmpval = tmpval.substr(0,10)+"...";
				}
				return "<a style='color:#333' target='_blank' title='"+val+"'>"+tmpval+"</a>"	
			}},
			{ "data": "agency",render:function(val,a,row){
				var tmpval = val;
				if(tmpval == undefined){
					tmpval = "";
				} 
				if(tmpval.length>10){
					tmpval = tmpval.substr(0,10)+"...";
				}
				return "<a style='color:#333' target='_blank' title='"+val+"'>"+tmpval+"</a>"
			}},
			{ "data": "_id",render:function(val,a,row){
				rowdata[val]= row;
				var url = row["url"];
				return  "<div class='operate'><a class='btn btn-primary' onclick='trackShow(\""+val+"\")'>详细</a>&nbsp&nbsp&nbsp"+
						"<a class='btn btn-sm btn-info' href='https://www.jianyu360.com/article/content/"+url+".html' target='_blank'>查看原文</a></div>"
			}}			
       	],
		"fnServerParams": function (e) {  //额外数据
			var resultinfo = sessionStorage.getItem("resultinfo");
			if(resultinfo != null && resultinfo.length >0){
				e.resultcoll = resultinfo.split("+")[0];
			}
      	}
	});
	ttable.on('init.dt', function () {
		/*查询所有结果表和对应的日志表*/
		gettasktestlog();
		/*抽取测试*/
		$(".opr").click(function(){
			var n=$(this).attr("opr");
			var htmlObj={},obj,tag=[],bts=[];
			var _tit="";
			switch(n){
			case "test":
				testtask=[
						{label:"任务名称",s_label:"s_taskname",type:"tpl_list_local",must:true,url:"/admin/task/gettaskname"},
						{label:"起始id",s_label:"s_startid",must:true},
						{label:"抽取数量",s_label:"s_datanum",placeholder:"5",must:true},
						{label:"结果版本",s_label:"s_resulttrack",must:true}
				]
				//测试启动按钮
				testtaskbtn=[
					{label:"Run",class:"btn-danger",
						fun:function(){
							var reg = /^[0-9]*$/;
							var id = $("#s_startid").val();
							var num = $("#s_datanum").val();
							var taskid = $("#s_taskname").val();
							var resulttrack = $("#s_resulttrack").val();
							var bcon = true;
							if(id == "" || resulttrack == ""){
								bcon = false;
								alert("红色标签的表单不能为空！");
								return
							}
							var resultcoll = "result_"+resulttrack; 
							var trackcoll = "track_"+resulttrack; 
							if(num != ""){//验证数字
								if(!reg.test(num)){
									bcon = false;
									alert("抽取数量填写错误！");
									return
								}
							}else{
								num = "5";
							}
							if(bcon){
								//抽取测试								
								$.post("/admin/task/test",{"startid":id,"num":num,"taskid":taskid,"resultcoll":resultcoll,"trackcoll":trackcoll},function(data){
									if(data&&data.rep){
										sessionStorage.setItem("resultinfo",resultcoll+"+"+data.version);
										sessionStorage.setItem("trackcoll",trackcoll);
										$("#myModal").modal("hide");
										alert("测试完成!");
										tablereload();
									}else{
										alert("起始id输入错误!");	
									}
								},'json')
								
							}else{
								alert("红色标签的表单不能为空！")
							}
						}
					}
				];
				_tit="抽取测试";
				tag = com.pushArry(tag,testtask);
				bts = com.pushArry(bts,testtaskbtn);
				htmlObj={
					title:_tit,
					tag:tag,
					bts:bts
				}
				OpenDialog(htmlObj,obj)
				break;
			}
		});
		var trackcoll = sessionStorage.getItem("trackcoll");
		var resultinfo = sessionStorage.getItem("resultinfo");
		if(trackcoll != null && resultinfo != null && trackcoll != "undefined"){
			var resultcoll = resultinfo.split("+")[0];
			var index = 0;
			$("#resultlog option").each(function(i,n){
				if($(this).val().indexOf(resultcoll) >= 0){
					index = i;
					return
				}
			})
			if(trackcoll == ""){
				trackcoll = "全部";
			}
			$("#resultlog").find("option:eq("+index+")").attr("selected",true);
			$("#tracklog").html("<option value=''>"+trackcoll+"</option>")
		}
	});
})

//详细
function trackShow(_id){
	trackId = _id;	
	var info = rowdata[_id];
	var resultinfo = sessionStorage.getItem("resultinfo");
	var rv = resultinfo.split("+");
	var version = rv[1];
	$.post("/admin/resulttrack/getversioninfo",{"version":version},function(data){
		var datainfo = data.versioninfo;
		var resulthtml = "";
		if(data && datainfo.length > 0){
			for(var i in datainfo){
				var key = datainfo[i]["s_field"];
				var val = info[key];
				if(val == undefined){
					val = ""
				}
				var tmp ="<div class='form-group'>"
						   +"<label for='code' class='col-sm-2 control-label'>"+datainfo[i]["s_name"]+":</label>"
						    +"<div class='col-sm-10 styleclass'>"
							   	+"<input type='text'id='"+key+"' class='form-control' value='"+val+"'>"
								+"<button type='button' class='btn btn-primary track'>追踪</button>"
							+"</div>"
						+"</div>"
				resulthtml = resulthtml + tmp;
			}
			$("#trackinfo").html(resulthtml)
			//追踪
			$("#modal-info-resulttrack .track").click(function(){
				var field = $(this).prev().attr("id");
				var trackcoll = sessionStorage.getItem("trackcoll");
				var resultinfo = sessionStorage.getItem("resultinfo");
				var resultcoll = resultinfo.split("+")[0];
				window.open("/admin/resulttrack/trackData/?trackId="+trackId+"&field="+field+"&trackcoll="+trackcoll+"&resultcoll="+resultcoll);
			});
			$("#modal-info-resulttrack").modal("show");
		}else{
			showTip("无详细信息!",1000);
		}
	},'json')
}

function checkclick(log){
	var logarr = log.split("+");
	var trackcoll = logarr[2];
	sessionStorage.setItem("resultinfo",logarr[1]+"+"+logarr[3]);
	sessionStorage.setItem("trackcoll",trackcoll);
	$("#tracklog").html("");
	if(trackcoll != "" && trackcoll != undefined){
		$("#tracklog").append("<option value=''>"+trackcoll+"</option>");
	}else{
		$("#tracklog").append("<option value='-1'>全部</option>");
	}
	ttable.ajax.reload();
}

function tablereload(){
	gettasktestlog();
	ttable.ajax.reload();
}

//获取所有对应的结果表和日志表
function gettasktestlog(){
	$("#dataTable_filter div").remove();
	clearcoll();//清理标签
	var opt2="<option value='-1'>全部</option>";
	var select2="<div class='form-group'><label for='name'>日志表：</label>"+
		"<select disabled id='tracklog' class='form-control input-sm'>"+
		opt2+
		"</select></div>"
	$("#dataTable_filter").prepend(select2);
	$.ajax({
		url:"/admin/task/gettasktestlog",
		type:"post",
		async:false,
		success:function(s){
			var resultselect = "<option value='-1'>全部</option>";
			if(s && s.data.length > 0){
				var r = s.data;
				for(var i in r){
					var opt1 = "<option value='"+r[i]["_id"]+"+"+r[i]["resultcoll"]+"+"+r[i]["trackcoll"]+"+"+r[i]["version"]+"'>"+r[i]["resultcoll"]+"</option>"
					resultselect = resultselect + opt1;
				}
			}
			var select1="<div class='form-group'><label for='name'>结果表：</label>"+
					"<select id='resultlog' onchange='checkclick(this.value)' class='form-control input-sm'>"+
					resultselect+
				"</select></div>"
			$("#dataTable_filter").prepend(select1);
		}
	})
	$("#dataTable_wrapper .col-sm-6").css({width:"100%"});
}

function clearcoll(){
	var clear = "<div class='form-group'>"+
					"<a class='btn btn-primary' onclick='cleardata()'style='padding: 2px 5px;'>清理</a>"+				
				"</div>";
	$("#dataTable_filter").prepend(clear);
}

//清理
function cleardata(){
	var val = $("#resultlog").val();
	if(val == "-1"){
		return
	}
	showConfirm("确定删除？", function() {
		var logarr = $("#resultlog").val().split("+");
		$.ajax({
			url:"/admin/resulttrack/clearcoll",
			type:"post",
			data:{"id":logarr[0],"resultcoll":logarr[1],"trackcoll":logarr[2]},
			success:function(r){
				if(r.rep){
					sessionStorage.setItem("trackcoll","");
					tablereload();
				}else{
					sessionStorage.setItem("trackcoll","");
					showTip("删除失败", 1000, function() {});
				}
			}
		})
	});
}

</script>