<?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) {
    $datas = findQuery('SELECT * FROM tbl_challenge WHERE status=0 order by id desc limit 1');
}





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






    $data = findQuery("SELECT  tbl_user.name,tbl_user.id,tbl_ward.ward_name,tbl_user.username, sum(tbl_transaction.Received) Received  FROM  tbl_transaction 
inner join tbl_transactiondetails on tbl_transactiondetails.hdrid=tbl_transaction.id
inner join tbl_user on tbl_user.id=tbl_transaction.volunteer left join tbl_ward on tbl_ward.id=tbl_user.ward_id
    where tbl_transaction.challengeid=$id  and tbl_transaction.Received>0   group by tbl_user.name,tbl_user.id,tbl_ward.ward_name,tbl_user.username order by Received desc");



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



'name'            => $data[$i]['name'],
'amount'          => $data[$i]['Received'],
'ward'            => $data[$i]['ward_name'],
'volunteernumber' => $data[$i]['username'],

];
        }
    }
} else {
    $message = 'false';
    $data2[] = ['error' => 'Not Found'];
}


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

return;

?>

