<? ob_start();
session_start();
require_once("../dbconnect.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<?php include_once("title.php");?>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
-->
</style>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
<link href="../pagecss.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="ajaxvalid.js"></script>
<?php 
if($_POST['statuschange']!="")
 {
  if($_POST['statuschange']==3) 
    {
	$prodlist=mysql_query("select * from tblcartdetails where CARTID=".$_POST['hidcartid']."");
	   while($prodrows=mysql_fetch_array($prodlist))
	     { 
		   //echo $prodrows['PRODUCTID'];
		  $selprod=mysql_query("select * from products where pid=".$prodrows['PRODUCTID']." ");
		     $selproducts=mysql_fetch_array($selprod);
			 $final=$selproducts['pqty']-$prodrows['QUANTITY'];
		  mysql_query("update products set pqty=".$final." where pid=".$selproducts['pid']."");
		 }

	 }
	
	echo  $fdf="update tblcart set status=".$_POST['statuschange']." where CARTID=".$_POST['hidcartid']." ";
	 $wew=mysql_query($fdf);
	 
if(!mysql_query("update tblcart set status=".$_POST['statuschange']." where CARTID=".$_POST['hidcartid']." "))
  die("error:".mysql_error());
   header("Location: orders.php?cartid=".$_POST['hidcartid']."");
  }

  
  	if($_GET['sortorder']=="") 
				  {
				  $inorder="kk";
				  }
				  else
				  $inorder=$_GET['sortorder']; 
				  
				  if($inorder=="kk")
				    $inorder="'%'";
				   if($_GET['sortby']!="")
				      {
					  $inorder=urlencode($_GET['sortby']);
					  }
					  
					   ?>


<style type="text/css">
<!--
.style2 {
	color: #21262A;
	font-size: 12px;
}
-->
</style>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td valign="top"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td><? require_once("header.php"); ?></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
            
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
              <tr>
                <td align="left" valign="middle"class="cate_pro" style="padding-right:15px;">ORDERS</td>
                <td align="right" style="padding-right:15px;"><table width="70%" cellpadding="0" cellspacing="0">
                  <tr>
                    <td><table border="0" cellpadding="0" cellspacing="0" width="100%">
                        <tbody>
                        </tbody>
                    </table></td>
                  </tr>
                  <!-- search -->
                  <tr>
                    <td width="100%"><table border="0" cellpadding="0" cellspacing="0" width="100%">
                        <tbody>
                          <tr>
                            <td align="right"><table border="0" cellpadding="0" cellspacing="0" width="100%">
                                <tbody>
                                  <tr>
                                   
                                    <td height="30" align="right">&nbsp;</td>
                                  </tr>
                                  <tr>
                                 
                                    <td height="30" align="right"><form action="orders.php" method="get" name="f1" id="f1" >
                                          Status: <select style="height:18px; font-size:10px;" name="sortorder" onchange="submit();" >
                                            <option value="0" <?php if($inorder==0) echo "selected"; ?> >-Null-</option>
                                            <option value="1" <?php if($inorder==1) echo "selected"; ?> >Not Verified</option>
                                            <option value="2" <?php if($inorder==2) echo "selected"; ?> >Paid</option>
                                            <option value="3" <?php if($inorder==3) echo "selected"; ?> >Shipped</option>
                                            <option value="4" <?php if($inorder==4) echo "selected"; ?> >Delivered</option>
                                            <option value="5" <?php if($inorder==5) echo "selected"; ?> >Cancelled</option>
                                          </select>
                                      </form>
									  
									  
									  
									  
									  
									  </td>
                                  </tr>
                                </tbody>
                            </table></td>
                          </tr>
                        </tbody>
                    </table></td>
                  </tr>
                </table></td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
              <tr>
                <td colspan="2" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td width="65%" valign="top" style="padding-right:1px;"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td height="35" colspan="3">
						
						
						<table border="0" cellpadding="2" cellspacing="0" width="100%">
                            <tr class="dataTableHeadingRow">
                                <td width="48" align="center" bgcolor="#D7D6CC" class="dataTableHeadingContent">ID</td>
                                <td width="213" align="left" bgcolor="#D7D6CC" class="dataTableHeadingContent">User Name</td>
                                <td width="1" align="left" bgcolor="#D7D6CC" class="dataTableHeadingContent">&nbsp;</td>
                                <td width="92" align="left" bgcolor="#D7D6CC" class="dataTableHeadingContent">Order Total  	</td>
                                <td width="1" align="right" bgcolor="#D7D6CC" class="dataTableHeadingContent">&nbsp;</td>
                                <td width="307" align="center" bgcolor="#D7D6CC" class="dataTableHeadingContent">Date Purchased&nbsp;&nbsp;&nbsp;</td>
                                <td width="132" align="left" bgcolor="#D7D6CC" class="dataTableHeadingContent">Status</td>
                                <td width="1" align="right" bgcolor="#D7D6CC" class="dataTableHeadingContent">&nbsp;</td>
                                <td width="45" align="center" bgcolor="#D7D6CC" class="dataTableHeadingContent">&nbsp;</td>
                                <td width="28" align="left" bgcolor="#D7D6CC" class="dataTableHeadingContent">&nbsp;</td>
                                <td width="48" align="left" bgcolor="#D7D6CC" class="dataTableHeadingContent">Actions</td>
                                <td colspan="2" align="center" bgcolor="#D7D6CC" class="dataTableHeadingContent">&nbsp;</td>
                                </tr>
                          
			                          <?php

				$tbl_name="tblcart";		//your table name
	// How many adjacent pages should be shown on each side?
	$adjacents = 2;
	
	/* 
	   First get total number of rows in data table. 
	   If you have a WHERE clause in your query, make sure you mirror it here.
	*/
$query = " Select count(*) as num from register as a, tblcart as b where a.user_id=b.USERID and b.status like ".$inorder." order by CARTID";
	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages[num];
	
	/* Setup vars for query. */
	$targetpage = "orders.php?sortorder=".$_GET[sortorder]; 	//your file name  (the name of this file)
	$limit = 5; 								//how many items to show per page
	$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;								//if no page var is given, set start to 0
	
	/* Get data. */
	$sql = "Select a.Fname,a.Lname,b.status,b.CARTID,b.USERID,b.GRANDTOTAL,b.time from register as a,tblcart as b where a.user_id=b.USERID and b.status like ".$inorder." order by CARTID desc LIMIT $start, $limit";
	$catres = mysql_query($sql);
	
	/* Setup page vars for display. */
	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
	$prev = $page - 1;							//previous page is page - 1
	$next = $page + 1;							//next page is page + 1
	$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
	$lpm1 = $lastpage - 1;						//last page minus 1
	
	/* 
		Now we apply our rules and draw the pagination object. 
		We're actually saving the code to a variable in case we want to draw it more than once.
	*/
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\">";
		//previous button
		if ($page > 1) 
			$pagination.= "<a href=\"$targetpage&page=$prev\">&laquo; previous</a>";
		else
			$pagination.= "<span class=\"disabled\">&laquo; previous</span>";	
		
		//pages	
		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";					
			}
		}
		elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
		{
			//close to beginning; only hide later pages
			if($page < 1 + ($adjacents * 2))		
			{
				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage&page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage&page=$lastpage\">$lastpage</a>";		
			}
			//in middle; hide some front and some back
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				$pagination.= "<a href=\"$targetpage&page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage&page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage&page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage&page=$lastpage\">$lastpage</a>";		
			}
			//close to end; only hide early pages
			else
			{
				$pagination.= "<a href=\"$targetpage&page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage&page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";					
				}
			}
		}
		
		//next button
		if ($page < $counter - 1) 
			$pagination.= "<a href=\"$targetpage&page=$next\">next &raquo;</a>";
		else
			$pagination.= "<span class=\"disabled\">next &raquo;</span>";
		$pagination.= "</div>\n";		

	}
 
										   
			while($catrow=mysql_fetch_array($catres))
				 {
				 
														    ?>
						  
						      <tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)">
                                <td width="48" height="28" align="center" class="dataTableContent">
								<? echo $catrow['CARTID'];?> <a href="orderdetails.php?cartid=<?php echo $catrow[3] ?>"> <img src="images/folder.gif" width="16" height="16" border="0" /></a>&nbsp;								</td>
                                <td align="left" class="dataTableHeadingContent" style="padding-left:15px;">
								<?php echo ucfirst($catrow[0])." ".$catrow[1]; ?>&nbsp;</td>
                                <td align="center"  class="dataTableHeadingContent">&nbsp;</td>
                                <td colspan="2" class="dataTableContent" align="left"><?php echo $catrow['GRANDTOTAL']; ?></td>
                                <td class="dataTableContent" align="center"><?php echo $catrow['time']; ?></td>
                                
						<?php if($_GET['editstatus']=="yes" && $_GET['cartid']==$catrow[3]) {?>
                                      <form action="orders.php" method="post" name="cartform<?php echo $catrow[3]; ?>" id="cartform<?php echo $catrow[3]; ?>">
                                        <td class="dataTableContent" align="center" style="padding-left:10px;" ><input type="hidden" value="<?php echo $catrow[3]; ?>" name="hidcartid" />
                                            <select onchange="cartform<?php echo $catrow[3]; ?>.submit();" style="height:18px; font-size:10px; text-decoration:none" name="statuschange" >
                                              <option value="1" <?php if($catrow[2]==1) {?>selected="selected" <?php }?> >Not Verified</option>
                                              <option value="2" <?php if($catrow[2]==2) {?>selected="selected" <?php }?> >Paid</option>
                                              <option value="3" <?php if($catrow[2]==3) {?>selected="selected" <?php }?> >Shipped</option>
                                              <option value="4" <?php if($catrow[2]==4) {?>selected="selected" <?php }?> >Delevered</option>
                                              <option value="5" <?php if($catrow[2]==5) {?>selected="selected" <?php }?> >Cancelled</option>
                                            </select>                                        </td>
                                      </form>
                                      <?php } else {
												 if($inorder=="'%'")
				   									 $inorder="kk";
												 ?>
                                      <td colspan="2" align="left"   style="padding-left:10px;" ><a class="dataTableContent" href="orders.php?sortorder=<?php echo $inorder; ?>&amp;page=<?php echo $_GET['page']; ?>&amp;cartid=<?php echo $catrow[3]; ?>&amp;editstatus=yes" >
                                        <?php if($catrow[2]==1)  echo "Not Verified"; if($catrow[2]==2)  echo "Paid"; if($catrow[2]==3)  echo "Shipped"; if($catrow[2]==4)  echo "delivered"; if($catrow[2]==5)  echo "Cancelled"; if($catrow[2]==0)  echo "-NULL-";}?>
                                      </a></td>
                                <td colspan="2" align="left" class="dataTableContent"><a href="deleteorders.php?cartid=<?php echo $catrow['CARTID'] ?>" onclick="return checkd();" ></a>&nbsp;</td>
                                <td width="14" align="center" class="dataTableContent"><a href="deleteorders.php?cartid=<?php echo $catrow['CARTID'] ?>" onclick="return checkd();" ><img src="images/icon_delete.gif" alt="Delete user" width="14" height="15" border="0" /></a></td>
                                <td width="14" align="center" class="dataTableContent">&nbsp;</td>
                                </tr>
							 <? } ?>
							<!---->  
                        </table>						</td>
                      </tr>
                      <tr>
                        <td width="22%" height="35">&nbsp;</td>
                        <td width="16%" align="center">
						<a href="addcategory.php?addcat=addcat"></a></td>
                        <td align="center"><?=$pagination?></td>
                        </tr>
                    </table></td>
                    </tr>
                </table></td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>

          <tr>
            <td><? require_once("footer.php"); ?></td>
          </tr>
          <tr>
            <td>&nbsp; </td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>
