<?php
session_start();
if(isset($_SESSION['SESS_ADMINLOGGEDIN']) == FALSE) {
    header("Location: " . $config_basedir."adminlogin.php");
}
	require("config.php");
	require("db.php");
	require("functions.php");
require("aheader.php");
?>
<section clas="sect">
    <div class="row">
        <div class="category" align="center">
<?php
	

	if(isset($_GET['func']) == TRUE) {

		if($_GET['func'] != "conf") {
            header("Location: " . $config_basedir ."adminorders.php");
		}
	
		$validid = pf_validate_number($_GET['id'], "redirect", $config_basedir);
	
		$funcsql = "UPDATE orders SET status = 10 WHERE id = " . $_GET['id'];
		$funcsql = "UPDATE orderitems SET status = 10";
		mysql_query($funcsql);

		header("Location: " . $config_basedir . "adminorders.php");
	}
	else {
		
		echo "<div class='ncat'><h2 align='center'>Outstanding orders</h2></div>";
        $sql = mysql_query("SELECT COUNT(id) FROM orders WHERE status = 20");
        $row = mysql_fetch_row($sql);
        // Here we have the total row count
        $rows = $row[0];
        // This is the number of results we want displayed per page
        $page_rows = 10;
        // This tells us the page number of our last page
        $last = ceil($rows/$page_rows);
        // This makes sure $last cannot be less than 1
        if($last < 1){
            $last = 1;
        }
        // Establish the $pagenum variable
        $pagenum = 1;
        // Get pagenum from URL vars if it is present, else it is = 1
        if(isset($_GET['pn'])){
            $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
        }
        // This makes sure the page number isn't below 1, or more than our $last page
        if ($pagenum < 1) { 
            $pagenum = 1; 
        } else if ($pagenum > $last) { 
            $pagenum = $last; 
        }
        // This sets the range of rows to query for the chosen $pagenum
        $limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows; 

        $orderssql = "SELECT * FROM orders WHERE status = 20 ORDER BY id DESC $limit";
		$ordersres = mysql_query($orderssql);
		$numrows = mysql_num_rows($ordersres);
		
		if($numrows == 0)
		{
			echo "<strong>No orders</strong>";
		}
		else
            
        {			$textline1 = "Results (<b>$rows</b>)";
         $textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
         // Establish the $paginationCtrls variable
         $paginationCtrls = '';
         // If there is more than 1 page worth of results
         if($last != 1){
             /* First we check if we are on page one. If we are then we don't need a link to 
the previous page or the first page so we do nothing. If we aren't then we
generate links to the first page, and to the previous page. */

             if ($pagenum > 1) {
                 $previous = $pagenum - 1;
                 $paginationCtrls .= '<a class="btn-page" href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Prev</a>  ';
                 // Render clickable number links that should appear on the left of the target page number

                 for($i = $pagenum-2; $i < $pagenum; $i++){

                     if($i > 0){
                         $paginationCtrls .= '<a class="btn-page" href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a>&nbsp; ';
                     }
                 }

             }

             // Render the target page number, but without it being a link
             $paginationCtrls .= ''.$pagenum.'&nbsp; ';
             // Render clickable number links that should appear on the right of the target page number
             for($i = $pagenum+1; $i <= $last; $i++){
                 $paginationCtrls .= '<a class="btn-page" href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a>  ';
                 if($i >= $pagenum+2){
                     break;
                 }
             }

             if ($pagenum <= $last-3) {
                 $paginationCtrls .= '<a class="btn-page" href="'.$_SERVER['PHP_SELF'].'?pn='.$last.'">&gt;&gt;</a>  ';

             }
             // This does the same as above, only checking if we are on the last page, and then generating the "Next"
             if ($pagenum != $last) {
                 $next = $pagenum + 1;
                 $paginationCtrls .= '  <a class="btn-page" href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';

             }


         }
         $list = '';	
			echo "<table cellspacing=10>";
		
			while($row = mysql_fetch_assoc($ordersres))
			{
				echo "<tr>";
					echo "<td>[<a href='adminorderdetails.php?id=" . $row['id'] . "'>View</a>]</td>";
					echo "<td>" . date("D jS F Y g.iA", strtotime($row['date'])) . "</td>";
					echo "<td>";
					
					if($row['registered'] == 1)
					{
						echo "Registered Customer";
					}
					else
					{
						echo "Non-Registered Customer";
					}
					
					echo "</td>";
		
                echo "<td>&#x20A6;" . sprintf('%.2f', $row['total']) . "</td>"; 
                echo "<td>". $row['scode']."</td>";
                echo "<td>". $row['spass']."</td>";
	
					echo "<td>";
					
					if($row['payment_type'] == 1)
					{
						echo "VoguePay";
					}
					else
					{
						echo "Bank";
					}
				
					echo "</td>";
			
					echo "<td><a href='adminorders.php?func=conf&id=" . $row['id'] . "'> Confirm Payment</a></td>";
				echo "</tr>";	
			}

			echo "</table>";
		}
	}
            ?><head>
            <style type="text/css">
                body{ font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}
                div#pagination_controls{font-size:21px;}
                div#pagination_controls > a{ color:#06F; }
                div#pagination_controls > a:visited{ color:#06F; }
            </style>
            </head>
            <body>
                <div align="center" style="margin-top:15px;">
                    <hr style="margin-bottom:15px;"/>
                    <p><?php echo $textline1; ?></p><p><?php echo $textline2; ?></p>
                    <p><?php echo $list; ?></p>
                    <?php echo $paginationCtrls; ?><?php echo $lastp; ?>
                </div>
            </body><p><br/><a href="inventory_list.php">Manage Inventory</a><br/></p>
            <p><br/><a href="adminall.php">All Transactions</a><br/></p>
        </div>
    </div>
</section>

<?php

	require("footer.php");
?>
	
