<?php

// array for JSON response
$response = array();
require_once __DIR__ . '/db_config.php';

// check for required fields
if (isset($_POST['companyname']) && isset($_POST['ticketnumber'])  && isset($_POST['submittedby']) && isset($_POST['expensetype']) && isset($_POST['description']) && isset($_POST['distanceinkms']) && isset($_POST['fromlocation']) && isset($_POST['tolocation']) && isset($_POST['location']) && isset($_POST['claimamount']) && isset($_POST['imagefilepath']) && isset($_POST['image']) && isset($_POST['dateandtime']) ) {
    
    $companyname= $_POST['companyname'];
    $ticketnumber= $_POST['ticketnumber'];
    $submittedby = $_POST['submittedby'];
	$expensetype= $_POST['expensetype'];
	$description= $_POST['description'];
    $distanceinkms= $_POST['distanceinkms'];
    $fromlocation= $_POST['fromlocation'];
    $tolocation= $_POST['tolocation'];
    $location= $_POST['location'];
    $claimamount= $_POST['claimamount'];
    $imagefilepath= $_POST['imagefilepath'];
    $image= $_POST['image'];
    $dateandtime= $_POST['dateandtime'];
    
    $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());


 
    $imageName = "../../../App/expenseimages/".$imagefilepath;
    $imageData = base64_decode($image);
    $source = imagecreatefromstring($imageData);
    $rotate = imagerotate($source, $angle, 0); // if want to rotate the image
    $imageSave = imagejpeg($rotate,$imageName,100);
    imagedestroy($source);


    // mysql inserting a new row
    $query ="INSERT INTO expense(companyname, ticketnumber, submittedby, expensetype, description, distanceinkms, fromlocation, tolocation, location, claimamount, imagefilepath, image, dateandtime) VALUES('$companyname', '$ticketnumber', '$submittedby', '$expensetype' , '$description' ,'$distanceinkms', '$fromlocation', '$tolocation', '$location', '$claimamount', '$imagefilepath','" . mysql_escape_string(file_get_contents($imageName)) . "', '$dateandtime')";
    $result=mysqli_query($con,$query);

    if ($result) {
        $response["s"] = 1;
    } else {
        $response["s"] = 0;
    }
    mysqli_close($con);
} else {
    $response["s"] = 2;
}
    echo json_encode($response);

?>