<?php

/*
$username = "";
$password = "";
$dbname = "";
$hostname = "";
$connect = mysqli_connect($hostname, $username, $password, $dbname);
*/

if (isset($_POST['email'])) {

  $name = $_POST['firstName'] . " " . $_POST['lastName'];
  $company = $_POST['userClass'];
  $email = $_POST['email'];
  $phone = $_POST['phone'];
  $message = "";
  $subject = "";
  $today = date("Y-m-d H:i:s");
  $ticket_code = "";
  $ticket_created = "";

  /* 
    $today_conc = date("md");

    $ticket_shuffle = strtoupper($today_conc.substr("$subject", 0, 3)
        .substr("$phone", -2));
    $ticket_raw_code = strtoupper(str_shuffle($ticket_shuffle));
    $ticket_code = implode("-", str_split($ticket_raw_code, 3));
    $ticket_status = "open";
    $ticket_created = $today;
    $ticket_valid = $today;

    $sql = "SELECT `id` FROM `customers` WHERE `email` = '$email'";
    $result = mysqli_query($connect, $sql);
    if (!$result) {
        echo mysqli_error($connect);
    }
    $num = mysqli_num_rows($result);

    if ($num > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $id = $row['id'];
        }

        $customer_id = $id;
    } else {
        $customer_id = 0;
    }
*/

  if ($subject == "Newsletter") {
  } else {

    echo $_POST['email'];

    /*
          $sql
            = "INSERT INTO `lm_tickets` (`ticket_name`, `ticket_company`, `ticket_code`, `ticket_status`, `ticket_subject`, `ticket_message`, `ticket_valid_from`, `ticket_phone`,`ticket_email`,`ticket_customer_id`, `ticket_created`) VALUES ('$name','$company', '$ticket_code', '$ticket_status', '$subject', '$message','$ticket_valid','$phone','$email','$customer_id', '$ticket_created')";
          $result = mysqli_query($connect, $sql);
          if (!$result) {
            echo mysqli_error($connect);
          }


          if ($company == "") {
            $company = "----";
          }

          */



    //send whatsapp alert to csu


    $INSTANCE_ID = '16';

    // TODO: Replace it with your gateway instance ID here

    $CLIENT_ID = "greenlite.com.ng@gmail.com";

    // TODO: Replace it with your Forever Green client ID here

    $CLIENT_SECRET = "e47a1cb8764d4e3aaa708e3c3d31d01b";

    // TODO: Replace it with your Forever Green client secret here

    $whatsapp_phone = "2348157984843";
    $whatsapp_group = "greenLITE CS-Unit";
    $whatsapp_message
      = "NEW CLIENT ALERT \n \nHello,\nThis customer just requested a phone call. \n \nSubject:\n$subject \n \nMessage:\n$message\n \nCustomer Details: \n$email \n$phone \n$company \n\nTicket Code: \n$ticket_code \n\nPlease call this customer and ensure that their issue is resolved. remember to log this ticket.\n \nThank you.\n\n";

    $postData = array(
      'group_admin' => "$whatsapp_phone",
      'group_name'  => "$whatsapp_group",
      'message'     => "$whatsapp_message"
    );
    $headers = array(
      'Content-Type: application/json',
      'X-WM-CLIENT-ID: ' . $CLIENT_ID,
      'X-WM-CLIENT-SECRET: ' . $CLIENT_SECRET
    );
    $url = 'http://api.whatsmate.net/v3/whatsapp/group/text/message/'
      . $INSTANCE_ID;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
    $response = curl_exec($ch);
    //echo "Response: ".$response;
    curl_close($ch);


    //send email to client acknowledging that their request was received

    $now = date('jS \, F Y');
    $date = $now;

    $msg = "

        <html>
        </head>

        </head>


        <body style='margin: 0 auto; padding: 0; width: 1000px; font-family: sans-serif; '  bgcolor='#f9f9f9'>
          <table style='width: 800px; padding: 0px; margin: 0 auto; font-family: sans-serif;' bgcolor='#f9f9f9'>
            <tr>
              <td>
          <table style='margin: 0 auto; width: 600px; border-spacing: 0em; font-family: sans-serif;'  bgcolor='#ffffff'>
          <tbody>
          <tr>
          <th style='padding: 0; margin: 0;'>
              <a href='https://thecarefair.com' target='_blank' rel='noopener'><img style='padding: 5px; margin: 10px auto; display: block; background-color: inherit;' title='thecarefair.com' src='https://thecarefair.com/assets/img/newlogo.png' width='100' /></a>

            <table style='min-height: 30px; margin: 0 auto; min-width: 100%; padding: 20px;' bgcolor='#ffffff'>
            <tbody>
            <tr>
            <th>
            <p style='color: #444444;  text-align: center; font-weight: 400;'>Dear $name,</p>
            <p style='color: #444444; line-height: 1.2em; font-weight: 400; text-align: center;'>We received your request. <br> We will reach out shortly. <br>Your support ticket code for this request is shown below. </p>
            </th>
            </tr>
            </tbody>
            </table>

          <table style='min-height: 30px; margin: 0 auto; min-width: 300px;  padding: 20px; font-size: 12px;' bgcolor='#ffffff'>
          <tbody>

            <caption>
              <h2 style='color: #444444;'><a href='https://thecarefair.com' target='_blank' title='This is your support ticket reference code' style='text-decoration: none; color: #444444; border: 1px solid #FCC829;
              background: #FCC829; border-radius: 40px; padding: 10px 20px;'><span>"
      . $ticket_code . "</span></a> </h2>
            </caption>

          <tr>
          <td>
          <p style='color: #444444; line-height: 1.2em; font-weight: 400; text-align: right; padding:0px 5px; margin: 0; '>Name:
          </p>
        </td>
        <td>
        <p style='color: #444444; line-height: 1.2em; font-weight: 400; text-align: left; padding: 0px 5px; margin: 0; '>"
      . $name . "
        </p>
        </td>
          </tr>
          <tr>
            <td>
          <p style='color: #444444; line-height: 1.2em; font-weight: 600; text-align: right; padding:0px 5px; margin: 0; '>Phone:
          </p>
        </td>
        <td>
        <p style='color: #444444; line-height: 1.2em; font-weight: 600; text-align: left; padding: 0px 5px; margin: 0; '>"
      . $phone . "
        </p>
        </td>
          </tr>

          <tr>
            <td>
          <p style='color: #444444; line-height: 1.2em; font-weight: 400; text-align: right; padding:0px 5px; margin: 0; '>Company:
          </p>
        </td>
        <td>
        <p style='color: #444444; line-height: 1.2em; font-weight: 400; text-align: left; padding: 0px 5px; margin: 0; '>"
      . $company . "
        </p>
        </td>
          </tr>
          
              <tr>
            <td>
          <p style='color: #444444; line-height: 1.2em; font-weight: 400; text-align: right; padding:0px 5px; margin: 0; '>Email:
          </p>
        </td>
        <td>
        <p style='color: #444444; line-height: 1.2em; font-weight: 400; text-align: left; padding: 0px 5px; margin: 0; '>"
      . $email . "
        </p>
        </td>
          </tr>

          <tr>
            <td>
          <p style='color: #444444; line-height: 1.2em; font-weight: 400; text-align: right; padding:0px 5px; margin: 0; '>Subject:
          </p>
        </td>
        <td>
        <p style='color: #444444; line-height: 1.2em; font-weight: 400; text-align: left; padding: 0px 5px; margin: 0; '> "
      . $subject . "

        </p>
        </td>
          </tr>
          
              <tr>
            <td>
          <p style='color: #444444; line-height: 1.2em; font-weight: 400; text-align: right; padding:0px 5px; margin: 0; '>Message:
          </p>
        </td>
        <td>
        <p style='color: #444444; line-height: 1.2em; font-weight: 400; text-align: left; padding: 0px 5px; margin: 0; '> "
      . $message . "

        </p>
        </td>
          </tr>
          
          

          <tr>
            <td>
          <p style='color: #444444; line-height: 1.2em; font-weight: 400; text-align: right; padding:0px 5px; margin: 0; '> Date:
          </p>
        </td>
        <td>
        <p style='color: #444444; line-height: 1.2em; font-weight: 400; text-align: left; padding: 0px 5px; margin: 0; '>"
      . $ticket_created . "

        </p>
        </td>
          </tr>
          </tbody>
          </table>

          <table style='min-height: 30px; margin: 0 auto; min-width: 100%; padding: 20px;' bgcolor='#ffffff'>
          <tbody>
          <tr>
          <th>
            <p style='color: #444444; font-size: 14px; line-height: 1em; font-weight: 400; text-align: center;'> Thank you for choosing <b> <a href='https://thecarefair.com' style='text-decoration: none; color: #444444;' target='_blank'><span> thecarefair.com. </span></a></b> </p>
        </th>
          </tr>
          </tbody>
          </table>


        <table style='min-height: 15px; min-width: 100%; margin: 0 auto; padding: 5px; border-top: 3px solid #FCC829;' bgcolor='#000000'>
          <tbody>
          <tr>
          <th style='text-align: left;'>
          <table style='font-size: 12px; color: #000000; text-align: left; text-decoration: none; min-height: 15px; min-width: 100%; margin: 0 auto; padding: 20px;'>
          <tbody>
          <tr>
          <th style='text-align: left; width:'>
            <a href='https://thecarefair.com' target='_blank' ><span><img class='footer-logo' style='display: block;' title='thecarefair.com' alt='thecarefair' src='https://thecarefair.com/assets/img/newlogo.png' width='80px'></span></a>
              <a style='text-decoration: none; color: #b12625; display: inline-block;' href='https://www.facebook.com/thecarefair/' target='_blank' rel='noopener'><span><img class='alignnone' style='width: 15px; padding: 10px 5px 10px 0px;' src='https://greenlite.com.ng/img/facebook.png' /></span></a>
              <a style='text-decoration: none; color: #b12625; display: inline-block;' href='https://www.instagram.com/thecarefair/' target='_blank' rel='noopener'><span><img style='width: 15px; padding: 10px 5px;' src='https://greenlite.com.ng/img/instagram.png' /></span></a>
              <a style='text-decoration: none; color: #b12625; display: inline-block;' href='https://twitter.com/thecarefair' target='_blank' rel='noopener'><span><img style='width: 15px; padding: 10px 5px;' src='https://greenlite.com.ng/img/twitter-gl.png' /></span></a>
              <a style='text-decoration: none; color: #ffffff; display: block; font-weight: 400; font-size: 10px;' href='https://thecarefair.com' target='_blank' rel='noopener'><span> &copy; thecarefair.com 2018 </span></a>
          </th>

          <th style='text-align: right;'>

          <a   href='mailto:info@thecarefair.com' style='text-decoration: none; color: #ffffff; display: block; font-weight: 400; '><span> info@thecarefair.com   </span> </a>
            <a href='mailto:support@thecarefair.com' style='text-decoration: none; color: #ffffff; display: block; font-weight: 400; '><span> support@thecarefair.com </span> </a>
              <a href='https://thecarefair.com' target='_blank' style='text-decoration: none; color: #FCC829; display: block; font-weight: 400; '><span> thecarefair.com </span> </a>

          </th>
          </tr>
          </tbody>
          </table>
          </th>
          </tr>
          </tbody>
          </table>
          </th>
          </tr>
          </tbody>
          </table>
        </td>
        </tr>
        </table>
        </body>


        </html>

        ";

    $headers = "From: support@thecarefair.com" . "\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    $diditsend = mail(
      $email,
      "thecarefair - Support Ticket: $ticket_code",
      $msg,
      $headers
    );

    exit();
  }
}

if (isset($_POST['sub_email'])) {

  $email = $_POST['sub_email'];

  echo $email;

  //send whatsapp alert to csu

  $INSTANCE_ID = '16';

  // TODO: Replace it with your gateway instance ID here

  $CLIENT_ID = "greenlite.com.ng@gmail.com";

  // TODO: Replace it with your Forever Green client ID here

  $CLIENT_SECRET = "e47a1cb8764d4e3aaa708e3c3d31d01b";

  // TODO: Replace it with your Forever Green client secret here

  $whatsapp_phone = "2348157984843";
  $whatsapp_group = "greenLITE CS-Unit";
  $whatsapp_message
    = "NEW EMAIL SUBSCRIBER \n \nCongratulations,\nThis customer just subscribed to your email listing. \n \nCustomer Details: \n$email  \n\nPlease call this customer and ensure that their issue is resolved. remember to log this ticket.\n \nThank you.\n\n";

  $postData = array(
    'group_admin' => "$whatsapp_phone",
    'group_name'  => "$whatsapp_group",
    'message'     => "$whatsapp_message"
  );
  $headers = array(
    'Content-Type: application/json',
    'X-WM-CLIENT-ID: ' . $CLIENT_ID,
    'X-WM-CLIENT-SECRET: ' . $CLIENT_SECRET
  );
  $url = 'http://api.whatsmate.net/v3/whatsapp/group/text/message/'
    . $INSTANCE_ID;
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
  $response = curl_exec($ch);
  //echo "Response: ".$response;
  curl_close($ch);
}