<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>监听列表</title>
<!-- jquery -->
<script type="text/javascript" src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<!-- 动画 -->
<link rel="stylesheet" href="../../../css/animate.css">
<!-- layerUI -->
<link rel="stylesheet" href="../../../lib/layui/css/layui.css">
<script type="text/javascript" src="../../../lib/layui/layui.js"></script>
<!-- table -->
<link rel="stylesheet" href="../../../lib/htable/common.css">
<script type="text/javascript" src="../../../lib/htable/jquery.aiyi.tools.table.js"></script>
<!-- font awesome-->
<link rel="stylesheet" href="../../../lib/font-awesome/css/font-awesome.min.css">
<!--<script type="text/javascript" src="../../../lib/layer-v3.1.1/layer/layer.js"></script>-->
<link rel="stylesheet" href="../../../lib/layer-v3.1.1/layer/theme/default/layer.css">
<!-- Ajax -->
<script type="text/javascript" src="../../../js/ajax/core.js"></script>
<script type="text/javascript" src="../../../js/layerAjaxMsg/default.js"></script>
</head>
<body class="layui-layout-body">
	<div class="layui-layout" style="padding:10px;">
		<fieldset class="layui-elem-field layui-field-title animated fadeInUp" style="margin-top: 20px;">
		   <legend>服务监听规则配置</legend>
		</fieldset>
		<button class="layui-btn animated fadeInUp" id="addBtn"><i class='layui-icon'>&#xe654;</i> 添加监听规则</button>
		<div id="table" class="animated fadeInUp" style="margin-top:10px;"></div>
	</div>
	
	
	<script type="text/javascript">
		var $table = $("#table").createTable({
            noPage:true,
            request:"list",
            colom:[
                ['name', '规则名称'],
                ['path', '映射规则'],
				['server', '所属监听域'],
                ['proxyPass', '代理地址'],
                ['root', '跟路径'],
                ['work', '操作']
            ],
            filter:function (item){
                item.name = (!item.name ? "-" : item.name);
                item.proxyPass = (!item.proxyPass ? "-" : item.proxyPass);
                item.root = (!item.root ? "-" : item.root);
            	item.work = "<a style='line-height:25px;height:25px;' class='layui-btn bj' href='javascript:;' desc = '" + item.pathId + "' nm='" + item.name + "' serv='" + item.server + "'><i class='fa fa-edit'></i> 编辑</a><a style='line-height:25px;height:25px;' class='layui-btn sc' href='javascript:;' desc='" + item.pathId + "' serv='" + item.server + "'><i class='fa fa-trash-o'></i> 删除</a>"
                return item;
            }, success: function (t) {
				t.find("td a").click(function(){
				    if($(this).hasClass("bj")){
				   	 	//编辑
				    		edit($(this).attr("serv"), $(this).attr("desc"))
				    		//parent.window.hTab.open($(this).attr("desc"), "lisner/list/edit/" + $(this).attr("desc"), "监听配置-" + $(this).attr("nm"));
					}else{
						var value = $(this).attr("desc");
						var lcoa = $(this).attr("serv");
						layui.use('form', function(){
							var layer = layui.layer;
							var c = layer.confirm('<p style="padding:10px;">删除监听配置之前请确保其内的监听规则已全部删除。您确定要删除嘛？</p>', {
								btn: ['确定','取消'], //按钮
								type:1
							}, function(){
								var loding = layer.msg("<i class='layui-icon layui-anim layui-anim-rotate layui-anim-loop'>&#xe63e;</i> 正在模拟启动, 请稍后...", {
									time: 0,
									shade: 0.3
								});
								//删除
								H.del("../rule/" + lcoa.replace(":", "_") + "/" + value + "/edit", {}, function(res){
									location = location;
								});

							}, function(){
								return true;
							});
						});
					}
				});
            }
        });

		$("#addBtn").click(function(){
		    edit(null);
		});
		
		function edit(serv, pathId){
			var name = "新建规则";
			if(pathId){
				name = "编辑规则";
			}
			if (!serv){
				serv = "null_null";
				pathId = "Lw==";
			}
			H.getTxt("../rule/" + serv.replace(":", "_") + "/" + pathId + "/edit", function(html){
				layui.use('form', function() {
					var layer = layui.layer;
					layer.open({
						type:1,
						title:"监听规则-" + name,
						content:"<div style='padding:10px;'>" + html + "</div>",
						btn:['保存', '取消'],
						area:['650px', '400px'],
						maxmin:true,
						yes:function(){
							var data = getFromData();
							for(var i in data.errorPage){
								var msg = "请规范填写统一错误页面，若不需要请删除错误的填写。";
								var ss = data.errorPage[i].status;
								if(ss.length < 1){
									throwError(msg);
								}
								for(var j in ss){
									if (!isNumber(ss[j])){
										throwError(msg);
									}
								}
								if(!data.errorPage[i].path){
									throwError(msg);
								}
							}
							if (!data.server) {
								throwError("必须选择一个监听域");
							}

							//保存
							layer.msg("<i class='layui-icon layui-anim layui-anim-rotate layui-anim-loop'>&#xe63e;</i> 正在模拟启动, 请稍后...", {
								time: 0,
								shade: 0.3
							});
							H.put("../rule/" + serv.replace(":", "_") + "/" + pathId + "/edit", data, function(res){
								location = location;
							});

							return false;
						}
					});
				});

			});
		}

		function throwError(msg){
			layui.use('form', function() {
				var layer = layui.layer;
				layer.msg(msg);
			});
		    throw  new Error(msg);
		}

        function isNumber(value) {
            var patrn = /^[0-9]*$/;
            if (patrn.exec(value) == null || value == "") {
                return false
            } else {
                return true
            }
        }
		
	</script>
</body>
</html>