<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type');
require_once('../lib/dbclass.php');





$datas = findQuery('SELECT * FROM tbl_challenge WHERE status=0 AND CURDATE() between challenge_startdate and challenge_enddate');











if ($datas) {
    $id = $datas[0]['id'];

    $message = 'true';



    $data = findQuery("Select 
    tbl_customer.name,Received
    from tbl_transaction
     inner join tbl_customer on tbl_customer.id=tbl_transaction.customerid where  tbl_transaction.challengeid=$id 
      and Received>0  order by Received desc,tbl_transaction.id desc");

    if ($data) {
        for ($i = 0;$i < count($data);$i++) {
            $data2[] = [



'name'   => $data[$i]['name'],
'amount' => $data[$i]['Received'],

];
        }
    }
} else {
    $message = 'true';
    $data    = findQuery('Select 
    tbl_customer.name,Received
    from tbl_transaction
     inner join tbl_customer on tbl_customer.id=tbl_transaction.customerid where tbl_transaction.donation=1  and Received>0  order by Received desc ');

    if ($data) {
        for ($i = 0;$i < count($data);$i++) {
            $data2[] = [



'name'   => $data[$i]['name'],
'amount' => $data[$i]['Received'],

];
        }
    }
}


echo json_encode(['Status' => $message, 'data' => $data2]);

return;

?>

