<?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 Equipment</title>
</head>
<body>

<!-- <nav class="navbar navbar-dark bg-primary navbar-expand-lg">
  <a class="navbar-brand" onclick="history.back()">Back</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse " id="navbarNav">
    <ul class="navbar-nav ml-auto pr-5">
      <li class="nav-item">
        <a class="nav-link active" href="index.php">Home</a>
      </li>
      <li class="nav-item">
        <a class="nav-link active" href="customer_list.php">Customer List</a>
      </li>
     
      <li class="nav-item">
        <a class="nav-link active" href="report.php">Report</a>
      </li>
      <li class="nav-item">
        <a class="nav-link active" href="logout.php">Logout</a>
      </li>
    </ul>
  </div>
</nav> -->




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



<form class = "border" action="admin_add_equipment.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 Equipment</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>Record ID</th>
      <td><input type="text" class="form-control" name="record_id" id=""></td>
    </tr>
    <tr>
      <th>Title</th>
      <td><input type="text" class="form-control" name="title" id=""></td>
    </tr>
    <tr>
      <th>Location</th>
      <td><input type="text" class="form-control" name="location" id=""></td>
    </tr>
    <tr>
      <th>Equipment Type</th>
      <td><input type="text" class="form-control" name="equipment_type" id=""></td>
    </tr>
    <tr>
      <th>Equipment Description</th>
      <td><input type="text" class="form-control" name="equipment_description" id=""></td>
    </tr>
    <tr>
      <th>Notes</th>
      <td><input type="text" class="form-control" name="notes" id=""></td>
    </tr>
    <tr>
      <th>Service Interval</th>
      <!-- <td><input type="text" class="form-control" name="service_interval" id=""></td> -->
      <td><select name="service_interval" id="" class="form-control" required>
    <option hidden disabled selected value> -- select an option -- </option>
    
    <option value="1">1 month</option>
    <option value="2">2 months</option>
    <option value="3">3 months</option>
    <option value="4">4 months</option>
    <option value="5">5 months</option>
    <option value="6">6 months</option>
    <option value="7">7 months</option>
    <option value="8">8 months</option>
    <option value="9">9 months</option>
    <option value="10">10 months</option>
    <option value="11">11 months</option>
    <option value="12">12 months</option>

    
    
  </select>
  </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"]))
{
    $record_id= $_POST["record_id"];
    $title= $_POST["title"];
    $location= $_POST["location"];
    $equipment_type= $_POST["equipment_type"];
    $equipment_description= $_POST["equipment_description"];
    $notes= $_POST["notes"];
    $service_interval= $_POST["service_interval"];

    $PictureName=$_FILES["equipment_image"]["name"];
    $PicturetemporaryName=$_FILES["equipment_image"]["tmp_name"];        
    
    echo "image name=".$PictureName;


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

      $Qry2="INSERT INTO `equipment`(`record_id`, `title`, `location`, `equipment_type`, `equipment_description`, `notes`, `service_interval`, `image`) VALUES ('$record_id', '$title', '$location', '$equipment_type', '$equipment_description', '$notes', '$service_interval', '$PictureName')";
      $Run2 = mysqli_query($con,$Qry2);
      if($Run2==true)
      {
        $equipment_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_equipment.php";
          </script>
          <?php
          unset($_POST["submit"]);

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

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


?>