#!/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`"
if [ "${CGI_ipgrp}" = "" ]; then
	for grp in `${FLOWEYE} table list | awk '{print $1}'`
	do
		CGI_ipgrp="${grp}"
		break
	done
fi

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script languate=javascript>
function onAddGroup()
{
	window.location.href = \"/cgi-bin/Protocol/ipgrp_add\";
}
function onDeleteGroup(grpid)
{
	if (confirm(\"ɾIPȺ飬ôͬصвԶᱻɾȷҪɾ?\")) 
		window.location.href = \"${myself}?action=delete\" + \"&ipgrp=\" + grpid;
}
function onGroupChanged(ipgrp)
{
	window.location.href = \"${myself}\" + \"?ipgrp=\" + ipgrp.value;
}
function onIPChanged(ipobj)
{
	var ip = document.getElementsByName(\"ipobj\")[0];
	ip.value = ipobj.value;
	document.getElementsByName(\"addip\")[0].disabled = false;
}
function onAddIP(ipgrp)
{
	var ipobj = document.getElementsByName(\"ipobj\")[0];
	ipobj.value = TrimAll(ipobj.value);
	if (ipobj.value == \"\") {
		alert(\"IPַ,10.1.1.110.1.1.0/2410.1.1.1-10.1.1.127!\");
		ipobj.select();
		return false;
	}
	window.location.href = \"${myself}\" + \"?action=addip&ipgrp=\" + ipgrp +\"&ipobj=\" + ipobj.value;
}
function onRmvIP(ipgrp)
{
	var ipobj = document.getElementsByName(\"ipobj\")[0];
	ipobj.value = TrimAll(ipobj.value);
	if (ipobj.value == \"\") {
		alert(\"ѡҪɾIPַ!\");
		return false;
	}
	window.location.href = \"${myself}\" + \"?action=rmvip&ipgrp=\" + ipgrp +\"&ipobj=\" + ipobj.value;
}
</script>
";
if [ "${CGI_action}" = "delete" ]; then
	errmsg=`${FLOWEYE} table remove ${CGI_ipgrp}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
		afm_load_page 0 "${myself}"
		exit 0
	fi
fi
if [ "${CGI_action}" = "addip" ]; then
	errmsg=`${FLOWEYE} table addip ${CGI_ipgrp} ${CGI_ipobj}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
		afm_load_page 0 "${myself}?ipgrp=${CGI_ipgrp}"
		exit 0
	fi
fi
if [ "${CGI_action}" = "rmvip" ]; then
	errmsg=`${FLOWEYE} table rmvip ${CGI_ipgrp} ${CGI_ipobj}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
		afm_load_page 0 "${myself}?ipgrp=${CGI_ipgrp}"
		exit 0
	fi
fi
if [ "${CGI_action}" = "loadfile" ]; then
	errmsg=`${FLOWEYE} table loadfile ${CGI_ipgrp} ${CGI_ipfile}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
	fi
	rm -f ${CGI_ipfile}
fi

echo -n "
<body>
"; cgi_show_title "Զ->IPȺ" 
echo -n "
<br>
<table style=\"width:800; border-bottom:1px #787882 solid; font:bold  14px; color:#0000ff\">
<tr>
	<td style=\"font:14px\"><b>һЩ˵</b></td>
	<td style=\"font:12px;\" align=right width=\"*\"></td>
</tr>
</table>
<br>
<table width=800 border=0 cellspacing=1 cellpadding=1 bgcolor=\"#ffffff\">
<tr id=row1>
	<td width=60></td>
	<td width=* style=\"color:#0000ff;font:13px\">1. ͬһȺеIPַص(ͬȺص)</td>
</tr>
<tr id=row1>
	<td width=60></td>
	<td width=* style=\"color:#0000ff;font:13px\">2. ÿʽΪa.b.c.da.b.c.d/nna.b.c.d-e.f.g.h</td>
</tr>
<tr id=row1>
	<td width=60></td>
	<td width=* style=\"color:#0000ff;font:13px\">3. IPбļΪıļļеÿһиʽͬ</td>
</tr>
</table>
<br>
<table width=800 border=0 cellspacing=1 cellpadding=1>
<tr id=tblhdr height=22>
	<td width=120 align=center>IPȺ</td>
	<td width=400 align=center>
		<select style=\"width:100%;height:100%;border:0\" name=ipgrp value=\"${CGI_ipgrp}\" onchange=\"onGroupChanged(this)\">
		";
			${FLOWEYE} table list | while read grpid grpname theothers
			do
				if [ "${grpid}" = "${CGI_ipgrp}" -o "${grpname}" = "${CGI_ipgrp}" ]; then
					echo "<option value=\"${grpid}\" selected>${grpname}</option>"
				else
					echo "<option value=\"${grpid}\">${grpname}</option>"
				fi
			done
		
echo -n "</select>
	</td>
        <td width=*   align=center>
	"; if [ "${CGI_ipgrp}" != "" ]; then 
echo -n "
	    <input type=button style=\"width:90;height:22px\" value=\"ɾIPȺ\" onclick=\"onDeleteGroup('${CGI_ipgrp}')\"></input>
	"; else 
echo -n "
            <input type=button disabled style=\"width:90;height:22px\" value=\"ɾIPȺ\"></input>	
	"; fi 
echo -n "
	  &nbsp;&nbsp;<input type=button style=\"width:90;height:22px\" value=\"IPȺ>>\" onclick=\"onAddGroup()\"></input>
	</td>
</tr>
<tr id=row1 height=350>
	<td align=center valign=center>&nbsp;&nbsp;<b>IPб</b></td>
	<td><select style=\"width:100%;height:100%;border:0\" name=iplist size=50 onchange=\"onIPChanged(this)\"> ";
		if [ "${CGI_ipgrp}" != "" ]; then
			${FLOWEYE} table get ${CGI_ipgrp} | sort | while read ipobj
			do
				echo "<option value=\"${ipobj}\">${ipobj}</option>"
			done
		fi 
echo -n "</select>
	</td>
	<td></td>
</tr>
<tr id=row2>
	<td></td>
	<td><input type=text style=\"width:100%\" name=ipobj></input></td>
	<td width=* align=center>
"; if [ "${CGI_ipgrp}" != "" ]; then 
echo -n "
		<input type=button style=\"width:90;height:22px\" value=\"IP\" onclick=\"onAddIP('${CGI_ipgrp}')\"></input>
	&nbsp;&nbsp;<input type=button name=addip disabled style=\"width:90;height:22px\" value=\"ɾIP\" onclick=\"onRmvIP('${CGI_ipgrp}')\"></input>
"; else 
echo -n "
		<input type=button disabled style=\"width:90;height:22px\" value=\"IP\"></input>
	&nbsp;&nbsp;<input type=button name=addip disabled style=\"width:90;height:22px\" value=\"ɾIP\"></input>
"; fi 
echo -n "
	</td>
</tr>
</table>
<form method=post action=\"${myself}\" enctype=\"multipart/form-data\">
<table width=800 border=0 cellspacing=1 cellpadding=1 bgcolor=\"#ffffff\">
<tr height=22>
        <td width=320 style=\"font:14px bold\" align=left><b>ļIPбڴļ·:</b></td>
        <td width=400 class=cssTDArial align=left>
                <input type=file name=ipfile style=\"width:100%\">
        </td>
        <td align=right width=*>
                <input type=hidden name=action value=\"loadfile\"></input>
		<input type=hidden name=ipgrp value=\"${CGI_ipgrp}\"></input>
                <input type=submit style=\"width:90\" value=\"ύбļ\"></input>
        </td>
</tr>
</table>
</form>
		
</body>
</html>
";