#!/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`"
returnurl="/cgi-bin/Route/wg_list"

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script language=\"javascript\">
function beforeAddWan(frm)
{
	var srcdsts = \"\";
	var ids = \"\";
	var pnames = \"\";
	var i;
	var table = document.getElementsByTagName(\"table\")[3].rows;
	
	for(i = 1;i < table.length; i++){
		if(table.item(i).cells[0].children[0].checked) {
			ids   += table.item(i).cells[0].children[0].value + \",\";
			srcdsts += table.item(i).cells[3].children[0].value + \",\";
			pnames += table[i].getAttribute(\"proxyname\") + \",\" + table.item(i).cells[3].children[0].value + \";\";
		}
	}
	if(ids != \"\") {
		document.getElementById(\"ids\").value = ids.substr(0, ids.length - 1);
		document.getElementById(\"srcdsts\").value = srcdsts.substr(0, srcdsts.length - 1);
		document.getElementById(\"pnames\").value = pnames.substr(0, pnames.length - 1);
		return true;
	}
	if(table.length > 1)
		alert(\"ԣδѡҪ롰${CGI_name}Ⱥ·!\");
	else
		alert(\"󣡵ǰ޿WAN··!\");
	return false;
}
function onCancel() 
{
	window.location.href = \"${returnurl}\";
}
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;
	}
	obj.parentElement.parentElement.children[3].children[0].disabled = !obj.checked;
}
function onSelectItem(obj)
{
	var i;
	var table = obj.parentElement.parentElement.parentElement.children;
	for(i = 1;i < table.length; i++){
		table.item(i).cells[0].children[0].checked = obj.checked;
		table.item(i).cells[3].children[0].disabled = !obj.checked;
	}
}
function onChangeSrcdst(obj)
{
	var i;
	var table = document.getElementsByTagName(\"table\")[3].rows;
	for(i = 1;i < table.length; i++){
		if(table.item(i).cells[0].children[0].checked)
			table.item(i).cells[3].children[0].children[obj.value-1].selected = true;
	}
}
function onLoadChange()
{
	var srcdsts = [${CGI_srcdsts}];
	var ids = [${CGI_ids}];
	var i, j;
	var table = document.getElementsByTagName(\"table\")[3].rows;
	
	if(ids.length == srcdsts.length) {
		for(i = 0;i < srcdsts.length; i++){
			for(j = 1; j < table.length; j++){
				if(table.item(j).cells[0].children[0].value == ids[i]) {
					table.item(j).cells[0].children[0].checked = true;
					table.item(j).cells[3].children[0].children[srcdsts[i]-1].selected = true;
					table.item(j).cells[3].children[0].disabled = false;
				}
			}
		}
	}
}
function showProxy(type, proxyname)
{
    var url;
	url = \"/cgi-bin/Monitor/proxy_show?proxyname=\" + proxyname;
    ShowWindow(url, \"\", 350, 400);
}
</script>
<body onload=\"onLoadChange()\">
";
if [ "${REQUEST_METHOD}" = "POST" -a "${CGI_pnames}" != "" ]; then
	operator_check "${myself}"
	wans=`echo "${CGI_pnames}" | tr ';' '\n' | awk -F "," \
		 'BEGIN{i=0;}{if(NF==2){print $1,$2; i++;}}END{exit i}'`
	if [ "$?" != "0" -a "${CGI_id}" != "" ]; then
		errcount=0
		echo "${wans}<br>"
		echo "<span style=\"color:#ff0000;\"><br>"
		while read name srcdst othe
		do
			errmsg=`${FLOWEYE} nat setwg id=${CGI_id} proxy=${name} weight=${srcdst}`
			if [ "$?" != "0" ]; then
				echo "	err: ${name} >>> ${errmsg}<br>"
				errcount=$((${errcount}+1))
			fi
		done << EOF
${wans}
EOF
		echo "</span><br>"
		if [ "${errcount}" != "0" ]; then
			afm_dialog_msg "з!"
		else
			echo "<script languate=\"javascript\">document.body.innerHTML=\"\";window.location.href = \"${returnurl}\";</script>"
		fi
	fi
fi
cgi_show_title "ӿ·->${CGI_name}->·" 

echo -n " 
<br>
<form method=post onsubmit=\"return beforeAddWan(this)\" action=\"${myself}\">
<table style=\"font:14px\" width=600 border=0 cellspacing=0 cellpadding=3 bgcolor=\"#ffffff\">
<tr id=row1>
	<td width=1 ></td>
    <td width=80 align=left>Ⱥ</td>
	<td width=400 align=left><b>${CGI_name}</b></td>
</tr>
</table>
<br>
<table style=\"font:15px\" width=600 border=0 cellspacing=0 cellpadding=3 bgcolor=\"#ffffff\">
<tr id=row1>
	<td width=1 ></td>
    <td align=left>ѡWAN·</td>
    <td align=left></td>
</tr>
</table>
<table width=700 border=0 cellspacing=1 cellpadding=1> 
<tr id=tblhdr>
	<td width=40 align=center><input type=checkbox style=\"color:#0000ff\" value='${id}' onclick=\"onSelectItem(this)\"/></td>
	<td width=40 align=center></td>
	<td width=140 align=center>·</td>
	<td width=100 align=right>Ȩ
		<select style=\"width:60\" value=\"1\" onchange=\"onChangeSrcdst(this)\">
			<option value=\"1\" selected=\"\">1</option>
			<option value=\"2\">2</option>
			<option value=\"3\">3</option>
			<option value=\"4\">4</option>
			<option value=\"5\">5</option>
			<option value=\"6\">6</option>
			<option value=\"7\">7</option>
			<option value=\"8\">8</option>
			<option value=\"9\">9</option>
			<option value=\"10\">10</option>
		</select>
	</td>
	<td width=80 align=right></td>
	<td width=120 align=right>IPַ</td>
	<td width=120 align=right>صַ</td>
	<td width=60  align=right>VLAN</td>
</tr>
";
	wanids=""
	if [ "${CGI_id}" != "" ]; then
		subproxy=`${FLOWEYE} nat showwg id=${CGI_id}`
		while read type pxytype wanid wanname status weight load inbps outbps theothers;
		do
			[ "${type}" != "wan" ] && continue;
			wanids="${wanids} ${wanid} |"
		done << EOF
${subproxy}
EOF
	fi
	wanids="${wanids%|*}"
	if [ "${wanids}" = "" ]; then
		proxyline=`${FLOWEYE} nat listproxy type=proxy.pppoe`
	else
		proxyline=`${FLOWEYE} nat listproxy type=proxy.pppoe | grep -Ev "(proxy|pppoe)(${wanids})"`
	fi
	idname="row1"
	natcount=0
	while read type id name ifname addr gateway dnsaddr vlan \
	gatewaymac flowcnt ttl inbytes outbytes inbps outbps dnsreqs dnsress linkup theothers
	do
		[ "${type}" != "proxy" -a "${type}" != "pppoe" ] && continue
		natcount=$((${natcount}+1))
		if [ ${linkup} -eq 1 ]; then
			color="#0000ff"
		else
			color="#ff0000"
		fi

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>${natcount}&nbsp;</td> 
    <td align=right><a style=\"color:${color}\" href=\"javascript:showProxy('${type}', '${name}')\">${name}&nbsp;</a></td>
	<td align=right style=\"color:#00ff00\">
		<select style=\"width:60\" value=\"1\" disabled>
			<option value=\"1\" selected=\"\">1</option>
			<option value=\"2\">2</option>
			<option value=\"3\">3</option>
			<option value=\"4\">4</option>
			<option value=\"5\">5</option>
			<option value=\"6\">6</option>
			<option value=\"7\">7</option>
			<option value=\"8\">8</option>
			<option value=\"9\">9</option>
			<option value=\"10\">10</option>
		</select>
    </td>
    <td align=right>${ifname}&nbsp;</td>
    <td align=right>${addr}&nbsp;</td>
    <td align=right>${gateway}&nbsp;</td>
";
	[ "${vlan}" = "0" ] && vlan=""

echo -n "
    <td align=right>${vlan}&nbsp;</td>
</tr>
";
		if [ "${idname}" = "row1" ]; then
			idname="row2"
		else
			idname="row1"
		fi
	done << EOF
${proxyline}
EOF

echo -n "
</table>
<table style=\"width:700; border-bottom:1px #787882 solid; color:#0000ff\">
<tr><td align=right>&nbsp;</td></tr>
</table>
<table style=\"width:700\"> 
<tr>
        <td align=right>
		<input type=hidden name=id value=\"${CGI_id}\" />
		<input type=hidden name=pnames id=pnames value=\"${CGI_pname}\" />
		<input type=hidden name=srcdsts id=srcdsts value=\"\" />
		<input type=hidden name=ids id=ids value=\"\" />
		<input type=hidden name=name value=\"${CGI_name}\" />
		<input type=submit id=newbtn value=\"\" />
		<input type=button id=newbtn value=\"ȡ\" onclick=\"onCancel()\" />
	</td>
</tr>
</table>
</form>
</body>
</html>";