<?php

require "conn.php";
$test=0;

$CategoryID = $_POST["CategoryID"];
$UserLat = $_POST["UserLat"];
$UserLongt = $_POST["UserLongt"];

$UserID = $_POST["UserID"];
$BoostPage = 0;
$ShowAdds = false;


$res = mysqli_query($con,"SELECT *, (6372.797 * acos(cos(radians($UserLat)) * cos(radians(ShopLat)) * cos(radians(ShopLongt) - radians($UserLongt)) + sin(radians($UserLat)) * sin(radians(ShopLat)))) AS distance FROM Shops WHERE CategoryID ='$CategoryID' AND HasStory='YES' AND Shops.Status = 'ACTIVE' HAVING distance <= 50 ORDER BY priority DESC , distance ASC");


//$res = mysqli_query($con,"SELECT * FROM Shops WHERE CategoryID ='$CategoryID'");
//$res = mysqli_query($con,"SELECT *, (6372.797 * acos(cos(radians($UserLat)) * cos(radians(ShopLat)) * cos(radians(ShopLongt) - radians($UserLongt)) + sin(radians($UserLongt)) * sin(radians(ShopLat)))) AS distance FROM Shops WHERE CategoryID ='$CategoryID' HAVING distance <= 50 || distance=NULL ORDER BY CategoryID DESC");

$result = array();


$i = 0;
while($row = mysqli_fetch_assoc($res)){

//$data = $row[0];


$row["TypeSlider"] = "Slider";


if(($i+1)%3==0){
	
	$ShowAdds = true;
	
	$res2 = mysqli_query($con,"SELECT *, (6372.797 * acos(cos(radians($UserLat)) * cos(radians(BoostLat)) * cos(radians(BoostLongt) - radians($UserLongt)) + sin(radians($UserLat)) * sin(radians(BoostLat)))) AS distance FROM BoostsByShop JOIN Shops ON BoostsByShop.ShopID = Shops.ShopID WHERE BoostTypeID ='3' AND BoostStatus = 'Active' HAVING distance <= 125 UNION SELECT *, (6372.797 * acos(cos(radians($UserLat)) * cos(radians(BoostLat)) * cos(radians(BoostLongt) - radians($UserLongt)) + sin(radians($UserLat)) * sin(radians(BoostLat)))) AS distance FROM BoostsByShop JOIN Shops ON BoostsByShop.ShopID = Shops.ShopID WHERE BoostTypeID ='3' AND BoostStatus = 'Active' AND BoostCity='MOROCCO' HAVING distance <= 22125 ORDER BY distance ASC LIMIT $BoostPage, 1");

	while($row2 = mysqli_fetch_assoc($res2)){
		
		
		

		
		


		$ShopID         = $row2["ShopID"];
		$BoostsByShopID = $row2["BoostsByShopID"];

		
		$row2["HasStory"]   =  "YES";
        $row2["StoryCount"] = "1";
		
		$row2["TypeSlider"] = "Boost"; 
		
		if($row2['BoostAction']=="PRODUCT"){
			
			$row2["ProductID"] = $row2["BoostLinkOrProductID"];
			
		}else if($row2['BoostAction']=="LINK"){
			
			$row2["OpenType"] = "LINK";
		}
		
		
		

		
		
		
		
		if($row2["BoostAction"]=="LINK"){
			$row2["OpenType"] = "LINK";
			$row2["OpenNow"] = $row2["BoostLinkOrProductID"];
			$row2["ProductID"] = "0";
		}else if($row2["BoostAction"]=="STORE"){
			$row2["OpenType"] = "SHOP";
			$row2["ShopID"] = $row2["BoostLinkOrProductID"];
			$row2["ProductID"] = "0";
		}else{
			$row2["OpenType"] = "PRODUCT";
			$row2["OpenNow"] = $row2["BoostLinkOrProductID"];
			$row2["ProductID"] = $row2["BoostLinkOrProductID"];
			
			
			$ProductID   = $row2["BoostLinkOrProductID"];
			$ProductID   = $row2["BoostLinkOrProductID"];
		}
		
		$result[] = $row2;
		
        unset($result[$i]['BoostsByShopID']);
		unset($result[$i]['BoostName']);
		unset($result[$i]['BoostTypeID']);
		unset($result[$i]['BoostPrice']);
		unset($result[$i]['BoostTimeDuration']);
		unset($result[$i]['BoostTimeStarted']);
		unset($result[$i]['BoostLat']);
		unset($result[$i]['BoostLongt']);
		unset($result[$i]['BoostCity']);
		unset($result[$i]['BoostPhoto']);
		unset($result[$i]['BoostAction']);
		unset($result[$i]['BoostLinkOrProductID']);
		unset($result[$i]['BoostStatus']);
		unset($result[$i]['lastUpdatedBoostsByShop']);
		unset($result[$i]['CreatedAtBoostsByShop']);		
				
				
		$ShopID = $row2["ShopID"];
		$ShopID = $row2["ShopID"];

		$res2 = mysqli_query($con,"SELECT * FROM BoostsByShop WHERE BoostsByShopID='$BoostsByShopID' limit 1");

        $result2 = array();
        
        while($row44 = mysqli_fetch_assoc($res2)){
        
        //$data = $row[0];
		
		$row44["StoryPhoto"]   =  $row44["BoostPhoto"];
        $row44["StotyType"] 	  =  "Photos";
		$row44["ProductId"]    = $row2["ProductID"];
		
		
		unset($result2[$i]['BoostsByShopID']);
		unset($result2[$i]['BoostName']);
		unset($result2[$i]['BoostTypeID']);
		unset($result2[$i]['BoostPrice']);
		unset($result2[$i]['BoostTimeDuration']);
		unset($result2[$i]['BoostTimeStarted']);
		unset($result2[$i]['BoostLat']);
		unset($result2[$i]['BoostLongt']);
		unset($result2[$i]['BoostCity']);
		unset($result2[$i]['BoostPhoto']);
		unset($result2[$i]['BoostAction']);
		unset($result2[$i]['BoostLinkOrProductID']);
		unset($result2[$i]['BoostStatus']);
		unset($result2[$i]['lastUpdatedBoostsByShop']);
		unset($result2[$i]['CreatedAtBoostsByShop']);
		
		if($row44["ProductId"]=="0"){
			
			$row44["ProductId"]="";
		}
		
        $result2[] = $row44;
		
		
		if($row44["ProductId"]!=""){
			
			
			//////////////////
			$foodid = $row44["ProductId"];
			
			$res233 = mysqli_query($con,"SELECT * FROM Foods WHERE FoodID= $foodid");


			$result233 = array();
			while($row233 = mysqli_fetch_assoc($res233)){
				
				
				$result233[] = $row233;
				
			}
			
			
			array_splice($result2[0], 20, 21, array($result233[0]));
			
			///////////////
			
			
		}
        
        $test=4;
        
        }
		
		
		
        
        array_splice($result[$i], 200, 210, array($result2));		
				
				
		$i++;
		
		
		
		

		
		break;
		
	}
	
	$BoostPage = $BoostPage + 1;
}



$result[] = $row;

$ShopID = $row["ShopID"];

 $res2 = mysqli_query($con,"SELECT * FROM ShopStory WHERE ShopID='$ShopID'");

        $result2 = array();
        $k = 0;
        while($row = mysqli_fetch_assoc($res2)){
        
        //$data = $row[0];
		
		if($row["ProductId"]=="0"){
			
			$row["ProductId"]="";
		}
		
		
        $result2[] = $row;
        
		
		
			
			
			
			//////////////////
			$foodid = $row["ProductId"];
			
			$res233 = mysqli_query($con,"SELECT * FROM Foods WHERE FoodID= $foodid");


			$result233 = array();
			
			$K2334 = 0;
			while($row233 = mysqli_fetch_assoc($res233)){
				
				
				$result233[] = $row233;
				
				
				
				$res2334 = mysqli_query($con,"SELECT * FROM ExtraCategory WHERE ProductID='$foodid'");

				$result2334 = array();
				$T = 0;
				while($row2334 = mysqli_fetch_assoc($res2334)){
				
				//$data = $row[0];
				
				$ExtraCategoryID = $row2334["ExtraCategoryID"];
				
				
				
				$result2334[] = $row2334;
				
				
				
				
					
					$res355 = mysqli_query($con,"SELECT * FROM ExtraInSideCategoty WHERE ExtraCategoryID='$ExtraCategoryID'");
		
					$result355 = array();
					
					
					while($row355 = mysqli_fetch_assoc($res355)){
					
					//$data = $row[0];
					$result355[] = $row355;
						
						
						
					}
					
					array_splice($result2334[$T], 20, 21, array($result355));
					$T++;
					
					
				
				
				
				
				}
				
				array_splice($result233[$k], 20, 21, array($result2334));
				
				$K2334++;
				
			}
			
			
			array_splice($result2[$k], 20, 21, array($result233[0]));
			
			///////////////
			
			$k++;
		
		
		
		
		
        $test=4;
        
        }
        
        array_splice($result[$i], 200, 210, array($result2));
        
        
        
        $isIntrst = 0;
        $res4 = mysqli_query($con,"SELECT * FROM Following WHERE UserID='$UserID' AND ShopID='$ShopID'");
        
        while($row4 = mysqli_fetch_assoc($res4)){
        

        
        $isIntrst = 1;
        
        }


        if($isIntrst==0){
             $array = [
            "isFollow" => "no",
            
            ];
        }else if($isIntrst==1){
             $array = [
                "isFollow" => "yes",
            ];
        }


        array_splice($result[$i], 400, 410, $array);
        
        $result[$i]['Follow'] = $result[$i]['1'];
        unset($result[$i]['1']);

        

$i++;


$test=4;

}
/////////////
//echo json_encode(array("result"=>$result));
if($test==4){
    $message ="sucssesfully";
    $success = true;
    $status_code = 200;
	
	if($ShowAdds==false){
		
			$res2 = mysqli_query($con,"SELECT *, (6372.797 * acos(cos(radians($UserLat)) * cos(radians(BoostLat)) * cos(radians(BoostLongt) - radians($UserLongt)) + sin(radians($UserLat)) * sin(radians(BoostLat)))) AS distance FROM BoostsByShop JOIN Shops ON BoostsByShop.ShopID = Shops.ShopID WHERE BoostTypeID ='3' AND BoostStatus = 'Active' HAVING distance <= 125 UNION SELECT *, (6372.797 * acos(cos(radians($UserLat)) * cos(radians(BoostLat)) * cos(radians(BoostLongt) - radians($UserLongt)) + sin(radians($UserLat)) * sin(radians(BoostLat)))) AS distance FROM BoostsByShop JOIN Shops ON BoostsByShop.ShopID = Shops.ShopID WHERE BoostTypeID ='3' AND BoostStatus = 'Active' AND BoostCity='MOROCCO' HAVING distance <= 22125 ORDER BY distance ASC LIMIT $BoostPage, 2");

	while($row2 = mysqli_fetch_assoc($res2)){
		
		
		

		
		


		$ShopID         = $row2["ShopID"];
		$BoostsByShopID = $row2["BoostsByShopID"];

		
		$row2["HasStory"]   =  "YES";
        $row2["StoryCount"] = "1";
		
		$row2["TypeSlider"] = "Boost"; 
		
		if($row2['BoostAction']=="PRODUCT"){
			
		}else if($row2['BoostAction']=="LINK"){
			
			$row2["OpenType"] = "LINK";
		}
		
		
		

		
		
		
		
		if($row2["BoostAction"]=="LINK"){
			$row2["OpenType"] = "LINK";
			$row2["OpenNow"] = $row2["BoostLinkOrProductID"];
			$row2["ProductID"] = "0";
		}else if($row2["BoostAction"]=="STORE"){
			$row2["OpenType"] = "SHOP";
			$row2["ShopID"] = $row2["BoostLinkOrProductID"];
			$row2["ProductID"] = "0";
		}else{
			$row2["OpenType"] = "PRODUCT";
			$row2["OpenNow"] = $row2["BoostLinkOrProductID"];
			$row2["ProductID"] = $row2["ProductID"];
			
			
			$ProductID   = $row2["BoostLinkOrProductID"];
			$ProductID   = $row2["BoostLinkOrProductID"];
			$row2["ProductID"] = $row2["BoostLinkOrProductID"];
			$row2["ProductID"] = $row2["BoostLinkOrProductID"];
		}
		
		$result[] = $row2;
		
        unset($result[$i]['BoostsByShopID']);
		unset($result[$i]['BoostName']);
		unset($result[$i]['BoostTypeID']);
		unset($result[$i]['BoostPrice']);
		unset($result[$i]['BoostTimeDuration']);
		unset($result[$i]['BoostTimeStarted']);
		unset($result[$i]['BoostLat']);
		unset($result[$i]['BoostLongt']);
		unset($result[$i]['BoostCity']);
		unset($result[$i]['BoostPhoto']);
		unset($result[$i]['BoostAction']);
		unset($result[$i]['BoostLinkOrProductID']);
		unset($result[$i]['BoostStatus']);
		unset($result[$i]['lastUpdatedBoostsByShop']);
		unset($result[$i]['CreatedAtBoostsByShop']);		
				
				
		$ShopID = $row2["ShopID"];
		$ShopID = $row2["ShopID"];

		$res2 = mysqli_query($con,"SELECT * FROM BoostsByShop WHERE BoostsByShopID='$BoostsByShopID' limit 1");

        $result2 = array();
        
        while($row44 = mysqli_fetch_assoc($res2)){
        
        //$data = $row[0];
		
		$row44["StoryPhoto"]   =  $row44["BoostPhoto"];
        $row44["StotyType"] 	  =  "Photos";
		$row44["ProductId"]    = $row2["ProductID"];
		
		
		unset($result2[$i]['BoostsByShopID']);
		unset($result2[$i]['BoostName']);
		unset($result2[$i]['BoostTypeID']);
		unset($result2[$i]['BoostPrice']);
		unset($result2[$i]['BoostTimeDuration']);
		unset($result2[$i]['BoostTimeStarted']);
		unset($result2[$i]['BoostLat']);
		unset($result2[$i]['BoostLongt']);
		unset($result2[$i]['BoostCity']);
		unset($result2[$i]['BoostPhoto']);
		unset($result2[$i]['BoostAction']);
		unset($result2[$i]['BoostLinkOrProductID']);
		unset($result2[$i]['BoostStatus']);
		unset($result2[$i]['lastUpdatedBoostsByShop']);
		unset($result2[$i]['CreatedAtBoostsByShop']);
		
		if($row44["ProductId"]=="0"){
			
			$row44["ProductId"]="";
		}
		
		
		if($row44["ProductId"]!=""){
			
			
			//////////////////
			$foodid = $row44["ProductId"];
			
			$res233 = mysqli_query($con,"SELECT * FROM Foods WHERE FoodID= $foodid");


			$result233 = array();
			while($row233 = mysqli_fetch_assoc($res233)){
				
				
				$result233[] = $row233;
				
			}
			
			
			array_splice($result2[0], 20, 21, array($result233));
			
			///////////////
			
			
		}
		
		
        $result2[] = $row44;
        
        $test=4;
        
        }
		
		
		
        
        array_splice($result[$i], 200, 210, array($result2));		
				
				
		$i++;
		
		
		
		

		
		
		
	}
		
		
	}
	

echo json_encode(array('status_code' => $status_code,'success' => $success ,"data"=>$result,"message"=>$message));
}
else{
	$message ="No data";
    $success = false;
    $status_code = 200;
		$result = []; 
		
	if($ShowAdds==false){
		
			$res2 = mysqli_query($con,"SELECT *, (6372.797 * acos(cos(radians($UserLat)) * cos(radians(BoostLat)) * cos(radians(BoostLongt) - radians($UserLongt)) + sin(radians($UserLat)) * sin(radians(BoostLat)))) AS distance FROM BoostsByShop JOIN Shops ON BoostsByShop.ShopID = Shops.ShopID WHERE BoostTypeID ='3' AND BoostStatus = 'Active' HAVING distance <= 125 UNION SELECT *, (6372.797 * acos(cos(radians($UserLat)) * cos(radians(BoostLat)) * cos(radians(BoostLongt) - radians($UserLongt)) + sin(radians($UserLat)) * sin(radians(BoostLat)))) AS distance FROM BoostsByShop JOIN Shops ON BoostsByShop.ShopID = Shops.ShopID WHERE BoostTypeID ='3' AND BoostStatus = 'Active' AND BoostCity='MOROCCO' HAVING distance <= 22125 ORDER BY distance ASC LIMIT $BoostPage, 1");

	while($row2 = mysqli_fetch_assoc($res2)){
		
		
		

		
		


		$ShopID         = $row2["ShopID"];
		$BoostsByShopID = $row2["BoostsByShopID"];

		
		$row2["HasStory"]   =  "YES";
        $row2["StoryCount"] = "1";
		
		$row2["TypeSlider"] = "Boost"; 
		
		if($row2['BoostAction']=="PRODUCT"){
			
		}else if($row2['BoostAction']=="LINK"){
			
			$row2["OpenType"] = "LINK";
		}
		
		
		

		
		
		
		
		if($row2["BoostAction"]=="LINK"){
			$row2["OpenType"] = "LINK";
			$row2["OpenNow"] = $row2["BoostLinkOrProductID"];
			$row2["ProductID"] = "0";
		}else if($row2["BoostAction"]=="STORE"){
			$row2["OpenType"] = "SHOP";
			$row2["ShopID"] = $row2["BoostLinkOrProductID"];
			$row2["ProductID"] = "0";
		}else{
			$row2["OpenType"] = "PRODUCT";
			$row2["OpenNow"] = $row2["BoostLinkOrProductID"];
			$row2["ProductID"] = $row2["BoostLinkOrProductID"];
			
			
			$ProductID   = $row2["BoostLinkOrProductID"];
			$ProductID   = $row2["BoostLinkOrProductID"];
		}
		
		$result[] = $row2;
		
        unset($result[$i]['BoostsByShopID']);
		unset($result[$i]['BoostName']);
		unset($result[$i]['BoostTypeID']);
		unset($result[$i]['BoostPrice']);
		unset($result[$i]['BoostTimeDuration']);
		unset($result[$i]['BoostTimeStarted']);
		unset($result[$i]['BoostLat']);
		unset($result[$i]['BoostLongt']);
		unset($result[$i]['BoostCity']);
		unset($result[$i]['BoostPhoto']);
		unset($result[$i]['BoostAction']);
		unset($result[$i]['BoostLinkOrProductID']);
		unset($result[$i]['BoostStatus']);
		unset($result[$i]['lastUpdatedBoostsByShop']);
		unset($result[$i]['CreatedAtBoostsByShop']);		
				
				
		$ShopID = $row2["ShopID"];
		$ShopID = $row2["ShopID"];

		$res2 = mysqli_query($con,"SELECT * FROM BoostsByShop WHERE BoostsByShopID='$BoostsByShopID' limit 1");

        $result2 = array();
        
        while($row44 = mysqli_fetch_assoc($res2)){
        
        //$data = $row[0];
		
		$row44["StoryPhoto"]   =  $row44["BoostPhoto"];
        $row44["StotyType"] 	  =  "Photos";
		$row44["ProductId"]    = $row2["ProductID"];
		
		
		
		unset($result2[$i]['BoostsByShopID']);
		unset($result2[$i]['BoostName']);
		unset($result2[$i]['BoostTypeID']);
		unset($result2[$i]['BoostPrice']);
		unset($result2[$i]['BoostTimeDuration']);
		unset($result2[$i]['BoostTimeStarted']);
		unset($result2[$i]['BoostLat']);
		unset($result2[$i]['BoostLongt']);
		unset($result2[$i]['BoostCity']);
		unset($result2[$i]['BoostPhoto']);
		unset($result2[$i]['BoostAction']);
		unset($result2[$i]['BoostLinkOrProductID']);
		unset($result2[$i]['BoostStatus']);
		unset($result2[$i]['lastUpdatedBoostsByShop']);
		unset($result2[$i]['CreatedAtBoostsByShop']);
		
		if($row44["ProductId"]=="0"){
			
			$row44["ProductId"]="";
		}
		
		
		
		
        $result2[] = $row44;
		
		
		if($row44["ProductId"]!=""){
			
			
			//////////////////
			$foodid = $row44["ProductId"];
			
			$res233 = mysqli_query($con,"SELECT * FROM Foods WHERE FoodID= $foodid");


			$result233 = array();
			while($row233 = mysqli_fetch_assoc($res233)){
				
				
				$result233[] = $row233;
				
			}
			
			
			array_splice($result2[0], 20, 21, array($result233[0]));
			
			///////////////
			
			
		}
		
        
        $test=4;
        
        }
		
		
		
        
        array_splice($result[$i], 200, 210, array($result2));		
				
				
		$i++;
		
		
		
		

		
		
		
	}
		
		
	}	
		
		
   echo json_encode(array('status_code' => $status_code,'success' => $success ,"data"=>$result,"message"=>$message));
}
die;
mysqli_close($con);
?>