
<?php
ini_set('display_errors', 'On');
error_reporting(E_ERROR);
include 'fpdf/fpdf.php';
// array for JSON response (default to failure)
$response = array();
$response["success"] = 0;
$response["message"] = "Failed";

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

//get json array
$mainjsonArr = json_decode($json, true);
class PDF extends FPDF
{
        function header(){
        $this->image('logo.png',10,6);
        $this->SetFont('Arial','B',14);
        $this->Cell(276,5,$table_name,0,0,'C');
        $this->Ln();
        $this->SetFont('Times','',12);
        //$this->Cell(276,10,'',0,0,'C');
        $this->Ln(30);
    }
    function footer(){
        $this->SetY(-15);
        $this->SetFont('Arial','',8);
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    }


// Colored table
function FancyTable($header, $data,$table_name)
{
   // print_r($header);
    //print_r($data);
    // Colors, line width and bold font
    $this->SetFillColor(255, 99, 71);
    $this->SetTextColor(255, 99, 71);
    $this->SetFont('Times','',20);
    $this->Write(30,$table_name,1,0,'R');
    $this->Ln(30);
    $this->SetFillColor(216, 243, 229);
    $this->SetTextColor(0,0,0);
    //$this->SetDrawColor(0,0,0);
    //$this->SetLineWidth(.1);
    $this->SetFont('Times','B',12);
    // Header
    $w = array(40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40);
    for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],14,$header[$i],1,0,'L',true);
    $this->Ln();
    
    // Color and font restoration
    $this->SetFillColor(220,222,221);
    $this->SetTextColor(0);
    $this->SetFont('Times','',12);
    // Data
    $fill = false;
    
    foreach($data as $row)
    {
        if($row[0]=="Total")
        {
            foreach($row as $col)
            {   
                $this->Cell($w[0],14,$col,'LR',0,'L',$fill);

            }
       
        //$this->SetTextColor(25);
        //$this->SetFillColor(224,235,255);
       
       /* $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
        $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
        $this->Cell($w[2],6,number_format($row[2]),'LR',0,'L',$fill);
        $this->Cell($w[3],6,number_format($row[3]),'LR',0,'L',$fill);
        $this->Cell($w[4],6,$row[4],'LR',0,'L',$fill);
        $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
        $this->Cell($w[0],6,$row[1],'LR',0,'L',$fill);
        $this->Cell($w[0],6,number_format($row[2]),'LR',0,'L',$fill);
        $this->Cell($w[0],6,number_format($row[3]),'LR',0,'L',$fill);
        $this->Cell($w[0],6,$row[4],'LR',0,'L',$fill);
        $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
        $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
        $this->Cell($w[2],6,number_format($row[2]),'LR',0,'L',$fill);
        $this->Cell($w[3],6,number_format($row[3]),'LR',0,'L',$fill);
        $this->Cell($w[4],6,$row[4],'LR',0,'L',$fill);
        $this->Cell($w[5],6,$row[4],'LR',0,'L',$fill);
        */

        $this->SetFont('Times','B',12);
        $this->SetFillColor(179, 218, 255);

        }
        else
        {
                foreach($row as $col)
            {   $coll=substr($col,0,14);
                if(strlen($coll)>13)
                {
                    $coll=$coll."...";
                }
                $this->Cell($w[0],12,$coll,'LR',0,'L',$fill);

            }
            /*
        $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
        $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
        $this->Cell($w[2],6,number_format($row[2]),'LR',0,'L',$fill);
        $this->Cell($w[3],6,number_format($row[3]),'LR',0,'L',$fill);
        $this->Cell($w[4],6,$row[4],'LR',0,'L',$fill);
        $this->Cell($w[5],6,$row[5],'LR',0,'L',$fill);
        $this->Cell($w[6],6,$row[6],'LR',0,'L',$fill);
        $this->Cell($w[7],6,number_format($row[7]),'LR',0,'L',$fill);
        $this->Cell($w[8],6,number_format($row[8]),'LR',0,'L',$fill);
        $this->Cell($w[9],6,$row[9],'LR',0,'L',$fill);
        $this->Cell($w[],6,$row[10],'LR',0,'L',$fill);
        $this->Cell($w[],6,$row[11],'LR',0,'L',$fill);
        $this->Cell($w[],6,number_format($row[12]),'LR',0,'L',$fill);
        $this->Cell($w[],6,number_format($row[13]),'LR',0,'L',$fill);
        $this->Cell($w[],6,$row[14],'LR',0,'L',$fill);
        $this->Cell($w[],6,$row[15],'LR',0,'L',$fill);
        */
        }
        $this->Ln();
        $fill = !$fill;
    }
     //Closing line
    $i=count($header);
    $i=$i*32;
    $this->Cell($i,0,'','T');
}





//
}

//$pdf = new PDF();

// Column headings
if(isset($mainjsonArr))
 {
     $pdf = new PDF('P','mm',array(625,625));
// Data loading
//$jsonArr = json_decode('');
foreach($mainjsonArr as $jsonArr)
{
    //print_r($jsonArr);
$email=$jsonArr['email'];
$table_name=$jsonArr['csv_type'];
$data=$jsonArr['data'];
$header = $data[0];
unset($data[0]);
$pdf->SetFont('Times');
//$pdf->AddPage();
//$pdf->BasicTable($header,$data);
//$pdf->AddPage();
//$pdf->ImprovedTable($header,$data);
$pdf->AddPage();
//$pdf->Write(276,5,$table_name,0,0,'L');
$pdf->FancyTable($header,$data,$table_name);
}
$tempfilepath="../FBTempReport/".$email."/";
mkdir($tempfilepath, 0777, true);
$tempfilepath="../FBTempReport/".$email."/".$email;
mkdir($tempfilepath, 0777, true);
 $tempfilepath="../FBTempReport/".$email."/".$email."/Fuel_Buddy_Report_".date("d-M-Y").".pdf";
$pdf->Output($tempfilepath,'F');

if(file_exists($tempfilepath) == true)
					{
				        $response["success"] = 1;
				        $response["message"] = " PDF created succesfully";
    			       
    		            echo json_encode($response);
					}
}
else {    
    
        // required field is missing
        $response["success"] = 0;
        $response["message"] = "Required field(s) is missing";
        echo json_encode($response);
    }
//$pdf->Output();
?>
