#!/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`"
[ "${CGI_bridge}" = "" ] && CGI_bridge=0
cgiargs="refresh=${CGI_refresh}&appid=${CGI_appid}&appname=${CGI_appname}&bridge=${CGI_bridge}"
cgiurl="${cgipath}?${cgiargs}"
[ "${width}" = "" ] && width=700
MOD_TAB_LIST="Э#/cgi-bin/Protocol/sysapp_config?appid=${CGI_appid}&appname=${CGI_appname} \
ͼ#/cgi-bin/Monitor/appview_graph?${cgiargs} \
û#/cgi-bin/Monitor/appview_topip?${cgiargs} \
йز#/cgi-bin/Monitor/appview_policy?${cgiargs}"

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script languate=javascript>
function onSelectApp(obj)
{
	window.location.href = \"/cgi-bin/Protocol/sysapp_list?group=${CGI_group}&groupname=${CGI_groupname}&appid=\" + obj.value; 
}
function onShowApp(appid, appname) 
{
	var url;
	url = \"/cgi-bin/Monitor/appview_topip?appid=\" + appid + \"&appname=\" + appname;
	ShowWindow(url, \"scrollbars=1,resizable=0\", 650, 800);
}
function beforeEditApp(frm) 
{
        var nodewttl = document.getElementsByName(\"nodettl\")[0];
        nodettl.value = TrimAll(nodettl.value);
        if (!IsDigitIn(flowttl.value, 30, 65535)) {
                alert(\"ڵڱ30~65535!\");
                nodettl.select();
                return false;
        }
        return true; 
}
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
	operator_check "${myself}?appid=${CGI_appid}&appname=${CGI_appname}"
	errmsg=`${FLOWEYE} node remove ${CGI_ipaddr} ${CGI_port}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	fi
elif [ "${CGI_action}" = "add" ]; then
	operator_check "${myself}?appid=${CGI_appid}&appname=${CGI_appname}"
	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}"
	fi
elif [ "${CGI_action}" = "config" ]; then
	operator_check "${myself}?appid=${CGI_appid}&appname=${CGI_appname}"
	errmsg=`${FLOWEYE} app set ${CGI_appid} nodettl=${CGI_nodettl} cachesn=${CGI_cachesn}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "òʧ:${errmsg}"
	fi
else
	if [ "${CGI_appid}" = "" ]; then
		for appid in `${FLOWEYE} app list2 | grep "^${CGI_group}" | awk '{print $2}'`
		do
			CGI_appid="${appid}"
			break
		done
	fi
fi

echo -n "
<body>
"; cgi_show_title "ӦЭ->${CGI_appname}" 
echo -n "
<br>
"; cgi_show_app_info ${CGI_appid} ${width} 
echo -n "
<br>
"; cgi_print_mod_header "Э" ${width} 
echo -n "
<br>
<form method=post onsubmit=\"return beforeEditApp(this)\" action=\"${myself}\">
"; 
output=`${FLOWEYE} app get ${CGI_appid}`
if [ "${output}" = "" ]; then
	afm_dialog_msg "Э${CGI_appid}!"
	echo "</body></html>"
	exit 0
fi
for nameval in ${output}; do
	eval "${nameval}"
done
CGI_flowttl="${flowttl}"
CGI_nodettl="${nodettl}"
CGI_cachesn="${cachesn}"

echo -n "
<table width=${width} border=0 cellspacing=1 cellpadding=2> 
<tr id=row1>
        <td width=20></td>
	<td width=100>ڵѡ</td>
	<td width=300><select name=cachesn value=\"${CGI_cachesn}\" style=\"width:100;height=21\">
	"; if [ "${CGI_cachesn}" = "1" ]; then 
echo -n "
		<option value=1 selected></option>
                <option value=0></option>
        "; else 
echo -n "
                <option value=1></option>
                <option value=0 selected></option>
        "; fi 
echo -n "
	</select></td>
	<td width=*></td>
</tr>
<tr id=row1>
        <td width=20></td>
        <td width=100>ڵ</td>
        <td width=300>
                <input type=text name=nodettl value=\"${CGI_nodettl}\" style=\"width:100;height:21\"></input>
                &nbsp;(,ΧΪ30~65535)
        </td>
	<td width=* align=right>
		<input type=submit style=\"width:80\" value=\"޸\" />
		<input type=hidden name=appid value=\"${CGI_appid}\" />
		<input type=hidden name=appname value=\"${CGI_appname}\" />
		<input type=hidden name=action value=\"config\" />
	</td>
</tr>
</table>
</form>
<br>
<table style=\"width:${width}; font-bold:true;font-size:15px; color:#CE6C04;border-bottom:0px #787882 solid\">
<tr>
    <td style=\"font:14px\"><b>Эڵ</b></td>
    <td width=*></td>
</tr>
</table>
<table width=${width} border=0 cellspacing=1 cellpadding=1>
<tr id=tblhdr height=22>
        <td width=160 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=right>
		<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>
";