{{template "inc"}}
<!-- Main Header -->
{{template "header"}}
<!-- Left side column. 权限菜单 -->
{{template "memu"}}
	<link href="/res/doublebox/css/hdw.css" rel="stylesheet" type="text/css">
	<script src="/res/doublebox/js/hdw.js"></script>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
	<section class="content-header">
		<h1>
			<small><a class="btn btn-primary" onclick="editorexport()">创建导出任务</a></small>
		</h1>
		<ol class="breadcrumb">
		  <li><a href="/admin/task/export"><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>
		              </tr>
		              </thead>
		            </table>
		        </div>
	          <!-- /.box-body -->
	        </div>
        <!-- /.box -->
		</div>
	</div>
  </section>
</div>
<div class="modal fade" id="modal-info-export">
  	<div class="modal-dialog">
	    <form id="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">
				<div class="form-group">
				    <label for="code" class="col-sm-2 control-label">名称:</label>
				    <div class="col-sm-10">
				      <input name="name" type="text" value="" class="form-control" placeholder="任务名称">
				    </div>
				</div>
				<div class="form-group">
				    <label for="code" class="col-sm-2 control-label">对比任务:</label>
				    <div class="col-sm-5">
				      	<select id="v1" name="v1" class="form-control">
							<option value="">请选择版本</option>
							{{range .vlist}}
								<option value="{{._id}}">{{.s_name}}</option>
							{{end}}
						</select>
					</div>
					<div class="col-sm-5">
				    <select id="v2" name="v2" class="form-control">
							<option value="">请选择版本</option>
							{{range .vlist}}
								<option value="{{._id}}">{{.s_name}}</option>
							{{end}}
						</select>
					</div>
				</div>
				<div class="form-group">
				    <label for="code" class="col-sm-2 control-label">数据库:</label>
				    <div class="col-sm-5">
				      <input name="dbaddr" type="text" value="" class="form-control" placeholder="数据库连接地址">
				    </div>
					<div class="col-sm-5">
				      <input name="dbname" type="text" value="" class="form-control" placeholder="数据库名称">
				    </div>
				</div>
				<div class="form-group">
				    <label for="code" class="col-sm-2 control-label">查询条件:</label>
					<div class="col-sm-10">
				      <input name="query" type="text" value="{}" class="form-control" placeholder="{}">
				    </div>
				</div>
				<div class="form-group">
				    <label for="code" class="col-sm-2 control-label">信息源:</label>
					<div class="col-sm-5">
				      <input name="table" type="text" value="" class="form-control" placeholder="信息表(bidding)">
				    </div>
					<div class="col-sm-5">
				      <input name="limit" type="text" value="" class="form-control" placeholder="信息量">
				    </div>
				</div>
				<div class="form-group">
				   <label for="code" class="col-sm-2 control-label">导出属性:</label>
				    <div class="col-sm-10" id="selectclear">
						<div class="doublebox">
							<select multiple="multiple" id="select1" style="overflow-x: scroll;"></select>
						</div>
						<div class="move">
							<button type="button" id="right" class="btn btn-primary">右移</button>
							<button type="button" id="left" class="btn btn-primary">左移</button>
						</div>	
						<div class="doublebox">
						  	<select multiple="multiple" id="select2" style="overflow-x: scroll;"></select>
						</div>
				    </div>
				</div>
		    </div>
		    <div class="modal-footer">
		        <button type="button" class="btn btn-default cancel" data-dismiss="modal" onclick="reset()">取消</button>
		        <button type="button" class="btn btn-primary save" onclick="save()">保存</button>
	    	</div>
		</div>
	    <!-- /.modal-content -->
		<input type="hidden" name="_id" value="">
	    </form>
  	</div>
  <!-- /.modal-dialog -->
</div>	
<!-- footer -->
{{template "dialog"}}
{{template "footer"}}

<script>
menuActive("export")
$(function () {
	ttable=$('#dataTable').DataTable({
		"paging"      : false,
		"lengthChange": false,
		"searching"   : true,
		"ordering"    : false,
		"info"        : true,
		"autoWidth"   : false,
		"ajax": {
			"url": "/admin/task/export/data",
			"type": "post",
			"data":{}
		},
		"language": {
            "url": "/res/dist/js/dataTables.chinese.lang"
        },
		"columns": [
            { "data": "name"},
			{ "data": "fields"},
			{ "data": "query"},
			{ "data": "limit"},
			{ "data": "state",render:function(val,a,row){
				if(row.state!=0){
					return "已完成"
				}else{
					return "待执行"
				}
			}},
			{"data":"_id",render:function(val,a,row){
				if(row.state!=0){
					return '<a class="btn btn-sm btn-success" href="../../res/down/'+row.filename+'"><i class="fa fa-fw fa-cloud-download"></i>下载</a>&nbsp;&nbsp;<a class="btn btn-sm btn-danger" href="#" onclick="del(\''+val+'\')">删&nbsp;&nbsp;除</a>'
				}else{
					return '<a class="btn btn-sm btn-success edit" href="#">编&nbsp;&nbsp;辑</a> &nbsp;&nbsp;<a class="btn btn-sm btn-danger" href="#" onclick="del(\''+val+'\')">删&nbsp;&nbsp;除</a>'
				}	
			}}
       	]
	});
	
	ttable.on('init.dt', function () {
		$("#dataTable").on('click','a.edit',function(){
			$("#v1 option:first").attr("selected",true).siblings("option").attr("selected",false);
			$("#v2 option:first").attr("selected",true).siblings("option").attr("selected",false);
			var obj=ttable.row($(this).closest("tr")).data();
			$.setForm('#dataform',obj);	
			$.ajax({
				url:"/admin/getfields",
				type:"post",
				success:function(r){
					if(r.data){	
						$("#select1").empty();
						$("#select2").empty();
						for(var i in r.data){
							if(i==0){
								continue;
							}
							f=r.data[i]
							var right=false
							for(var k in obj["fields"]){
								if (f._id==obj["fields"][k]){
									right=true
								}
							}
							if (right){									
								$("#select2").append("<option title='"+f.s_name+"' value='"+f._id+"'>"+f.s_name+"</option>");
							}else{
								$("#select1").append("<option title='"+f.s_name+"' value='"+f._id+"'>"+f.s_name+"</option>");									
							}
						}		
					}
				}
			})
			$("#v1").find("option[value='"+obj["v1id"]+"']").attr("selected",true);
			$("#v2").find("option[value='"+obj["v2id"]+"']").attr("selected",true);
			if(obj["state"]!=0){
				$(".save").attr({"disabled":"disabled"});
			}
			$("input[name='_id']").val(obj["_id"]);
			$("#modal-info-export").modal("show");
		})
	})
})

function del(_id){
	showConfirm("确定删除?", function() {
		$.ajax({
			url:"/admin/task/export/del",
			type:"post",
			data:{"_id":_id},
			success:function(r){
				if(r.rep){				
					ttable.ajax.reload();
				}else{
					showTip("删除失败", 1000, function() {});
				}
			}
		})
	});
}

function editorexport(){
	$("#v1 option:first").attr("selected",true).siblings("option").attr("selected",false);
	$("#v2 option:first").attr("selected",true).siblings("option").attr("selected",false);
	$(".save").removeAttr("disabled");
	$.ajax({
		url:"/admin/getfields",
		type:"post",
		success:function(r){
			if(r.data){	
				$("#select1").empty();
				$("#select2").empty();
				for(var i in r.data){
					if(i==0){
						continue;
					}
					f=r.data[i]
					$("#select1").append("<option title='"+f.s_name+"' value='"+f._id+"'>"+f.s_name+"</option>");
				}	
				$("#modal-info-export").modal("show");		
			}
		}
	})
}

function save(){
	var name=$("input[name='name']").val();
	if(name==""){
		showTip("请输入任务名称", 1000, function() {});
		return false;
	}
	var v1=$("#v1").find("option:selected").val();
	var v1name=$("#v1").find("option:selected").text();
	var v2=$("#v2").find("option:selected").val();
	var v2name=$("#v2").find("option:selected").text();
	if(v1==v2||v1==""||v2==""){
		showTip("请输入正确的版本", 1000, function() {});
		return false;
	}
	var dbaddr=$("input[name='dbaddr']").val();
	var dbname=$("input[name='dbname']").val();
	if(dbaddr==""||dbname==""){
		showTip("请输入数据库信息", 1000, function() {});
		return false;
	}
	var fields = {};
	$("#select2 option").each(function(i,val){
		fields[i]=this.value
	})
	if(fields.length<1){
		showTip("请输入导出属性", 1000, function() {});
		return false;
	}
	var query=$("input[name='query']").val();
	var table=$("input[name='table']").val();
	var limit=$("input[name='limit']").val();
	var _id=$("input[name='_id']").val();
	$.ajax({
		url:"/admin/task/export/save",
		type:"post",
		data:{"_id":_id,"name":name,"v1":v1,"v1name":v1name,"v2":v2,"v2name":v2name,"dbaddr":dbaddr,"dbname":dbname,"fields":fields,"query":query,"table":table,"limit":limit},
		success:function(r){
			if(r){
				showTip("任务已创建", 500, function() {});
				window.location.href="/admin/task/export"	
			}else{
				showTip("任务创建失败！", 1000, function() {});
			}
		}
	})
}
</script>
