<?php

error_reporting(0);
include('header.php');
include("db_con.php");
include("qr_code_url.php");
// include("login_check_admin.php");
$equipment_id = $_GET["equipment_id"];

session_start();

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 Report Issue</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_report_issue.php?equipment_id=<?php echo $_GET["equipment_id"];?>" enctype="multipart/form-data" method="post" id="form">


<table class="table table-borderless">

  <tr>
    <thead class="text-center">
      <th colspan="2" >Add Report Issue</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>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>Message</th>
      <td><textarea name="message" class="form-control" id="" cols="30" rows="10"></textarea></td>
    </tr>
    
    <tr>
      <th></th>
      <td><input type="checkbox"  onchange="document.getElementById('submit').disabled = !this.checked;" /> Confirm</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" id="submit" style="display:block" class="btn btn-primary" name="submit" value="Send" required disabled ></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"];
    $type= "report";
    $date= $_POST["date"];
    // $description= $_POST["description"];
    // $notes= $_POST["notes"];
    // $completed_by= $_POST["completed_by"];
    $message= $_POST["message"];
    // $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 `equipment` WHERE `date` = '$date'"; 
    // $Run5=mysqli_query($con,$Qry5);
    // if(mysqli_num_rows($Run5)<1)
    // {

      $Qry2="INSERT INTO `log`(`equipment_id`, `type`, `date`, `message`) VALUES ('$equipment_id', '$type', '$date', '$message')";
      $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);
        // 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 = "Report@".$title; //getting subject line from client
       
        //Email body I will receive
        $message = $message;
        
        //Message for client confirmation
        // $message2 = "Dear " . $name . "\n"
        // . "Thank you for contacting us. We will get back to you shortly!" . "\n\n"
        // . "You submitted the following message: " . "\n" . $_POST['message'] . "\n\n"
        // . "Regards," . "\n" . "- HMA WebDesign";
        
        //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.";
          }
        // 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
          // include('smtp/PHPMailerAutoload.php');
          // $html='Or bhai kia hal hai';

          // function smtp_mailer($to,$subject, $msg){
          //   echo "function run<br>";
          //   $mail = new PHPMailer(); 
          //   $mail->SMTPDebug  = 3;
          //   $mail->IsSMTP(); 
          //   $mail->SMTPAuth = true; 
          //   $mail->SMTPSecure = 'tls'; 
          //   $mail->Host = "smtp.gmail.com";
          //   $mail->Port = 587; 
          //   $mail->IsHTML(true);
          //   $mail->CharSet = 'UTF-8';
          //   $mail->Username = "a.rehman4facebook@gmail.com";// email from send
          //   $mail->Password = "kectowezytggzxmt";// password of Email
          //   $mail->SetFrom("a.rehman4facebook@gmail.com");
          //   $mail->Subject = $subject;
          //   $mail->Body =$msg;
          //   $mail->AddAddress($to);
          //   $mail->SMTPOptions=array('ssl'=>array(
          //     'verify_peer'=>false,
          //     'verify_peer_name'=>false,
          //     'allow_self_signed'=>false
          //   ));
          //   if(!$mail->Send()){
          //     echo $mail->ErrorInfo;
          //     echo "not sent";
          //   }else{
          //     echo "sent";
          //     return 'Sent';
          //   }
          // }
          // /// mail end
          // echo smtp_mailer('anokhatv786@gmail.com','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>$('#user_id_already_exist_error').css('display','inline');</script>";
          
    //       exit();
    // }

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


}
?>