<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @author		Jade Xia
 * @copyright	Copyright (c) 2010 - 2011 天夏网络.
 * @link		http://www.tianxianet.com
 */
class Shop extends Controller
{
	var $site_title;
	
	function Shop()
	{
		parent::Controller();
		$this->load->database();
		$this->load->model('com_model');
		$this->site_title=$this->config->item('sys_site_title');
		$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');
	}
	
	function index()
	{
		$this->load->config('flash_img_config');
		$this->load->config('hot_cats_config');
		$this->load->config('shop_config');
		
		$link = $this->common_model->get_records('SELECT title,hplink FROM '.$this->db->dbprefix.'shop_link ORDER BY seqorder ASC ,id DESC');
		$data = array(
			'site_title' => $this->site_title,
			'cat_url' => $this->config->item('cat_url'),
			'industry_catalog' => $this->config->item('industry_catalog'),
			'top_nav' => $this->config->item('top_nav'),
			'hot_keyword' => $this->config->item('hot_keyword'),
			'flash_img' => $this->config->item('flash_img'),
			'shop' => $this->config->item('shop'),
			'hot_cats' => $this->config->item('hot_cats'),
			'link' => $link
		);
		
		$this->load->library('top/TaobaokeItemsGetRequest','','req');
		$this->req->setPid($this->config->item('pid'));
		$this->req->setFields('title,pic_url,price,click_url,num_iid');
		$this->req->setSort('credit_desc');
		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);
		$this->req->setPageSize(8);
		//女装
		$this->req->setCid(16);
		$resp = $this->top->execute($this->req);
		if($resp && isset($resp['taobaoke_items']['taobaoke_item']))
		{
			$data['nvz'] = $resp['taobaoke_items']['taobaoke_item']; 
		}
		//男装
		$this->req->setCid(30);
		$resp = $this->top->execute($this->req);
		if($resp && isset($resp['taobaoke_items']['taobaoke_item']))
		{
			$data['men'] = $resp['taobaoke_items']['taobaoke_item']; 
		}
		//美容
		$this->req->setCid(50010788);
		$resp = $this->top->execute($this->req);
		if($resp && isset($resp['taobaoke_items']['taobaoke_item']))
		{
			$data['mr'] = $resp['taobaoke_items']['taobaoke_item']; 
		}
		
		//箱包
		$this->req->setCid(50006842);
		$resp = $this->top->execute($this->req);
		if($resp && isset($resp['taobaoke_items']['taobaoke_item']))
		{
			$data['xb'] = $resp['taobaoke_items']['taobaoke_item']; 
		}
		
		//店铺
		$this->load->library('top/TaobaokeShopsGetRequest','','req');
		$this->req->setPid($this->config->item('pid'));
		$this->req->setFields('click_url,shop_title,seller_credit');
		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);
		$this->req->setStartCredit('4diamond');
		if($end_credit = $this->config->item('end_credit')) $this->req->setEndCredit($end_credit);
		$this->req->setPageSize(18);
		
		//女装店
		$this->req->setCid(14);
		$resp = $this->top->execute($this->req);
		if($resp && isset($resp['taobaoke_shops']['taobaoke_shop']))
		{
			$data['nvzd'] = $resp['taobaoke_shops']['taobaoke_shop']; 
		}
		//男装店
		$this->req->setCid(37);
		$resp = $this->top->execute($this->req);
		if($resp && isset($resp['taobaoke_shops']['taobaoke_shop']))
		{
			$data['mend'] = $resp['taobaoke_shops']['taobaoke_shop']; 
		}
		
		//美容店
		$this->req->setCid(15);
		$resp = $this->top->execute($this->req);
		if($resp && isset($resp['taobaoke_shops']['taobaoke_shop']))
		{
			$data['mrd'] = $resp['taobaoke_shops']['taobaoke_shop']; 
		}
		
		//箱包
		$this->req->setCid(31);
		$resp = $this->top->execute($this->req);
		if($resp && isset($resp['taobaoke_shops']['taobaoke_shop']))
		{
			$data['xbd'] = $resp['taobaoke_shops']['taobaoke_shop']; 
		}
		
		$this->load->view(TPL_FOLDER.'shop',$data);
	}
	
	function get_catalog()
	{
		header("Content-Type:application/json");
		$stype = $this->input->post('stype');
		$json = '{';
		if($stype == 'goods')
		{
			$this->load->config('industry_catalog_config');
			$catalog = $this->config->item('industry_catalog');
			$i = 1;
			foreach($catalog as $v)
			{
				if(count($catalog) == $i) $json .= $v['cid'].':"'.$v['cat_name'].'"';
				else $json .= $v['cid'].':"'.$v['cat_name'].'",';
				$i++;
			}
		}
		elseif($stype == 'shop')
		{
			$this->load->config('shop_catalog_config');
			$catalog = $this->config->item('shop_catalog');
			$i = 1;
			foreach($catalog as $v)
			{
				if(count($catalog) == $i) $json .= $v['cid'].':"'.$v['cat_name'].'"';
				else $json .= $v['cid'].':"'.$v['cat_name'].'",';
				$i++;
			}
		}
		$json .= '}';
		echo $json;
	}
	
	function sitemap()
	{
		$catalog = $this->_parse_catalog(0);
		$nav = '所有商品分类';
		$data = array(
			'site_title'		=> $nav.'--'.$this->site_title,
			'nav'		=> ' &gt; '.$nav,
			'catalog'       => $catalog,
			'cat_url' => $this->config->item('cat_url'),
			'industry_catalog' => $this->config->item('industry_catalog'),
			'top_nav' => $this->config->item('top_nav'),
			'hot_keyword' => $this->config->item('hot_keyword'),
			'flash_img' => $this->config->item('flash_img')
		);
		
		$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');
		$this->req->setStartCommissionrate('1000');
		$this->req->setEndCommissionrate('5000');
		$this->req->setStartCredit('2diamond');
		$this->req->setEndCredit('5goldencrown');
		$this->req->setSort('commissionNum_desc');
		$this->req->setPageSize(20);
		
		//随机获取cid
		$rnd_key = array_rand($catalog);
		$this->req->setCid($catalog[$rnd_key]->cid);
		
		$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."sitemap",$data);
	}
	
	function _parse_catalog($id)
	{
		static $catalog_arr=array();
		$sql = 'SELECT id,cid,cat_name,parent_id,is_has_chd FROM '.$this->db->dbprefix."shop_industry_catalog WHERE parent_id = ".$id;
		$sql .= ' ORDER BY seqorder ASC,id DESC';
		$catalog = $this->common_model->get_records($sql);
		foreach($catalog as $row)
		{
			$catalog_arr[]=$row;
			if($row->is_has_chd==1)
			{
				$this->_parse_catalog($row->id);
			}
		}
		return $catalog_arr;
	}
}
?>