#!/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 "";
if [ "${PALANG}" = "en" ]; then
	LANG_001="Cache|Device"
	LANG_002="Cache|Group"
	LANG_003="Policy"
fi
CGI_browsertitle="iXCache"
. ../../common/common.sh
cfgroot="/cgi-bin/App/ixcache"
myself="${cfgroot}/`basename $0`"
MOD_TAB_LIST="${LANG_001:=豸}#${cfgroot}/ixcache_list ${LANG_002:=}#${cfgroot}/ixcgroup_list ${LANG_003:=ǣ}#${cfgroot}/webmain"

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script language=\"javascript\">
function onAddCache()
{
	window.location.href = \"/cgi-bin/App/ixcache/ixcache_add\";
}
function showProxy(proxyname)
{
	var url;
	url = \"/cgi-bin/Monitor/proxy_show?proxyname=\" + proxyname;
	ShowWindow(url, \"\", 350, 400);
}
function editCache(id)
{
	window.location.href = \"/cgi-bin/App/ixcache/ixcache_edit?id=\" + id;
}
function deleteCache(id)
{
	if (confirm(\"汻ɾ,صĲҲᱻɾ,ȷҪɾ?\"))
		window.location.href = \"${myself}?action=rmvcache&id=\" + id;
}
function showIPGrp(gname)
{
	var url = \"/cgi-bin/Protocol/ipgrp_list?ipgrp=\" + gname;
	ShowWindow(url, \"\", 860, 630);
}
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 getifstats()
{
        var i;
        var stateobj;
        var inbpsobj, outbpsobj;
        var inppsobj, outppsobj;
        ajxs({type: 'POST',
               url: '${cfgroot}/ajax_ixcache_list',
             error: function(status){setTimeout(getifstats, 3000);},
           success: function(data) {
                var d;
                try {
                        d = eval('('+data+')');
                } catch(e) {
                        setTimeout(getifstats, 3000);
                        return;
                }
                for (i = 0; i < d.length; i++) {
                        stateobj = document.getElementById(\"state_\" + d[i].id);
                        if (stateobj != null) {
                                if (d[i].state == \"active\")
                                        stateobj.src = \"/img/yes.png\";
                                else
                                        stateobj.src = \"/img/no.png\";
                        }
                        var versionobj = document.getElementById(\"version_\" + d[i].id);
			if (versionobj != null) versionobj.innerText = d[i].version;
                        var filenumobj = document.getElementById(\"filenum_\" + d[i].id);
			if (filenumobj != null) filenumobj.innerText = d[i].filenum;
                        var mirrorsobj = document.getElementById(\"mirrors_\" + d[i].id);
			if (mirrorsobj!= null) mirrorsobj.innerText = d[i].mirrors;
                        var hitsobj = document.getElementById(\"hits_\" + d[i].id);
			if (hitsobj != null) hitsobj.innerText = d[i].hits;
                        var freeverobj = document.getElementById(\"freever_\" + d[i].id);
			if (freeverobj != null) {
				if (d[i].freever == \"0\")
					freeverobj.innerText = \"רҵ\";
				else
					freeverobj.innerText = \"Ѱ\";
			}
                }
                setTimeout(getifstats, 3000);
        }});
}
function onload()
{
	getifstats();
}
</script>
";
if [ "${CGI_action}" = "rmvcache" ]; then
	operator_check "${myself}"
	errmsg=`${FLOWEYE} ixcache remove id=${CGI_id}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ʧ:${errmsg}"
	fi
fi

echo -n "
<body onload=\"onload()\">
"; cgi_print_mod_header "${LANG_001:=豸}" 1280 
echo -n "
<table width=1280 border=0 cellspacing=1 cellpadding=1> 
<tr id=tblhdr>
	<td width=35  align=center></td>
	<td width=100 align=right>豸</td>
	<td width=100 align=right>豸ַ</td>
	<td width=100 align=right></td>
	<td width=100 align=right>汾</td>
	<td width=90  align=right>ļ</td>
	<td width=100 align=right>··</td>
	<td width=100 align=right>һ</td>
	<td width=100  align=right>״̬</td>
	<td width=100  align=right>ǣ</td>
	<td width=100  align=right>д</td>
	<td width=* align=right><a style=\"color:#0000ff;font-size:14px\" href=\"javascript:onAddCache()\">豸>>&nbsp;</a></td>
</tr>
";
idname="row1"
${FLOWEYE} ixcache list | while read id name proxy ip nexthop state rteid mirrors hits ttl freever filenum version theothers
do 
echo -n "
	<tr id=${idname}>
	<td align=center>${id}</td>
	<td align=right>${name}&nbsp;</td>
	<td align=right>${ip}&nbsp;</td>
"; if [ "${freever}" = "0" ]; then 
echo -n "
	<td align=right id=\"freever_${id}\">רҵ</td>
"; else 
echo -n "
	<td align=right id=\"freever_${id}\"></td>
"; fi 
echo -n "
"; [ "${version}" = "NULL" ] && version="" 
echo -n "
	<td align=right id=\"version_${id}\">${version}</td>
	<td align=right id=\"filenum_${id}\">${filenum}</td>
	<td align=right><a style=\"color:#0000ff\" href=\"javascript:showProxy('${proxy}')\">${proxy}</a></td>
	<td align=right>${nexthop}&nbsp;</td>
	"; if [ "${state}" = "active" ]; then 
echo -n "
	<td align=right><img id=\"state_${id}\" src=\"/img/yes.png\" style=\"margin-right:5px;vertical-align:middle;\" /></td>
	"; else 
echo -n " 
	<td align=right><img id=\"state_${id}\" src=\"/img/no.png\" style=\"margin-right:5px;vertical-align:middle;\" /></td>
	"; fi 
echo -n "
	<td align=right id=\"mirrors_${id}\">${mirrors}&nbsp;</td>
	<td align=right id=\"hits_${id}\">${hits}&nbsp;</td>
	<td align=right>
		<img style=\"margin-top:3px;\" src=\"/img/edit.png\" title=\"޸Ĳ\" onclick=\"editCache('${id}')\" />&nbsp;
		<img style=\"margin-top:3px;\" src=\"/img/delete.png\" title=\"ɾ豸\" onclick=\"deleteCache('${id}')\" />&nbsp;
	</td>
	</tr>
";
	if [ "${idname}" = "row1" ]; then
		idname="row2"
	else
		idname="row1"
	fi
done 

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