{{template "inc"}}
<!-- Main Header -->
{{template "header"}}
<!-- Left side column. 权限菜单 -->
{{template "memu"}}
<link href="../res/doublebox/css/hdw.css" rel="stylesheet" type="text/css">
<style>.myred{ color:red;   }</style>
<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 opr" opr="new">新增版本</a></small>*/}}
			<small><button type="button" class="btn btn-primary" data-toggle="modal" onclick='createOneVersion("")'>新增版本</button></small>
		</h1>
		<ol class="breadcrumb">
		  <li><a href="/admin/version"><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-version" >
	<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" id="version_title">新增版本</h4>
				</div>
				<div class="modal-body">
					<div class="form-group" >
						<label for="code" class="col-sm-2 control-label myred"  >版本名称:</label>
						<div class="col-sm-10">
							<input name="name" type="text" id="version" value="" class="form-control" placeholder="版本名称">
						</div>
					</div>
					<div class="form-group" >
						<label for="code" class="col-sm-2 control-label myred" >描述:</label>
						<div class="col-sm-10">
							<input name="name" type="text" id="s_descript" value="" class="form-control" placeholder="描述信息">
						</div>
					</div>
					<div class="form-group">
						<label for="code" id="fieldname" class="col-sm-2 control-label">城市抽取:</label>
						<div class="col-sm-10">
							<select class="form-control " id="isextractcity">
								<option value=false>否</option>
								<option value=true>是</option>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label for="code" id="fieldname" class="col-sm-2 control-label">克隆版本:</label>
						<div class="col-sm-10">
							<select id="s_pversionid" name="v1" class="form-control">
							</select>
						</div>
					</div>
					<div class="form-group">
						<label for="code" id="fieldname" class="col-sm-2 control-label">克隆属性:</label>
						<div class="col-sm-10">
							<select class="form-control" id="iscopyfiled">
								<option value=false>否</option>
								<option value=true>是</option>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label for="code" id="fieldname" class="col-sm-2 control-label">抽取附件:</label>
						<div class="col-sm-10">
							<select class="form-control" id="isfiles">
								<option value=false>否</option>
								<option value=true>是</option>
							</select>
						</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("version")
$(function () {
	ttable=$('#dataTable').DataTable({
		"paging"      : false,
		"lengthChange": false,
		"searching"   : true,
		"ordering"    : false,
		"info"        : true,
		"autoWidth"   : false,
		"ajax": {
			"url": "/admin/version/data",
			"type": "post",
			"data":{}
		},
		"language": {
            "url": "/res/dist/js/dataTables.chinese.lang"
        },
		"columns": [
            { "data": "version"},
			{ "data": "s_username"},
			{ "data": "s_descript","width":"25%"},
			{ "data": "isuse",render:function(val,a,row){
				tmp=""
				if(val){
					tmp="<a href='#' title='停用' onclick='use(\""+row._id+"\","+false+")'><i class='fa fa-fw fa-circle text-green'></i></a>已启用"
				}else{
					tmp="<a href='#' title='启用' onclick='use(\""+row._id+"\","+true+")'><i class='fa fa-fw fa-circle text-red'></i></a>未启用"
				}
				return tmp
			}},
			{ "data":"_id","width":"28%",render:function(val,a,row){
				tmp = '<div class="btn-group">'+
					'<a class="btn btn-sm btn-primary" href="/admin/rulepre?version='+row.version+'">前置规则</a>'+
					'<a class="btn btn-sm btn-info" href="/admin/ruleback?version='+row.version+'">后置规则</a>'+
					'<a class="btn btn-sm btn-warning" href="/admin/tag?version='+row.version+'">标签库</a>'+
					'<a class="btn btn-sm btn-danger" href="/admin/clear?version='+row.version+'">函数清理</a>'+
					'</div>';
				return  tmp
			}},
			{ "data":"_id","width":"25%",render:function(val,a,row){
				return '<div class="btn-group">'+
						'<a class="btn btn-sm btn-success" href="/admin/version/info?vid='+val+'" >属性</a>'+
						'<a class="btn btn-sm btn-warning" href="/admin/version/blockinfo?vid='+val+'" >分块</a>'+
						'<a class="btn btn-sm btn-info" href="/admin/version/pkginfo?vid='+val+'" >分包</a>'+
						/*'<a class="btn btn-sm btn-primary opr" opr="edit">编&nbsp;&nbsp;辑1</a>'+*/
						"<a class=\"btn btn-sm btn-primary opr\" href='#' onclick=\"edit('"+val+"')\">编&nbsp;&nbsp;辑</a> &nbsp;"+
						'<a class="btn btn-sm btn-danger" href="#" onclick="del(\''+val+'\',\''+row["version"]+'\')">删&nbsp;&nbsp;除</a>'
					'</div>';
			}}
       	]
	});
	ttable.on('init.dt', function () {
		$(".opr").click(function(){
			var n=$(this).attr("opr")
			var _tit="",htmlObj={},obj,tag=[]
			switch(n){
			case "edit":
				obj=ttable.row($(this).closest("tr")).data();
			case "new":
				tag=[
					{label:"版本名称",s_label:"version",placeholder:"版本名称",must:true},
					{label:"描述",s_label:"s_descript",placeholder:"描述信息",must:true},
					{label:"城市抽取",s_label:"isextractcity",type:"tpl_list_local",list:[{"s_name":"是","_id":true},{"s_name":"否","_id":false}],default:false},
					{label:"克隆版本",s_label:"s_pversionid",type:"tpl_list_local",url:"/admin/getversions"},
					{label:"克隆属性",s_label:"iscopyfiled",type:"tpl_list_local",list:[{"s_name":"是","_id":true},{"s_name":"否","_id":false}],default:false},
					{label:"是否附件",s_label:"isfiles",type:"tpl_list_local",list:[{"s_name":"是","_id":true},{"s_name":"否","_id":false}],default:false},
					{label:"附件字段",s_label:"s_filefileds",type:"tpl_list_local",url:"/admin/getfields"},
					{s_label:"isuse",type:"tpl_hidden"},
					{s_label:"_id",type:"tpl_hidden"}
				]
				if(n == "new"){
					_tit="新增版本";
				}else{
					_tit="编辑版本";
					tag[0]={label:"版本名称",s_label:"version",placeholder:"版本名称",must:true,disabled:true}
				}
				htmlObj={
					title:_tit,
					tag:tag,
					bts:[
						{label:"保存",class:"btn-primary",
							fun:function(){
								var obj={}
								var bcon=true
								$("#_con").find("input[id!=s_show],textarea").each(function(i,el){
									var val=$(el).val();
									obj[el.id]=$(el).val()
									if(el.id!="_id"&&$(el).attr("must")&&!val){
										bcon=false
										return false
									}
								})
                                console.log(obj)
                                return;
								if (bcon){
									if(obj.isuse != "true"){
										obj.isuse = false
									}
									$.post("/admin/version/save",obj,function(data){
										if(data&&data.rep){
											window.location.href="/admin/version"			
										}else{
											showTip("已存在！",1000)
										}
									},'json')
								}else{
									alert("红色标签的表单不能为空！")
								}
							}
						}
					]
				}
			OpenDialog(htmlObj,obj)
			break;
			}
		});
	})
})
function use(_id,isuse){
	smg=""
	if(isuse){
		smg="确定启用？"
	}else{
		smg="确定停用？"
	}
	showConfirm(smg, function() {
		$.ajax({
			url:"/admin/version/use",
			type:"post",
			data:{"_id":_id,"isuse":isuse},
			success:function(r){
				if(r.rep){				
					window.location.href="/admin/version"	
				}else{
					showTip("启用失败", 1000, function() {});
				}
			}
		})
	});
}
function del(_id,version){
	showConfirm("确定删除?", function() {
		$.ajax({
			url:"/admin/version/del",
			type:"post",
			data:{"_id":_id,"version":version},
			success:function(r){
				if(r.rep){				
					window.location.href="/admin/version"
				}else{
					showTip("删除失败", 1000, function() {});
				}
			}
		})
	});
}
var field = {{.field}};
var clearMap = {"资金来源": "funds","中标单位":"winner","招标机构": "agency","项目范围": "projectscope",
	"项目编号": "projectcode","评标方法": "bidmethod", "开标日期": "bidopentime","监理项目费率": "supervisorrate",
	"候选人": "winnerorder", "城市": "city","成交状态": "bidstatus", "采购单位联系人": "buyerperson", "采购单位电话": "buyertel",
	"采购单位": "buyer","币种": "currency", "项目名称": "projectname","中标金额": "bidamount", "预算": "budget",}


//创建标签
function createOneVersion(){
	_id = "";
	$("#version_title").html("新增版本");
	$("#version").val("").attr({"disabled":false,"placeholder":"版本名称"});//版本
	$("#s_descript").val("").attr({"placeholder":"描述信息"});//描述
	$("#s_pversionid").empty();
	$("#s_pversionid").append("<option value=''></option>")
	$("#select1").empty();
	$("#select2").empty();
	for(var i in clearMap){
		$("#select1").append("<option  value="+clearMap[i]+">"+i+"</option>");
		//$("#select1").append("<option value='"+clearArr[i]+"'>"+clearArr[i]+"</option>");
	}
	$.ajax({
		url:"/admin/getversions",
		type:"post",
		data:{},
		success:function(r){
			if(r.data){
				for(var i in r.data){
					$("#s_pversionid").append("<option value='"+r.data[i]._id+"'>"+r.data[i].version+"</option>");
					//$("#select1").append("<option value='"+clearArr[i]+"'>"+clearArr[i]+"</option>");
				}
			}
		}
	})

	$("#modal-info-version").modal("show");
}
//保存
function save(){
    var version = $("#version").val();//版本
    var s_descript = $("#s_descript").val();//描述
    var isextractcity = $("#isextractcity").val()//是否抽取城市
    var s_pversionid = $("#s_pversionid").val()//克隆版本
    var iscopyfiled = $("#iscopyfiled").val()//克隆属性
    var isfiles = $("#isfiles").val()//是否附件
    var s_filefileds = [];
    $("#select2 option").each(function(i,val){
        s_filefileds[i] = this.value
    })
	// var s_filefiledsJsonStr = JSON.stringify(s_filefileds)
	var s_filefiledsJsonStr = s_filefileds.join(',')
    if(fieldname == "" ||s_descript==""){
        alert("版本名称或者描述填写不完整！");
        return false;
    }
    /*console.log({"_id":_id,"s_descript":s_descript,"version":version,"isextractcity":isextractcity,"s_pversionid":s_pversionid,"iscopyfiled":iscopyfiled,"isfiles":isfiles,"s_filefileds":s_filefiledsJsonStr})*/
    // {{/*data:{"_id":_id,"s_descript":s_descript,"version":version,"clearJsonStr":clearJsonStr,"username":{{index (session "user") "name" }}},*/}}
    $.ajax({
        url:"/admin/version/save",
        type:"post",
        data:{"_id":_id,"s_descript":s_descript,"version":version,"isextractcity":isextractcity,"s_pversionid":s_pversionid,"iscopyfiled":iscopyfiled,"isfiles":isfiles,"s_filefileds":s_filefiledsJsonStr},
        success:function(r){
			if(r&&r.rep){
				window.location.href="/admin/version"
			}else{
				showTip("已存在！",1000)
			}
        }
    })
}
//编辑
function edit(id){
	_id = id;
	$("#version_title").html("编辑版本");
	$.ajax({
		url:"/admin/version/dataById?_id="+id,
		type:"get",
		success:function(r){
			if (r.req){
				$("#version").empty();
				$("#version").val(r.data.version).attr("disabled",true);//版本
				$("#s_descript").empty();
				$("#s_descript").val(r.data.s_descript);//描述
				$("#isextractcity")[0].value=r.data.isextractcity;//城市抽取
				$.ajax({
					url:"/admin/getversions",
					type:"post",
					success:function (rv) {
						$("#s_pversionid").empty();
						for(var i in rv.data){
							$("#s_pversionid").append("<option value='"+rv.data[i]._id+"'>"+rv.data[i].version+"</option>");
							//$("#select1").append("<option value='"+clearArr[i]+"'>"+clearArr[i]+"</option>");
						}
						$("#s_pversionid")[0].value=r.data.s_pversionid;//克隆版本
						$("#iscopyfiled")[0].value=r.data.iscopyfiled;//克隆属性
						$("#isfiles")[0].value=r.data.isfiles;//是否附件
						//导出属性
						$("#select1").empty();
						$("#select2").empty();
						var s_filefileds=[]
						s_filefileds = r.data.s_filefileds
						// console.log(r.data.s_filefileds)
						// if (typeof r.data.s_filefileds=="string"){
						// 	s_filefileds = JSON.parse(r.data.s_filefileds)
						// }
						for(var i in clearMap){
							var flag = true;
							for(var j in s_filefileds){
								// console.log(i,s_filefileds[j])
								if(clearMap[i] == s_filefileds[j]){
									$("#select2").append("<option title='"+i+"' value='"+clearMap[i]+"'>"+i+"</option>");
									flag = false;
								}
							}
							if(flag){
								$("#select1").append("<option title='"+i+"' value='"+clearMap[i]+"'>"+i+"</option>");
							}
						}
						$("#modal-info-version").modal("show");
					}
				})
			}else {
				alert(r)
			}
		}
	})





}

</script>
