<?php

error_reporting(0);
include('header.php');
include("db_con.php");
include("qr_code_url.php");
include("login_check_admin.php");
session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add Engineer</title>
</head>
<body>


<div class="container">
<!-- <button type="button" class="btn btn-primary btn-lg btn-block">Reports</button> -->



<form class = "border" action="admin_add_engineer.php" enctype="multipart/form-data" method="post" id="form">


<table class="table table-borderless text-center">

  <tr>
    <thead class="text-center">
      <th colspan="2" >Add Engineer</th>
      
    </thead>
   </tr>
   <tr>
   <td colspan="2"><span style="display:none"id="user_id_already_exist_error" class="text-danger text-center"><div class="alert alert-danger py-0 mt-2 mb-0" role="alert">Given First Name and Last Name are already exist</div></span></td>
   </tr>

    <tr>
    
      <th>Engineer Name</th>
      <td><input type="text" class="form-control" name="engineer_name" id=""></td>
    </tr>
    <tr>
      <th>Passcode</th>
      <td><input type="number" class="form-control" name="passcode" id=""></td>
    </tr>
   
    <!-- <tr>
      <th>Image</th>
      <td><input type="file" name="equipment_image" class="form-control" accept="image/*"  required></td>
    </tr> -->

    
    <tr>
    <!-- <td colspan=""><input type="submit" style="display:block" class="btn btn-danger" name="delete" value="Delete" required ></td> -->
    <td colspan="2" align="center"><input type="submit" style="display:block" class="btn btn-primary" name="submit" value="Save" required ></td>
</tr>
</table>
    <!-- <tr>
      <input type="submit" class="form-control btn btn-primary" name="submit" value="Save">
      <input type="submit" class="form-control btn btn-danger" name="delete" value="Delete">
    </tr> -->
</form>
</div>

</body>
</html>

<?php

if(isset($_POST["submit"]))
{
    $engineer_name= $_POST["engineer_name"];
    $passcode= $_POST["passcode"];
   
    // $PictureName=$_FILES["equipment_image"]["name"];
    // $PicturetemporaryName=$_FILES["equipment_image"]["tmp_name"];        
    
    // echo "image name=".$PictureName;


    $Qry5="SELECT * FROM `engineer` WHERE `engineer_name` = '$engineer_name'"; 
    $Run5=mysqli_query($con,$Qry5);
    if(mysqli_num_rows($Run5)<1)
    {

      $Qry2="INSERT INTO `engineer`(`engineer_name`, `passcode`) VALUES ('$engineer_name', '$passcode')";
      $Run2 = mysqli_query($con,$Qry2);
      if($Run2==true)
      {
        $engineer_id = mysqli_insert_id($con);

        // $location = "images/equipment/".$equipment_id;
        // if(!is_dir($location))
        // {
        //   mkdir($location, 0755);     
         
        // }
        // move_uploaded_file($PicturetemporaryName,"images/equipment/".$equipment_id."/".$PictureName);
        
        // require_once 'phpqrcode/qrlib.php';
        // QRcode::png($qr_code_url."?equipment_id=".$equipment_id, "images/equipment/".$equipment_id."/qr_code_".$equipment_id.".jpg");
      

          ?>
          <script>
              //alert("Image Uploaded successfully");     

              if ( window.history.replaceState ) 
              {
                window.history.replaceState( null, null, window.location.href );
              }
              //location.href = "admin_add_engineer.php";
          </script>
          <?php
          unset($_POST["submit"]);

      }
      else
      {
          ?>
          <script>
            alert("Engineer registration failed");
          </script>
          <?php
      }
    }
    else
    {
      echo "<script>$('#user_id_already_exist_error').css('display','inline');</script>";
          
          exit();
    }

}
unset($_POST["submit"]);   


?>