<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * goods search page
 * @author		Jade Xia
 * @copyright	Copyright (c) 2010 - 2011 天夏网络.
 * @link		http://www.tianxianet.com
 *
 */
class Search extends Controller
{
	var $site_title;
		
	function Search()
	{
		parent::Controller();
		$this->load->database();
		$this->load->model('com_model');
		$this->load->library('TopClient','','top');
		$this->load->config('top_config');
		$this->load->config('top_nav_config');
		$this->load->config('hot_keyword_config');
		$this->load->config('industry_catalog_config');
		$this->load->config('cat_url_config');
		$this->site_title=$this->config->item('sys_site_title');
	}
	
	function index()
	{
		parse_str($_SERVER['QUERY_STRING'],$get);
		
		$cid = 0;
		if(isset($get['cid']) && $get['cid'] != '')
		{
			$cid = $get['cid'];
		}
		
		$nav_arr=array(
			'tb'=>'shop_industry_catalog',
			'base' =>CTL_FOLDER.'search',
			'nav_title'=>'商品搜索',
			'catalog_id' =>$cid,
			'is_list' => TRUE
		);
		$nav = $this->com_model->get_nav($nav_arr);
		
		if($cid > 0)
		{
			$subc = $this->common_model->get_records('SELECT cat_name,cid FROM '.$this->db->dbprefix.'shop_industry_catalog WHERE parent_id = (SELECT id FROM '.$this->db->dbprefix.'shop_industry_catalog WHERE cid = ?)',array($cid));
		}
		
		$data = array(
			'site_title' => $nav['nav_title'].'--'.$this->site_title,
			'cat_url' => $this->config->item('cat_url'),
			'nav'		=> $nav['nav_str'],
			'industry_catalog' => $this->config->item('industry_catalog'),
			'top_nav' => $this->config->item('top_nav'),
			'hot_keyword' => $this->config->item('hot_keyword'),
			'cid' => $cid,
			'keyword' => '',
			'total' => 0,
			'paginate' => ''
		);
		if(isset($subc)) $data['subc'] = $subc;
		
		$this->load->library('top/TaobaokeItemsGetRequest','','req');
		$this->req->setPid($this->config->item('pid'));
		$this->req->setFields('title,pic_url,price,click_url,num_iid,shop_click_url,commission_num');
		if($start_commission_rate = $this->config->item('start_commission_rate')) $this->req->setStartCommissionrate($start_commission_rate);
		if($end_commission_rate = $this->config->item('end_commission_rate')) $this->req->setEndCommissionrate($end_commission_rate);
		if($start_credit = $this->config->item('start_credit')) $this->req->setStartCredit($start_credit);
		if($end_credit = $this->config->item('end_credit')) $this->req->setEndCredit($end_credit);
		$page_size = 21;
		if($this->config->item('page_size'))
		{
			$page_size = $this->config->item('page_size');
			if($page_size > 40) $page_size = 40;
			$this->req->setPageSize($page_size);
		}
		else $this->req->setPageSize($page_size);
		if(isset($get['keyword']) && $get['keyword'] != '') 
		{
			$data['keyword'] = $get['keyword'];
			$this->req->setKeyword($data['keyword']);
			$data['keyword'] = urlencode($data['keyword']);
		}
		if($cid > 0)
		{
			$this->req->setCid($cid);
		}
		if(isset($get['start_price']) && $get['start_price'] != '') $this->req->setStartPrice($get['start_price']);
		if(isset($get['end_price']) && $get['end_price'] != '') $this->req->setEndPrice($get['end_price']);
		$page_no = 1;
		if(isset($get['page_no']) && $get['page_no'] != '')
		{
			$page_no = $get['page_no'];
			$this->req->setPageNo($page_no);
		}
		else $this->req->setPageNo($page_no);
		$data['sort'] = 'commissionNum_desc';
		if(isset($get['sort']) && $get['sort'] != '') 
		{
			$data['sort'] = $get['sort'];
			$this->req->setSort($data['sort']);
		}
		else $this->req->setSort($data['sort']);
		
		$resp = $this->top->execute($this->req);
		if($resp && isset($resp['taobaoke_items']['taobaoke_item']))
		{
			$data['query'] = $resp['taobaoke_items']['taobaoke_item']; 
		}
		if(isset($resp['total_results']))
		{
			$data['total'] = $resp['total_results']; 
			$data['paginate'] = $this->com_model->get_page_nav($data['total'],$page_no,$page_size);
		}
		
		//畅销排行
		$this->req->setStartCommissionrate('2000');
		$this->req->setEndCommissionrate('5000');
		$this->req->setStartCredit('3diamond');
		$this->req->setEndCredit('5goldencrown');
		$this->req->setSort('commissionNum_desc');
		$this->req->setPageSize(11);
		
		$resp = $this->top->execute($this->req);
		if($resp && isset($resp['taobaoke_items']['taobaoke_item']))
		{
			$data['sale_top'] = $resp['taobaoke_items']['taobaoke_item']; 
		}
		$this->load->view(TPL_FOLDER.'search',$data);
	}
}
?>