#!/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`"
if [ "${PALANG}" = "en" ]; then
	LANG_001="Route->Policy->Route"
	LANG_002="ID"
	LANG_003="Current"
	LANG_004="All"
	LANG_005="Source Interface"
	LANG_006="UserGroup"
	LANG_008="Src(IP/Port)"
	LANG_009="Dst(IP/Port)"
	LANG_0010="Protocol"
	LANG_0011="APP"
	LANG_0012="DSCP"
	LANG_0013="User"
	LANG_0014="Action"
	LANG_0015="Dst WAN/LAN"
	LANG_0016="Next-Hop"
	LANG_0017="Match"
	LANG_0018="Note"
	LANG_0019="Add"
	LANG_0020="Any"
	LANG_0021="ippxy"
	LANG_0022="nonippxy"
	LANG_0023="route"
	LANG_0024="NULL"
	LANG_0025="DNAT IP"
	LANG_0026="Policy"
	LANG_0027="Policy|Scheduler"
	LANG_0028="Modify"
	LANG_0029="Delete"
	LANG_0030="Enable"
	LANG_0031="Disable"
	LANG_0032="Confirm to delete policy:"
	LANG_0033="PPPoE Servers"
	LANG_0034="Error"
	LANG_0035="Delete routing policy"
	LANG_0036="Confirm to delete:"
	LANG_0037="Yes"
	LANG_0038="No"
fi
MOD_TAB_LIST="${LANG_0026:=ת}#/cgi-bin/App/pktrule/webmain"
[ "${CGI_type}" = "" ] && CGI_type="active"

echo -n "
<style type=\"text/css\">
#row1:hover,#row2:hover, #row3:hover, #row4:hover {background:#f2f2f2}
#rightBtn{position:absolute; top:9px; right:10px; display:flex; justify-content:flex-end; color:#666666}
</style>
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<body onload=\"onload()\">
"; cgi_print_mod_header "${LANG_0026:=ת}" "100%" 
echo -n "
<div id=\"rightBtn\">
	<div>
		<span class=\"likea\" onclick=\"onAddRule()\">
			<img src=\"/img/a_add.png\" class=\"rightIcon\">&nbsp;${LANG_0019:=Ӳ}&nbsp;
		</span>
	</div>
</div>
<table width=\"100%\" border=0 cellspacing=1 cellpadding=1 class=\"table-striped\"> 
	<tr id=tblhdr class=\"tbot-border\">
		<td width=40 align=left style=\"font-size:11px\">&nbsp;${LANG_002:=}</td>
		<!--
		<td width=50 align=right> 
			<select id=\"rt_type\" style=\"width:55px;border:none;font-weight:bold;\" onchange=\"load_rtlist()\">
				<option value=active>${LANG_003:=ǰ}</option>
				<option value=all>${LANG_004:=}</option>
			</select>
		</td>
		-->
		<td width=80 align=right>${LANG_006:=û}</td>
		<td width=100 align=right>${LANG_005:=Դӿ}</td>
		<td width=50  align=right>VLAN</td>
		<td width=50  align=right>TTL</td>
		<td width=110 align=right>${LANG_008:=Դַ/˿}</td>
		<td width=110 align=right>${LANG_009:=Ŀַ/˿}</td>
		<td width=40  align=right>${LANG_0010:=Э}</td>
		<td width=80  align=right>${LANG_0011:=Ӧ}</td>
		<td width=50  align=right>${LANG_0012:=DSCP}</td>
		<td width=50  align=right>${LANG_0014:=}</td>
		<td width=110 align=right>${LANG_0015:=Ŀ·}</td>
		<td width=110 align=right>${LANG_0016:=һ}</td>
		<td width=70  align=right>${LANG_0017:=ƥ}</td>
		<td width=*   align=right style=\"font-size:11px\">${LANG_0018:=ע}</td>
		<td width=70  align=right></td>
	</tr> 
</table>
<div style=\"height:90%; overflow-y:auto\">
	<table id=tab_rtlist width=\"100%\" border=0 cellspacing=1 cellpadding=1 class=\"table-striped\" ></table>
</div>
<script language=\"javascript\">
function showIPGrp(gname)
{
	var url = \"/cgi-bin/Protocol/ipgrp_list?ipgrp=\" + gname;
	ShowWindow(url, \"\", 860, 630); 
}
function showApp(appid, appname)
{
	var url = \"/cgi-bin/Monitor/app_show?appid=\" + appid + \"&title=\" + appname;
	ShowWindow(url, \"\", 700, 540); 
}
function onAddRule() 
{
	window.location.href = \"/cgi-bin/App/pktrule/pktrule_add\";
}
function modifyRule(id) 
{
	window.location.href = \"/cgi-bin/App/pktrule/pktrule_set?id=\" + id;
}
function deleteRule(id)
{
	layer.confirm(\"${LANG_0032:=ȷҪɾ}<b style='color:red'>\" + id + \"</b>\", 
	{icon:3, title: \"Confirm\", btn: ['${LANG_0037:=ȷ}', '${LANG_0038:=ȡ}']},
	function(index){
		layer.close(index);
		var tbl = document.getElementById(\"tab_rtlist\");
		var trl = document.getElementsByName(\"row_\" + id)[0];
		AJXS({
			type: 'POST', 
			url: '/cgi-bin/App/pktrule/ajax_pkt_route?action=route_remove&id=' + id,
			error: function(status){},
			success: function(data) {
				var json = eval('('+data+')');
				if ( json.code == 1 )
					alert_err(json.msg);
				else
					tbl.deleteRow(trl.rowIndex);
			}
		});
	}); 
}
function enablePolicy(id)
{
	AJXS({
        type: 'POST', 
        url: '/cgi-bin/App/pktrule/ajax_pkt_route?action=route_enable&id=' + id,
        error: function(status){},
        success: function(data) {
			var json = eval('('+data+')');
			
			if(json.code != 0)
				alert_err(json.msg);
			else 
				getpolicystats();
		}
	});
}
function disablePolicy(id)
{
	AJXS({
        type: 'POST', 
        url: '/cgi-bin/App/pktrule/ajax_pkt_route?action=route_disable&id=' + id,
        error: function(status){},
        success: function(data) {
			var json = eval('('+data+')');
			
			if(json.code != 0)
				alert_err(json.msg);
			else 
				getpolicystats();
		}
	});
}
function load_rtlist()
{    
    var tid = 'row1';
	var html = \"\";
	//var type = document.getElementById(\"rt_type\").value;
	var  type = \"all\";
	
    layer.load(1);
	AJXS({
        type: 'POST', 
        url: '/cgi-bin/App/pktrule/ajax_pkt_route?action=route_list&type='+type,
        error: function(status){},
        success: function(data) {
            layer.closeAll(\"loading\");
            var json = eval('('+data+')');
			
            var wantbl = document.getElementById(\"tab_rtlist\");
            while (wantbl.rows.length > 0)
                wantbl.deleteRow(-1);
            for (var i = 0; i < json.length; i++) {
                var r = json[i];
                //tr
                var trow = wantbl.insertRow(-1);
                trow.setAttribute(\"name\", \"row_\"+r.polno);
                if ( r.disabled != '0' ) {
                    trow.id = 'row4';
                    var stateHtml = '<img src=\"/img/enable.png\" style=\"margin-top: 3px\" onclick=\"enablePolicy('+r.polno+')\" title=\"${LANG_0030:=ò}\" />&nbsp;';
					stateHtml = '<span id=state_'+r.polno+'>'+stateHtml+'</span>';
				} else {
					var stateHtml = '<img src=\"/img/disable.png\" style=\"margin-top: 3px\" onclick=\"disablePolicy('+r.polno+')\" title=\"${LANG_0031:=ò}\" />&nbsp;';
					stateHtml = '<span id=state_'+r.polno+'>'+stateHtml+'</span>';
					if ( r.state == 0 ) {
						var color = \"#ff0000\";
						trow.id = 'row3';
					} else {
						var color = \"#0000fff\";
						trow.id = tid;
						tid = tid == \"row1\" ? \"row2\" : \"row1\";
					}
                }
                var pos = 0;
                var cell = trow.insertCell(pos++);  //
                cell.setAttribute(\"width\", \"40\");
                cell.innerHTML = r.polno;
				/*
                cell = trow.insertCell(pos++); //
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"50\");
				if(r.rtptimename == \"NULL\") r.rtptimename = \"${LANG_0020:=ʱ}\";
				if(r.active == \"1\")
					cell.innerHTML = r.rtptimename;
				else
					cell.innerHTML = '<span style=\"color:#c0c0c0\">'+r.rtptimename+'</span>';
				*/
				
				cell = trow.insertCell(pos++); //û
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"80\"); 
                cell.innerHTML = r.pname;
				
                cell = trow.insertCell(pos++); //Դӿ
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"100\");
                cell.innerHTML = '<div id=inif_'+r.polno+'></div>';
				
                cell = trow.insertCell(pos++); //VLAN
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"50\");
                cell.innerHTML = (r.vlan == \"0-0\") ? \"\" : r.vlan;
                cell = trow.insertCell(pos++); //TTL
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"50\");
                cell.innerHTML = (r.ttl == \"0-255\") ? \"\" : r.ttl;
				
				r.srcport = r.srcport == \"any\" ? \"\" : \":\" + r.srcport;
				if(r.srctype == \"table\")
					html = '<span class=\"likea\" onclick=showIPGrp(\"'+r.srcip+'\")>' + r.srcip + r.srcport + '</span>';
				else
					html = r.srcip + r.srcport;
				
                cell = trow.insertCell(pos++); //Դַ
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"110\");
                cell.innerHTML = html;
				r.dstport = r.dstport == \"any\" ? \"\" : \":\" + r.dstport;
				if(r.dsttype == \"table\")
					html = '<span class=\"likea\" onclick=showIPGrp(\"'+r.dstip+'\")>' + r.dstip + r.dstport + '</span>';
				else
					html = r.dstip + r.dstport;
                cell = trow.insertCell(pos++); //Ŀַ
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"110\");
                cell.innerHTML = html;
                cell = trow.insertCell(pos++); //Э
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"40\");
                cell.innerHTML = r.proto;
				
                cell = trow.insertCell(pos++); //Ӧ
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"80\");
				if(r.appid == \"any\")
					html = \"any\";
				else
					html = '<span class=\"likea\" onclick=showApp(\"'+r.appid+'\",\"'+r.appname+'\")>'+r.appname+'</span>';
                cell.innerHTML = html;   
				
                cell = trow.insertCell(pos++); //DSCP
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"50\");
                cell.innerHTML = (r.dscp == \"0\") ? \"\" : r.dscp;
				
				if(r.usrtype == \"ippxy\")
					html = \"${LANG_0021:=}\";
				else if(r.usrtype == \"nonippxy\")
					html = \"${LANG_0022:=Ǵ}\";
				else
					html = \"any\";
				switch(r.action) {
					case \"nat\":
						html = \"NAT\";
						break;
					case \"cgnat\":
						html = \"CGNAT\";
						break;
					case \"route\":
						html = \"${LANG_0023:=·}\";
						break;
					case \"\":
						html = \"<b>${LANG_0021:=ߴ}</b>\";
						r.nhtype = \"__ippxy__\";
						break;
				}
				
                cell = trow.insertCell(pos++); //
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"50\");
                cell.innerHTML = html;
				switch(r.nhtype) {
					case \"__ippxy__\":
						html = '<span id=nexthop_'+r.polno+'>${LANG_0021:=·}</span>';
						break;
					case \"NULL\":
						html = '<span id=nexthop_'+r.polno+'><b style=\"color:red\">${LANG_0024:=·}</b></span>';
						break;
					case \"wg\":
						html = '<span class=\"likea\" id=nexthop_'+r.polno+' style=\"color:'+color+'\" onclick=showWanGroup(\"'+r.nexthop+'\")>'+r.nexthop+'</span>';
						break;
					default:
						html = '<span class=\"likea\" id=nexthop_'+r.polno+' style=\"color:'+color+'\" onclick=showProxy(\"'+r.nexthop+'\")>'+r.nexthop+'</span>';
						break;
				}
                cell = trow.insertCell(pos++); //Ŀ·
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"110\");
                cell.innerHTML = html;
				
				if ( r.gateway == \"0.0.0.0\" || r.gateway == \"NULL\" ) r.gateway = \"\"
				
				if ( r.gwtype == \"ip\")
					html = '<span id=gateway_'+r.polno+'>' + r.gateway + '</span>';
				else if (r.gwtype == \"wg\")
					html = '<span class=\"likea\" id=gateway_'+r.polno+' style=\"color:'+color+'\" onclick=showWanGroup(\"'+r.gateway+'\")>' + r. gateway + '</span>';
				else
					html = '<span class=\"likea\" id=gateway_'+r.polno+' style=\"color:'+color+'\" onclick=showProxy(\"'+r.gateway+'\")>' + r.gateway + '</span>';
				
                cell = trow.insertCell(pos++); //һ
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"110\");
				cell.innerHTML = html;
				
                cell = trow.insertCell(pos++); //ƥ
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"70\");
                cell.innerHTML = '<div id=pkts_'+r.polno+'>0</div>';
                cell = trow.insertCell(pos++); //ע
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"*\");
                cell.innerHTML = r.desc;
				
                cell = trow.insertCell(pos++); //
                cell.setAttribute(\"align\", \"right\");
                cell.setAttribute(\"width\", \"70\");
				
				var editHtml = '<img src=\"/img/edit.png\" style=\"margin-top: 3px\" onclick=\"modifyRule('+r.polno+')\" title=\"${LANG_0028:=޸Ĳ}\" />&nbsp;';
                var deleteHtml = '<img src=\"/img/delete.png\" style=\"margin-top: 3px\" onclick=\"deleteRule('+r.polno+')\" title=\"${LANG_0029:=ɾ}\" />&nbsp;';
                cell.innerHTML = editHtml + deleteHtml + stateHtml;  
            }
        }
    });
}
function getpolicystats()
{
	var i, pktobj;
	var row;
	//var type = document.getElementById(\"rt_type\").value;
	var type = \"all\";
	AJXS({type: 'POST', 
		url: '/cgi-bin/App/pktrule/ajax_pkt_route?action=route_list&type=' + type,
		error: function(status){},
		success: function(data) { 
			var d, inif;
			d = eval('('+data+')');
			for (i = 0; i < d.length; i++) {
				pktobj = document.getElementById(\"pkts_\" + d[i].polno);
				if (pktobj != null) pktobj.innerHTML = d[i].pkts;
				
				inif = document.getElementById(\"inif_\" + d[i].polno);
				row = document.getElementsByName(\"row_\" + d[i].polno)[0];
				
				if (row == null) continue;
				
				if (d[i].iniftype == \"posvr\") {
					if (d[i].inifname == \"all\")
						inif.innerHTML = \"${LANG_0033:=PPPOE}\";
					else
						inif.innerHTML = d[i].inifname;
				}
				else
				if (d[i].iniftype == \"proxy\") {
					if (d[i].standby == \"yes\") {
						inif.innerHTML = \"<a style=color:#00ff00 href=javascript:showProxy('\" + 
						d[i].inifname + \"')>\" + d[i].inifname + \"</a>\";
						row.id = \"row4\";
					}
					else {
						inif.innerHTML = \"<a style=color:#0000ff href=javascript:showProxy('\" + 
						d[i].inifname + \"')>\" + d[i].inifname + \"</a>\";
						row.id = \"row1\";
					}
				} else
				if (d[i].iniftype == \"if\") {
					inif.innerHTML = \"<a style=color:#0000ff href=javascript:showIf(d[i].inifname)>\" +
					d[i].inifname + \"</a>\";
				} else {
					inif.innerHTML = d[i].inifname;
				}
				if (d[i].disabled == \"1\") {
					row.id = \"row4\";
					var stateHtml = '<img src=\"/img/enable.png\" style=\"margin-top: 3px\" onclick=\"enablePolicy('+d[i].polno+')\" title=\"${LANG_0030:=ò}\" />&nbsp;';
				} else {
					var stateHtml = '<img src=\"/img/disable.png\" style=\"margin-top: 3px\" onclick=\"disablePolicy('+d[i].polno+')\" title=\"${LANG_0031:=ò}\" />&nbsp;';
					if (d[i].state == \"0\") {
						row.id = \"row3\";
						var color = \"#ff0000\";
					} else {
						row.id = \"row1\";
						var color = \"#0000ff\";
					}
				}
				document.getElementById(\"nexthop_\" + d[i].polno).style.color = color;
				if(d[i].gwtype != \"ip\")
					document.getElementById(\"gateway_\" + d[i].polno).style.color = color;
				document.getElementById(\"state_\" + d[i].polno).innerHTML = stateHtml;
			}
		}
	});
}
function onTypeChanged(obj)
{
	var url = \"${myself}?type=\" + obj.value;
	window.location.href = url;
}
function onload()
{
	load_rtlist();
	getpolicystats();
	setInterval(getpolicystats, 3000);
}
</script>
</body>
</html>
";