#!/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 onAddProxy()
{
	window.location.href = \"/cgi-bin/Route/proxy_add\";
}
function onImportProxy()
{
	window.location.href = \"/cgi-bin/Route/proxy_import\";
}
function onExportProxy()
{
	window.location.href = \"/cgi-bin/Route/proxy_export\";
}
function onDeleteProxy(name)
{
	if (confirm(\"ϵͳɾʹô·ĹȷҪɾ·?\")) 
		window.location.href = \"${myself}?action=delete&proxyname=\" + name; 
}
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 onAddIflan()
{
        window.location.href = \"/cgi-bin/Route/iflan_add\";
}
function onDeleteIflan(name)
{
        if (confirm(\"ϵͳɾʹӿص·ɲԣȷҪɾ?\"))
                window.location.href = \"${myself}?action=delete&proxyname=\" + name;
}
function pppoeRedial(name)
{
	window.location.href = \"${myself}?action=redial&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 showIf(ifname)
{
        var url = \"/cgi-bin/Monitor/ifview?ifname=\" + ifname + \"&title=ӿ->\" + ifname;
        ShowWindow(url, \"\", 840, 800);
}
function onSelectItemSub(obj)
{
	var table = obj.parentElement.parentElement.parentElement.children;
	var count = 0;
	
	for(i = 1;i < table.length; i++) {
		if(table.item(i).cells[0].children[0].checked)
			count++;
	}
	if(obj.checked) {
		table.item(0).cells[0].children[0].checked = (count == table.length - 1);
	} else {
		table.item(0).cells[0].children[0].checked = false;
		if(count == 0){
			table.item(0).cells[9].children[0].style.color  = '#545454';
			return ;
		}
	}
	table.item(0).cells[9].children[0].style.color  = '#0000ff';
}
function onSelectItem(obj)
{
	var i;
	var table = obj.parentElement.parentElement.parentElement.children;
	if(table.length > 1) {
		for(i = 1;i < table.length; i++)
			table.item(i).cells[0].children[0].checked = obj.checked;
		if(obj.checked) {
			table.item(0).cells[9].children[0].style.color  = '#0000ff';
			return;
		}
	}
	table.item(0).cells[9].children[0].style.color  = '#545454';
}
function onDeleteSelectItem(obj)
{
	var table = document.getElementsByTagName(\"table\")[4].rows;
	var items = \"\";
	var i;
	
	for(i = 1;i < table.length; i++)
	{
		if(table[i].cells[0].childNodes[0].checked)
			items += table[i].cells[0].childNodes[0].value + \",\";
	}
	if(items!=\"\" && confirm(\"ϵͳɾѡ·ӿںص·ɲԣȷҪɾ?\"))
	{
		items=items.substr(0, items.length - 1);
		window.location.href = \"${myself}?action=batchdelete&items=\"+items;
	}
}
</script>
";
if [ "${CGI_action}" = "delete" ]; then
	operator_check "${myself}"
	errmsg=`${FLOWEYE} nat rmvproxy ${CGI_proxyname}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ʧ:${errmsg}"
	fi
elif [ "${CGI_action}" = "redial" ]; then
	operator_check "${myself}"
	errmsg=`${FLOWEYE} nat setpppoe name=${CGI_proxyname} redial=1`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ʧ:${errmsg}"
	fi
elif [ "${CGI_action}" = "batchdelete" ]; then
	operator_check "${myself}"
	errcount=0
	allcount=0
	CGI_items=`echo ${CGI_items} | tr ',' ' '`
	for id in ${CGI_items}
	do
		errmsg=`${FLOWEYE} nat rmvproxy ${id}`
		[ "$?" != "0" ] && errcount=$((${errcount}+1))
		allcount=$((${allcount}+1))
	done
	[ "${errcount}" != "0" ] && afm_dialog_msg "ܹ${allcount}ʧ${errcount}: ·ʹУ޷ɾ"
fi

echo -n "
<body>
";
cgi_show_title "Ӧ·->->ӿ·"

echo -n "
<br>
<table style=\"width:1080; font-bold:true;font-size:15px; color:#CE6C04;border-bottom:0px #787882 solid\">
<tr>
        <td style=\"font:14px\"><b>LANӿ</b></td>
        <td width=* style=\"font:14px\" align=right><a href=\"javascript:onAddProxy()\"></a></td>
</tr>
<table width=1080 border=0 cellspacing=1 cellpadding=1> 
<tr id=tblhdr>
	<td width=100 align=center>ӿ</td>
	<td width=130 align=right></td>
	<td width=100 align=right>ӿ״̬</td>
	<td width=160 align=right>IPַ</td>
	<td width=160 align=right></td>
	<td width=60  align=right>VLAN</td>
	<td width=180 align=right>(out/in)</td>
	<td width=* align=right><a style=\"color:#0000ff;font-size:14px\" href=\"javascript:onAddIflan()\">ӽӿ>>&nbsp;&nbsp;&nbsp;</a></td>
</tr>
";
idname="row1"
${FLOWEYE} nat listproxy type=routerif | while read type id name state ifname addr netmask vlan mtu \
inbytes outbytes inbps outbps theothers
do 
echo -n "
    <tr id=${idname}>
    <td align=right><a style=\"color:#0000ff\" href=\"javascript:showProxy('rtif', '${name}')\">${name}&nbsp;</a></td>
    <td align=right><a style=\"color:#0000ff\" href=\"javascript:showIf('${ifname}')\">${ifname}&nbsp;</a></td>
    "; if [ ${state} -eq 1 ]; then 
echo -n "
	<td align=right style=\"color:#00ff00\">&nbsp;</td>
    "; else 
echo -n "
	<td align=right style=\"color:#ff0000\">ͨ&nbsp;</td>
    "; fi 
echo -n "
    <td align=right>${addr}&nbsp;</td>
    <td align=right>${netmask}&nbsp;</td>
    "; [ "${vlan}" = "0" ] && vlan="" 
echo -n "
    <td align=right>${vlan}&nbsp;</td>
    <td align=right>${outbps}&nbsp;/&nbsp;${inbps}</td>
    <td align=right>
	<a style=\"color:#0000ff\" href=\"/cgi-bin/Route/iflan_edit?iflanname=${name}\">༭</a>&nbsp;&nbsp;&nbsp;
	<a style=\"color:#0000ff\" href=\"javascript:onDeleteIflan('${name}')\">ɾ</a>&nbsp;&nbsp;&nbsp;
    </td>
    </tr>
";
    if [ "${idname}" = "row1" ]; then
        idname="row2"
    else
        idname="row1"
    fi
done 

echo -n "
</table>
<br>
<table style=\"width:1080; font-bold:true;font-size:15px; color:#CE6C04;border-bottom:0px #787882 solid\">
<tr>
    <td style=\"font:14px\"><b>WAN·</b></td>
	<td width=60 align=right style=\"background: snow;\"></td>
</tr>
</table>
<table width=1080 border=0 cellspacing=1 cellpadding=1> 
<tr id=tblhdr>
	<td width=40 align=center>&nbsp;<input type=checkbox style=\"color:#0000ff\" value='${id}' onclick=\"onSelectItem(this)\"/></td>
	<td width=40 align=center></td>
	<td width=120 align=center>·</td>
	<td width=80 align=right></td>
	<td width=90 align=right>·״̬</td>
	<td width=140 align=right>IPַ</td>
	<td width=140 align=right>صַ</td>
	<td width=60  align=right>VLAN</td>
	<td width=160 align=right>(out/in)</td>
	<td width=* align=right>
		<a style=\"color:#545454;font-size:14px\" href=\"javascript:void(0)\" onclick=\"onDeleteSelectItem(this)\">ɾѡ&nbsp;&nbsp;</a>
		<a style=\"color:#0000ff;font-size:14px\" href=\"javascript:onAddProxy()\">·>>&nbsp;&nbsp;&nbsp;</a>
	</td>
</tr>
";
	idname="row1"
	natcount=0
	${FLOWEYE} nat listproxy type=proxy.pppoe | while read type id name ifname addr gateway dnsaddr vlan \
	gatewaymac flowcnt ttl inbytes outbytes inbps outbps dnsreqs dnsress linkup theothers
	do
		natcount=$((${natcount}+1))

echo -n "
    <tr id=${idname} proxyname=\"${name}\" proxytype=\"${type}\" linkup=\"${linkup}\" ifname=\"${ifname}\" vlan=\"${vlan}\">
	<td align=right><input type=checkbox style=\"color:#0000ff\" value='${id}' onclick=\"onSelectItemSub(this)\">&nbsp;&nbsp;</td>
	<td align=right><a style=\"color:#0000ff\" href=\"javascript:showProxy('${type}', '${name}')\">${natcount}&nbsp;</a></td>
    <td align=right><a style=\"color:#0000ff\" href=\"javascript:showProxy('${type}', '${name}')\">${name}&nbsp;</a></td>
    <td align=right><a style=\"color:#0000ff\" href=\"javascript:showIf('${ifname}')\">${ifname}&nbsp;</a></td>
";
		if [ ${linkup} -eq 1 ]; then

echo -n "
	<td align=right style=\"color:#00ff00\">&nbsp;</td>
";
		else

echo -n "
	<td align=right style=\"color:#ff0000\">ͨ&nbsp;</td>
";
		fi

echo -n "
    <td align=right>${addr}&nbsp;</td>
    <td align=right>${gateway}&nbsp;</td>
";
	[ "${vlan}" = "0" ] && vlan=""

echo -n "
    <td align=right>${vlan}&nbsp;</td>
    <td align=right>${outbps}&nbsp;/&nbsp;${inbps}</td>
    <td align=right>
";
		if [ "${type}" = "pppoe" ]; then 

echo -n "
		<a style=\"color:#0000ff\" href=\"javascript:pppoeRedial('${name}')\">ز</a>&nbsp;&nbsp;&nbsp;
";
		fi

echo -n "
		<a style=\"color:#0000ff\" href=\"javascript:editProxy('${type}', '${name}')\">༭</a>&nbsp;&nbsp;&nbsp;
		<a style=\"color:#0000ff\" href=\"javascript:onDeleteProxy('${name}')\">ɾ</a>&nbsp;&nbsp;&nbsp;
    </td>
</tr>
";
		if [ "${idname}" = "row1" ]; then
			idname="row2"
		else
			idname="row1"
		fi
	done

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