<?php
session_start();
include('../../../../include/config_animal.php');


   $name=$_POST['name'];
   $username=$_POST['username'];
   $password=$_POST['password'];
   $aadhar=$_POST['aadhar'];
   $forest_id=$_POST['forest_id'];
   $phone=$_POST['phone'];
   $email=$_POST['email'];
   $lat=$_POST['lat'];
   $longi=$_POST['longi'];
   $radius=$_POST['radius'];

   $mpin=$_POST['mpin'];
  
  if(!empty($_FILES['userImage']['name'])){

   $image=$_FILES['userImage']['name'];
   }
   else{
      $image='';
   }



   $last='';
   $query2=mysqli_query($conn,"SELECT * FROM rfo WHERE rfo_id=(SELECT MAX(rfo_id) FROM rfo) ");
    while($row1=mysqli_fetch_assoc($query2)){
        $last=$row1['rfo_id'];
    }                                                   
   $last++;
   $rfo_id = sprintf('%05d', $last);


 // Insert record
  $query = "INSERT INTO rfo (rfo_id,username,password,name,phone,aadhar,forest_id,email,lat,longi,radius,status,image,mpin) 
    VALUES ('$rfo_id', '$username', '$password', '$name', '$phone', '$aadhar', '$forest_id', '$email', '$lat', '$longi', '$radius', 'Active', '$image','$mpin')";

 if(mysqli_query($con,$query)){	


   if (is_array($_FILES)) {
    if (is_uploaded_file($_FILES['userImage']['tmp_name'])) {
        $sourcePath = $_FILES['userImage']['tmp_name'];
        $targetPath = "../../../../API/RFO Images/" . $_FILES['userImage']['name'];
        if (move_uploaded_file($sourcePath, $targetPath)) {
         
         $image=$_FILES['userImage']['name'];
         


        }
    }
       else{
               echo 'no_image';
            }
}


echo 'Success';

			}

		else{
			echo 'error';

		}






