<?php
require_once('phpmailer.php');


if( $_SERVER['REQUEST_METHOD'] != 'POST' ){
	header ("HTTP/1.0 404 Not Found");
	echo json_encode(['type'=>'error','message'=>'Pogrešan metod']);
	exit();
}

$data = json_decode(file_get_contents("php://input"));
$dataKeys = array_keys((array)$data);
$requiredKeys = ['name','email','company','phone','website', 'message'];

if( count( array_diff($requiredKeys,$dataKeys) ) > 0 )
{
	header ("HTTP/1.0 404 Not Found");
	echo json_encode(['type'=>'error','message'=>'Pogrešni parametri']);
	exit();
}
/* else
{
		foreach($data as $name => $value)
		{
			if( ($value == '' || $value == ' ') && !in_array($name, ['message', 'website']) )
			{
				echo json_encode(['type'=>'error','message'=>'Pogrešni parametri 2']);
				exit();
			}
		}
} */


$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->addAddress('aleksandar.radukin@homepage.rs');
$mail->addAddress('branko.rodic@homepage.rs');
$mail->From = $data->email;
$mail->FromName = $data->name;
$mail->isHTML(true);
$mail->Subject = $data->name.' putem landingpage-a vam je poslao puruku';
$mail->Body = '

<html><body>
	<table>
		<tr>
			<td>Ime :</td>
			<td>'.$data->name.'</td>
		</tr>
		<tr>
			<td>Kompanija :</td>
			<td>'.$data->company.'</td>
		</tr>
		<tr>
			<td>Email :</td>
			<td>'.$data->email.'</td>
		</tr>
		<tr>
			<td>Web Site :</td>
			<td>'.$data->website.'</td>
		</tr>
		<tr>
			<td>Telefon :</td>
			<td>'.$data->phone.'</td>
		</tr>
		<tr>
			<td rowspan="2">Dopunski pitanje :</td>
		</tr>
		<tr>
			<td rowspan="2">'.$data->message.'</td>
		</tr>
	</table>
</body></html>';

if(!$mail->send()) {
	echo json_encode(['type'=>'error','message'=>'Slanje poruke neuspešno']);
	exit();
}

echo json_encode(['type'=>'success','message'=>'Poruka uspešno poslata.']);
exit();
