<?php 	
session_start();
require_once '../../includes/conn.php'; 

$sql = "SELECT orders.order_id,orders.user_id,orders.order_date, orders.client_name, orders.client_contact,
 orders.payment_status,orders.pre_due,orders.today_total,orders.grand_total, 
 orders.payment_status,orders.custom_invoice_no ,payment_status.ps_id ,payment_status.ps_name

FROM orders 
left join `payment_status` on payment_status.ps_id=orders.payment_status 
WHERE orders.order_type = 2 and orders.order_status = 1  and orders.user_id ='".$_SESSION['id']."' order by orders.order_id desc ";
$result = $con->query($sql);


$output = array('data' => array());

if($result->num_rows > 0) { 
 
 $paymentStatus = ""; 
 $x = 1;

 while($row = $result->fetch_array()) {
 	$orderId = $row[0];

 	$countOrderItemSql = "SELECT count(*) FROM order_item WHERE order_id = $orderId";
 	$itemCountResult = $con->query($countOrderItemSql);
 	$itemCountRow = $itemCountResult->fetch_row();


 		// active 
		
 	if($row[5] == 1) { 		
 		$paymentStatus = "<span class='badge rounded-pill bg-primary'>$row[12]</span>"; 
 	   } 
else if($row[5] == 2) { 		
 		$paymentStatus = "<span class='badge rounded-pill bg-info'>$row[12]</span>"; 
 	} 
else if($row[5] == 3) { 		
 		$paymentStatus = "<span class='badge rounded-pill bg-success'>$row[12]</span>"; 
 	}	
else if($row[5] == 4) { 		
 		$paymentStatus = "<span class='badge rounded-pill bg-warning'>$row[12]</span>"; 
 	}	
		
else { 		
 		$paymentStatus = "<span class='badge rounded-pill bg-danger'>$row[12]</span>"; 
 	}
	
	// /else
	
	  	

 	$button = '<!-- Single button -->
	
	<center> 
	<a href="#" class="float-left text-primary fw-bold"  onclick="printOrderWithPreSP('.$orderId.')">
                <i class="bi bi-printer"></i>
                <span>Print</span>
              </a>
			   &nbsp;||&nbsp;
			  <a class="float-left text-danger fw-bold" href="orders-retail.php?o=editOrd&i='.$orderId.'" >
                <i class="bi bi-pencil-square"></i>
                <span>Edit</span>
              </a>
			  &nbsp;||&nbsp;
			  <a target="_blank" class="float-left text-success fw-bold" href="product-order-status?o=OrdStatus&i='.$orderId.'" >
               <i class="bi bi-link fw-bold"></i>                 
              </a>
			  &nbsp;||&nbsp;
			 <a  href="removeOrder_parmanently.php" class="float-left text-danger fw-bold" data-bs-toggle="modal" data-bs-target="#removeOrderModal" id="removeOrderModalBtn" onclick="removeOrder('.$orderId.')" >
               <i class="bi bi-trash"></i> 
              </a>
	</center> 
	
	  ';		

 	$output['data'][] = array( 		
 		// image
 		//$x,
		// order Id
 		$row[10],
 		// order date
 		//$row[1],
 		// client name
 		date("d-m-Y ", strtotime($row[2])),
 		// client contact
 		$row[3], 
     // client contact
 	//	$row[4],
		
	// $row[6],	
    // $itemCountRow, 	
    // $row[7],
	 	
$row[8],	 	
 		$paymentStatus,
	 
 		// button
 		$button 		
 		); 	
 	$x++;
 } // /while 

}// if num_rows

$con->close();

echo json_encode($output);

?>