<?php 

require_once("conn.php");
$data=array();
if (!empty($_POST['image'])  && isset($_POST['userid']) && isset($_POST['seasonid'])  && isset($_POST['grower_num']) && isset($_POST['id'])){


		$growerid=0;
		$image_found=0;
		$seasonid=$_POST['seasonid'];
		$userid=$_POST['userid'];
		$grower_num=$_POST['grower_num'];
		$sqliteid=$_POST['id'];
		$created_at=$_POST['created_at'];
		$hectares=$_POST['hectares'];


		$sql = "Select growers.id from growers  where  grower_num='$grower_num'";
		$result = $conn->query($sql);
		    
		 if ($result->num_rows > 0) {
		   // output data of each row
		   while($row = $result->fetch_assoc()) {

		    // product id
		   
		   $growerid=$row["id"];
		   
		    
		   }

		 }



			 //check farm
		// $sql1 = "Select id from grower_signatures  where  growerid=$growerid and seasonid=$seasonid";
		// $result = $conn->query($sql1);
		 
		//  if ($result->num_rows > 0) {
		//    // output data of each row
		//    while($row = $result->fetch_assoc()) {

		//     // product id
		//    $image_found=$row["id"];
		//   // $growerid=$row["id"];
		   
		    
		//    }

		//  }

		 if ($growerid>0) {


		 	$file_name=$_POST['grower_num'] ."-signature-".time().".jpg";
			$path="../images/".$file_name;

			
				// code...
				$grower_farm_sql = "INSERT INTO grower_signatures_schemes(userid,seasonid,growerid,image_location,created_at,hectares) VALUES ($userid,$seasonid,$growerid,'$file_name','$created_at','$hectares')";
			   //$sql = "select * from login";
			   if ($conn->query($grower_farm_sql)===TRUE) {
			   
			    $last_id = $conn->insert_id;

				if (file_put_contents($path, base64_decode($_POST['image']))) {

				$temp=array("response"=>"success","id"=>$sqliteid);
               	array_push($data,$temp);
               	
                }
				
				}else{
					$temp=array("response"=>$conn->error,"id"=>$sqliteid);
               	array_push($data,$temp);
				}

			

		 }else{
		 	$temp=array("response"=>"Grower not found");
     		array_push($data,$temp);
		 }



	
}else{
	$temp=array("response"=>"field empty");
     array_push($data,$temp);
}


echo json_encode($data);


?>