<?php  include_once('../../config/db.php');  //include_once('../config/check.php'); 






$query = "   SELECT a.*,b.name bname,c.name cname,d.fname dfname,d.lname dlname,e.name ename,
             g.name gname,h.name hname    FROM orders a
             LEFT JOIN clients b ON a.client_id=b.id
             LEFT JOIN groups c ON a.group_id=c.id
             LEFT JOIN sys_user d ON a.uid=d.id
             LEFT JOIN orderstatus e ON a.status=e.id 
             LEFT JOIN branches g on a.branch_id=g.id
             LEFT JOIN ordersdeliverymethod h ON a.delivery_method =h.id

             ORDER BY a.id desc 

              ";





$statement = $conn->prepare($query);
$statement->execute();
$number_filter_row = $statement->rowCount();

$result = $statement->fetchAll();

$data = array();



foreach($result as $row)
{
  
          $currentid = $row["id"] ;

          $query = "   SELECT a.product_id,a.quantity,b.name pname  FROM order_products a 
                  LEFT JOIN products b ON a.product_id=b.id
                  where a.trans_id = '$currentid'  ";
          $statement = $conn->prepare($query);
          $statement->execute();
          $products  = $statement->fetchAll();
//           foreach ($products as $product){

// $plist = '<span class="label label-success label-sm"> '.$product["pname"].' </span>';

//           }


 $actionButton = '<input type="radio" name="myradio" class="myradio" value="' . $row["id"] . '" /> </i></a>';
 //$client = ' <p> '.$row["bname"].' <br> '.$row["client_loan_balance"].'  </p> ' ; 
 $approve = ' <form method="post" action="apporder" >  <button type="submit" style="background:none;border:none;padding:0px; outline:none; " name="approve" value="' . $row["id"] . '"   "> <i class="fa fa-check-circle fa-lg" style="color:green;" ></i>  </button></form> ' ; 
 $edit = ' <form method="post" action="#" >  <button type="submit" style="background:none;border:none;padding:0px; outline:none; " name="edit" value="' . $row["id"] . '"   "> <i class="fa fa-edit" style="color:green;" ></i>  </button></form> ' ; 
 $delete = ' <form method="post" action="" >  <button type="submit" style="background:none;border:none;padding:0px; font-color:red; outline:none; " name="delete" value="' . $row["id"] . '"   "> <i class="fa fa-trash" style="color:red;" ></i>  </button></form> ' ; 
 $status = '<h5><span class="label label-success label-sm"> '.$row["ename"].' </span>  </h5>' ; 
  
// $action = '<span><form method="post" action="#" >  <button type="submit" style="background:none;border:none;padding:0px; outline:none; " name="edit" value="' . $row["id"] . '"   "> <i class="fa fa-edit" style="color:green;" ></i>  </button></form></span><span><form method="post" action="" >  <button type="submit" style="background:none;border:none;padding:0px; font-color:red; outline:none; " name="delete" value="' . $row["id"] . '"   "> <i class="fa fa-trash" style="color:red;" ></i>  </button></form></span> ' ; 
// $action = '<span> <form method="post" action="#" ><i class="fa fa-trash"></i> </form> </span><span> <i class="fa fa-trash"></i> </span> ' ; 


 $sub_array = array();
 $sub_array[] =  $approve  ;

  $sub_array[] = $row['ename'];
  $sub_array[] = $row['gname'] ;

 $sub_array[] = $row['cname'];
  $sub_array[] = $row['bname'] ;
  $sub_array[] =  number_format($row['total_price'], 2, '.', ','); 
 //$sub_array[] = $row['total_price'];
 $sub_array[] = $row['hname'];
 $sub_array[] = $row['delivery_location'];

 foreach ($products as $product)
     {
         // $plist = '<span> '.$product["pname"].' </span>';
    // $sub_array[] = $product["pname"];
      $sub_array[] = '<span>'.$product["pname"].' x'.''. number_format($product["quantity"], 0).' </span>';
   }
 
 
// $sub_array[] = $row['dfname'].' '.$row['dlname'];

  foreach ($products as $product)
     {
         // $plist = '<span> '.$product["pname"].' </span>';
    // $sub_array[] = $product["pname"];
      $sub_array[] = '<span> '.$product["pname"].' </span>';
   }
 //$sub_array[] =  $action  ;
 
//  $sub_array[] =  $edit  ;
//  $sub_array[] =  $delete ;

 $data[] = $sub_array;

}

function count_all_data($conn,$query)
{
 $statement = $conn->prepare($query);
 $statement->execute();
 return $statement->rowCount();
}

$output = array(
                       "draw"       =>  $number_filter_row ,
                        "recordsTotal"   =>  count_all_data($conn,$query),
                        "recordsFiltered"  =>  $number_filter_row,
                        "data"       =>  $data
);

echo json_encode($output);

?>




