#!/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`"
returnurl="/cgi-bin/Route/plan_list"
IPE_PPPOEPLAN=${PGPATH}/bin/ipe_pppoeplan

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script language=\"javascript\">
function onCancel()
{
	window.location.href = \"${returnurl}\";
}
function showProxy(type, proxyname)
{
        var url;
		url = \"/cgi-bin/Monitor/proxy_show?proxyname=\" + proxyname;
        ShowWindow(url, \"\", 350, 400);
}
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; 
	} 
}
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; 
	} 
}
function onSubmintSelectItem(obj)
{
	var table = document.getElementsByTagName(\"table\")[2].rows;
	var items = new Array();
	var i;
	
	for(i = 1; i < table.length; i++)
	{
		if(table[i].cells[0].childNodes[0].checked)
			items.push(table[i].getAttribute(\"proxyname\"));
	}
	if(items.length > 0) {
		if(confirm(\"ȷҪΪ⡰\"+items.length+\"·Ӽƻ?\"))
			window.location.href = \"/cgi-bin/Route/plan_edit?act=add&data=\"+items.join(' ');
	}
	else
		alert(\"δѡκ·\");
}
function ajxs(settings)
{
	var http;
	if (typeof XMLHttpRequest != 'undefined') 
		http = new XMLHttpRequest();
	else {
		try {
			http = new ActiveXObject(\"Msxml2.XMLHTTP\");    
		}catch (e) {
			try {
				http = new ActiveXObject(\"Microsoft.XMLHTTP\");
			} catch (e) {return ;}
		}
	}
	http.open(settings.type, settings.url, true);
	http.setRequestHeader(\"Accept\", \"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\");
	http.setRequestHeader(\"Cache-Control\", \"no-cache\");
	http.setRequestHeader(\"Accept-Language\", \"zh-CN,zh;q=0.8\");
	http.setRequestHeader(\"Content-Type\", \"text/html; charset=GB2312\");
	http.onreadystatechange = function() { 
		if (http.readyState == 4) {
			if (http.status == 200)
				settings.success(http.responseText);
			else
				settings.error(http.status);
		}
	}
	http.send(null);
}
function getstatus()
{
	var table = document.getElementsByTagName(\"table\")[2].rows;
	var items = new Array();
	var i;
	
	for(i = 1; i < table.length; i++)
		items.push(table[i].getAttribute(\"proxyname\"));
	if(items.length <= 0)
		return;
	ajxs({type: 'POST', 
				 url: '/cgi-bin/common/plan_action?action=status&items=' + items.join(' '), 
				 error: function(status){setTimeout(getstatus, 3000);},
				 success: function(data){ 
		// var d = JSON.parse(data);
		var d;
		try {
			d = eval('('+data+')');
		}catch(e) {
			setTimeout(getstatus, 1000);
			return;
		}
		var i;
		var table = document.getElementsByTagName(\"table\")[2].rows;
		if(table.length == d.data.length) {
			for(i=1; i < d.data.length; i++) {
				table[i].cells[4].childNodes[0].innerHTML = d.data[i].username + ':' + d.data[i].password;
				if(d.data[i].linkup == 1) {
					table[i].cells[5].childNodes[0].innerHTML = \"\";
					table[i].cells[5].childNodes[0].style.color = \"#00ff00\";
				} else {
					table[i].cells[5].childNodes[0].innerHTML = \"ͨ\";
					table[i].cells[5].childNodes[0].style.color = \"#ff0000\";
				}
				table[i].cells[6].childNodes[0].innerHTML = d.data[i].addr;
				table[i].cells[7].childNodes[0].innerHTML = d.data[i].gwmac;
			}
		}
		else
			setTimeout(getstatus, 1000);
	}});
}
function onloaddoc()
{
	getstatus();
}
</script>
<body onload=\"onloaddoc();\">
";
cgi_show_title "PPPOEƻ->Ӽƻ->ѡ·"

echo -n "
<br>
<table style=\"width:960; font-bold:true;font-size:15px; color:#CE6C04;border-bottom:0px #787882 solid\">
<tr>
    <td style=\"font:14px\"><b>PPPOE-WAN·</b></td>
    <td width=*></td>
</tr>
</table>
<table width=920 border=0 cellspacing=1 cellpadding=1> 
<tr id=tblhdr>
	<td width=30  align=center><input type=checkbox style=\"color:#0000ff\" value='${id}' onclick=\"onSelectItem(this)\"/></td>
	<td width=40  align=center></td>
	<td width=120 align=right>·</td>
	<td width=80 align=right></td>
	<td width=200 align=right>ʺ</td>
	<td width=100 align=right>·״̬</td>
	<td width=120 align=right>IPַ</td>
	<td width=120 align=right>صַ</td>
</tr>
";
idname="row1"
natcount=0
plan_list=`${IPE_PPPOEPLAN} -l`
${FLOWEYE} nat listproxy type=pppoe | while read type id name ifname addr gateway dnsaddr vlan \
gatewaymac flowcnt ttl inbytes outbytes inbps outbps dnsreqs dnsress linkup theothers
do
	proxyname=`echo "${plan_list}" | grep -E "^${name} "`
	[ "${proxyname}" != "" ] && continue
	
	natcount=$((${natcount}+1))

echo -n "
    <tr id=${idname} proxyname=\"${name}\" proxytype=\"${type}\" linkup=\"${linkup}\" ifname=\"${ifname}\" vlan=\"${vlan}\">
		<td align=center><input type=checkbox style=\"color:#0000ff\" value='${name}' onclick=\"onSelectItemSub(this)\"></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>&nbsp;</td>
		<td align=right><a style=\"color:#0000ff\" href=\"javascript:showIf('${ifname}')\">${ifname}</a>&nbsp;</td>
		<td align=right><span></span>&nbsp;</td>
"; if [ ${linkup} -eq 1 ]; then 
echo -n "
		<td align=right><span style=\"color:#00ff00\"></span>&nbsp;</td>
"; else 
echo -n "
		<td align=right><span style=\"color:#ff0000\">ͨ</span>&nbsp;</td>
"; fi 
echo -n "
		<td align=right><span>${addr}</span>&nbsp;</td>
		<td align=right><span>${gateway}</span>&nbsp;</td>
	</tr>
"; if [ "${idname}" = "row1" ]; then
		idname="row2"
	else
		idname="row1"
	fi
done

echo -n "
</table>
<table style=\"width:920; border-bottom:1px #787882 solid; color:#0000ff\">
<tr><td align=right>&nbsp;</td></tr>
</table>
<table style=\"width:920\">
<tr>
	<td align=right>
		<input type=\"button\" style=\"width:80\" value=\"ύ\" onclick=\"onSubmintSelectItem(this)\"></input>
		<input type=\"button\" style=\"width:80\" value=\"\" onclick=\"onCancel()\"></input>
	</td>
</tr>
</table>
</body>
</html>
";