#!/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/Maintain/`basename $0`"
MOD_TAB_LIST="#/cgi-bin/Maintain/dhcp_config ̬#/cgi-bin/Maintain/dhcp_static ԼϢ#/cgi-bin/Maintain/dhcp_lease"
echo "<body>"
cgi_show_title "->DHCP" 1000 
echo "<br>"
cgi_print_mod_header "ԼϢ" 1000 

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script language=\"javascript\">
	function showip(ipaddr) {
		var url = \"/cgi-bin/Monitor/ipview_data?ipaddr=\" + ipaddr;
		ShowWindow(url, \"\", 650, 700);
	}
	function add_lease(mac, ip, desc) {
		window.location.href = \"${myself}?ADD=\" + mac + \"&IP=\" + ip + \"&DESC=\" + desc;
	}
	function del_lease(mac, ip) {
		window.location.href = \"${myself}?DEL=\" + mac + \"&IP=\" + ip;
	}
</script>
";
touch ${PGETC}/dhcpd.conf
touch ${PGETC}/dhcpd.static
.     ${PGETC}/dhcpd.conf
touch ${DATAPATH}/dhcpd.lease
if [ "X$CGI_ADD" != "X" ]; then
	operator_check "$myself"
	if [ "X$ENABLE_STATIC" = "X" ]; then
		afm_dialog_msg "þ̬!"
		afm_load_page 0 "/cgi-bin/Maintain/dhcp_config"
	else
		CGI_ADD=`echo $CGI_ADD | sed 's/-/:/g'`
		errmsg=`${PGPATH}/bin/ipe_dhctl del lease mac=$CGI_ADD nip=$CGI_IP`
		errmsg=`${PGPATH}/bin/ipe_dhctl add lease mac=$CGI_ADD nip=$CGI_IP desc=$CGI_DESC type=0`
		afm_dialog_msg "ɹ!"
		afm_load_page 0 "$myself"
	fi
fi
if [ "X$CGI_DEL" != "X" ]; then
	operator_check "$myself"
	CGI_DEL=`echo $CGI_DEL | sed 's/-/:/g'`
	errmsg=`${PGPATH}/bin/ipe_dhctl del lease mac=$CGI_DEL nip=$CGI_IP`
	afm_dialog_msg "ɹ!"
	afm_load_page 0 "$myself"
fi

echo -n "
<br>
<form method=\"post\">
<table width=\"900\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\">
<tr id=tblhdr>
	<td width=\"80\"  align=\"center\">ID</td>
	<td width=\"130\" align=\"center\">MACַ</td>
	<td width=\"130\" align=\"center\">IP</td>
	<td width=\"130\" align=\"center\">ʱ</td>
	<td width=\"130\" align=\"center\">ʱ</td>
	<td width=\"130\" align=\"center\">/ע</td>
	<td width=\"*\" align=\"center\"></td>
</tr>
";
idname="row1"
${PGPATH}/bin/ipe_dhctl show leases 2>/dev/null | while read id mac ip lease expires hostname
do
	echo "<tr id=$idname>
		<td align=\"center\">$id</td>
		<td align=\"center\">$mac</td>
		<td align=\"center\">
			<a href=\"javascript:showip('${ip}')\">&nbsp;$ip</a>
		</td>
		<td align=\"center\">$lease</td>
	"
	if [ "X$expires" = "X0" ]; then
		echo "
			<td align=\"center\">̬</td>
			<td align=\"center\">$hostname</td>
			<td align=\"center\">
				<a style=\"color:#0000ff\" href=\"javascript:del_lease('$mac', '$ip')\">ת̬</a>&nbsp;
		"
	else
		echo "
			<td align=\"center\">$expires</td>
			<td align=\"center\">$hostname</td>
			<td align=\"center\">
				<a style=\"color:#0000ff\" href=\"javascript:add_lease('$mac', '$ip', '$hostname')\">ת̬</a>&nbsp;
		"
	fi
	echo "
			<a style=\"color:#0000ff\" href=\"javascript:del_lease('$mac', '$ip')\">ɾ</a>&nbsp;
			</td>
		</tr>
	"
	if [ "X$idname" = "Xrow1" ]; then
		idname="row2"
	else
		idname="row1"
	fi
done

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