<?php
$servername = "localhost";
$username = "codesite_codesitex";
$password = "~O25D_)lgLc4VL7H";
$db = "codesite_codesitex";

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







if ((isset($_POST['inquiry'])) || (isset($_POST['company'])) || (isset($_POST['company_attachment'])))  {
$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['company'];

$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);
        	$sql = "INSERT INTO content(content,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";
          }
    
    //   $ip =$_SERVER['REMOTE_ADDR'];
    //   $data['ip'] = $_SERVER["REMOTE_ADDR"];
    
    
     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, ");
            }
            
          
        }
      
      
   
    
    $sql1 = "SELECT * FROM  `content` WHERE id=$new_id"; 

   
      $result = $conn->query($sql1);
      
      if($result) {
    $return = array();
    while($row = $result->fetch_assoc()) {
      $return = $row;
    $data_value = json_decode($return['content']);
    
         }
  } else {
    $return = array();
  }

$to = "brief@codesitex.com";

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

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

<div style='margin-top:-10px'>
      <p>Hi, <br> <br> Use Below URL to access E-book details for</p>
      <p style='line-height: 1.2;'><b>Name</b>: $data_value->company_name</p>
      <p style='line-height: 1.2;'><b>Email</b>: $data_value->company_email</p>
      <p style='line-height: 1.2;'><b>Zib</b>: $data_value->company_zip</p>
      <p style='line-height: 1.2;'><b>Buisness</b>: $data_value->company_business</p>
      <p style='line-height: 1.2;'><b>Services</b>: $data_value->company_services</p>
      <p><b>url</b>: https://codesitex.com/questionnaire/content/show_data.php?id=$new_id</p><br>
       <p>Thank you <br><br>Regards,<br><br>Content</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: Code Sitex <leads@codesitex.com>' . "\r\n";
//$headers .= 'Cc: myboss@example.com' . "\r\n";

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

 }

?>