{{template "inc"}}
<!-- Main Header -->
{{template "header"}}
<!-- Left side column. 权限菜单 -->
{{template "memu"}}
	<link href="/res/jsoneditor/css/jsoneditor.css" rel="stylesheet" type="text/css">
 	<script src="/res/jsoneditor/js/jsoneditor.js"></script>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
	<section class="content-header">
		<h1>
			<small><button class="btn btn-primary createOneTag" onclick='createOneTag("")'>新增标签</button></small>
		</h1>
		<ol class="breadcrumb">
		  <li><a href="/admin/version"><i class="fa fa-dashboard"></i> 版本控制</a></li>
		  <li><a href="/admin/version">版本管理</a></li>
		  <li><a href="/admin/site_management/tag?version={{.version}}&vid={{.vid}}&pid={{.pid}}">标签库</a></li>
		  <li class="active"><a href="/admin/site_management/onetag?version={{.version}}&tagname={{.tagname}}&tp={{.tp}}&parentid={{.parentid}}">{{.tagname}}</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="dataTagTable" 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-addonetag">
  	<div class="modal-dialog">
	    <form id="addone-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">{{.tagname}}信息</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">
					   	<select class="form-control" id="addone-name"></select>
					</div>
					<!--<span class="modifyCheck hide check">请选择</span>-->
				</div>
				<div class="form-group">
				    <label for="code" class="col-sm-2 control-label">版本:</label>
				    <div class="col-sm-10">
				      <input id="addone-version" 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">
						<div id="jsonadd" style="height:300px"></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="saveaddone()">保存</button>
	    	</div>
		</div>
	    <!-- /.modal-content -->
	    </form>
  	</div>
  <!-- /.modal-dialog -->
</div>
<!-- /.modal -->
<!--编辑标签--> 
<div class="modal fade" id="modal-info-editonetag">
  	<div class="modal-dialog">
	    <form id="editone-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">{{.tagname}}信息</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 id="editone-name" type="text" 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">
				    	<div id="jsoneditor" style="height:300px"></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="saveeditdata()">保存</button>
	    	</div>
			<!--<div class="modal-footer">
		         <input id="editone-id" type="hidden" class="form-control" name="_id">
	    	</div>-->
		</div>
	    <!-- /.modal-content -->
	    </form>
  	</div>
  <!-- /.modal-dialog -->
</div>
<!-- /.modal -->

<!-- footer -->
{{template "footer"}}

<script>
menuActive("version")
var ttableonetag
var tp = {{.tp}};
var parentid = {{.parentid}}
var pratagname = {{.tagname}};
var version = {{ .version}};
var username = {{index (session "user") "name" }};
var editor = null;
var oldcontent = "";
var id = "";
$(function () {
	ttableonetag=$('#dataTagTable').DataTable({
		"lengthChange": false,
		"searching"   : true,
		"ordering"    : false,
		"info"        : true,
		"autoWidth"   : false,
		"paging"	  : true,
		
		"ajax": {
			"url": "/admin/site_management/onetag/data",
			"type": "post",
			"data":{"version":{{ .version}},"vid":{{ .vid}},"pid":{{ .pid}},"tagname":{{.tagname}} ,"parentid":{{.parentid}}}
		 },
		"language": {
			"url": "/res/dist/js/dataTables.chinese.lang"
        },
		{{if eq .tp "citysim" "cityall"}}
		"columnDefs": [
				{ "targets": 4 ,"bVisible": false} //隐藏列
			],
		{{end}}
		"columns": [
            { "data": "s_name"},
			{ "data": "s_creater"},
			{ "data": "l_intime"},
			{ "data": "s_type"},
			{ "data": "content",render:function(val,a,row){
				var strval = JSON.stringify(val);
				if(strval.length > 50){
					strval = strval.substr(0,50)+"...";
				}
				return  strval;
			}},
			{ "data": "_id",render:function(val,a,row){
				return  "<a href='#' onclick=\"editonetag('"+val+"','"+row["s_name"]+"')\"><i class='fa fa-fw fa-edit text-yellow'></i></a> &nbsp;"+
						"<a href='#' onclick='delonetag(\""+val+"\")'><i class='fa fa-fw fa-trash text-red'></i></a>"
			}}
       	]
	});
	//ttableonetag.on('init.dt', function () {});
})

//编辑
function editonetag(_id,name){
	$("#jsoneditor").html('');
	id = _id;
	$.ajax({
		url:"/admin/site_management/onetag/searchone",
		type:"post",
		data:{"_id":_id},
		success:function(r){
			if(r.data != null){
				var container = document.getElementById('jsoneditor');
				showJson(r.data.content,container);
			}
		}
	})
	$("#editone-name").val(name);
	$("#modal-info-editonetag").modal("show");
}

function showJson(content,container){
	var options = {
    	mode: 'code',
    	modes: ['code', 'tree'], // allowed modes
    	onError: function (err) {
      	alert(err.toString());
    	}
  	};
	editor =  new JSONEditor(container, options, content);
	oldcontent = editor.getText();
}

//编辑保存
function saveeditdata(){
	var newcontent = editor.getText();
	var sy = document.getElementById('errorjson').style.display;
	if(newcontent == "" || newcontent == oldcontent || sy == "block"){
		alert("表单填写不完整或内容未修改！");
			return false;
	}
	$.ajax({
		url:"/admin/site_management/onetag/editsave",
		type:"post",
		data:{"_id":id,"content":newcontent},
		success:function(r){
			if(r.rep){
				$("#editone-dataform")[0].reset();
				$("#modal-info-editonetag").modal("hide");
				ttableonetag.ajax.reload();
			}else{
				alert("保存失败");
			}
		}
	});
}


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

//创建标签
function createOneTag(){
	$("#jsonadd").html('');
	var arr = [];
	var url = "/admin/getfields";
	if(tp == "citysim" || tp == "cityall"){
		$("#labelname").html("省/直辖市");
		url = "/admin/getcitys";
	}else if(tp == "province"){
		$("#labelname").html("省份");
		url = "/admin/getprovinces";
	}
	$.ajax({
		url:url,
		type:"post",
		async:false,
		success:function(r){
			if(r.data != null){
				arr = r.data;
			}
		}
	});
	$("#addone-name").empty();
	$("#addone-name").append("<option value=''>--请选择--</option>");
	for(var i in arr){
		if(arr[i]["s_name"] != ""){
			$("#addone-name").append("<option value='"+arr[i]["s_name"]+"+"+arr[i]["s_field"]+"'>"+arr[i]["s_name"]+"</option>");
		}
	}
	var container = document.getElementById('jsonadd');
	showJson("",container);
	$("#modal-info-addonetag").modal("show");
}

//保存创建的标签
function saveaddone(){
	var name = $("#addone-name").val();
	namearr = name.split("+");
	content = editor.getText();
	var contentrep = content.replace(/\s+/g,"");
	if(contentrep == "" || contentrep == "[]" || contentrep == "{}" || name == "" || (tp != "citysim" && tp != "cityall" && content[0] == "{")){
		alert("表单填写不完整或格式不正确！");
		return false;
	}
	if(namearr.length != 2){
		alert("名称或省/直辖市数据错误!");
		return false;
	}
	$.ajax({
		url:"/admin/site_management/onetag/create",
		type:"post",
		data:{"pratagname":pratagname,"name":namearr[0],"field":namearr[1],"version":version,"tp":tp,
		"content":content,"username":username,"parentid":parentid,"pid":{{.pid}},"vid":{{.vid}}},
		success:function(r){
			if(r.rep){
				$("#addone-dataform")[0].reset();
				$("#modal-info-addonetag").modal("hide");
				ttableonetag.ajax.reload();
			}else{
				alert("保存失败");
			}
		}
	});
}
</script>
