#!/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 -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 onSelect(obj) {
		var ipmacopts = document.getElementsByName(\"IPMACOPT\");
		for (var i = 0; i < ipmacopts.length; i++) 
			ipmacopts[i].checked = obj.checked;
		if (ipmacopts.length > 0) {
			var btn = document.getElementsByName(\"btnDel\")[0];
			btn.disabled = !obj.checked;
		}
	}
	function onCheck(obj) {
		var count = 0;
		var ipmacopts = document.getElementsByName(\"IPMACOPT\");
		for (var i = 0; i < ipmacopts.length; i++) {
			if (ipmacopts[i].checked) count++;
		}
		var btn = document.getElementsByName(\"btnDel\")[0];
		if (count > 0)
			btn.disabled = false;
		else
			btn.disabled = true;
	}
	function beforeDelAll() {
		var ipmacopts;
		var ipaddrs;
		var macaddrs;
		var ipmaclist = \"\";
		ipmacopts = document.getElementsByName(\"IPMACOPT\");
		macaddrs  = document.getElementsByName(\"MACADDR\");
		ipaddrs   = document.getElementsByName(\"IPADDR\");
		for (var i = 0; i < ipmacopts.length; i++) {
			if (ipmacopts[i].checked) {
				if (ipmaclist == \"\")
					ipmaclist = macaddrs[i].value + \"+\" + ipaddrs[i].value;
				else
					ipmaclist += \"|\" + macaddrs[i].value + \"+\" + ipaddrs[i].value;
			}
		}
		if (ipmaclist == \"\") {
			alert(\"ѡҪɾľ̬!\");
			return false;
		}
		var dellist = document.getElementsByName(\"DELIST\")[0];
		dellist.value = ipmaclist;
		return true;
	}
	function onManual() {
		window.location.href = \"/cgi-bin/Maintain/dhcp_static_edit\";
	}
	function onFile() {
		window.location.href = \"/cgi-bin/Maintain/dhcp_static_bat\";
	}
	function static_edit(mac, ip, desc) {
		window.location.href = \"/cgi-bin/Maintain/dhcp_static_edit?EDIT=\" + mac + \"&IP=\" + ip + \"&DESC=\" + desc;
	}
	
	function static_del(mac, ip) {
		window.location.href = \"${myself}?DELETE=\" + mac + \"&IP=\" + ip;
	}
</script>
";
touch ${PGETC}/dhcpd.conf
touch ${PGETC}/dhcpd.static
.     ${PGETC}/dhcpd.conf
touch ${DATAPATH}/dhcpd.lease
if [ "X$CGI_ACTION" = "XDELSELECT" ]; then
	operator_check "$myself"
	echo $CGI_DELIST | sed 's/-/:/g' | tr "|" "\n" | while read line
	do
		mac=`echo $line | awk -F'+' '{print $1}'`
		nip=`echo $line | awk -F'+' '{print $2}'`
		[ "X$mac" = "X" ] && continue;
		[ "X$nip" = "X" ] && continue;
		errmsg=`${PGPATH}/bin/ipe_dhctl del lease mac=$mac nip=$nip`
	done
	afm_dialog_msg "ɹ!"
	afm_load_page 0 "$myself"
fi
if [ "X$CGI_DELETE" != "X" ]; then
	operator_check "$myself"
	CGI_DELETE=`echo $CGI_DELETE | sed 's/-/:/g'`
	errmsg=`${PGPATH}/bin/ipe_dhctl del lease mac=$CGI_DELETE nip=$CGI_IP`
	afm_dialog_msg "ɹ!"
	afm_load_page 0 "$myself"
fi
echo "<body>"
cgi_show_title "->DHCP" 1000 
echo "<br>"
cgi_print_mod_header "̬" 1000 

echo -n "
<table width=\"900\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" bgcolor=\"#ffffff\">
<tr id=\"row1\">
	<form method=post onsubmit=\"beforeDelAll()\" action=\"${myself}\">
		<td width=\"666\" align=\"right\">
			<input type=hidden name=ACTION value=\"DELSELECT\"/>
			<input type=hidden name=DELIST value=\"\"/>
			<input type=submit style=\"width:90;height:22\" name=btnDel value=\"ɾѡ\" disabled/>
		</td>
	</form>
	<td width=\"*\" align=\"right\">
		<input type=\"button\" style=\"width:100;height:22\" value=\"ֹ>>\" onclick=\"onManual()\"/>
		&nbsp;&nbsp;
		<input type=\"button\" style=\"width:100;height:22\" value=\"ļ>>\" onclick=\"onFile()\"/>
	</td>
</tr>
</table>
<table width=\"900\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\">
<tr id=\"tblhdr\">
	<td width=\"80\"  align=\"center\">
		<input type=\"checkbox\" onclick=\"onSelect(this)\"/>&nbsp;
	</td>
	<td width=\"80\"  align=\"center\">ID</td>
	<td width=\"130\" align=\"center\">MACַ</td>
	<td width=\"130\" align=\"center\">̬IP</td>
	<td width=\"200\" align=\"center\">ע</td>
	<td width=\"130\" align=\"center\"></td>
</tr>
";
idname="row1"
count=1
cat ${PGETC}/dhcpd.static | sed 's/:/-/g' | while read mac ip desc
do
	if [ "X$mac" = "X" -o "X$ip" = "X" ]; then
		continue
	fi
	echo "
		<tr id=\"$idname\">
			<td align=\"center\">
				<input type=\"checkbox\" name=\"IPMACOPT\" onclick=\"onCheck(this)\"/>&nbsp;
				<input type=hidden name=MACADDR value=\"$mac\"/>
				<input type=hidden name=IPADDR  value=\"$ip\"/>
			</td>
			<td align=\"center\">$count</td>
			<td align=\"center\">$mac</td>
			<td align=\"center\">
				<a href=\"javascript:showip('${ip}')\">&nbsp;$ip</a>
			</td>
			<td align=\"center\">$desc</td>
			<td align=\"center\">
				<a style=\"color:#0000ff\" href=\"javascript:static_edit('$mac', '$ip', '$desc')\">޸</a>
				&nbsp;&nbsp;
				<a style=\"color:#0000ff\" href=\"javascript:static_del('$mac', '$ip')\">ɾ</a>
			</td>
		</tr>"
	if [ "X$idname" = "Xrow1" ]; then
		idname="row2"
	else
		idname="row1"
	fi
	count=$(($count + 1))
done

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