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

   
   $rfo_name=$_POST['rfo_name'];
   $rfo_id=$_POST['rfo_id'];
   $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 guard WHERE guard_id=(SELECT MAX(guard_id) FROM guard) ");
    while($row1=mysqli_fetch_assoc($query2)){
        $last=$row1['guard_id'];
    }                                                   
   $last++;
   $guard_id = sprintf('%05d', $last);


 // Insert record
  $query = "INSERT INTO guard (rfo_id,rfo_name,guard_id,username,password,name,phone,aadhar,forest_id,email,lat,longi,radius,status,image,mpin) 
    VALUES ('$rfo_id','$rfo_name','$guard_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/Guard Images/" . $_FILES['userImage']['name'];
        if (move_uploaded_file($sourcePath, $targetPath)) {
         
         $image=$_FILES['userImage']['name'];
         


        }
    }
       else{
               echo 'no_image';
            }
}


echo 'Success';

         }

      else{
         echo 'error';

      }






