<?php
$servername = "localhost";
$username = "graveprm_paraapp";
$password = "0m=s^TD[a8G3";
$db = "graveprm_paraapp";

// Create connection
$conn = new mysqli($servername, $username, $password , $db);


if ((isset($_POST['inquiry'])) || (isset($_POST['book'])) || (isset($_POST['ip'])))  {
$body_att = 'colspan="2" align="left" style="background-color:#000;color:#E84704;text-align: center;"';
$body_att1 = 'colspan="2" align="left" style="background-color:#57585B;color:#fff;"';
// Get data
$data = (isset($_POST['inquiry'])) ? $_POST['inquiry'] : $_POST['book'];

      $error=array();
      $extension = array('jpg','png','jpeg','gif','pdf','doc','csv','zip');
      $file_name = array_filter($_FILES['image']['name']);
      $data_img = json_encode($file_name);
       
           
        //   $data_value = json_encode($data);
        $data_value = base64_encode(json_encode($data)); // Added by developer
        	$sql = "INSERT INTO book(book,company_attachment)
            VALUES('$data_value','$data_img')";
            if ($conn->query($sql) === TRUE) {
             $new_id =  $conn->insert_id;        
            //return "true"
            // echo $new_id;
          }else{
             // return "false";
          }
    
          
          
          
          
          
          foreach($_FILES["image"]["tmp_name"] as $key => $value){
             $data_img=$_FILES["image"]["name"][$key];
             $file_tmp=$_FILES["image"]["tmp_name"][$key];
              $ext=pathinfo($data_img,PATHINFO_EXTENSION);
              
              if(in_array($ext,$extension)) {
                if(!file_exists("./images/uploads/".$data_img)) {
                    move_uploaded_file($file_tmp=$_FILES["image"]["tmp_name"][$key],"./images/uploads/".$data_img);
                    
                    
                }
                else {
                    $filename=basename($file_name,$ext);
                    $newFileName=$filename.time().".".$ext;
                    move_uploaded_file($file_tmp=$_FILES["image"]["tmp_name"][$key],"./images/uploads/".$data_img);
                   
                }
            }
            else {
                array_push($error,"($file_name, ");
            }
            
          
        }
          
    
    //   $ip =$_SERVER['REMOTE_ADDR'];
    //   $data['ip'] = $_SERVER["REMOTE_ADDR"];
      
      
   
    
    $sql1 = "SELECT * FROM  `book` WHERE id=$new_id"; 

   
      $result = $conn->query($sql1);
      
      if($result) {
    $return = array();
    while($row = $result->fetch_assoc()) {
      $return = $row;
    // $data_value = json_decode($return['book']);
    $data_value = json_decode(base64_decode($return['book'])); // Added by developer
         }
  } else {
    $return = array();
  }

$to = "brief@paradigmappstudio.com";

$subject = (isset($_POST['inquiry'])) ? "Inquiry - Form Alert" : "Book Cover - Form";

$message = "
<html>
<head>
<title>Book Cover - Form</title>
</head>
<body>

<div style='margin-top:-10px'>
      <p>Hi, <br> <br> Use Below URL to access Book details for</p>
      <p style='line-height: 1.2;'><b>Title</b>: $data_value->book_title</p>
      <p style='line-height: 1.2;'><b>Slogan</b>: $data_value->slogan</p>
      <p style='line-height: 1.2;'><b>Name</b>: $data_value->fullname</p>
      <p style='line-height: 1.2;'><b>Email Detail</b>: $data_value->email</p>
      <p style='line-height: 1.2;'><b>Book size</b>: $data_value->book_size</p>
      <p><b>url</b>: https://paradigmappstudio.com/questionnaire/book-cover/show_data.php?id=$new_id</p><br>
       <p>Thank you <br><br>Regards,<br><br>Book Cover</p>
      </div>";
       
  
	
$message.= "</body></html>";


//   echo $message;

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

$headers .= 'From: Para Digm App Studio <leads@paradigmappstudio.com>' . "\r\n";
$headers .= 'Cc: info@paradigmappstudio.com' . "\r\n";

if(mail($to,$subject,$message,$headers)){
  // echo "1";
	$parm['status'] = 1;
//   echo json_encode($parm);
  header('Location: thankyou.php');
}
else{
    $parm['status'] = 0;
    // echo json_encode($parm);
    header('Location: index.php');

}

 }

?>