<!--#include virtual="/_appDB/db_connection.asp" -->
<!--#include virtual="/_appDB/functions.asp" -->
<!--#include virtual="/super-user/scripts/admin_functions.asp" -->
<!--#include virtual="/super-user/scripts/enc_function.asp" -->
<!--#include virtual="/super-user/scripts/adm_inst.asp" -->
<!--#include virtual="/super-user/scripts/eml_check.asp" -->
<%
response.buffer=true
IF NOT(str_u_lev="0") THEN
	response.write("You do not have permission to view this page")
	response.end
END IF
IF REQUEST("users")="delete" THEN
	delID=sanitizeRequest("deleteID","querystring")
	IF delID <> "" AND IsNumeric(delID) THEN
		dbCBK.Execute("delete from cbk_school_users WHERE `userID`='"&delID&"' limit 1")
		response.redirect("/super-user/control-panel/?go=users&notify=User+account+deleted+successfuly&nact=success")
	END IF
END IF
t_sel=sanitizeRequest("sf","querystring")
IF t_sel <> "" AND IsNumeric(t_sel) THEN
'ok
ELSE
t_sel=""
END IF
%>
<script type="text/javascript">
$(document).ready(function(){
	$('#schoolID').change(function(){
		window.location='/super-user/control-panel/?go=users&sf='+this.value+'&q='+dgbi('q').value;
	});
	$('.hovrow').mouseenter(function(){
		dgbi('cui'+this.id).src = '/super-user/img/cui_w.png';
	});
	$('.hovrow').mouseleave(function(){
		dgbi('cui'+this.id).src = '/super-user/img/cui.png';
	});
	$('#sq').click(function(e){
		e.preventDefault();
		var q = document.getElementById('q').value;
		if(!q){miniNotify('Please enter a valid search term.','error');return false;}else{document.forms.qf.submit();}
	});
});
</script>
<%
Function fetchUCount()
	SET rsDC=dbCBK.Execute("SELECT count(*) as u_count FROM cbk_school_users limit 1")
	IF rsDC.EOF THEN
		fetchUCount="0"
		Exit Function
	END IF
	DO WHILE NOT rsDC.EOF
	  fetchUCount = rsDC("u_count")
	  rsDC.MoveNext
	  loop
	  rsDC.Close
	  SET rsDC = Nothing	
End Function
%>
<div class="maincontent" id="maincontent">
	<div class="col1">
	<form action="/super-user/control-panel/" method="get" name="qf">
	<input type="hidden" name="go" value="users" />
		<div class="col1third">
			<div class="bookingpanel" id="bookingpanel1">
				<h2>Manage User Accounts</h2>
				<span>There are <%=fetchUCount()%> total active users.</span>
				<div class="clear s20"></div>
				<table>
					<tr>
						<td style="width:50%;"><div class="adminbutton"><a href="/super-user/control-panel/?go=users&users=add" style="width:100%;"><span class="adm_add">add user</span></a></div></td>
						<td style="width:50%;">&nbsp;</td>
					</tr>
				</table>
				<div class="clear"></div>
			</div>
		</div>
		<div class="col1third">
			<div class="bookingpanel" id="bookingpanel2">
				<h2>Search Users</h2>
				<span>by name or email.</span>	
				<div class="clear s20"></div>
				<div class="formfields"><input type="text" name="q" id="q" value="<%=sanitizeRequest("q","querystring")%>" style="width:78%;float:left;border-top-right-radius:0px;border-bottom-right-radius:0px;" /><input type="submit" name="sq" id="sq" value="search" style="float:left;width:20%;border-top-right-radius:3px;border-bottom-right-radius:3px;font-size:12px;" /></div>			
				<div class="clear s20"></div>
			</div>
		</div>
		<div class="col1third">
			<div class="bookingpanel" id="bookingpanel3">
				<h2>Filters</h2>
				<span>Refine your search results</span>				
				<div class="clear s20"></div>
				<div class="formfields">
					<select name="schoolID" id="schoolID" style="width:49%;float:left;">
							<option value="0">All Schools</option>
							<%
							SET rsBA = dbCBK.Execute("SELECT `schoolID`,`schoolname` FROM cbk_schools order by `schoolname` asc")%>
							<%
							DO WHILE NOT rsBA.EOF
								t_schid=rsBA("schoolID")
								t_schnm=rsBA("schoolname")
							%>
								<option value="<%=t_schid%>" <%IF (CStr(t_schid)=CStr(t_sel)) THEN response.write("selected='selected'")%>><%=t_schnm%></option>
							<%
								rsBA.MoveNext
								loop
								rsBA.Close
								SET rsBA = Nothing
							%>
						</select>
				</div>
				<div class="clear"></div>
			</div>
		</div>
		<div class="clear"></div>
		</form>
	</div>
	<div class="clear s20"></div>
	<div class="col3 dataTable">
				<%
				
				strSQL = "SELECT `userID`, `email`, `contactname`, `userPOSITION`, `userLEVEL` FROM cbk_school_users WHERE `userID`>'0'"
				str_sql_xtras=""
				IF t_sel<> "" AND t_sel <> "0" THEN
					strSQL=strSQL & " AND `schoolID`='"&t_sel&"'"
					str_sql_xtras=" AND `schoolID`='"&t_sel&"'"
				END IF
				IF request("q")<> "" THEN
					q=sanitizeRequest("q","querystring")
					strSQL=strSQL & " AND `userID`='"&q&"'"
					strSQL=strSQL & " OR `userID`>'0' AND `contactname` LIKE '%"&q&"%'" & str_sql_xtras
					strSQL=strSQL & " OR `userID`>'0' AND `email` LIKE '%"&q&"%'" & str_sql_xtras
					strSQL=strSQL & " OR `userID`>'0' AND `userPOSITION` LIKE '%"&q&"%'" & str_sql_xtras
				ELSE
					q=""
				END IF
				
				oBy = " order by `contactname` asc"
				strSQL=strSQL & oBy
					strCSQL = replace(strSQL,"SELECT `userID`, `email`, `contactname`, `userPOSITION`, `userLEVEL`","SELECT count(*) AS rCount")
					SET rsBC = dbCBK.Execute(strCSQL)
					rCount=0
					DO WHILE NOT rsBC.EOF
						rCount=CInt(rsBC("rCount"))
					rsBC.MoveNext
					loop
					rsBC.Close
					SET rsBC = Nothing

				'results per page
				 perpage=100
                'number of pages
				 nPages=CInt(rCount/perpage)
				 IF nPages < 1 THEN nPages=1 END IF
                'current page
				 curPage=sanitizeRequest("page","querystring")
				 IF (curPage <> "") THEN
					IF IsNumeric(curPage) AND curPage > "0" THEN
						curPage=Cint(curPage)
					ELSE
						curPage=1
					END IF
				 ELSE
					curPage=1
				 END IF
				IF curPage > nPages THEN
					curPage=nPages
				END IF

				endPoint=perPage*curPage
				startPoint=endPoint-perPage
	
				strSQL=strSQL & " limit " & startPoint & ", " & perpage
				SET rsB=dbCBK.Execute(strSQL)
				%>
				<%IF rsB.EOF THEN%>
				<table cellpadding="0" cellspacing="0">
					<tbody>
					<tr>
						<td>
							<p style="text-align:center;">None found.</p>
						</td>
					</tr>
				<%ELSE%>
				<div class="paging">
					<table>
						<tr>
							<td style="padding:5px;text-align:center;width:25%;color:#d9dbdc;">
							<%IF curPage > 1 THEN%>
								<a href="/super-user/control-panel/?go=users&page=<%=curPage-1%>&q=<%=q%>&cType=<%=cT%>&sOrder=<%=s%>&display=<%=d%>">previous</a>
							<%ELSE%>
								previous
							<%END IF%>
							</td>
							<td style="padding:5px;text-align:center;width:50%;">Page <%=curPage%> of <%=nPages%></td>
							<td style="padding:5px;text-align:center;width:25%;color:#d9dbdc;">
							<%IF npages > curPage THEN%>
								<a href="/super-user/control-panel/?go=users&page=<%=curPage+1%>&q=<%=q%>&cType=<%=cT%>&sOrder=<%=s%>&display=<%=d%>">next</a>
							<%ELSE%>
								next
							<%END IF%>
							</td>
						</tr>
					</table>
				</div>
				<div class="clear s10"></div>
				<table cellpadding="0" cellspacing="0">
					<tbody>
						<tr>
							<th>&nbsp;</th>
							<th>User Name</th>
							<th>Full Name</th>
							<th>Position</th>
							<th>Type</th>
							<th>&nbsp;</th>
						</tr>
				<%END IF%>
				<%	rowN=1
					DO WHILE NOT rsB.EOF
				%>	
				<tr id="hr<%=rowN%>" class="hovrow linked-row <%IF NOT(IsOdd(rowN)) THEN response.write("hlightrow")%>" data-link="/super-user/control-panel/?go=users&users=view&view=<%=rsB("userID")%>">
					<td style="text-align:center;">
						<img src="/super-user/img/cui.png" alt="user" style="width:30px;" id="cuihr<%=rowN%>" />
					</td>
					<td><%=rsB("email")%></td>
					<td><%=PCase(rsB("contactname"))%></td>
					<td><%=rsB("userPOSITION")%></td>
					<td><%IF rsB("userLEVEL")="0" THEN response.write("&#x1f512; Administrator") ELSE response.write("Standard") END IF%></td>
					<td>
						<div class="adminbutton"><a title="delete this user account" href="/super-user/control-panel/?go=users&users=delete&deleteID=<%=rsB("userID")%>" style="width:100%;"><span class="adm_delete">delete</span></a></div>
					</td>
				</tr>
				<%
					rowN=rowN+1
					rsB.MoveNext
					loop
					rsB.Close
					SET rsB=Nothing
				%>			
			</tbody>
		</table>
	</div>
</div>