<?php
include_once("include/main.inc.php");

$admin_sql = mysqli_query($GLOBALS['dbconn'], "select * from admin_details");
$admin_res = mysqli_fetch_array($admin_sql);
$referer = $_SERVER['HTTP_REFERER'];
$ip = $_SERVER['REMOTE_ADDR'];

if (isset($_POST['username'])) {
    $username = !empty($_POST['username']) ? $_POST['username'] : '';
    $contact_detail = !empty($_POST['mobile']) ? $_POST['mobile'] : '';
    if($username != '' && $contact_detail != '' && $ip != '' ){
    $sql_whatsapp = "insert into tbl_enquiry set en_name='$username', en_mobile='$contact_detail',en_ip='$ip', en_url='$referer', en_date=now()";
    $qry = mysqli_query($GLOBALS['dbconn'], $sql_whatsapp);

  $to = $admin_res['email'];
 $to2 = $admin_res['cc'];
$to3='webpulseindia@gmail.com';
// $to3 = 'developer.wpsrnc@gmail.com';
$email = 'enquiry@webpulseindia.com';
    // $to3 = 'vicky.wpsranchi@gmail.com';
    $emailto = $to . ',' . $to2;
    $subject = utf8_encode('New Leads via whatsapp from https://www.milkmandairyequipment.com');
//     $message = "

// Url: $referer

// Name: " . $username . "

// Mobile: " . $contact_detail . "


// ";
$htmlContent =  '<table border="0" width="100%">
        <tbody>
  <tr>
    <td colspan="2">
      <strong>Dear Admin</strong></td>
  </tr>
  <tr>
    <td colspan="2">
      Enquiry Received with following info :</td>
  </tr>
  <tr>
    <td colspan="2">
      &nbsp;</td>
  </tr>
  <tr>
    <td width="26%">
      <strong>Post URL : </strong></td>
    <td>
      <span style="margin-top: 15px;">' . $referer . '</span></td>
  </tr>
   <tr>
    <td width="26%">
      <strong>Name : </strong></td>
    <td>
      <span style="margin-top: 15px;">' . $username . '</span></td>
  </tr>
 <tr>
    <td width="26%">
      <strong>Contact No : </strong></td>
    <td>
      <span style="margin-top: 15px;">' . $contact_detail . '</span></td>
  </tr>

</tbody>
       
     </table>';
    // $headers = 'From: ' . 'Webpulseindia.com' . ' enquiry@webpulseindia.com' . "\r\n" .
    //     'Reply-To: ' . $username . 'noreply' . "\r\n" .
    //     'X-Mailer: PHP/' . phpversion();

    // if (mail($emailto, $subject, $message, $headers)) {
    if($to!='')
         {
         smtp_mailer($to, $email, $name, $subject, $htmlContent);
         }
         if($to2!='')
         {
         smtp_mailer($to2, $email, $name, $subject, $htmlContent);
         }
   
         smtp_mailer($to3, $email, $name, $subject, $htmlContent);
        
        $resp = [
                'message_response' => 'https://api.whatsapp.com/send?phone=919814154545&text=Hello, I was on Your Website '.$referer.' and wanted to Enquire about your Services.',
                'status' => true
            ];
            echo json_encode($resp);
            exit; 
}else {
        $resp = [
            'message_response' => 'All the fields are mandatory!!!',
            'status' => false
        ];
        echo json_encode($resp);
        exit;
    }
}
        ?>