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

$redeemedObj =  new  redeemed();
// Logical section of page
// $redeemedObj =  new  redeemed();

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

// $filename=basename($_SERVER['SCRIPT_FILENAME']);
// if($filename == 'redeemedlist.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']=='approveRedeemed' && isset($_REQUEST['id']) && $_REQUEST['id']!=""){
    $redeemedDetails =$redeemedObj->fromId($_REQUEST['id']);
    $redeemedData =array(
        "approved_by"=>$_SESSION['admin_id'],
        "approved_at"=>date("Y-m-d"),
        "externalRefID"=>w::getReferenceId(20,5)
    );
    $rs =$redeemedObj->updateRecords($redeemedData,$_REQUEST['id']);
    $loyalty_actionObj = new loyalty_action();
    if($rs){
        $loyalty_actionData =array(
            "loyalty_action_action"=>"Verified",
        );
        $rs =$loyalty_actionObj->updateRecords($loyalty_actionData,$redeemedDetails->action_id);
        if($rs){
            echo json_encode(array('error'=>false,'msg'=>'Success.'));
            exit;
        }
    }
    exit;
}
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 = " approved_by=0 ";
    // Array of order by 
    $orderby = array(" redeemed_id desc ");
    $tables = array('tbl_loyalty_action l','tbl_dependant d');
    $jfields = array('l.loyalty_action_id = r.action_id','d.dependant_id = r.dependant_id');
    $jtype   = array("INNER","INNER"); 
    $data    = array('where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype, 'orderBy' => $orderby, 'limit' => $limit);
    
    $redeemedDetails = $redeemedObj->getAllRecords($data);
    $recordsTotal   =   $redeemedObj->getAllCount($data);
        
    if ($redeemedDetails && count($redeemedDetails)>0){
        foreach ($redeemedDetails as $key => $redeemed){
            $status = '<a href="javascript:void(0)" title="Approve Redeemed" onclick="approveRedeemed('.$redeemed->redeemed_id.')"><span class="badge badge-success">Approve</span></a>';
            // $status .= ' <a href="javascript:void(0)" title="Decline Redeemed"><span class="badge badge-danger" onclick="declineClaim('.$claim->loyalty_action_id.')">Decline</span></a>';
            
            $response[] = array( 
                w::d($redeemed->dependant_fname)." ".w::d($redeemed->dependant_lname),
                $redeemed->rewardName,
                $redeemed->loyalty_action_rpoints,
                $redeemed->utid,
                $redeemed->faceValue,
                $redeemed->quantity,
                date('Y-m-d',strtotime($redeemed->created_at)),
                $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');
?>