{{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><button type="button" class="btn btn-primary" data-toggle="modal" onclick='createOneClear("")'>新增函数清理</button></small>
		</h1>
		<ol class="breadcrumb">
		  <li><a href="/admin/version"><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="clearTable" 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-addclear">
  	<div class="modal-dialog">
	    <form id="addclear-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" id="fieldname" class="col-sm-2 control-label">名称:</label>
				    <div class="col-sm-10">
					   	<select class="form-control" id="addfieldname"></select>
					</div>
				</div>
				<div class="form-group">
				    <label for="code" class="col-sm-2 control-label">版本:</label>
				    <div class="col-sm-10">
				      <input id="clearversion" type="text" value="{{.version}}" class="form-control" disabled>
				    </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="up" class="btn btn-primary">上移</button>
							<button type="button" id="right" class="btn btn-primary">右移</button>
							<button type="button" id="left" class="btn btn-primary">左移</button>
							<button type="button" id="down" 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" data-dismiss="modal" onclick="reset()">取消</button>
		        <button type="button" class="btn btn-primary" onclick="addclearsave()">保存</button>
	    	</div>
		</div>
	    <!-- /.modal-content -->
	    </form>
  	</div>
  <!-- /.modal-dialog -->
</div>
<!-- /.modal -->
<!-- footer -->
{{template "footer"}}

<script>
menuActive("version")
var field = {{.field}};
var _id = "";
//var clearArr = ["cutspace","cutallspace","cutSymbol","cutNotPrs","clearAllWord","clearMaxAmount","clearProjectName","toint","tofloat","totimestamp","tomoney","getcurrency","getrate","getPhone","rateToFloat"]; 
var clearMap = {"中文符号转英文":"chiToEng","去除首尾空格":"cutspace","去除所有空格":"cutallspace","清理符号":"cutSymbol","清理不成对符号后面的内容":"cutNotPrs","清理全部是汉字或者特殊符号的情况":"clearAllWord","过滤大于1万亿":"clearMaxAmount","清理项目名称":"clearProjectName","转int":"toint","转float":"tofloat","转时间戳":"totimestamp","转换金额":"tomoney","获取币种":"getcurrency","获取汇率":"getrate","取手机号":"getPhone","费率转小数":"rateToFloat"}
$(function () {
	ttableclear=$('#clearTable').DataTable({
		"lengthChange": false,
		"searching"   : true,
		"ordering"    : false,
		"info"        : true,
		"autoWidth"   : false,
		"paging"	  : true,
		
		"ajax": {
			"url": "/admin/clear/data",
			"type": "post",
			"data":{"version":{{ .version}} }
		 },
		"language": {
            "url": "../res/dist/js/dataTables.chinese.lang"
        },
		"columns": [
            { "data": "s_name"},
			{ "data": "s_version"},
			{ "data": "s_creater"},
			{ "data": "l_intime"},
			{ "data": "clearInfo"},
			{ "data": "_id",render:function(val,a,row){
				return  "<a href='#' onclick=\"editclear('"+val+"','"+row["s_name"]+"','"+row["clearInfo"]+"')\"><i class='fa fa-fw fa-edit text-yellow'></i></a> &nbsp;"+
						"<a href='#' onclick=\"delclear('"+val+"')\"><i class='fa fa-fw fa-trash text-red'></i></a>"
			}}
       	]
	});
})

//创建标签
function createOneClear(){
	_id = "";
	$("#addfieldname").removeAttr("disabled");
	$("#addfieldname").empty();
	$("#select1").empty();
	$("#select2").empty();
	$("#addfieldname").append("<option value=''>--请选择--</option>");
	for(var i in field){
		if(field[i]["s_name"] != ""){
			$("#addfieldname").append("<option value='"+field[i]["s_name"]+"+"+field[i]["s_field"]+"'>"+field[i]["s_name"]+"</option>");
		}
	}
	for(var i in clearMap){
		$("#select1").append("<option title='"+i+"' value='"+i+"+"+clearMap[i]+"'>"+i+"</option>");
		//$("#select1").append("<option value='"+clearArr[i]+"'>"+clearArr[i]+"</option>");
	}
	$("#modal-info-addclear").modal("show");
}

//保存
function addclearsave(){
	var fieldname = $("#addfieldname").val();
	var version = $("#clearversion").val();
	var clearArr = [];
	$("#select2 option").each(function(i,val){
		clearArr[i] = this.value
	})
	var clearJsonStr = JSON.stringify(clearArr)
	if(fieldname == "" || clearJsonStr == ""){
		alert("表单填写不完整！");
		return false;
	}
	$.ajax({
		url:"/admin/clear/save",
		type:"post",
		data:{"_id":_id,"fieldname":fieldname,"version":version,"clearJsonStr":clearJsonStr,"username":{{index (session "user") "name" }}},
		success:function(r){
			if(r.rep){
				$("#addclear-dataform")[0].reset();
				$("#modal-info-addclear").modal("hide");
				ttableclear.ajax.reload();
			}else{
				$("#addclear-dataform")[0].reset();
				$("#modal-info-addclear").modal("hide");
				alert("保存失败");
			}
		}
	})
}

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

//编辑
function editclear(id,name,clearInfo){
	_id = id; 
	$("#addfieldname").empty();
	$("#select1").empty();
	$("#select2").empty();
	$("#addfieldname").append("<option value='"+name+"'>"+name+"</option>");
	$("#addfieldname").attr("disabled",true);
	var clearData = clearInfo.split(",");
	for(var i in clearMap){
		var flag = true;
		for(var j in clearData){
			if(i == clearData[j]){
				$("#select2").append("<option title='"+i+"' value='"+i+"+"+clearMap[i]+"'>"+i+"</option>");
				flag = false;
			}
		}
		if(flag){
			$("#select1").append("<option title='"+i+"' value='"+i+"+"+clearMap[i]+"'>"+i+"</option>");
		}
	}
	$("#modal-info-addclear").modal("show");
}

</script>
