<?php


header('Access-Control-Allow-Origin: *');


header('Content-Type: application/json');





$url="https://".$_SERVER['HTTP_HOST'].":443/api/agritun_recolte/all?filter=&field=&start=&limit=&filters[0][co][4][fl]=id_parcel&filters[0][co][4][op]=equal&filters[0][co][4][vl]=".$_GET['id_parcel']."&filters[0][co][4][lg]=and&sort_field=&sort_order=ASC";





$curl = curl_init($url);


curl_setopt($curl, CURLOPT_URL, $url);


curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);





$headers = array(


   "X-Api-Key: ".$_GET['X-Api-Key'],


   "X-Token: ".$_GET['X-Token'],


);


curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);


//for debug only!


curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);


curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);





$resp = curl_exec($curl);


curl_close($curl);





$re= json_decode($resp, true);



$total=0;
$cout=0;
$hay_chkara=0;
$nchira_chkara=0;
$hay_kg=0;
$hay="";
$nchira_kg=0;
$nchira="";

$tot=$re['total']; 








for ($i=0;$i<$tot;$i++){


$total += $re['data']['agritun_recolte'][$i]['nbr_pieds']; 


$cout += $re['data']['agritun_recolte'][$i]['cout']; 





	if ($re['data']['agritun_recolte'][$i]['q_unite']=="كغ"){


		$hay_kg += $re['data']['agritun_recolte'][$i]['hay']; 


		$nchira_kg += $re['data']['agritun_recolte'][$i]['nchira']; 


	}


	if ($re['data']['agritun_recolte'][$i]['q_unite']=="شكارة"){


		$hay_chkara += $re['data']['agritun_recolte'][$i]['hay']; 


		$nchira_chkara += $re['data']['agritun_recolte'][$i]['nchira']; 


	}


	


}


if ($hay_kg > 0){


	$hay .= $hay_kg." <i style=' color: grey;font-size: 14px; '> كغ </i> ";


}


if ($hay_chkara > 0){


	if ($hay_kg > 0){


		$hay .= "  <i style=' color: grey;font-size: 14px; '> و </i>  ";


	}


	$hay .= $hay_chkara." <i style=' color: grey;font-size: 14px; '> شكارة </i> ";


}





if ($nchira_kg > 0){


	$nchira .= $nchira_kg." <i style=' color: grey;font-size: 14px; '> كغ </i> ";


}


if ($hay_chkara > 0){


	if ($nchira_kg > 0){


		$nchira .= "  <i style=' color: grey;font-size: 14px; '> و </i>  ";


	}


	$nchira .= $nchira_chkara." <i style=' color: grey;font-size: 14px; '> شكارة </i> ";


}











echo '{"total":"'.$total.'","hay":"'.$hay.'","nchira":"'.$nchira.'","cout":"'.$cout.'"}';


?>