<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="../common/common.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<% String path=request.getContextPath();%>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link type="text/css" rel="stylesheet" href="<%=path%>/css/base.css"/>
    <link type="text/css" rel="stylesheet" href="<%=path%>/css/layout.css"/>
    <link href="<%=path%>/css/style.css" rel="stylesheet"/>
    <link href="<%=path%>/css/zTreeStyle.css" rel="stylesheet"/>

<script>
	$(function(){
		$('.singleSelect').select2();
	});
</script>

<script type="text/javascript">
var uuid='${uuid}';
$(function(){
	var templateCode = $("#templateCode").val();
	if(templateCode != ''){
		exchangeLianTong();
	}

	var access_key =$("#access_key").val();
	if(access_key==''){
		$("#access_key").val(uuid);
	}
	var http_response ='${paConfig.http_response}';
	$("#http_response").val(http_response);
});


    function exchangeLianTong() {
    	//alert("${templateCode}");
    	var templateCode = $("#templateCode").val();

    	$.ajax({
			url: "<%=path%>/paList/getHttpTemplateMms.html?templateCode="+templateCode,
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			type: "POST",
			dataType : 'json',
			success: function(data){
				//alert(JSON.stringify(data.template));
				expand(data.template);
				setEditVal();
				showUrl(data.template)
			},
			error:function (message) {
				//+JSON.stringify(message)
                alert("模版错误，请重新选择！");
            }

		});


    }
    function showUrl(template){
		var templateDefaultVar =  eval('(' + template.templateDefaultVar + ')');//{"template_name":"buyun","receiveType":"push"}
    	if(templateDefaultVar.receiveType == "push"){
    		//document.getElementById("access_url").type="text";
    		$("#access_td1").show();
    		$("#access_td2").show();
    		//$("#access_url").val('http://:3001/push/report/'+$("#access_key").val());
			var access_url = ''
			let template_type = templateDefaultVar.template_type;
			if(template_type&&template_type == "csp" || template_type == "gkCsp"){
				access_url = '状态报告：http://${systemIp}:3001/push/csp-report/'+$("#access_key").val()
						+'</br> 上行：  http://${systemIp}:3001/push/csp-report/'+$("#access_key").val()
						+'</br> 素材审核状态：  http://${systemIp}:3001/push/csp-notify/'+$("#access_key").val();

			}else if(template_type&&(template_type == "telecomCsp"||template_type == "unicomCsp")){
				access_url = '状态报告：http://${systemIp}:3001/push/telecom-unicom-report/'+$("#access_key").val()
						+'</br> 上行：  http://${systemIp}:3001/push/telecom-unicom-report/'+$("#access_key").val()
						+'</br> 素材审核状态：  http://${systemIp}:3001/push/telecom-unicom-report/'+$("#access_key").val();

			}else{
				access_url = '状态报告：http://${systemIp}:3001/push/mms/report/'+$("#access_key").val()
						+'</br> 上行：  http://${systemIp}:3001/push/mms/reply/'+$("#access_key").val()
						+'</br> 模版审核通知：  http://${systemIp}:3001/push/mms/notify/'+$("#access_key").val()
						+'</br> 签名审核通知：  http://${systemIp}:3001/push/mms/sign/'+$("#access_key").val();
			}

    		$("#access_url").html(access_url);
    	}else{
    		$("#access_td1").hide();
    		$("#access_td2").hide();
    	}
    }
    function expand(template) {
		$("#templateVar").val(template.templateVar);
		$('.expand_tr').remove();
		var trs = '<tr class="expand_tr"><th >动态参数</th><td colspan="3" width="100%"></td></tr>';
		//alert(template.templateVar);
		if(template.templateVar == ''){
			return;
		}
		var templateVar =  eval('(' + template.templateVar + ')');
    	//alert(templateVar);

    	for (var i = 0; i < templateVar.length; i++) {
    		var tr = '';
    		var type = 'text';
    		if(templateVar[i].type == 'string'){
    			type = 'text';
    		}else if(templateVar[i].type == 'number'){
    			type = 'number';
    		}
    		if(i%2 == 0){
    			trs +='<tr class="expand_tr">';
    			tr = '<th scope="row"><div class="floatLeft"><i class="star">*</i>'+templateVar[i].cnname+'</div></th>'
            	+'<td  width="42%"><input type="'+type+'" class="s220_text" id="'+templateVar[i].name+'" name="'+templateVar[i].name+'" placeholder="'+templateVar[i].cnname
            	+'" maxlength="'+templateVar[i].len+'" style="width:300px" oninput = "value=value.trim()" /></td>';
    		}else{
    			//<td class="w120"><div class="floatLeft">发送分包大小</div></td>
    			tr = '<td class="w120"><div class="floatLeft"><i class="star">*</i>'+templateVar[i].cnname+'</div></td>'
            	+'<td ><input type="'+type+'" class="s220_text" id="'+templateVar[i].name+'" name="'+templateVar[i].name
            	+'" placeholder="'+templateVar[i].cnname+'" maxlength="'+templateVar[i].len+'" style="width:300px" oninput = "value=value.trim()" /></td>';
    		}

        	trs += tr;
        	if(i%2 != 0){
        		trs +='</tr>';
        	}
        	//alert(trs);
		}
   		$('#expand_table').append(trs);
	}
    function setEditVal(){
    	var channelTemplatevar='${paConfig.channelTemplatevar}'

    	if(channelTemplatevar !=''){
			var channelTemplatevar2 = ${empty paConfig.channelTemplatevar?'{}':paConfig.channelTemplatevar}
			for ( var iterable_element in channelTemplatevar2) {
				$("#"+iterable_element).val(channelTemplatevar2[iterable_element]);
			}
    	}
    }
	function goback() {
		window.location.href="../paList/PaMmsConfigList.html";
    }
	/** 修改通道配置 */
	doeditHttpConfig=function (){
		/*增加验证*/
		var name=$("#name").val();
		var accessnumber=$("#accessnumber").val();
		var protocol_type = $("#protocol_type").val();

		var supportUnicom=$("#supportUnicom").val();
		var supportMobile=$("#supportMobile").val();
		var supportTelcom=$("#supportTelcom").val();
		/* var supportInternational=$("#supportInternational").val(); */
		var templateCode= $("#templateCode").val();


		var reg=/^[-\+]?\d+(\.\d+)?$/;
		var ipexpre=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
		var httpexpre=/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
		var numberexpre = /^(\+|-)?\d+($|\.\d+$)/;
		var stringnumberexpre =  /^[0-9a-zA-Z]*$/g;  //判断字符串是否为数字和字母组合

		var batchsize=$("#batchsize").val();

		var ipReg = new RegExp(ipexpre);
		var httpReg = new RegExp(httpexpre);

		if(templateCode== ''||templateCode==null){
			alert("请选择一个模版!");
			return false;
		}
		//var templateVar=  eval('(' + $("#templateVar").val() + ')');
		if(!$('#supportUnicom').is(':checked')&&!$('#supportMobile').is(':checked')&&!$('#supportTelcom').is(':checked')) {
			alert("至少选一个支持的运营商");
			return false;
		}
		if(supportUnicom !=1&&supportMobile!=1&&supportTelcom!=1){
			alert("至少选一个支持的运营商");
			return false;
		}

		if (!name) {
			alert("网关名称不能为空!");
			return false;
		}
		if (!accessnumber) {
			alert("接入号不能为空!");
			return false;
		}else if(!numberexpre.test(accessnumber)){
			alert("接入号只能为纯数字!");
			return false;
		}

		if (accessnumber.length > 20) {
			alert("总接入号不能超过20位!");
	        return false;
	    }


		// http协议
//		if (!pa_url) {
//			alert("通道调用地址不能为空");
//			return false;
//		} else if (!httpexpre.test(pa_url)) {
//			alert('请输入合法的http(s)地址，例如：http//..或者https://')
//			return false;
//		}
//		if (!pa_url) {
//			alert("通道调用地址不能为空");
//			return false;
//		}
		if (!batchsize) {
			alert("发送分包大小不能为空");
			return false;
		} else {
			var patrn = /^\d{1,}$/;
			if (batchsize != '' && !patrn.test(batchsize)) {
				alert('发送分包大小格式不正确');
				return false
			}
		}

		  /* submitForm('editPaHttpConfig', '../paList/checkAccessnumber.html',
					function(data) {
				if (data && data.returnCode == '200') {
//					Dialog.alertSucc(data.msg);
				}
				}, function(data) {
					if (data && data.returnCode == '201') {
						alert("温馨提示："+data.msg);
					}
				}); */

			submitForm('editPaHttpConfig', '../mms/savePaHttpMmsConfig.html',
				function(data) {
					if (data && data.returnCode == '200') {
						Dialog.alertSucc(data.msg);
					}
					window.location.href = "../paList/PaMmsConfigList.html";
				}, function(data) {
					if (data && data.returnCode == '201') {
						Dialog.alertError(data.msg);
					}
				});

	};
</script>


<%--</head>--%>
	<form id="editPaHttpConfig" method="post" action="${basepage}/paList/PaHttpConfigList.html">
		<input type="hidden" name="id" id="id" value="${paConfig.id}" />
		<input type="hidden" name="protocol_type" id="protocol_type" value="4" />
		<input type="hidden" name="templateVar" id="templateVar" />
		<div class="container2">

			<div class="main_p20">
				<div>
					<div class="infoShow">
						<h3 class="info_tit"></h3>
						<table width="100%" border="0" cellspacing="0" cellpadding="0" class="infotable" id= "expand_table">
							<tr>
								<th scope="row"><div class="floatLeft"><i class="star">*</i>选择模版---${paConfig.templateCode}</div></th>
								<td width="42%">
									<select id="templateCode" name="templateCode" onchange="javascript:exchangeLianTong()" class="form_control form_white" >
	                                    <option value="" <c:if test="${paConfig.templateCode == ''}">selected</c:if>>请选择</option>
	                                    <c:forEach items="${templates}" var="template">
				                			<option value="${template.templateCode}" <c:if test="${paConfig.templateCode == template.templateCode}">selected</c:if> >${template.templateName}</option>
				                		</c:forEach>
	                                </select><%-- <label><i class="star"><b>？？？</b></i> <a  href="${basepage}/paList/PaHttpTemplateList.html?">(不清楚选择哪个,点击查找)</a></label> --%>
								</td>
								<td class="w120"><div class="floatLeft"><i class="star">*</i>支持运营商:</div></td>
								<td colspan="2">
								<input id="supportUnicom" name="is_support_unicom" type="checkbox" value="1"
                            		<c:if test="${paConfig.is_support_unicom==1}">checked</c:if> /> 联通
                            	<input id="supportMobile" name="is_support_mobile" type="checkbox" value="1"
                            		<c:if test="${paConfig.is_support_mobile==1}">checked</c:if> /> 移动
                            	<input id="supportTelcom" name="is_support_telecom" type="checkbox" value="1"
                            		<c:if test="${paConfig.is_support_telecom==1}">checked</c:if> /> 电信
                            	<%-- <input id="supportInternational" name="is_support_international" type="checkbox" value="1"
                            		<c:if test="${paConfig.is_support_international==1}">checked</c:if> /> 国际  --%>
                            		</cite>
								</td>

							</tr>

							<%-- 接入号使用同一个--%>
							<tr>
								<th scope="row"><div class="floatLeft"><i class="star">*</i>网关名称:</div></th>
								<td colspan="1"><input id="name" type="text" class="s220_text" name="name" value="${paConfig.name}" placeholder="网关名称"  style="width:300px"/></td>
								<td class="w120"><div class="floatLeft"><i class="star">*</i>接入号（三网统一）</div></td>
								<td>
								    <input id="accessnumber" type="text" class="s220_text" name="accessnumber" value="${paConfig.accessnumber}" placeholder="接入号"  style="width:300px"/>
								</td>
							</tr>



							<tr id="httpresponse" >
                                <th scope="row"><div class="floatLeft">状态和上行推送响应</div></th>
                                <td width="42%"><input type="text" class="s220_text" id="http_response" name="http_response"  placeholder="http响应"  style="width:300px"/></td>
                                <td class="w120"><div class="floatLeft">发送分包大小</div></td>
                                <td><input type="text" class="s220_text" id="batchsize" name="batchsize" value="${paConfig.batchsize}" placeholder="发送分包大小"  style="width:300px"/></td>
                            </tr>
							<tr>
								<th scope="row">
									<div class="floatLeft">供应商</div>
								</th>
								<td width="42%">
									<select id="supplier_id" name="supplier_id" class="singleSelect s220_text">
										<option value="">请选择</option>
										<c:forEach items="${suppliser}" var="suppliers">
											<option
													<c:if test="${suppliers.id==paConfig.supplier_id}">selected</c:if>
													value="${suppliers.id}">${suppliers.id} ${suppliers.supplier_name}</option>
										</c:forEach>
									</select>
								</td>
								<td class="w120">
									<div class="floatLeft">合作公司</div>
								</td>
								<td colspan="2">
									<select class="singleSelect s220_text" name="cooperateCompany" id="company">
										<option value="">请选择</option>
										<c:forEach items="${company}" var="item" varStatus="vs">
											<option value="${item.id}"
													<c:if test="${paConfig.cooperateCompany == item.name}">selected</c:if>>${item.name}</option>
										</c:forEach>
									</select>
								</td>
							</tr>

							<tr>
								<th scope="row">
									<div class="floatLeft">引流拦截策略</div>
								</th>
								<td width="42%">
									<input type="radio" name="attractLimit" value="0" ${paConfig.attractLimit == null || paConfig.attractLimit == '' || paConfig.attractLimit == 0 ? 'checked="checked"' : ''} /><label>不拦截</label>
									<input type="radio" name="attractLimit" value="1" ${paConfig.attractLimit == 1 ? 'checked="checked"' : ''} /><label>拦截</label>
								</td>
								<td class="w120">
									<div class="floatLeft">通道连接属性</div>
								</td>
								<td colspan="2">
									<select name="linkProperties" class="singleSelect s220_text">
										<option value="0" <c:if test="${paConfig.linkProperties == 0 || empty paConfig.linkProperties}">selected</c:if>>直连</option>
										<option value="1" <c:if test="${paConfig.linkProperties == 1}">selected</c:if>>三方</option>
									</select>
								</td>
							</tr>

                            <tr id="httpresponse" >
                                <th scope="row"><div class="floatLeft">备注</div></th>
                                <td width="42%"><textarea rows="3" cols="47" name="desc">${paConfig.desc}</textarea></td>
                                <td class="w120">单价</td>
                                <td><input type="number" class="s220_text" id="price" name="price" <c:if test="${audit_json.flag==1}">disabled</c:if>
                                 value="${paConfig.price}" placeholder="通道单价" style="width:300px"/></td>
                            </tr>
                           <%--  <tr id="channelsize" >
								<th scope="row"><div class="floatLeft">单个企业专用</div></th>
                                <td width="42%"><input type="text" class="s220_text" id="spid" name="spid" value="${paConfig.spid}" placeholder="企业编号"  style="width:300px"/></td>

								<td class="w120"><div class="floatLeft">最大拓展位</div></td>
                                <td><input type="text" class="s220_text" id="max_expand" name="max_expand" value="${paConfig.max_expand}" placeholder="最大拓展位" style="width:300px"/></td>
							</tr> --%>
							 <tr id="channelsize2" >
							 	<th scope="row" id="access_td1" style="display: none;"><div class="floatLeft">推送地址</div></th>
                                <td id="access_td2" style="display: none;" width="100%"colspan="3"> <input type="hidden" class="s220_text" id="access_key" name="access_key" value="${paConfig.access_key}" placeholder="http推送秘钥" readonly="readonly"  />
                                	<div id="access_url"></div>
                                	<!-- <input type="text" class="s220_text" id="access_url" name="access_url"  placeholder="http推送地址" readonly="readonly"  style="width:300px"/> -->
                                </td>
							</tr>
                            <div id ="expand_div">

                            </div>

					</table>
					</div>
				<div class="btn_box1">
					<div class="btn_m1">
						<button type="button" class="btn btn_green" onclick="doeditHttpConfig()">
							确认</button>
						<button type="button" class="btn" onclick="goback()">返回</button>
					</div>
				</div>
			</div>
			</div>
		</div>
	</form>

