// Service : packagelistingresult
$mainframe = JFactory::getApplication();
$option = JRequest::getVar('option','com_jewebservices','','string');
$post = JRequest::get ('post');  
$uri =& JURI::getInstance();
$url= $uri->root();
$this->_table_prefix = "#__jetour_";
global $HTTP_RAW_POST_DATA;
 
$post = json_decode($HTTP_RAW_POST_DATA, true);

 

//$packagedata = array();
//$post['param_catid'] = 1;
//$post['user_id'] = 761;

/*echo "<pre>";
print_r($post);
exit;*/

/*$post['countryid'] = 37;
$post['themeid'] = 2;*/


//$post['favoritelist'] = 1;
//$post['startfrom'] = 0;
//$post['user_id'] = 764;

$add_query = '';
$packagedata = '';
//$image_path=$url.'components/com_jetour/assets/package_images/thumb_';
$image_path=$url.'components/com_jetour/assets/';
		if($post['startfrom'])
			$post['startfrom'] = $post['startfrom'];
		else
			$post['startfrom'] = 0;
			
			
		if(isset($post['favoritelist']) && @$post['favoritelist']==1){
		
			if($post['user_id']){
				$favoriteq	= 'SELECT DISTINCT package_id FROM '.$this->_table_prefix.'favoritepackage WHERE user_id='.$post['user_id'];
				$db->setQuery($favoriteq);
				$favoritepackage =  $db->loadColumn();
				$favorite_pack = implode(',',$favoritepackage);
			if(count($favoritepackage)){
			$add_query = ' AND p.id IN ('.$favorite_pack.')';
			$query =' SELECT p.id,p.thumbimage,p.packagetype_id,p.start_date,p.end_date,p.pname,p.day,p.night,p.min_price,p.currency,p.pack_desc FROM '.$this->_table_prefix.'package as p  WHERE p.published=1 '.
					' AND ( `start_date` <= DATE(NOW()) AND `end_date` >= DATE(NOW()) ) '.
					' '.$add_query.
					' ORDER BY p.id DESC LIMIT '.$post['startfrom'].',20';
					$db->setQuery($query);
	 
					$packagedata =  $db->loadObjectList();
				}else{
					$arr = array('success' => 0, 'message' => "Empty Favorite List" ); 
 					echo json_encode($arr);exit;
				}
			}
		
		}else{
		
		if(isset($post['countryid']) && $post['countryid']!=0)
			$countryid = $post['countryid'];
			
		//$post['budjetid'] = 1;
		
		if(isset($post['themeid']) && $post['themeid']!=0)
		{ 
				$themequerydata = array();
				if($post['themeid']!=0)
				{
				
				$themequery	= 'SELECT package_id FROM '.$this->_table_prefix.'package_theme WHERE theme_id='.$post['themeid'];
				$db->setQuery($themequery);
				$themequerydata= $db->loadColumn();
				
				}
			  
				
				if(count($themequerydata)!=0) {
					 
						$package_ids	= implode(',',$themequerydata);
					 
				 	$add_query	.= ' AND p.id IN ('.$package_ids.')';
				} /*else {
				 	$add_query	.= ' AND p.id=0';
				}*/
			
		}
		
		if(isset($post['budjetmin']) && @$post['budjetmin']){
			$add_query	.= ' AND p.min_price>='.$post['budjetmin'];
		}
		if(isset($post['budjetmax']) && @$post['budjetmax']){
			$add_query	.= ' AND p.min_price<='.$post['budjetmax'];
		}
		
		
		//$post['param_catid'] = 1;
		if($post['param_catid']){
			$add_query	.= ' AND p.pack_categoryid='.$post['param_catid'];
		}
		
		if($post['param_themeid']) {
				$add_query	.= ' AND p.id IN (SELECT package_id FROM '.$this->_table_prefix.'package_theme WHERE theme_id='.$post['param_themeid'].')';
			}
		
		 if($countryid){
		
			 $query =' SELECT p.id,p.thumbimage,p.packagetype_id,p.start_date,p.end_date,p.pname,p.day,p.night,p.min_price,p.currency,p.pack_desc FROM '.$this->_table_prefix.'package as p  WHERE p.published=1 '.
						' AND ( `start_date` <= DATE(NOW()) AND `end_date` >= DATE(NOW()) ) '.
						' AND (FIND_IN_SET( '.$countryid.',getcountrylst) OR p.country_id='.$countryid.') '.$add_query.
						' ORDER BY p.id DESC LIMIT '.$post['startfrom'].',20';
		}else{
			$query =' SELECT p.id,p.thumbimage,p.packagetype_id,p.start_date,p.end_date,p.pname,p.day,p.night,p.min_price,p.currency,p.pack_desc FROM '.$this->_table_prefix.'package as p  WHERE p.published=1 '.
						' AND ( `start_date` <= DATE(NOW()) AND `end_date` >= DATE(NOW()) ) '.
						' '.$add_query.
						' ORDER BY p.id DESC LIMIT '.$post['startfrom'].',20';
		}
			$db->setQuery($query);
	 
			$packagedata =  $db->loadObjectList();



		
		
		}	
			 

if(count($packagedata)){
	$favoritepackage = array();
	if($post['user_id']){
		$favoriteq	= 'SELECT DISTINCT package_id FROM '.$this->_table_prefix.'favoritepackage WHERE user_id='.$post['user_id'];
		$db->setQuery($favoriteq);
		$favoritepackage =  $db->loadColumn();
		$favorite_pack = implode(',',$favoritepackage);
	}
	 
 $packagelist = array();
 for($d=0; $d<count($packagedata ); $d++){
  $packagelistdata = array();
  $theme_id = "";
  $packagelistdata['packageid'] = $packagedata [$d]->id;

 $packagelistdata['packagesharelink'] = rtrim($url,'/').JRoute::_('index.php?option=com_jetour&view=holidaypackages&Itemid=101&package_id='.$packagedata[$d]->id);
  $packagelistdata['favorite_package'] = 0;
  if(count($favoritepackage)){
  	if(in_array($packagedata[$d]->id,$favoritepackage))
  		$packagelistdata['favorite_package'] = 1;
	else
		$packagelistdata['favorite_package'] = 0;
  }
  
  	$sql="SELECT t.thname FROM ".$this->_table_prefix."package_theme as pt INNER JOIN ".$this->_table_prefix."theme as t ON t.id=pt.theme_id WHERE pt.package_id=".$packagedata [$d]->id;
	$db->setQuery($sql);
	$thmid = $db->loadColumn();
  	$theme_id = implode(', ',$thmid);
  
  	$sqlreg="SELECT region_id FROM ".$this->_table_prefix."package_stopfront WHERE package_id=".$packagedata [$d]->id;
	$db->setQuery($sqlreg);
	$regionidss = implode(',',$db->loadColumn());
	
	if(count($db->loadColumn())){
  
  	$sql="SELECT city_name FROM ".$this->_table_prefix."city WHERE city_id IN(".$regionidss.")" ;	
	$db->setQuery($sql);
	$regions = implode (', ',$db->loadColumn());
	
	}else{
	$regions = "";
	}
   if($packagedata[$d]->currency=='0')
	$currencysymbol = '¥';
	 if($packagedata[$d]->currency=='1')
	$currencysymbol = '€';
	 if($packagedata[$d]->currency=='2')
	$currencysymbol = 'LBP';
	 if($packagedata[$d]->currency=='3')
	$currencysymbol = '$';
  
  $packagelistdata['flighttypes']='';
  
  if($packagedata[$d]->packagetype_id==6){
	 $packagelistdata['flighttypes'] = "Fixed Departure";
  }else{
  	if(strpos($theme_id,'Charter')!==FALSE)
	 $packagelistdata['flighttypes'] = "Charter Flight";
	else if(strpos($theme_id,'Fixed Departure')!==FALSE)
	 $packagelistdata['flighttypes'] = "Fixed Departure";
	 else
	 $packagelistdata['flighttypes'] = "";
  }
  
  
  $packagelistdata['packagename'] = $packagedata [$d]->pname;
  
  $areplace = array('<p> </p>','<div> </div>','<br /><br /><br />');
 
   $packagelistdata['pack_description'] =$packagedata[$d]->pack_desc;

$descriptionss = str_replace('<p>','', preg_replace("@[\\r|\\n|\\t]+@", "", $packagedata[$d]->pack_desc));
$descriptionss = str_replace('</p>','<br>', $descriptionss);
$descriptionss = str_replace('<br> <br>','<br>', $descriptionss);

// $packagelistdata['pack_description_iphon'] = strip_tags($packagedata[$d]->pack_desc);
  $packagelistdata['pack_description_iphon'] = $descriptionss;
  $packagelistdata['more_details'] = $url.'index.php?option=com_jetour&view=holidaypackages&apllications_page=1&package_id='.$packagedata[$d]->id.'&tmpl=component';
  $packagelistdata['packagedays'] = $packagedata [$d]->day;
  $packagelistdata['packagenight'] = $packagedata [$d]->night;
  
   
  $queryimage = 'SELECT pimage FROM '.$this->_table_prefix.'image WHERE p_id='.$packagedata[$d]->id.' AND main_image=1';
				  
  $db->setQuery($queryimage);
		
 $bigimage = $db->loadResult(); 
 //echo JPATH_ROOT.DS.'components/com_jetour/assets/package_images/'.$bigimage;exit;
  if($bigimage!='') { 
  if(isset($post['iwidth']) && isset($post['iheight'])){
 	if(!file_exists(JPATH_ROOT.'/'.'components/com_jetour/assets/package_images/'.$post['iwidth'].'_'.$post['iheight'].$bigimage)) {
		$image = JPATH_ROOT.'/components/com_jetour/assets/package_images/'.$bigimage;
		$im = new Imagick($image);
					 
		$geo = $im->getImageGeometry();

		// crop the image
		if(($geo['width']/$post['iwidth']) < ($geo['height']/$post['iheight']))
		{
			$im->cropImage($geo['width'], floor($post['iheight']*$geo['width']/$post['iwidth']), 0, (($geo['height']-($post['iheight']*$geo['width']/$post['iwidth']))/2));
		}
		else
		{
			$im->cropImage(ceil($post['iwidth']*$geo['height']/$post['iheight']), $geo['height'], (($geo['width']-($post['iwidth']*$geo['height']/$post['iheight']))/2), 0);
		}
		
		$im->ThumbnailImage( $post['iwidth'], $post['iheight'] );
		
		$im->writeImage(JPATH_ROOT.'/'.'components/com_jetour/assets/package_images/'.$post['iwidth'].'_'.$post['iheight'].$bigimage);
		$im->destroy();
	}
	}
 }
 
  if($bigimage!='') { 
  	if(isset($post['iwidth']) && isset($post['iheight'])){
		if( file_exists(JPATH_ROOT.'/'.'components/com_jetour/assets/package_images/'.$post['iwidth'].'_'.$post['iheight'].$bigimage) ) {
			$myimage_name	= 'package_images/'.$post['iwidth'].'_'.$post['iheight'].$bigimage;
		} else {
			$myimage_name	= 'images/noimage.jpg';
		}
		}else{
			
		if( file_exists(JPATH_ROOT.'/'.'components/com_jetour/assets/package_images/'.$bigimage) ) {
			$myimage_name	= 'package_images/'.$bigimage;
		} else {
			$myimage_name	= 'images/noimage.jpg';
		}
		
		}
	} else {
		$myimage_name	= 'images/noimage.jpg';
	}
  $packagelistdata['thumbimage'] = str_replace(' ','%20',$image_path.$myimage_name);
  $packagelistdata['start_date'] = $packagedata [$d]->start_date;
  $packagelistdata['end_date'] = $packagedata [$d]->end_date;
  $packagelistdata['themename'] = $theme_id;
  $packagelistdata['regionname'] = $regions;
  $packagelistdata['startingprice'] = $currencysymbol.' '.$packagedata [$d]->min_price;
  
  
  $packagelist[] =$packagelistdata; 
   
 }
 $arr = array('success' => 1, 'packagelist' => $packagelist);
 echo json_encode($arr,JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP);exit;
}else{
 $arr = array('success' => 0, 'message' => "No Records Found" ); 
 echo json_encode($arr);exit;
}