<?php
vendor('taobao.TopSdk');
class cateAction extends FirstendAction {
	public function _initialize() {
        parent::_initialize();
        $this->_mod = D('items')->cache(true, 5 * 60);
    }
public function index(){
$page	= I('p',1 ,'intval');
$size	= 60;
$cid		= I('cid',0 ,'intval');
$sort	= I('sort', 'new');
$start = $size * ($page - 1);
$this->assign('txt_sort', $sort);
$this->assign('cid', $cid);
$key    = I("k");
$key    = urldecode($key);
$where['status'] = 'underway';
if($key){
 $where['title|tags'] = array( 'like', '%' . $key . '%' );
 $this->assign('k', $key);
}
if($cid){
 $where['cate_id'] = $cid;
}
$order = 'is_commend desc,ordid asc';
switch ($sort){
    		case 'new':
				$order.= ', coupon_start_time DESC';
				break;
			case 'price':
				$order.= ', price DESC';
				break;
			case 'rate':
				$order.= ', coupon_rate ASC';
				break;
			case 'hot':
				$order.= ', volume DESC';
				break;
			case 'default':
				$order.= ', '.C('yh_index_sort');
}

$items_list = $this->_mod->where($where)->field('id,pic_url,title,coupon_price,price,quan,shop_type,volume,add_time')->order($order)->limit($start . ',' . $size)->select();	
$count =$this->_mod->where($where)->count();
$pager = $this->_pager($count, $size);
$this->assign('p', $page);
$this->assign('page', $pager->kshow());
$this->assign('total_item', $count);
$this -> assign('page_size',$size);
if($items_list){
$today=date('Ymd');
$goodslist=array();
foreach($items_list as $k=>$v){
$goodslist[$k]['id']=$v['id'];
$goodslist[$k]['pic_url']=$v['pic_url'];
$goodslist[$k]['title']=$v['title'];
$goodslist[$k]['coupon_price']=$v['coupon_price'];
$goodslist[$k]['price']=$v['price'];
$goodslist[$k]['quan']=$v['quan'];
$goodslist[$k]['shop_type']=$v['shop_type'];
$goodslist[$k]['volume']=$v['volume'];	
if($today==date('Ymd',$v['add_time'])){
$goodslist[$k]['is_new']=1;	
}else{
$goodslist[$k]['is_new']=0;		
}
if(C('APP_SUB_DOMAIN_DEPLOY')){
$goodslist[$k]['linkurl']=U('/detail/',array('id'=>$v['id']));
}else{
$goodslist[$k]['linkurl']=U('detail/index',array('id'=>$v['id']));
}
	
}
}
$appkey=C('yh_taobao_appkey');
$appsecret=C('yh_taobao_appsecret');
$apppid=C('yh_taobao_pid');
$apppid=explode('_', $apppid);
$AdzoneId=$apppid[3];
$count=count($items_list);
if(!empty($appkey) && !empty($appsecret) && $key && $count<10 && !empty($AdzoneId)){
vendor('taobao.taobao');
$c = new \TopClient();
$c->appkey = $appkey;
$c->secretKey = $appsecret;
$c->format = 'json';
$req = new \TbkDgMaterialOptionalRequest();
$req->setAdzoneId($AdzoneId);
$req->setPlatform("1");
$req->setPageSize("100");
$req->setSort("tk_total_sales_des");
$req->setHasCoupon("true");
$req->setQ((string)$key);
$req->setPageNo("1");
$resp = $c->execute($req);
$resp = json_decode(json_encode($resp), true);
$resp=$resp['result_list']['map_data'];	
//$item=array();
$patterns = "/\d+/";

foreach($resp as $k=>$v){
//preg_match_all($patterns,$v['coupon_info'],$arr);
//$quan=$arr[0];
$goodslist[$k+$count]['quan']=$v['coupon_amount'];
$goodslist[$k+$count]['coupon_click_url']=$v['coupon_share_url'];
$goodslist[$k+$count]['num_iid']=$v['item_id'];
$goodslist[$k+$count]['title']=$v['title'];
$goodslist[$k+$count]['coupon_price']=$v['zk_final_price']-$goodslist[$k+$count]['quan'];
if($v['user_type']=="1"){
$goodslist[$k+$count]['shop_type']='B';	
}else{
$goodslist[$k+$count]['shop_type']='C';	
}
$goodslist[$k+$count]['commission_rate']=$v['commission_rate']; //比例
$goodslist[$k+$count]['price']=$v['zk_final_price'];
$goodslist[$k+$count]['volume']=$v['volume'];
$goodslist[$k+$count]['pic_url']=$v['pict_url'];
}
 
}

$this->assign('list',$goodslist);

if($cid){
$cateinfo=$this->_cate_mod->where('id='.$cid)->field('id,name,seo_title,seo_keys,seo_desc')->find();
}
 if($key){
 $this->_config_seo(array(
            'cate_name' => $cateinfo['name'],
            'title' => '搜索"' . $key . '"的优惠券结果页 - '. C('yh_site_name'),
            'seo_keywords' => $cateinfo['seo_keys'],
            'seo_description' => $cateinfo['seo_desc']
   ));
	
 }else{
 $this->_config_seo(C('yh_seo_config.cate'), array(
            'cate_name' => $cateinfo['name'],
            'seo_title' => $cateinfo['seo_title'],
            'seo_keywords' => $cateinfo['seo_keys'],
            'seo_description' => $cateinfo['seo_desc']
   ));
  }
 
$this->display();


}


	
	
}