<?php
include("include/config.php");
include("include/protect.class.php");
include("include/jdf.php");
include('include/functions.php');
include("fckeditor/fckeditor.php") ;
include("include/thumbnail_create.php");

$prot=new protect();
if ($prot->showPage) {
$db=new DB($globalConfig['dbase'], $globalConfig['dbhost'],$globalConfig['dbuser'], $globalConfig['dbpass']);
mysql_query("SET NAMES utf8");

if($_GET['status']){$where='where status ='.$_GET['status'].'';}


$page = $_GET['page'];
$limit = $_GET['limit'];
if (!($limit)){
$limit = 50;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
$numresults = $db->query("select * from orders $where  "); // the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
if ($numrows == 0){
$errornum = 1;// bah, modify the "Not Found" error for your needs.
}

$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows%$limit) {
$pages++;} // has remainder so add one page

$current = ($page/$limit) + 1; // Current page number.

if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
 
else{
$last = $numrows;} // If last results page, last result equals total number of results.

//escape from PHP mode.


?>
<!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=utf-8" />
<title>سفارشات</title>

<?php include("maintemplate/mainheader.php");?>
<SCRIPT LANGUAGE="JavaScript">
function checkAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
}
//  End -->
</script>
</head>
<body>
<div id="wpwrap">
<div id="wpcontent">
	<?php include("include/menutop.php");?> 
<div id="wpbody">
	<?php include("include/header.php");?>
    <div style="overflow:hidden;" id="wpbody-content">
        <div id="screen-meta">
            <div class="wrap">
                <div id="icon-index" class="icon32"><br></div>
                <h2>صفحه اصلی</h2>
                <div id="dashboard-widgets-wrap">
                    <div id="dashboard-widgets" class="metabox-holder">
                        
                        
                        
                         <div class="postbox-container" style="width:98%;">
                                <div id="normal-sortables" class="meta-box-sortables ui-sortable"><div id="dashboard_recent_comments" class="postbox ">
                                   <div class="handlediv" title="برای جانشینی کلیک کنید"><br></div><h3 class="hndle"><span> صفحه اصلی</span></h3>
                                <div style="padding:10px; line-height:24px">
                             <div align="center" >
                             <?php if(isset($_GET['add'])){
								 $db->execute("update  orders set 
														descriptionuser='".$_POST['description']."'
													     where order_id = '".(int)$_GET['add']."'
														");
														
							echo debug_message('توضیحات با موفقیت افزوده شد','2','');
								 }?>
                             <?php if($errornum=='1'){
								 echo 'نتیجه ای یافت نشد';
								 }?>


<?php
if(isset($_GET['act'])){
$action = $_GET['act'];}
switch($action){
case 'details':

?>
<p><b>سفارش شماره <?php echo $_GET['Oid'] ;?></b></p>
<a href="javascript:history.go(-1)"><b>بازگشت به لیست سفارشات</b></a>

<table id="ver-zebra" dir="rtl">


    <colgroup>
    	<col class="vzebra-odd" />
    	<col class="vzebra-even" />
    	<col class="vzebra-odd" />
        <col class="vzebra-even" />
    	<col class="vzebra-odd" />
    	<col class="vzebra-even" />
    </colgroup>
    <thead>
    
        <tr>
        <td colspan="6">مشخصات کاربر :</td>
        </tr>
        
    </thead>
    <tbody>    
    
    
    
<?php
$sql1 = $db -> query("select  * from orders where order_id = '".(int)$_GET['Oid']."' ");
$res1 = $db -> fetchNextArray($sql1);
	$euser=$db->query("select * from u_users where ID='".$res1['customerID']."'");
	$user=$db->fetchNextArray($euser);
?>
	<tr>
		<td align="right">نام کاربر : <?=$res1['customerName']?></td>
    </tr> <tr>   
		<td align="right">ایمیل : <?=$user['email']?></td>
    </tr> <tr>    
		<td align="right">آدرس : <?=$res1['customerAddress']?></td>
    </tr>   <tr>  
		<td align="right">موبایل : <?=$res1['customerMobile']?></td>
    </tr>  <tr>  
		<td align="right">توضیحات : <?=$res1['description']?></td>
    </tr>
    <tr>  
		<td align="right">کدپستی : <?=$res1['postalcode']?></td>
    </tr>
      <tr>
      		<td align="right">تاریخ سفارش : <?=tep_hejri2($res1['dateOrder'])?></td>
     </tr>

</tbody>

</table>



<table id="ver-zebra" dir="rtl">
    <colgroup>
    	<col class="vzebra-odd" />
    	<col class="vzebra-even" />
    	<col class="vzebra-odd" />
    	<col class="vzebra-even" />
    </colgroup>
    <thead>
        <tr>
        <td colspan="5" >جزئیات سفارش  :</td>
        </tr>    
    
    	<tr>
        	<th scope="col" id="vzebra-comedy">نام محصول</th>
            <th scope="col" id="vzebra-adventure">تعداد</th>
            <th scope="col" id="vzebra-comedy">قیمت واحد </th>
            <th scope="col" id="vzebra-adventure">قیمت ( تومان )</th>
        </tr>
    </thead>
    <tbody>    

<?php
$All_price=0;
$sql = $db -> query("select * from orders_products where orders_id = '".(int)$_GET['Oid']."'  ");
while($res = $db -> fetchNextArray($sql)){
	$price = $res['products_price'] * $res['productsCount'];

	   

		   
	   
?>	
	<tr>
		<td align="center"><?=$res['products_name']?></td>
		<td align="center"><?=$res['productsCount']?> </td>
		<td align="center"><?=Convertnumber2farsi(number_format($res['products_price']))?> </td>
		<td align="center"><?='<b>'.Convertnumber2farsi(number_format($price)).'<b>'?></td>
	</tr>
<?php
$All_price =$price+$All_price;
}
?>	
</tbody>
<tfoot>
<tr>
<td colspan="5" >جمع هزینه   : <?='<b>'.Convertnumber2farsi(number_format($All_price)).'</b>'?> تومان</td>
</tr>
</tfoot>
</table>


<?php
break;
case 'del':
$sql = $db -> query("select status from orders where order_id = '".$_GET['Oid']."' ");
$res = $db -> fetchNextArray($sql);
$sql_del_orders = $db -> query("delete from orders where order_id = '".$_GET['Oid']."'");
echo 'شما با موفقیت حذف کردید';

break;


case 'return_status':
//echo $_POST['type_pay'];
$sql=$db->query("select * from orders where order_id='".(int)$_POST['oID_radio']."' ");
$row=$db->fetchNextArray($sql);


					$db->execute("update  orders set 
														status='".$_POST['status']."'
													     where order_id = '".(int)$_POST['oID_radio']."'
														");
				      echo debug_message('سرویس با موفقیت به وضعیت ( '.get_status_orders($_POST['status']).' ) تغییر کرد ','2','');
					
					
                print'<br/><a href="javascript:history.go(-1)"><b>بازگشت</b></a><br/>';
				?>
                <form action="services.php?add=<?php echo (int)$_POST['oID_radio'];?>" method="post" >

                <tr class="even" valign="top">
<th scope="row"><label for="description">توضیحات</label></th>
<td><textarea name="description" id="description" cols="50" rows="2"></textarea>
</td>
</tr>
<input name="process" class="button-primary" value="ذخیره" type="submit">
</form>
<?php 
break;

default:
?> 
<p dir="rtl"><b>لیست سفارشات :</b></p>
<?php
echo '<div align="center" style="border-bottom:1px dashed #036; direction:ltr;  width="500px" >' ;
 if ($page != 0) {// Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<table><tr><td><a  href=\"$PHP_SELF?page=$back_page&limit=$limit\" class=\"pagefirst\"></a>    </td>");
 }else{
 echo("<table><tr><td><a  href=\"\" class=\"pagefirst\"></a>    </td>");
 }
 
 
for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
 $ppage = $limit*($i - 1);
 if ($ppage == $page){
 echo("<td ><div class=\"pagenumtext\"> <b >".Convertnumber2farsi($i)."</b> صفحه </div></td>");} // If current page don't give link, just text.
 else{
 echo("<td><a  href=\"$PHP_SELF?page=$ppage&limit=$limit\" class=\"pagenum\"  >".Convertnumber2farsi($i)."</a></td>");}
}

if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo("<td><a  href=\"$PHP_SELF?page=$next_page&limit=$limit\"class=\"pagelast\"></a></td></tr></table>");
}else{
echo("<td><a  href=\"\"class=\"pagelast\"></a></td></tr></table>");
}

echo "</div>";


?>


<form method="post" action="services.php?act=return_status&status=<?=$_GET['status']?>" name="myform">

<table align="center"><tr>
<td><input type="radio" value="2"   name="status" />: در انتظار  |</td>

<td><input type="radio" value="1"   name="status" />: لغو  |</td>

<td><input type="radio" value="3"    name="status" />: در حال پردازش  |</td>

<td> <input type="radio" value="5"    name="status" />: ارسال شده |</td>

<td> <input type="radio" value="6"    name="status" />: تحویل شده </td>
<tr><td  colspan="5" align="center"> <input  type="submit" value="ارسال"  class="art-button"  /></td>
</tr></table>


<table id="rounded-corner"dir="rtl">

    <thead>
    	<tr>
        	<th scope="col" id="rounded-q5">کد</th>
            <th scope="col" id="rounded-q3"> كاربر </th>
            <th scope="col" id="rounded-q5">تاریخ</th>
            <th scope="col" id="rounded-q3">مبلغ </th>
            <th scope="col" id="rounded-q3">وضعیت</th>
            <th scope="col" id="rounded-q3">پرداخت</th>
            <th scope="col" id="rounded-q5">امکانات</th>
        </tr>
    </thead>
    <tbody>
<?php
$sql_total = $db->query("select * from orders  $where order by dateOrder desc LIMIT $page, $limit  ");
$j=1;
while($res_total = $db->fetchNextArray($sql_total)){
$addPrice = 0;



	$oID = $res_total['order_id'];
	if($res_total['payment']){$payment = $res_total['payment'] ;}
	$factNum = intval('1000') + $res_total['order_id'];

	$date = $res_total['dateOrder'];
	$time = substr($res_total['dateOrder'],10);
	

	
	echo '<tr>
	
			<td>'.$factNum.'</td>
			<td>'.$res_total['customerName'].'<br />'.$res_total['customerEmail'].'</td>
			<td  style="font-size:11px">'.tep_hejri1($date).'<br>'.$time.'</td>
			<td>'.number_format($res_total['priceFinal']) .'</td>';
	echo 	'<td style="font-size:10px">'.get_status_orders($res_total['status']).'<br/>'.$res_total['descriptionuser'].'</td>';
	
	echo 	'<td style="font-size:10px">'.stateEpaye($oID).'</td>';

	echo 	'<td width="60px">
	              <a href="services.php?act=details&Oid='.$oID.'&status='.$_GET['status'].'"><img src="images/locate.png" title="جزییات سفارش"></a>
	              <a href="services.php?act=del&Oid='.$oID.'&status='.$_GET['status'].'"><img src="images/icon_err.gif" title="حذف"></a>
	              <input type="radio" value="'.$oID.'" name="oID_radio" id="list"  /> 
	        </td>';
	
	//}
	echo '</tr>';

 echo '<input type="hidden" value="'.$j.'" name="j">';


 $j=$j+1;
}
?>

</tbody><tbody></tbody></table>

<table align="center" dir="rtl"><tr>
<td><input type="radio" value="2"   name="status" />: در انتظار  |</td>

<td><input type="radio" value="1"   name="status" />: لغو  |</td>

<td><input type="radio" value="3"    name="status" />: در حال پردازش  |</td>

<td> <input type="radio" value="5"    name="status" />: ارسال شده |</td>

<td> <input type="radio" value="6"    name="status" />: تحویل شده </td>

<tr><td  colspan="5" align="center"> <input  type="submit" value="ارسال"  class="art-button"  /></td>
</tr></table>
</form>


<?php



break;
}
?>
                                              
                                                </div>
                             
                               
                                
                                </div>

                              </div>
                            </div>
                        </div>  
                    </div>
                    <div class="clear"></div>
                </div><!-- dashboard-widgets-wrap -->
            </div><!-- wrap -->
			<div class="clear"></div></div><!-- wpbody-content -->
			<div class="clear"></div></div><!-- wpbody -->
			<div class="clear"></div></div><!-- wpcontent -->
        </div>
    </div><!-- wpcontent -->
</div><!-- wpwrap -->
<?php include("include/footer.php");?>
</div>
</div>
</body>
</html><?php } ?>