<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
include 'dbconfig.php';

if(isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
}

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$json = json_decode(file_get_contents('php://input'), true);
$user_id=$json['user_id'];
$name=$json['name'];
$phone=$json['mobile'];
$email=$json['email'];
$password=isset($json['password']) ? $json['password'] :'';
$customers_picture=$json['profile'];
if($customers_picture!=null){
$base_path=$_SERVER["DOCUMENT_ROOT"]; 
$image = substr($customers_picture, strpos($customers_picture, ",") + 1); 
$img = base64_decode($image);
$dir="workapp/console/image/profile/";
if (!file_exists($dir) and !is_dir($dir)) {
				mkdir($dir);
} 
$uploadfile = $dir."/pic_".time().".jpg";
$uploadfile1 = "pic_".time().".jpg";
file_put_contents($base_path.'/'.$uploadfile,$img);
$ins="UPDATE user_details SET image='$uploadfile1' WHERE user_id='$user_id'";
$ins_result=$conn->query($ins);
}
$ins="UPDATE user_details SET name='$name',phone='$phone',email='$email' WHERE user_id='$user_id'";
$ins_result=$conn->query($ins);

if(!empty($password)){
$ins="UPDATE users SET password='$password' WHERE user_id='$user_id'";    
}

$responseData = array('success'=>'1','message'=>"success");
$data = array("data" => $responseData);
echo json_encode($data);    
$conn->close();
?>