#!/bin/sh
#This script is created by ssparser automatically. The parser first created by MaoShouyan
printf "Content-type: text/html;charset=gb2312
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 showProxy(type, proxyname)
{
        var url;
	url = \"/cgi-bin/Monitor/proxy_show?proxyname=\" + proxyname;
        ShowWindow(url, \"\", 350, 400);
}
function editProxy(type, name)
{
	window.location.href = \"/cgi-bin/Route/proxy_edit?proxyname=\" + name;
}
function onEditWG(id, name, type)
{
	window.location.href = \"/cgi-bin/Route/wg_edit?id=\" + id + 
		\"&name=\" + name + \"&type=\" + type;
}
function onAddWGwan(id, name)
{
	window.location.href = \"/cgi-bin/Route/wg_addwan?id=\" + id + \"&name=\" + name;
}
function onEditWGwan(id, name, proxy, weight)
{
	window.location.href = \"/cgi-bin/Route/wg_editwan?id=\" + id + 
		\"&name=\" + name + \"&proxy=\" + proxy + \"&weight=\" + weight;
}
function onRmvWGwan(id, proxy)
{
	if (confirm(\"ȷҪWANȺɾ·?\"))
		window.location.href = \"${myself}?action=rmvwgwan&id=\" + id + \"&proxy=\" + proxy;
}
function onBatchChgWGwan(obj)
{
	var i;
	var table = obj.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.rows;
	var data = GetSelectItemValue(table);
	
	if(data.length > 0) 
		window.location.href = \"/cgi-bin/Route/wg_modify?data=\" + data.join(',');
}
function onBatchRmvWGwan(obj)
{
	var i;
	var table = obj.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.rows;
	var data = GetSelectItemValue(table);
	
	if(data.length > 0) {
		if (confirm(\"ȷҪɾѡе·?\") == false)
			return ;
		window.location.href = \"${myself}?action=batrmvwgwan&data=\" + data.join(',');
	}
}
function isSelectItemAll(obj, index, cellnum)
{
	var i;
	for(i = index;i < obj.length; i++) {
		if(obj.item(i).cells[cellnum].children[0].checked == false)
			return false;
	}
	return true;
}
function GetSelectItemValue(obj)
{
	var i, j, table, id, wan, weight, 
		data = new Array();
	
	for(i = 1;i < obj.length; i++) {
		table = obj.item(i).cells[6].children[0].rows
		for(j = 0;j < table.length; j++) {
			if(table.item(j).cells[0].children[0].checked) {
				id = obj.item(i).cells[5].children[0].value;
				wan = table.item(j).cells[0].children[0].value;
				weight = table.item(j).cells[0].children[0].getAttribute('weight');
				data.push(id + '|' + wan + '|' + weight);
			}
		}
	}
	return data;
}
function onSelectItemChack(obj)
{
	var i;
	var tr, table = obj.parentElement.parentElement.parentElement.rows;
	
	tr = obj.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;
	tr.cells[5].children[0].checked = isSelectItemAll(table, 0, 0);
	
	table = tr.parentElement.parentElement.rows;
	table.item(0).cells[5].children[0].checked = isSelectItemAll(table, 1, 5);
}
function checkeItem(obj, ischecked)
{
	var i;
	
	for(i = 0;i < obj.length; i++)
		obj.item(i).cells[0].children[0].checked = ischecked;
}
function onSelectItemSub(obj)
{
	var i;
	var table = obj.parentElement.parentElement.lastElementChild.children.item(0).rows;
	
	checkeItem(table, obj.checked);
	table = obj.parentElement.parentElement.parentElement.rows;
	table.item(0).cells[5].children[0].checked = isSelectItemAll(table, 1, 5);
}
function onSelectItem(obj)
{
	var i;
	var table = obj.parentElement.parentElement.parentElement.rows;
	if(table.length > 1) {
		for(i = 1;i < table.length; i++) {
			table.item(i).cells[5].children[0].checked = obj.checked;
			checkeItem(table.item(i).cells[6].children[0].rows, obj.checked);
		}
	} 
}
function onWGTrend(id, name)
{
	var url = \"/cgi-bin/Monitor/wg_graph?time=day&id=\" + id + \"&name=\" + name;
        ShowWindow(url, \"\", 750, 600);
}
</script>
";
if [ "${CGI_action}" = "rmvwgwan" ]; then
	operator_check "${myself}"
	errmsg=`${FLOWEYE} nat setwg id=${CGI_id} proxy=${CGI_proxy} weight=-1`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ʧ:${errmsg}"
	fi
fi
if [ "${CGI_action}" = "batrmvwgwan" ]; then
	operator_check "${myself}"
	for wan in `echo ${CGI_data} | sed 's/,/ /g'`
	do
		echo ${wan} | sed 's/|/ /g' | \
		while read id wan weight othe
		do
			errmsg=`${FLOWEYE} nat setwg id=${id} proxy=${wan} weight=-1`
			if [ "$?" != "0" ]; then
				echo "ɾ${wan}ʧ:${errmsg}"
				errbox=yes
			fi
		done
	done
	if [ "${errbox}" = "yes" ]; then
		afm_dialog_msg "ʧܣԺԡ"
	fi
fi

echo -n "
<body>
"; cgi_show_title "Ӧ·->->·Ⱥ" 
echo -n "
<br>
<table width=1080 border=0 cellspacing=0 cellpadding=0> 
<tr id=tblhdr>
	<td width=60  align=center></td>
	<td width=120 align=right>Ⱥ</td>
	<td width=100 align=right></td>
	<td width=70  align=right>TTL</td>
	<td width=120 align=right>(out/in)</td>
	<td width=30  align=center><input type=checkbox style=\"color:#0000ff\" onclick=\"onSelectItem(this)\"/></td>
	<td width=*   align=center>
	<table width=\"100%\" border=0 cellspacing=0 cellpadding=0> 
	<tr height=19>
		<td width=30  align=center></td>
		<td width=100 align=left>·</td>
		<td width=80  align=right></td>
		<td width=80  align=right></td>
		<td width=140 align=right>(out/in)</td>
		<td width=*   align=right>
		<a style=\"color:#0000ff\" href=\"javascript:void(0);\" onclick=\"onBatchChgWGwan(this)\">޸Ⱥ</a>&nbsp;&nbsp;
		<a style=\"color:#0000ff\" href=\"javascript:void(0);\" onclick=\"onBatchRmvWGwan(this)\">ɾ&nbsp;</a></td>
	</tr>
	</table>
	</td>
</tr>
";
idname="row1"
${FLOWEYE} nat listwg | while read id name pxycnt type ttl inbps outbps members
do 
echo -n "
	<tr id=${idname} height=22>
	<td align=center>${id}&nbsp;
		<a style=\"color:#0000ff\" href=\"javascript:onAddWGwan('${id}', '${name}')\">[+]</a>&nbsp;
		<img src=\"/img/trend.png\" onclick=\"onWGTrend('${id}', '${name}')\"  title=\"\" />&nbsp;
	</td>
	"; if [ "${name}" = "undefined" ]; then 
echo -n "
	<td align=right><a style=\"color:#0000ff\" href=\"javascript:onEditWG('${id}', '${name}', '${type}')\">δ&nbsp;</a></td>
	"; else 
echo -n "
	<td align=right><a style=\"color:#0000ff\" href=\"javascript:onEditWG('${id}', '${name}', '${type}')\">${name}&nbsp;</a></td>
	"; fi 
echo -n "
	";	
	case "${type}" in
	"srcdst")
		echo "<td align=right>Դ+Ŀ&nbsp;</td>"
		;;
	"src")
		echo "<td align=right>Դַ&nbsp;</td>"
		;;
	"dst")
		echo "<td align=right>Ŀĵַ&nbsp;</td>"
		;;
	"spdp")
		echo "<td align=right>ַ+˿&nbsp;</td>"
		;;
	*)
		echo "<td align=center></td>"
		;;
	esac 
	
echo -n "
	<td align=right>${ttl}&nbsp;</td>
    	<td align=right>${outbps}&nbsp;/&nbsp;${inbps}</td>
	<td width=30 align=center><input type=checkbox style=\"color:#0000ff\" value='${id}' onclick=\"onSelectItemSub(this)\"/></td>
	<td align=left>
	<table width=\"100%\" border=0 cellspacing=0 cellpadding=0> 
	"; 
	newidname="row1"
	${FLOWEYE} nat showwg id=${id} | while read type pxytype wanid wanname status weight load inbps outbps theothers; do 
		[ "${type}" != "wan" ] && continue;
	
echo -n "
	<tr id=row5>
		<td width=30 align=center><input type=checkbox style=\"color:#0000ff\" value='${wanname}' weight='${weight}' onclick=\"onSelectItemChack(this)\"/></td>
	"; if [ ${status} -eq 1 ]; then 
echo -n "
    	<td width=100 align=left><a style=\"color:#0000ff\" href=\"javascript:showProxy('${pxytype}', '${wanname}')\">${wanname}&nbsp;</a></td>
	"; else 
echo -n "
    	<td width=100 align=left><a style=\"color:#ff0000\" href=\"javascript:showProxy('${pxytype}', '${wanname}')\">${wanname}&nbsp;</a></td>
	"; fi 
echo -n "
	<td width=80  align=right>${weight}</td>
	<td width=80  align=right>${load}</td>
	<td width=140 align=right>${outbps}&nbsp;/&nbsp;${inbps}&nbsp;</td>
	<td width=*  align=right>
		<img style=\"margin-top:2px\" src=\"/img/edit.png\" title=\"޸Ĳ\" onclick=\"onEditWGwan('${id}', '${name}', '${wanname}', '${weight}')\" />&nbsp;
		<img style=\"margin-top:2px\" src=\"/img/delete.png\" title=\"ȥ·\" onclick=\"onRmvWGwan('${id}', '${wanname}')\" />&nbsp;
	</tr>
	"; done 
echo -n "
	</table>
	</td>
	</tr>
"; if [ "${idname}" = "row1" ]; then
        idname="row2"
    else
        idname="row1"
    fi
done

echo -n "
</table>
</body>
</html>
";