<?php

require_once 'config.php';
$error=false;
$errors=array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
	
	$email=$_POST['email'];
	$first_name=$_POST['first_name'];
	$lastname=$_POST['last_name'];
	$address=$_POST['address'];
	$city=$_POST['city'];
	$pin_code=$_POST['pin_code'];
	$floor=$_POST['floor'];
	$unit_no=$_POST['unit_no'];
	$country_id=$_POST['country_id'];
	$category=implode(',',$_POST['category']);
	$phone=$_POST['phone'];
	$profile_pic=$_POST['profile_pic'];
	if($profile_pic!=''){
			$destinationPath= getcwd().'/uploads/user';
			if(!file_exists($destinationPath)){
				mkdir($destinationPath,0777,true);
			}
		
			$image_parts = explode(";base64,", $profile_pic);
			$image_type_aux = explode("image/", $image_parts[0]);
			$image_type = $image_type_aux[1];
			
			$image_base64 = base64_decode($profile_pic);
			$name=$merchant_title.uniqid(). '.png';
			$file = getcwd().'/uploads/user/'.$name ;
			file_put_contents($file, $image_base64);
			$filePath='https://app.trendinfotechs.com/api/uploads/user/'.$name;
		}else{
			
			$old=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM users WHERE id= ".$_POST['user_id']));
			$filePath=$old['profile_pic'];
		}
	
		$createCustomer=mysqli_query($con,"UPDATE users  SET first_name = '".$first_name."',last_name = '".$last_name."',address = '".$address."',phone='".$phone."',country_id='".$country_id."',category='".$category."',city = '".$city."',profile_pic='".$filePath."',pincode = '".$pin_code."',floor='".$floor."',unit_no='".$unit_no."' WHERE id= ".$_POST['user_id']);
		
		if($createCustomer){
			$errors['success']='Profile updated Successfully ';
			$errors['status']='true';
			$errors['nick_name']=$first_name;
			$errors['first_name']=$first_name;
			
			$errors['email']=$email;
			$errors['address']=$address;
			$errors['floor']=$floor;
			$errors['unit_no']=$unit_no;
			$errors['city']=$city;
			$errors['country_id']=$country_id;
			$errors['category']=$category;
			$errors['profile_pic']=$filePath;
			$errors['pincode']=$pin_code;
			$errors['phone']=$phone;
		}else{
			$error=true;
			$errors['success']='Something went wrong!!!, Please try again.';
			$errors['status']='false';
		}
	
	echo json_encode($errors);
	
}
?>