<?php 
// Include main file
include_once('../libs/main.php');
// Check tango_order Login
include_once('checkLogin.php');

$tango_orderObj =  new  tango_order();
// Logical section of page
// $tango_orderObj =  new  tango_order();

// $adminlogObj = new admin_log();
// $modulepagesObj = new module_pages();

// $filename=basename($_SERVER['SCRIPT_FILENAME']);
// if($filename == 'tango_orderlist.php')
// {
//     $module_id = $adminlogObj->getModuleId($filename);
//     $page_id = $adminlogObj->getPageId($filename);
//     $allpages = $modulepagesObj->fromId($page_id);
//     $pagename = $allpages->page;

// }
if (isset($_REQUEST['act']) && $_REQUEST['act']=='load'){
    
    $records = $_REQUEST['length'];
    $start=0;
    if(isset($_GET['start']) && $_GET['start']!='0'){
        $page = $_GET['start'];
    }
    $start=($page-1)*$limit;
    $limit = array('start'=>$_GET['start'],'record'=>$records);
    $where = " 1=1 ";

    if(isset($_REQUEST['redeemed_id']) && $_REQUEST['redeemed_id']!=""){
        $where .= " AND redeemed_id='".$_REQUEST['redeemed_id']."' ";    
    }
    
    // Array of order by 
    $orderby = array(" tango_order_id desc ");
    $data    = array('where' => $where, 'orderBy' => $orderby, 'limit' => $limit);
    
    $tango_orderDetails = $tango_orderObj->getAllRecords($data);
    $recordsTotal   =   $tango_orderObj->getAllCount($data);
        
    if ($tango_orderDetails && count($tango_orderDetails)>0){
        foreach ($tango_orderDetails as $key => $tango_order){
            $res  =json_decode($tango_order->response,1);    
            $sender = "";
            foreach ($res['sender'] as $key => $s) {
                $sender .="<b>".$key.":</b> ".$s."<br/>";
            }
            $receiver = "";
            foreach ($res['recipient'] as $key => $s) {
                $receiver .="<b>".$key.":</b> ".$s."<br/>";
            }
           
            $response[] = array( 
                "<b>Reference Order ID:</b> ".$tango_order->referenceOrderID."<br/><b>External Ref ID:</b> ".$tango_order->externalRefID."<br/><b>Purchase Order Number:</b> ".$tango_order->purchaseOrderNumber,
                "<b>Reward Name ID:</b> ".$res['rewardName']."<br/><b>UtID:</b> ".$res['utid'],
                $sender,
                $receiver,
                $tango_order->amount,
                $tango_order->status
            );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

// Include Template 
include_once('../libs/routs.php');
?>