<?php 
require_once 'config.php';
$error=false;
$errors=array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	$leaderid=$_POST['user_id'];
	$folderid=$_POST['folder_id'];
/*$city=$_POST['city'];
    $merchant_url=$_POST['merchant_url'];
	$cate=$_POST['cate_id'];
	$user_id=$_POST['user_id'];
	$country_id=$_POST['country_id'];*/
	$user=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM users WHERE id=".$leaderid));
	$merchant_title=$user['first_name'];

	$front_picture=$_POST['image'];
//	$back_picture=$_POST['back_flyer'];*/
	$no=mysqli_query($con,"SELECT * FROM imagebyfolder WHERE folderid =".$folderid." AND leaderid=".$leaderid);
	if(mysqli_num_rows($no)>=20){
		$error=true;
		$errors['status']=false;
		$errors['success']='You have already reached to the limit. Please delete photo of this folder  to upload more.';
	}
	if($error==false)
	{

		if($front_picture!=''){
			$destinationPath= getcwd().'/uploads/insta';
			if(!file_exists($destinationPath)){
				mkdir($destinationPath,0777,true);
			}
		//	$file_name=time().'_'.$_FILES['image']['name'];
			
			//$tempName=$_FILES['image']['tmp_name'];
			//move_uploaded_file($tempName,$destinationPath.'/'.$file_name);
			//$filePath=ROOT.'/uploads/insta/'.$file_name;
			$image_parts = explode(";base64,", $front_picture);
			$image_type_aux = explode("image/", $image_parts[0]);
			$image_type = $image_type_aux[1];
			//$image_base64 = base64_decode($image_parts[1]);
			$image_base64 = base64_decode($front_picture);
			$name=$merchant_title.uniqid(). '.png';
			$file = getcwd().'/uploads/insta/'.$name ;
			file_put_contents($file, $image_base64);
			$filePath='https://app.trendinfotechs.com/api/uploads/insta/'.$name;
		}else{
			$filePath='';
		}
	
		
		
		$createEmployee=mysqli_query($con,"INSERT INTO imagebyfolder (folderid,leaderid,image,status) VALUES('".$folderid."','".$leaderid."','".$filePath."',1)");
		
		if($createEmployee){
			$errors['status']=true;
			$errors['success']='Imgage added successfully.';
		}else{
			$error=true;
			$errors['status']=false;
			$errors['success']='Something went wrong!!!, Please try again.';
		}
	}
	 
	 
	 	echo json_encode($errors);
}