<?php
include "config.php";
header('Content-Type: application/json');
$valid_passwords = array("LifeLCT" => "LifLinCnslt");
$valid_users = array_keys($valid_passwords);

$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];

$validated = (in_array($user, $valid_users)) && ($pass == $valid_passwords[$user]);

if (!$validated) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    $responsefull = array('error_message' => 'Unathorized: please provide a valid password!', 'data' => []);
} else {

$datas = json_decode(file_get_contents('php://input'), true);


//$user_id = $datas['user_id'];
$name = $datas['name'];
$phone = $datas['phone'];
$email = $datas['email'];
$gender = $datas['gender'];
$message = $datas['message'];
$nation = $datas['nation'];
$dep_name = $datas['dep_name'];
$doc_name = $datas['doc_name'];
$app_date = $datas['app_date'];

$opd_no_status = $datas['opd_no_status'];
$opd_no = $datas['opd_no'];

$deviceID = $datas['device_id'];
$deviceToken = $datas['device_token'];
$deviceType = 'Android';

    if($name && $email)
    {



            $data = array("name" => $name,"email" => $email,"phone" => $phone,"opd_no" => $opd_no,"gender" => $gender,"message" => $message,"dep_name" => $dep_name,"doc_name" => $doc_name,"app_date" => $app_date,"nation" => $nation);

        ///////////////////////Mail/////////////////////////////
        $bodyuser = "<table cellSpacing='0' cellPadding='0'  width='100%' border='0' ><TR><TD align='Center' height='25' style='FONT-WEIGHT: bold; FONT-SIZE: 9pt; COLOR: #ffffff; FONT-FAMILY: Verdana; BACKGROUND-COLOR:#00b09b'><sup>The </sup> Lifeline : Appointment Form</TD></TR><TR><TD align='left'><table width='100%' border='0' cellPadding='1' cellSpacing='0'><TR bgcolor='#f1f1f1'><TD align='right' height='25' style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Name &nbsp;</TD><TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD><TD width='49%' height='25' align='left' style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$name."</TD></TR>
        <TR bgcolor='#e6e6e6'><TD align='right' height='25' style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Gender &nbsp;</TD><TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD><TD width='49%' height='25' align='left' style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$gender."</TD></TR>

        <TR bgcolor='#f1f1f1'><TD align='right' height='25' style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Contact No&nbsp;</TD><TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD><TD width='49%' height='25' align='left' style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$phone."</TD></TR>

        <TR bgcolor='#e6e6e6'><TD align='right' height='25' style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Email &nbsp;</TD><TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD><TD width='49%' height='25' align='left' style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$email."</TD></TR>

        <TR bgcolor='#f1f1f1'><TD align='right' height='25' style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Nationality&nbsp;</TD><TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD><TD width='49%' height='25' align='left' style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$nation."</TD></TR>

        <TR bgcolor='#e6e6e6'><TD align='right' height='25' style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Department&nbsp;</TD><TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD><TD width='49%' height='25' align='left' style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$dep_name."</TD></TR>

        <TR bgcolor='#f1f1f1'><TD align='right' height='25' style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Doctor&nbsp;</TD><TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD><TD width='49%' height='25' align='left' style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$doc_name."</TD></TR>

        <TR bgcolor='#e6e6e6'><TD align='right' height='25' style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Appointment Date&nbsp;</TD><TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD><TD width='49%' height='25' align='left' style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$app_date."</TD></TR>

        <TR bgcolor='#f1f1f1'><TD align='right' height='25' style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>OPD Number&nbsp;</TD><TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD><TD width='49%' height='25' align='left' style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$opd_no."</TD></TR>

        <TR bgcolor='#e6e6e6'><TD align='right' height='25' style='FONT-WEIGHT: bold; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>Message&nbsp;</TD><TD height='25' align='center' valign='middle' style='FONT-WEIGHT: bold; COLOR: #000000'>:</TD><TD width='49%' height='25' align='left' style='FONT-WEIGHT: normal; FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana'>".$message."</TD></TR></table></TD></TR></table>";

///////////// Mailgun to send mail ////////////////
        $tos = 'booking@lifelinehospitalkerala.com , pro@lifelinehospitalkerala.com';
        //$tos = 'abijith@meridian.net.in';
        $useremail = $email;
        $toName = "The Lifeline - Appointment Form";
		$fromName = $name;

		$url = 'https://api.sendgrid.com/';
        $user = 'meridianit';
        $pass = 'Meridian786';

        //////////////////////////////////mail for user/////////////////////////////////
            /*
		    $json_string = array(
			//
			'to' => array(
				$useremail
			),
			'category' => 'test_category'
			);
			$params = array(
				'api_user'  => $user,
				'api_key'   => $pass,
				'x-smtpapi' => json_encode($json_string),
				'to'        => $useremail,
				'subject'   => 'The Lifeline : Book an Appointment from website',
				'html'      => $bodyuser,
				'text'      => 'The Lifeline :: Book an Appointment from website',
				'from'      => 'The Lifeline <info@lifelinehospitalkerala.com>',
			);
			$request =  $url.'api/mail.send.json';
			// Generate curl request
			$session = curl_init($request);
			// Tell curl to use HTTP POST
			curl_setopt ($session, CURLOPT_POST, true);
			// Tell curl that this is the body of the POST
			curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
			// Tell curl not to return headers, but do return the response
			curl_setopt($session, CURLOPT_HEADER, false);
			curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
			// obtain response
			$response = curl_exec($session);
			curl_close($session);
			// print everything out
			$mailmsg=$response;
			*/
			
			
			$headers = "From: " . strip_tags('info@lifelinehospitalkerala.com') . "\r\n";
            $headers .= "Reply-To: ". strip_tags('info@lifelinehospitalkerala.com') . "\r\n";
            //$headers .= "CC: rajeeshpp@meridian.net.in\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

            if(mail($useremail,'The Lifeline : Book an Appointment from website', $bodyuser, $headers)){

                $mailresponse = 1;
                //echo $mailmsg=$mailresponse;

            }
        //////////////////////////////////mail for user/////////////////////////////////

        //////////////////////////////////mail for admin/////////////////////////////////
            /*
			$json_string = array(

				//

				'to' => array(
					$tos
				),
				'category' => 'test_category'
				);
				$params = array(
					'api_user'  => $user,
					'api_key'   => $pass,
					'x-smtpapi' => json_encode($json_string),
					'to'        => $tos,
					'subject'   => 'The Lifeline : Appointment Form',
					'html'      => $bodyuser,
					'text'      => 'The Lifeline :: Appointment Form',
					'from'      => $fromName . '<'.$useremail.'>',
				);
				$request =  $url.'api/mail.send.json';
				// Generate curl request
				$session = curl_init($request);
				// Tell curl to use HTTP POST
				curl_setopt ($session, CURLOPT_POST, true);
				// Tell curl that this is the body of the POST
				curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
				// Tell curl not to return headers, but do return the response
				curl_setopt($session, CURLOPT_HEADER, false);
				curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
				// obtain response
				$response = curl_exec($session);
				curl_close($session);
                // print everything out

                $mailmsg = json_decode($response, true);
                */
                
                $headers = "From: " . strip_tags($useremail) . "\r\n";
                $headers .= "Reply-To: ". strip_tags($useremail) . "\r\n";
                //$headers .= "CC: rajeeshpp@meridian.net.in\r\n";
                $headers .= "MIME-Version: 1.0\r\n";
                $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
    
                if(mail($tos,'The Lifeline : Appointment Form', $bodyuser, $headers)){
    
                    $mailresponse = 1;
                    //echo $mailmsg=$mailresponse;
    
                }
        //////////////////////////////////mail for admin/////////////////////////////////


        $response = array("status" => "true","message" => "Appointment booked successfully", "data" => $data);



    }
    else{


        $response =array("status" => "false", "message" => "parameter missing", "data" => '');
    }
}
if(isset($response))
{
	echo json_encode($response, JSON_UNESCAPED_SLASHES);
}
?>