#!/bin/sh
#This script is created by ssparser automatically. The parser first created by MaoShouyan
printf "Content-type: text/html
Cache-Control: no-cache

"
echo -n "";
. ../common/common.sh 
myself="/cgi-bin/Route/`basename $0`"

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script language=\"javascript\">
function beforeAddProxy(frm)
{
	var wan = document.getElementsByName(\"wan\")[0];
	var port  = document.getElementsByName(\"port\")[0];
	var mapip = document.getElementsByName(\"mapip\")[0];
	var mapport = document.getElementsByName(\"mapport\")[0];
	if (wan.value == \"\") {
		alert(\"ѡWAN·!\");
		wan.select();
		return false;
	}
	port.value = TrimAll(port.value);
	mapport.value = TrimAll(mapport.value);
	if (!IsDigitIn(mapport.value, 0, 65535)) {
		alert(\"Ƿ˿!\");
		mapport.select();
		return false;
	}
	mapip.value = TrimAll(mapip.value);
	if (!IsIPAddr(mapip.value)) {
		alert(\"ǷIP!\");
		mapip.select();
		return false;
	}
	return true;
}
function onCancel() 
{
	window.location.href = \"/cgi-bin/Route/portmap_list\";
}
</script>
";
if [ "${REQUEST_METHOD}" = "POST" ]; then
	operator_check "${myself}"
	errmsg=`${FLOWEYE} nat setportmap id=${CGI_id} wan=${CGI_wan} port=${CGI_port} \
proto=${CGI_proto} mapip=${CGI_mapip} mapport=${CGI_mapport}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ʧ:${errmsg}"
	else
		afm_load_page 0 "/cgi-bin/Route/portmap_list"
		exit 0
	fi
else
	for nameval in `${FLOWEYE} nat getportmap id=${CGI_id}`
	do
		eval "CGI_${nameval}"
	done
fi

echo -n "
<body>
"; cgi_show_title "->˿ӳ->༭" 
echo -n "
<br>
<form method=post onsubmit=\"return beforeAddProxy(this)\" action=\"${myself}\">
<table style=\"font:14px\" width=600 border=0 cellspacing=0 cellpadding=3 bgcolor=\"#ffffff\">
<tr id=row1>
	<td></td>
        <td align=left>WAN·</td>
	<td align=left>
		<select name=wan style=\"width:120\" value=\"${CGI_wan_name}\">
		";
		${FLOWEYE} nat listproxy | while read type id name theothers
		do
			[ "${type}" = "rtif" ] && continue
			if [ "${name}" = "${CGI_wan_name}" ]; then
				echo "<option value=\"${name}\" selected>${name}</option>"
			else
				echo "<option value=\"${name}\">${name}</option>"
			fi
		done
		${FLOWEYE} nat listwg | while read id name theothers
		do
			if [ "_wg.${name}" = "${CGI_wan_name}" ]; then
				echo "<option value=\"_wg.${name}\" selected>${name}</option>"
			else
				echo "<option value=\"_wg.${name}\">${name}</option>"
			fi
		done	
		
echo -n "
		</select>
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>WAN˿</td>
        <td align=left><input type=text name=port style=\"width:120\" value=\"${CGI_port}\" />&nbsp;(ʽΪNNNN-MM)</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>Э</td>
        <td align=left>
		<select name=proto style=\"width:120\" value=\"${CGI_proto}\">
		"; if [ "${CGI_proto}" = "tcp" -o "${CGI_proto}" = "TCP" ]; then
			echo "<option value=\"tcp\" selected>TCP</option>"
			echo "<option value=\"udp\">UDP</option>"
		    else
			echo "<option value=\"tcp\">TCP</option>"
			echo "<option value=\"udp\" selected>UDP</option>"
		    fi 
echo -n "
		</select>
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>ӳIP</td>
        <td align=left>
		<input type=text name=mapip style=\"width:120px\" value=\"${CGI_mapip}\" />&nbsp;(xxx.xxx.xxx.xxx)
	</td>
</tr>
<tr id=row1>
	<td></td>
        <td align=left>ӳ˿</td>
        <td align=left><input type=text name=mapport style=\"width:120px\" value=\"${CGI_mapport}\" />&nbsp;(0ʾʹԭ˿)</td>
</tr>
</table>
<table style=\"width:600; border-bottom:1px #787882 solid; color:#0000ff\">
<tr><td align=right>&nbsp;</td></tr>
</table>
<table style=\"width:600\"> 
<tr>
        <td align=right>
		<input type=hidden name=id value=\"${CGI_id}\" />
		<input type=submit id=newbtn style=\"width:80\" value=\"ύ\" />
		<input type=button id=newbtn style=\"width:80\" value=\"ȡ\" onclick=\"onCancel()\" />
	</td>
</tr>
</table>
</form>
</body>
</html>
";