<?php 
require_once 'config.php';
$error=false;
$errors=array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $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=".$user_id));
	$merchant_title=$user['first_name'];
	$no_of_flyer=$_POST['no_of_flyer'];
	$merchant_email=$user['email'];
	$merchant_phone=$user['phone'];
	
	
	/* if(empty($store)){
		$error=true;
		$errors['store']='Please select store name';
	} */
	
	if(empty($city)){
		$error=true;
		$errors['city']='Please select city';
	}
	
	if(empty($cate)){
		$error=true;
		$errors['cate']='Please select category name';
	}
	$front_picture=$_POST['front_flyer'];
	$back_picture=$_POST['back_flyer'];
	
	if($error==false)
	{
		if($front_picture!=''){
			$destinationPath= getcwd().'/uploads/flyer';
			if(!file_exists($destinationPath)){
				mkdir($destinationPath,0777,true);
			}
			//$file_name=time().'_'.$_FILES['front']['name'];
			
			//$tempName=$_FILES['front']['tmp_name'];
			//move_uploaded_file($tempName,$destinationPath.'/'.$file_name);
			//$filePath=ROOT.'/uploads/flyer/'.$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/flyer/'.$name ;
			file_put_contents($file, $image_base64);
			$filePath='https://oglshake.com/api/uploads/flyer/'.$name;
		}else{
			$filePath='';
		}
		if($back_picture!=''){
			$destinationPathback= getcwd().'/uploads/flyer';
			if(!file_exists($destinationPathback)){
				mkdir($destinationPathback,0777,true);
			}
			$image_parts1 = explode(";base64,", $back_picture);
			$image_type_aux1 = explode("image/", $image_parts1[0]);
			$image_type1 = $image_type_aux1[1];
			//$image_base64_1 = base64_decode($image_parts1[1]);
			$image_base64_1 = base64_decode($back_picture);
			$name1=$merchant_title.uniqid(). '.png';
			$file1 = getcwd().'/uploads/flyer/'.$name1 ;
			file_put_contents($file1, $image_base64_1);
			$filePathback='https://oglshake.com/api/uploads/flyer/'.$name1;
		}else{
			$filePathback='';
		}
		
		
		
		$createEmployee=mysqli_query($con,"INSERT INTO flyers (country_id,city_id,cate_id,merchant_title,merchant_url,no_of_flyer,merchant_email,merchant_phone,front_flyer,back_flyer,status,created_by) VALUES('".$country_id."','".$city."','".$cate."','".$merchant_title."','".$merchant_url."','".$no_of_flyer."','".$merchant_email."','".$merchant_phone."','".$filePath."','".$filePathback."',1,'".$user_id."')");
		
		if($createEmployee){
			$errors['success']='Flyer created successfully.';
		}else{
			$error=true;
			$errors['success']='Something went wrong!!!, Please try again.';
		}
	}
	 
	 
	 	echo json_encode($errors);
}