#!/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/Protocol/`basename $0`"
MOD_TAB_LIST="#/cgi-bin/Protocol/sysapp_config?appid=${CGI_appid}&appname=${CGI_appname} ڵ#/cgi-bin/Protocol/sysapp_node?appid=${CGI_appid}&appname=${CGI_appname}"

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script languate=javascript>
function beforeAddNode(frm) 
{
	var ipaddr = document.getElementsByName(\"ipaddr\")[0];
	var port   = document.getElementsByName(\"port\")[0];
	ipaddr.value = TrimAll(ipaddr.value);
	if (!IsIPAddr(ipaddr.value)) {
		alert(\"ЧIPַ!\");
		ipaddr.select();
		return false;
	}
	port.value = TrimAll(port.value);
	if (!IsDigitIn(port.value, 1, 65535)) {
		alert(\"ЧĶ˿ں!\");
		port.select();
		return false;
	}
	return true;
}
function deleteNode(ipaddr, port)
{
	if (confirm(\"ȷҪɾýڵ?\")) {
		window.location.href = \"${myself}?action=remove\" + \"&ipaddr=\" + ipaddr + \"&port=\" + port +
			\"&appid=${CGI_appid}&appname=${CGI_appname}\";
	}
}
</script>
";
if [ "${CGI_action}" = "remove" ]; then
	errmsg=`${FLOWEYE} node remove ${CGI_ipaddr} ${CGI_port}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
	fi
fi
if [ "${CGI_action}" = "add" ]; then
	nodetype=""
	if [ "${CGI_tcp}" = "yes" -a "${CGI_udp}" = "yes" ]; then
		nodetype="both"
	elif [ "${CGI_tcp}" = "yes" ]; then
		nodetype="tcp"
	elif [ "${CGI_udp}" = "yes" ]; then
		nodetype="udp"
	fi
	errmsg=`${FLOWEYE} node add ${CGI_ipaddr} ${CGI_port} ${CGI_appid} ${nodetype}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
	fi
fi

echo -n "
<body>
"; cgi_show_title "ӦЭ->${CGI_appname}" 
echo -n "
<br>
"; cgi_print_mod_header "ڵ" 813 
echo -n "
<br>
<table width=800 border=0 cellspacing=1 cellpadding=1>
<tr id=tblhdr height=22>
        <td width=180 align=center>ڵIP</td>
        <td width=120 align=center>ڵ˿</td>
	<td width=120 align=center>TCP</td>
	<td width=120 align=center>UDP</td>
        <td width=* align=center></td>
</tr>
";
idname="row1"
${FLOWEYE} node listuser | while read appid ipaddr port tcp udp
do  
	if [ "${appid}" = "${CGI_appid}" ]; then 
		if [ "${tcp}" = "NONE" ]; then
			tcp="no"
		else
			tcp="yes"
		fi
		if [ "${udp}" = "NONE" ]; then
			udp="no"
		else
			udp="yes"
		fi
	
echo -n "
       		<tr id=${idname}>
       		<td align=center>${ipaddr}</td>
       		<td align=center>${port}</td>
		<td align=center>${tcp}</td>
		<td align=center>${udp}</td>
       		<td align=center><a style=\"color:#0000ff\" href=\"javascript:deleteNode('${ipaddr}', '${port}')\">ɾ</a></td>
		</tr>
	"; fi 
echo -n "
";
       	if [ "${idname}" = "row1" ]; then
               	idname="row2"
       	else
               	idname="row1"
       	fi
done

echo -n "
<form method=post onsubmit=\"return beforeAddNode(this)\" action=\"${myself}\">
<tr>
	<td><input type=text name=ipaddr style=\"width:100%\"></input></td>
	<td><input type=text name=port style=\"width:100%\"></td>
	<td><select name=tcp style=\"width:100%\"><option value=yes>yes</option><option value=no>no</option></select></td>
	<td><select name=udp style=\"width:100%\"><option value=yes>yes</option><option value=no>no</option></select></td>
	<td align=center>
		<input type=submit value=\"\" style=\"width:80px\"></input>
		<input type=hidden name=appname value=\"${CGI_appname}\"></input>
		<input type=hidden name=action value=\"add\"></input>
		<input type=hidden name=appid value=\"${CGI_appid}\"></input>
	</td>
</tr>
</form>
</table>
</body>
</html>
";