<?php

error_reporting(0);
session_start();

include('header.php');
include("db_con.php");
include("qr_code_url.php");
// include("login_check_admin.php");4



if(!isset($_GET["equipment_id"]))
{
  echo"<tr class='alert alert-danger text-center'><td colspan='3'>Please scane QR code to access this page</td></tr>";
}
else
{
  
   $equipment_id = $_GET["equipment_id"];
   $Qry3="SELECT * FROM `equipment` WHERE `equipment_id` = '$equipment_id'"; 
    $Run3=mysqli_query($con,$Qry3);
    $Data3=mysqli_fetch_assoc($Run3);
    $title = $Data3["title"];

?>

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




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



<form class = "border" action="user_add_repair.php?equipment_id=<?php echo $_GET["equipment_id"];?>" enctype="multipart/form-data" method="post" id="form">


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

  <tr>
    <thead class="text-center">
      <th colspan="2" >Add Repair</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>
   <input type="hidden" class="form-control" name="equipment_id" id="" value="<?php echo $equipment_id;?>">
   <!-- <input type="hidden" class="form-control" name="type" id="" value="report"> -->
   <tr>
      <th>Engineer Passcode</th>
      <td><input type="number" class="form-control" name="passcode" id=""></td>
    </tr>
    <tr>
    
      <th>Date</th>
      <td><input type="date" class="form-control" name="date" id=""></td>
    </tr>
    <tr>
      <th>Description</th>
      <td><input type="text" class="form-control" name="description" id=""></td>
    </tr>
    <tr>
      <th>Notes</th>
      <td><input type="text" class="form-control" name="notes" id=""></td>
    </tr>
    <tr>
      <th>Completed by</th>
      <td><input type="text" class="form-control" name="completed_by" id=""></td>
    </tr>
    
    <tr>
      <th>Image</th>
      <td><input type="file" name="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="Send" 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"]))
{
    $equipment_id= $_POST["equipment_id"];
    $passcode= $_POST["passcode"];
    $type= "repair";
    $date= $_POST["date"];
    $description= $_POST["description"];
    $notes= $_POST["notes"];
    $completed_by= $_POST["completed_by"];
    // $next_service= date('Y-m-d', strtotime('+1 year', strtotime($date)));
   
    $PictureName=$_FILES["image"]["name"];
    $PicturetemporaryName=$_FILES["image"]["tmp_name"];        
    
    //echo "image name=".$PictureName;


    $Qry5="SELECT * FROM `engineer` WHERE `passcode` = '$passcode'"; 
    $Run5=mysqli_query($con,$Qry5);
    if(mysqli_num_rows($Run5)>0)
    {

      $Qry2="INSERT INTO `log`(`equipment_id`, `type`, `date`, `description`, `notes`, `image`, `completed_by`) VALUES ('$equipment_id', '$type', '$date', '$description', '$notes', '$PictureName', '$completed_by')";
      $Run2 = mysqli_query($con,$Qry2);
      if($Run2==true)
      {
        $log_id = mysqli_insert_id($con);

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

       /// mail start
       $mailto = "service@epms.ie";  //My email address
       //getting customer data
       $name = $_POST['name']; //getting customer name
       $fromEmail = "admin@epms.ie"; //getting customer email
       $phone = $_POST['tel']; //getting customer Phome number
       $subject = "Repair@".$title; //getting subject line from client
      
       //Email body I will receive
       //$message = $message;
       
       //Message for client confirmation
       $message = "Date : " . $date . "\n"
       . "Description : " . $description . "\n\n"
       . "Notes : " . $notes . "\n\n"
       . "Completed by : " . $completed_by . "\n\n";
                 
       //Email headers
       $headers = "From: " . $fromEmail; // Client email, I will receive
       // $headers2 = "From: " . $mailto; // This will receive client
       
       //PHP mailer function
       
         $result1 = mail($mailto, $subject, $message, $headers); // This email sent to My address
         // $result2 = mail($fromEmail, $subject2, $message2, $headers2); //This confirmation email to client
       
         //Checking if Mails sent successfully
       
         if ($result1) {
        //   echo "Your Message was sent Successfully!";
         } else {
           echo "Sorry! Message was not sent, Try again Later.";
         }
        //     /// mail end
        //     echo smtp_mailer('Service@epms.ie','subject',$html);//emai to send

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

      }
      else
      {
          ?>
          <script>
            alert("log registration failed");
          </script>
          <?php
      }
    }
    else
    {
      echo "<script>alert('Engineer passcode does not exist.');</script>";
          
          exit();
    }

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



}

?>