<?php

//header('Access-Control-Allow-Origin: http://coopervision.com.hk');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');

$servername = "apps.webpuppies.com.sg";
$username = "webpuppi_master";
$password = "internal123";
$dbname = "webpuppi_cv";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8mb4');
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sqlin = "INSERT INTO hkclariti_signups (name, contact, email, couponid, product, shopid, allow, datetime)
VALUES ('".$_POST['name']."', '".$_POST['mobile']."', '".$_POST['email']."', '".strtoupper(substr(md5($_POST['email']), 0, 8))."'  ,'".$_POST['smproduct']."','".$_POST['shopid']."','".$_POST['terms']."','".date("Y-m-d H:i:s")."')";
$conn->query($sqlin);

$sql = "SELECT * FROM hkclariti_outlets WHERE id='".$_POST['shopid']."'";
$result = $conn->query($sql);
$shop = [];
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
       $shop = $row;
    }
    
}

$name = $_POST['name'];
$couponid = strtoupper(substr(md5($_POST['email']), 0, 8));
$product = $_POST['smproduct'];
$shopname = $shop['name'];
$shopaddress = $shop['address'];
$shopcontact = $shop['telno'];

$to = $_POST['email'];
$subject = '=?UTF-8?B?' . base64_encode('感謝登記 clariti 1 day') . '?=';
$headers = "From: CooperVision Hong Kong <no-reply@coopervision.com>\r\n" .
                "Reply-To: no-reply@coopervision.com\r\n" .
                "MIME-Version: 1.0\r\n" .
                "Content-Type: text/html; charset=utf-8\r\n";

ob_start();
include 'edm.php'; //execute the file as php
$body = ob_get_clean();


mail($to, $subject, $body, $headers);



header('Location: http://coopervision.com.hk/zh-hant/thankyou');


//send email
/*
$sql = "SELECT * FROM hkclariti_outlets";
$result = $conn->query($sql);
$json = array();

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
       $json[] = $row;
    }
    echo json_encode($json);
} else {
    echo "0 results";
}

*/
$conn->close();