<?php
ini_set('display_errors', 'On');
error_reporting(E_ERROR);
require('phpmailer/class.phpmailer.php');
//array for JSON response
$response = array();

//get json string
$json = file_get_contents('php://input');

//get json array
$jsonArr = json_decode($json, true);
$vehnames=$jsonArr['veh_names'];
$email="shivajichavan007@gmail.com";
$from_date=$jsonArr['from_date'];
$To_date=$jsonArr['to_date'];
 error_log("\ngot in", 3, "my-errors.log");
if(!empty($email))
 {
     error_log("\ngot in", 3, "my-errors.log");
     //echo"entered";
$to =$email;      
$from = "support@fuelbuddy-app.com"; 
$subject = "Fuel Buddy Report"; 
$message ='<html>';

$message .='<body>';
$message .='<table width="100%" cellspacing="0" cellpadding="0"style="width: 100%;border:  border-radius: 8px;">';
$message .='<tr>';
$message .='<td align="center" valign="top" style="padding: 3px;text-align: left;border-bottom: 1px solid #ddd;";>';
$message .='<br>';
$message .='<table  table-layout: fixed; width="70%" cellspacing="0" cellpadding="0" style="border-radius: 8px; width: 100%;">';
$message .='<tr bgcolor="#6399ad" style="background-color:#6399ad;">';
$message .='<td align="left" valign="top" style="padding: 8px;text-align: left;border-bottom: 1px solid #ddd;";><img src="http://www.fuelbuddy-app.com/pdfreports/pdfreports/images/logo.png" width="auto" height=100%; style="display:block;"></td>';
$message .='</tr>';
$message.='</br>';
 $message.='<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>';
    $message.='<script type="text/javascript">';
     $message.="google.charts.load('current', {'packages':['corechart']});";
      $message.='google.charts.setOnLoadCallback(drawChart);';

$message.= "function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Year', 'Sales', 'Expenses'],
          ['2004',  1000,      400],
          ['2005',  1170,      460],
          ['2006',  660,       1120],
          ['2007',  1030,      540]
        ]);

        var options = {
          title: 'Company Performance',
          curveType: 'function',
          legend: { position: 'bottom' }
        };";

        $message.="var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));";

       $message.=' chart.draw(data, options);';
     $message.=' }';
    $message.='</script>';
  
   $message.=' <div id="curve_chart" style="width: 900px; height: 500px"></div>';
 
$message .='<tr style="background-color:#6399ad">';
$message .='<td align="left" valign="top" bgcolor="#6399ad" style="background-color:#6399ad"><table width="150%" border="0" cellspacing="0" cellpadding="15">';
//$message .='<td>Email: <a href="mailto:name@yourcompanyname.com" style="color:#000000; text-decoration:none;">support@fuelbuddy.com </a></td>';
//$message .='<td>Website: <a href="http://www.fuelbuddy-app.com" target="_blank" style="color:#000000; text-decoration:none;">www.fuelbuddy-app.com</a></td></td>';
$message .='</tr>
</table></td></tr>';
$message .='</table>';
$message .='</body></html>';
/*
// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
//$email="shivajichavan007@gmail.com";
// attachment name
$filename ="trip_data.pdf";
$filename2 ="trip2_data.pdf";

// encode data (puts attachment in proper format)
//$tempfilepath="../FBReceiptsTemp/".$email."/".$email;
$attachment =chunk_split(base64_encode(file_get_contents("../FBTempReport/".$email."/".$email."/trip_data.pdf")));
$attachment2 =chunk_split(base64_encode(file_get_contents("../FBTempReport/".$email."/".$email."/trip2_data.pdf")));
// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
//$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .=$message.$eol;

// attachment
$body .= "--".$separator.$eol;

$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$filename.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename2."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$filename2;
$body .= $attachment2.$eol;
$body .= "--".$separator."--";

//mail($to, $subject, $body, $headers);
*/
$attachment ="../FBTempReport/".$email."/".$email."/Fuel_Buddy_Report_".date("d-M-Y").".pdf";
$attachment2 ='../FBTempReport/'.$email.'/'.$email.'.pdf';
$attachment3="../FBTempReport/".$email."/".$email.".zip";


$filename ="Fuel_Buddy_Report_".date("d-M-Y").'.pdf';
$filename2 ="Fuel_Buddy_Receipts.pdf";
$filename3="Fuel_Buddy_csv.zip";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "ssl";
$mail->Port     = 465;  
$mail->Username = "support-ios@fuelbuddy-app.com";
$mail->Password = "Apple2321";
$mail->Host     = "fuelbuddy-app.com";
$mail->Mailer   = "smtp";
$mail->SetFrom($from);
$mail->AddAddress($to);	
$mail->Subject =$subject;
$mail->WordWrap   = 80;
$mail->MsgHTML($message);
if(file_exists($attachment) == true)
{
$mail->addattachment($attachment,$filename);    
}
if(file_exists($attachment2) == true)
{
$mail->addattachment($attachment2,$filename2);    
}
if(file_exists($attachment3) == true)
{
$mail->addattachment($attachment3,$filename3);    
}

$mail->IsHTML(true);

if(!$mail->Send()) {
    $response["success"] = 0;
    $response["message"] = "reports not sent sucessfully";
    echo json_encode($response);
    error_log("\ngot in2", 3, "my-errors.log");
    
} else {
    error_log("\ngot in3", 3, "my-errors.log");
     error_log("files".$file,3,"my-errors.log");
      $file="../FBTempReport/".$email;
    removeDirectory($file);
	$response["success"] = 1;
    $response["message"] = " reports sent sucessfully";
    echo json_encode($response);
   
    
}	


}

else {    
    
        $response["success"] = 0;
        $response["message"] = "Required field(s) is missing";
        echo json_encode($response);
    }
    function removeDirectory($path) {
  $files = glob($path . '/*');
 error_log($files,3,"my-errors.log");
  foreach ($files as $file) {
    is_dir($file) ? removeDirectory($file) : unlink($file);
  }
  rmdir($path);
  return;
}
?>

