<?php
	session_start();
	//important files for function
    include "includes/avtr_connect.php";
    include "includes/avtr_functions.php";
    
	// Initialize Pagination & Session
	sessinit();
	
	$cat = $_SESSION['cat_ID'];
	$gen = $_SESSION['gender'];
	
	$page = (int)(!isset($_GET["page"]) ? 1 : $_GET["page"]);
	if ($page <= 0) $page = 1;
	$per_page = 19;
	$startpoint = ($page * $per_page) - $per_page;
	$avtr_item = mysqli_query($conn,"SELECT * FROM avtr_items WHERE cat_ID=" . $cat . " AND gender=" . $gen . " LIMIT $startpoint , $per_page");
    $avtr_cat=mysqli_query($conn, "SELECT * FROM avtr_categories");    
?>
<!DOCTYPE html>
<html lang="en">
    <head>
		<meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Avatar Creator</title>
				<link rel="shortcut icon" href="images/favicon.png" type="img/x-icon">
				<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
				<link rel="stylesheet" href="css/avatar.css">
                <script type="text/javascript" src="js/jquery-1.12.4.js"></script>
                <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
                <script type="text/javascript" src="js/bootstrap.min.js"></script>
				<script type="text/javascript" src="js/avatar.js"></script>

    </head>
    <body>
		<div class="container mainbg text-center" style="margin-top: 25px;margin-bottom: 25px;">
			<div id="gender" class="box z-depth-1-half tabbg" style="display: <?php echo ($gen < 1) ? 'block' : 'none'; ?>;">
			<form class="" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" id="choices">
			<fieldset>
				<table id='userTable' class="table table-bordered table-hover">
					<thead>
						<tr>
							<th class='text-center row col-md-2' colspan="5">
							<h2>Choose your gender</h2>
							</th>
						</tr>
					</thead>
					<tbody>
							<tr>
							<td class='col-md-2'>
							</td>
							<td class='col-md-2'>
							<button class='btn btn-info btn-xs btn-block' type='submit' id='male' target='male' name='gender' value='1'>Male</button>
							</td>
							<td class='col-md-2'>
							</td>
							<td class='col-md-2'>
							<button class='btn btn-info btn-xs btn-block' type='submit' id='female' target='female' name='gender' value='2'>Female</button>
							</td>
							<td class='col-md-2'>
							</td>
						</tr>
					</tbody>
				</table>
			</fieldset>
			</form>
			</div>
			<div id="imgbuild" class="catdisplay" style="display: <?php echo ($gen > 0) ? 'block' : 'none'; ?>;margin-top: 15px;">
						<?php
						// Build the Avatar
						$cnt = 	1;
						while ($cnt != 21){
						
						$imgchk = mysqli_query($conn, "SELECT * FROM avtr_categories WHERE ID=$cnt");
						$imgbld = mysqli_fetch_array($imgchk, MYSQLI_ASSOC);
						$g =  $imgbld['catName'];
						$h = $g ."ID";
						
						// Have we chosen items?
						if(isset($_POST[$h])){
							$_SESSION[$h]=$_POST[$h];
						}elseif(isset($_SESSION[$h])){
							$itm = $_SESSION[$h];
						}else{
						$_POST[$h] = 'blankavatar.png';
						$_SESSION[$h]=$_POST[$h];
						}
						
						echo '				<div id="div' . $imgbld['catName'] . '" class="cat' . $imgbld['catName'] . '">
					<img src="catImages/' . $imgbld['catName'] . '/' . $_SESSION[$h] . '" id="1">
				</div>';
						$cnt++;
						}
						?>

			</div>			
			<div id="imgsave" style="display: <?php echo ($gen > 0) ? 'block' : 'none'; ?>;">
				<form class="" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" id="imagecreate">
					<fieldset>
						<button type="submit" class="imggif" name="saveimg" alt="Save as GIF" title="Save as GIF" value="1"></button>
						<button type="submit" class="imgjpg" name="saveimg" alt="Save as JPG" title="Save as JPG" value="2"></button>
						<button type="submit" class="imgpng" name="saveimg" alt="Save as PNG" title="Save as PNG" value="3"></button>
					</fieldset>
				</form>
			</div>
				<div id="categories" class="box z-depth-1-half tabbg" style="display: <?php echo ($gen > 0) ? 'block' : 'none'; ?>;">
			<form class="" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" id="categories">
			<fieldset>
				<table id='userTable' class="table table-bordered table-hover">
					<thead>
						<?php
						$c = 0; // Our counter
						$n = 5; // Each Nth iteration would be a new table row
						while (list($ID,$catName) = $avtr_cat->fetch_array(MYSQLI_NUM)){
						// If $c is divisible by $n...
						if($c % $n == 0 && $c != 0){
							// New table row
							echo '</tr><tr>';
						}					
						$c++;
							if (isset($_SESSION['cat_ID'])){
								if($_SESSION['cat_ID']==$ID){
									echo "	<th class='text-center row col-md-2'>
												<button class='showItems btn btn-primary btn-xs btn-block' type='submit' id='" . $ID . "' target='" . $ID . "' name='category' value='" . $ID . "'>" . $catName . "</button>
											</th>";
								}else{							
									echo "	<th class='text-center row col-md-2'>
												<button class='showItems btn btn-info btn-xs btn-block' type='submit' id='" . $ID . "' target='" . $ID . "' name='category' value='" . $ID . "'>" . $catName . "</button>
											</th>";
								}
							}else{							
									echo "	<th class='text-center row col-md-2'>
												<button class='showItems btn btn-info btn-xs btn-block' type='submit' id='" . $ID . "' target='" . $ID . "' name='category' value='" . $ID . "'>" . $catName . "</button>
											</th>";
							}
						}
						?>
						</tr>
					</thead>
				</table>
			</fieldset>
			</form>
			</div>
			<div id="items" class="targetItems box effect7 tabbg" style="display: <?php echo ($gen > 0) ? 'block' : 'none'; ?>;margin-bottom: 25px;">
			<form class="" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" id="items">
			<fieldset>
						<?php
						$c2 = 0; // Our counter
						$n2 = 5; // Each Nth iteration would be a new table row
						$tbl = 0;
					
						// Build the item table now						
						while (list($avtr_ID,$name,$description,$cat_ID,$cost,$filename,$thumbnail,$quantity,$gender) = $avtr_item->fetch_array(MYSQLI_NUM)){
						
						// Get the associated category for each item
						$avtr_catlst=mysqli_query($conn, "SELECT avtr_categories.* FROM avtr_categories WHERE ID = $cat_ID");
						$avtr_catlst2 = mysqli_fetch_assoc($avtr_catlst);
						
						if($cat_ID != $tbl){
						echo '<table class="table table-bordered table-hover">';
						echo '<tbody>';
						echo '<td class="col-md-2"><button type="submit" id="' . $avtr_catlst2['catName'] . 'ID" name="' . $avtr_catlst2['catName'] . 'ID" value="blankavatar.png" alt="Clear Selection" title="Clear Selection"><img src="catImages/' . $avtr_catlst2['catName'] . '/btn_avatarclear.png" width="50" height="50"></button></td>';
							$c2++;
						}
						
						// If $c is divisible by $n...
						if($c2 % $n2 == 0 && $c2 != 0){
							// New table row
							echo '</tr><tr>';
						}					
						$c2++;
						
							echo "	<td class='col-md-2'>
										<button type='submit' id='" . $avtr_catlst2['catName'] . "ID' name='" . $avtr_catlst2['catName'] . "ID' value='" . $filename . "' alt='Choose Item' title='" . $name . "'><img src='catImages/" . $avtr_catlst2['catName'] . "/" . $thumbnail . "' width='50' height='50'></button>
									</td>";
						$tbl = $cat_ID;
						}
						?>
						</tr>
					</tbody>
				</table>
			</fieldset>
			</form>
			<center>
				<a href='includes/avtr_clear.php' role='button' class='float btn btn-warning btn-xs btn-fab' style="margin-bottom: 10px;">Start Over</a>
			</center>
						<?php
					 // displaying pagination.
						echo pagination($cat,$gen,$per_page,$page,$url='?');
						?>
			</div>
		</div>
	</body>
</html>
<?php
ob_end_flush();
?>
