<?php

/**
 * Provide a admin area view for the "user"-page of the plugin
 *
 * @link       http://www.weedesign.de/weebotLite.html
 * @since      1.0.0
 *
 * @package    weebotLite
 * @subpackage weebotLite/admin/template
 */
 
?>

<div class="wrap">

	<h1 class="wp-heading-inline"><?php echo esc_html( get_admin_page_title() ); ?></h1>
	
	<?php
		/* 
		*	show "create"-link if there is no action defined
		*/
		if(isset($_GET["method"])) { 
			if($_GET["method"]=="support") {
				?><a href="<?php echo admin_url( 'users.php' ); ?>" class="page-title-action"><?php esc_attr_e( 'Add new', $this->plugin_name ); ?></a><?php
			}
		} 
	?>
	
	<hr class="wp-header-end">
	
	<?php 
	
		/* 
		*	if something was posted
		*/
		if(isset($_GET["post"])) { 
			if(isset($_GET["action"])) {
				switch($_GET["action"]) {
					
					/* 
					*	delete user
					*/
					case "delete":
						$delete = $this->delete("user",$_GET["post"]);
						$this->template("chat/user/delete");
					break;
					
					/* 
					*	view user
					*/
					case "view":
						$this->template("chat/user/view",array("user"=>$_GET["post"]));
					break;
				}
			}
		} else {
		
				/* 
				*	get options
				*/
				$options = get_option($this->plugin_name,$this->default_options());
		
				/* 
				*	get the user list
				*/
				$user = $this->get_page_data("user","type='support'","name","user");
				
				/* 
				*	get the online user
				*/
				$online_user = $this->get_online_user();
				
				?>
		
					<ul class="subsubsub">
						<li class="all"><a href="?page=<?php echo $_GET["page"]; ?>"<?php if(!isset($_GET["method"])&&$user["search"]===false) { ?> class="current"<?php } ?>><?php esc_attr_e( 'All', $this->plugin_name ); ?> <span class="count">(<?php echo number_format($user["all_count_real"],0,".","."); ?>)</span></a> |</li>
						<li class="publish"><a href="?page=<?php echo $_GET["page"]; ?>&amp;method=support"<?php if(isset($_GET["method"])) { if($_GET["method"]=="support") { ?> class="current"<?php } } ?>><?php esc_attr_e( 'Support', $this->plugin_name ); ?> <span class="count">(<?php echo number_format($user["all_count_special"],0,".","."); ?>)</span></a></li>
						<?php
							if($options["online"]==1) {
								?><li class="online">| <a href="?page=<?php echo $_GET["page"]; ?>&amp;method=online"<?php if(isset($_GET["method"])) { if($_GET["method"]=="online") { ?> class="current"<?php } } ?>><?php esc_attr_e( 'Online', $this->plugin_name ); ?> <span class="count">(<?php echo number_format(count($online_user),0,".","."); ?>)</span></a></li><?php
							}
						?>
						<?php
							if($user["search"]!==false) {
								?><li>| <a href="?page=<?php echo $_GET["page"]; ?>&amp;s=<?php echo $user["s"]; ?>" class="current"><?php echo $user["s"]; ?> <span class="count">(<?php echo number_format($user["all_count_search"],0,".","."); ?>)</span></a></li><?php
							}
						?>
					</ul>
				
				<?php
					/* 
					*	get the online user list
					*/
					if(isset($_GET["method"])) {
						if($_GET["method"]=="online") {
							$user = $this->get_page_data("user","type!='support' and online > ".(time()-6),"name","user");
						}
					}
				?>
			
					<hr class="wp-header-end">
					
					<form action="?page=<?php echo $_GET["page"]; ?>&amp;orderby=<?php echo $user["orderby"]; ?>&amp;order=<?php echo $user["order"]; if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>" method="post" id="userForm">

						<p class="search-box">
							<label class="screen-reader-text" for="post-search-input"><?php esc_attr_e( 'Search User', $this->plugin_name ); ?>:</label>
							<input type="search" id="post-search-input" name="s" value="">
							<input type="submit" id="search-submit" class="button" value="<?php esc_attr_e( 'Search User', $this->plugin_name ); ?>">
						</p>
					
						<?php
						
							if($user["start"]>$user["all_count"]) {
								
								?><script type="text/javascript">document.location="?page=<?php echo $_GET["page"]; ?>";</script><?php
								
							} else {
								
								if(count($user["user"])>0) {
								
									?>
									
										<div class="tablenav top">
											<div class="alignleft actions bulkactions">
												<label for="bulk-action-selector-top" class="screen-reader-text"><?php esc_attr_e( 'Select bulk action', $this->plugin_name ); ?></label>
												<select name="action[]" id="bulk-action-selector-top">
													<option value="-1"><?php esc_attr_e( 'Bulk Actions', $this->plugin_name ); ?></option>
													<option value="trash"><?php esc_attr_e( 'Delete', $this->plugin_name ); ?></option>
												</select>
												<input type="submit" class="button" value="<?php esc_attr_e( 'Apply', $this->plugin_name ); ?>">
											</div>
											<div class="tablenav-pages">
												<span class="displaying-num"><?php echo number_format($user["all_count"],0,".","."); ?> <?php esc_attr_e( 'user', $this->plugin_name ); ?></span>
												<span class="pagination-links">
												<?php 
													if(isset($_GET["start"])) { 
														if($user["page"]>2) {
															?><a class="tablenav-pages-navspan" href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=<?php echo $user["orderby"]; ?>&amp;order=<?php echo $user["order"]; if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>">«</a><?php
														}
														?><a class="tablenav-pages-navspan" href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=<?php echo $user["orderby"]; ?>&amp;order=<?php echo $user["order"]; if(ceil($user["limit"]*($user["page"]-1))-$user["limit"]!=0) { echo '&amp;start='.(ceil($user["limit"]*($user["page"]-1))-$user["limit"]); } if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>">‹</a><?php
													}
												?>
												<span class="screen-reader-text"><?php esc_attr_e( 'Current Page', $this->plugin_name ); ?></span>
												<span id="table-paging" class="paging-input">
													<span class="tablenav-paging-text"><?php echo number_format(ceil($user["page"]),0,".","."); ?> <?php esc_attr_e( 'of', $this->plugin_name ); ?> <span class="total-pages"><?php echo number_format(ceil($user["all_count"]/$user["limit"]),0,".","."); ?></span></span>
												</span>
												<?php 
													if($user["all_count"]/$user["limit"]>1) { 
														if($user["page"]<$user["all_count"]/$user["limit"]) {
															?>
																<a href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=<?php echo $user["orderby"]; ?>&amp;order=<?php echo $user["order"]; ?>&amp;start=<?php echo $user["limit"]*($user["page"]);  if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>" class="tablenav-pages-navspan">›</a>
																<a href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=<?php echo $user["orderby"]; ?>&amp;order=<?php echo $user["order"]; ?>&amp;start=<?php echo (ceil($user["all_count"]/$user["limit"])*$user["limit"])-$user["limit"]; if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>" class="tablenav-pages-navspan">»</a>
															<?php
														}
													}
												?>
												</span>
											</div>
											<br class="clear">
										</div>
								
										<table class="wp-list-table widefat fixed striped pages">
											
											<thead>
												<tr>
													<td id="cb" class="manage-column column-cb check-column">
														<label class="screen-reader-text" for="cb-select-all-1"><?php esc_attr_e( 'Select All', $this->plugin_name ); ?></label>
														<input id="cb-select-all-1" type="checkbox">
													</td>
													<th scope="col" id="title" class="manage-column column-title column-primary sortable <?php echo $user["orderAnti"]; ?>">
														<a href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=name&amp;order=<?php echo $user["orderAnti"]; if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>">
															<span><?php esc_attr_e( 'Name', $this->plugin_name ); ?></span>
															<span class="sorting-indicator"></span>
														</a>
													</th>
													<th scope="col" id="type" class="manage-column column-title column-primary sortable <?php echo $user["orderAnti"]; ?>">
														<a href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=type&amp;order=<?php echo $user["orderAnti"]; if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>">
															<span><?php esc_attr_e( 'Type', $this->plugin_name ); ?></span>
															<span class="sorting-indicator"></span>
														</a>
													</th>
													<th scope="col" id="email" class="manage-column column-title column-primary sortable <?php echo $user["orderAnti"]; ?>">
														<a href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=email&amp;order=<?php echo $user["orderAnti"]; if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>">
															<span><?php esc_attr_e( 'E-Mail', $this->plugin_name ); ?></span>
															<span class="sorting-indicator"></span>
														</a>
													</th>
													<th scope="col" id="date" class="manage-column column-date sortable <?php echo $user["orderAnti"]; ?>">
														<a href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=date&amp;order=<?php echo $user["orderAnti"]; if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>">
															<span><?php esc_attr_e( 'Online', $this->plugin_name ); ?></span>
															<span class="sorting-indicator"></span>
														</a>
													</th>
												</tr>
											</thead>

											<tbody id="the-list">
							
											<?php
									
											for($i=0;$i<$user["limit"];$i++){
												if(isset($user["user"][$i])) {
													
													?>
													
														<tr id="post-<?php echo $user["user"][$i]->id; ?>" class="iview author-self level-0 post-<?php echo $user["user"][$i]->id; ?> type-page status-publish hentry">
															<th scope="row" class="check-column">
																<label class="screen-reader-text" for="cb-select-<?php echo $user["user"][$i]->id; ?>"><?php esc_attr_e( 'Select', $this->plugin_name ); ?></label>
																<input id="cb-select-<?php echo $user["user"][$i]->id; ?>" type="checkbox" name="user[]" value="<?php echo $user["user"][$i]->id; ?>">
															</th>
															<td class="title column-title has-row-actions column-primary page-title" data-colname="Title">
																<div class="locked-info">
																	<span class="locked-avatar"></span>
																	<span class="locked-text"></span>
																</div>
																<strong>
																	<a class="row-title" href="?page=<?php echo $_GET["page"]; ?>&amp;post=<?php echo $user["user"][$i]->id; ?>&amp;action=view" aria-label="View">
																		<?php 
																			if($user["user"][$i]->user==-1) {
																				if(!empty($user["user"][$i]->name)) {
																					echo esc_html($user["user"][$i]->name);
																				} else {
																					esc_attr_e( 'anonym', $this->plugin_name );
																				}
																			} else {
																				if ($user_data = get_userdata($user["user"][$i]->user)) {
																					echo $user_data->data->display_name;
																				} else {
																					esc_attr_e( 'anonym', $this->plugin_name );
																				}
																			}
																		?>
																	</a>
																</strong>
																<div class="row-actions">
																	<span class="view">
																		<a href="?page=<?php echo $_GET["page"]; ?>&amp;post=<?php echo $user["user"][$i]->id; ?>&amp;action=view" aria-label="View"><?php esc_attr_e( 'View', $this->plugin_name ); ?></a>
																		|
																	</span>
																	<span class="trash">
																		<a href="?page=<?php echo $_GET["page"]; ?>&amp;post=<?php echo $user["user"][$i]->id; ?>&amp;action=delete" data-id="<?php echo $user["user"][$i]->id; ?>" data-table="user" class="weebotLite-delete"><?php esc_attr_e( 'Trash', $this->plugin_name ); ?></a>
																	</span>
																</div>
															</td>
															<td class="type column-type" data-colname="Type">
																<?php 
																	switch($user["user"][$i]->type) {
																		case "session":
																			esc_attr_e( 'Guest', $this->plugin_name ); 
																		break;
																		case "user":
																			esc_attr_e( 'Wordpress User', $this->plugin_name );
																		break;
																		case "support":
																			esc_attr_e( 'Support', $this->plugin_name );
																		break;
																	}
																?>
															</td>
															<td class="author column-author" data-colname="Author">
																<?php 
																	if($user["user"][$i]->user==-1) {
																		if(!empty($user["user"][$i]->email)) {
																			echo esc_html($user["user"][$i]->email);
																		} else {
																			esc_attr_e( 'not defined', $this->plugin_name );
																		}
																	} else {
																		if ($user_data = get_userdata($user["user"][$i]->user)) {
																			echo $user_data->data->user_email;
																		} else {
																			esc_attr_e( 'not defined', $this->plugin_name );
																		}
																	}
																?>
															</td>
															<td class="date column-date" data-colname="Date"><abbr title="<?php echo $user["user"][$i]->online; ?>"><?php echo date('Y.m.d H:i:s', $user["user"][$i]->online); ?></abbr></td>
														</tr>
													
													<?php
													
												}
											}
									
											?>
									
											</tbody>
											<tfoot>
												<tr>
													<td class="manage-column column-cb check-column">
														<label class="screen-reader-text" for="cb-select-all-2"><?php esc_attr_e( 'Select All', $this->plugin_name ); ?></label>
														<input id="cb-select-all-2" type="checkbox">
													</td>
													<th scope="col" class="manage-column column-title column-primary sortable <?php echo $user["orderAnti"]; ?>">
														<a href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=name&amp;order=<?php echo $user["orderAnti"]; if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>">
															<span><?php esc_attr_e( 'Name', $this->plugin_name ); ?></span>
															<span class="sorting-indicator"></span>
														</a>
													</th>
													<th scope="col" id="type" class="manage-column column-title column-primary sortable <?php echo $user["orderAnti"]; ?>">
														<a href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=type&amp;order=<?php echo $user["orderAnti"]; if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>">
															<span><?php esc_attr_e( 'Type', $this->plugin_name ); ?></span>
															<span class="sorting-indicator"></span>
														</a>
													</th>
													<th scope="col" id="email" class="manage-column column-title column-primary sortable <?php echo $user["orderAnti"]; ?>">
														<a href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=email&amp;order=<?php echo $user["orderAnti"]; if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>">
															<span><?php esc_attr_e( 'E-Mail', $this->plugin_name ); ?></span>
															<span class="sorting-indicator"></span>
														</a>
													</th>
													<th scope="col" class="manage-column column-date sortable <?php echo $user["orderAnti"]; ?>">
														<a href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=date&amp;order=<?php echo $user["orderAnti"]; if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>">
														<span><?php esc_attr_e( 'Online', $this->plugin_name ); ?></span>
														<span class="sorting-indicator"></span>
														</a>
													</th>
												</tr>
											</tfoot>

										</table>
										
										<div class="tablenav bottom">
											<div class="alignleft actions bulkactions">
												<label for="bulk-action-selector-bottom" class="screen-reader-text"><?php esc_attr_e( 'Select bulk action', $this->plugin_name ); ?></label>
												<select name="action[]" id="bulk-action-selector-bottom">
													<option value="-1"><?php esc_attr_e( 'Bulk Actions', $this->plugin_name ); ?></option>
													<option value="trash"><?php esc_attr_e( 'Delete', $this->plugin_name ); ?></option>
												</select>
												<input type="submit" class="button" value="<?php esc_attr_e( 'Apply', $this->plugin_name ); ?>">
											</div>
											<div class="tablenav-pages">
												<span class="displaying-num"><?php echo number_format($user["all_count"],0,".","."); ?> <?php esc_attr_e( 'user', $this->plugin_name ); ?></span>
												<span class="pagination-links">
												<?php 
													if(isset($_GET["start"])) { 
														if($user["page"]>2) {
															?><a class="tablenav-pages-navspan" href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=<?php echo $user["orderby"]; ?>&amp;order=<?php echo $user["order"]; if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>">«</a><?php
														}
														?><a class="tablenav-pages-navspan" href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=<?php echo $user["orderby"]; ?>&amp;order=<?php echo $user["order"]; if(ceil($user["limit"]*($user["page"]-1))-$user["limit"]!=0) { echo '&amp;start='.(ceil($user["limit"]*($user["page"]-1))-$user["limit"]); } if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>">‹</a><?php
													}
												?>
												<span class="screen-reader-text"><?php esc_attr_e( 'Current page', $this->plugin_name ); ?></span>
												<span id="table-paging" class="paging-input">
													<span class="tablenav-paging-text"><?php echo number_format(ceil($user["page"]),0,".","."); ?> of <span class="total-pages"><?php echo number_format(ceil($user["all_count"]/$user["limit"]),0,".","."); ?></span></span>
												</span>
												<?php 
													if($user["all_count"]/$user["limit"]>1) { 
														if($user["page"]<$user["all_count"]/$user["limit"]) {
															?>
																<a href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=<?php echo $user["orderby"]; ?>&amp;order=<?php echo $user["order"]; ?>&amp;start=<?php echo $user["limit"]*($user["page"]);  if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>" class="tablenav-pages-navspan">›</a>
																<a href="?page=<?php echo $_GET["page"]; ?>&amp;orderby=<?php echo $user["orderby"]; ?>&amp;order=<?php echo $user["order"]; ?>&amp;start=<?php echo (ceil($user["all_count"]/$user["limit"])*$user["limit"])-$user["limit"]; if(isset($_GET["method"])) { ?>&amp;method=<?php echo $_GET["method"]; } if($user["search"]!==false) { ?>&amp;s=<?php echo $user["s"]; } ?>" class="tablenav-pages-navspan">»</a>
															<?php
														}
													}
												?>
												</span>
											</div>
											<br class="clear">
										</div>
									
									<?php
								}
							
							}
							
						?>
					
					</form>
					
				<?php 

		} 
	?>

</div>
