<?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');

extract($_POST);
$data                = $_REQUEST;
$id                  = $_REQUEST['id'];
$challenge_startdate = date('Y-m-d');


$data = findQuery("select * from  `tbl_challenge` where '$challenge_startdate'  between `challenge_startdate` and `challenge_enddate` and status=0");

$datae = findQuery("SELECT tbl_transactiondetails.item,tbl_transactiondetails.qty,tbl_transactiondetails.rate FROM `tbl_transaction` 
inner join tbl_transactiondetails on tbl_transaction.id=tbl_transactiondetails.hdrid where tbl_transactiondetails.hdrid=$id order by tbl_transactiondetails.item ");

$prod1 = 0;
$prod2 = 0;
$prod3 = 0;


if ($datae) {
    if ($datae[0]['item']) {
        if ($datae[0]['item'] == 1) {
            $prod1 = 1;
            $qty1  = $datae[0]['qty'];
        } elseif ($datae[0]['item'] == 2) {
            $prod2 = 2;
            $qty2  = $datae[0]['qty'];
        } else {
            $prod3 = 3;
            $qty3  = $datae[0]['qty'];
        }
    }

    if ($datae[1]['item']) {
        if ($datae[1]['item'] == 1) {
            $prod1 = 1;
            $qty1  = $datae[10]['qty'];
        } elseif ($datae[1]['item'] == 2) {
            $prod2 = 2;
            $qty2  = $datae[1]['qty'];
        } else {
            $prod3 = 3;
            $qty3  = $datae[1]['qty'];
        }
    }

    if ($datae[2]['item']) {
        if ($datae[2]['item'] == 1) {
            $prod1 = 1;
            $qty1  = $datae[2]['qty'];
        } elseif ($datae[2]['item'] == 2) {
            $prod2 = 2;
            $qty2  = $datae[2]['qty'];
        } else {
            $prod3 = 3;
            $qty3  = $datae[2]['qty'];
        }
    }
} else {
    $message = 'false';
    $data2[] = ['error' => 'Not Found'];
    echo json_encode(['Status' => $message, 'data' => $data2]);

    return;
}





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

        $product_name2   = '';
        $product_image2  = '';
        $product_amount2 = '';
        $delivary_date2  = '';

        if ($data[$i]['product_name2']) {
            $product_name2   = $data[$i]['product_name2'];
            $product_image2  = 'https://chcentertirur.in//Challenge/upload/' . $data[$i]['product_image2'];
            $product_amount2 = $data[$i]['product_amount2'];
            $delivary_date2  = date('d-m-Y', strtotime($data[$i]['delivary_date2']));
        }
        $product_name3   = '';
        $product_image3  = '';
        $product_amount3 = '';
        $delivary_date3  = '';

        if ($data[$i]['product_name3']) {
            $product_name3   = $data[$i]['product_name3'];
            $product_image3  = 'https://chcentertirur.in//Challenge/upload/' . $data[$i]['product_image3'];
            $product_amount3 = $data[$i]['product_amount3'];
            $delivary_date3  = date('d-m-Y', strtotime($data[$i]['delivary_date3']));
        }

        $data2[] = [


'prodid1'                 => 1,
'product_name1'           => $data[$i]['product_name1'],
'product_image1'          => 'https://chcentertirur.in//Challenge/upload/' . $data[$i]['product_image1'],
'product_amount1'         => $data[$i]['product_amount1'],
'hasordered1stproduct'    => $prod1 > 0 ? 1 : 0,
'firstorderedproductqty'  => (int) $qty1,
'prodid2'                 => 2,
'product_name2'           => $product_name2,
'product_image2'          => $product_image2,
'product_amount2'         => $product_amount2,
'hasordered2ndproduct'    => $prod2 > 0 ? 1 : 0,
'secondorderedproductqty' => (int) $qty2,
'prodid3'                 => 3,
'product_name3'           => $product_name3,
'product_image3'          => $product_image3,
'product_amount3'         => $product_amount3,
'delivary_date3'          => $delivary_date3,
'hasordered3rdproduct'    => $prod3 > 0 ? 1 : 0,
'thirdorderedproductqty'  => (int) $qty3,










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

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

return;

?>

