<?php
class detailAction extends FirstendAction {
public function _initialize() {
        parent::_initialize();
		$this->_mod = D('items');
}
	
	
public function index(){
	
$id = I('id', '','trim');
$item = $this->_mod->where(array('id' => $id))->find(); !$item && $this->_404();

if($this->getRobot()==false)
 {
 $last_time=date('Y-m-d',$item['last_time']);
$today=date('Y-m-d',time());
		 
//if($last_time!=$today || $item['ding']==1 || ($item['tk']==1 && $item['que']==0) ){
if($last_time!=$today){
//if('1'=='1'){	
$api_err='no';
$apiurl=$this->tqkapi.'/gconvert';
$apidata=array(
'tqk_uid'=>$this->tqkuid,
'time'=>time(),
'good_id'=>''.$item['num_iid'].''
);
$token=$this->create_token(trim(C('yh_gongju')),$apidata);
$apidata['token']=$token;
$res = json_decode($res, true);
$me=$res['me'];
if(strlen($me)>0){
$activityId =$item['Quan_id']?'&activityId='.$item['Quan_id']:'';
$quanurl='https://uland.taobao.com/coupon/edetail?e='.$me.$activityId.'&itemId='.$item['num_iid'].'&pid='.trim(C("yh_taobao_pid")).'&af=1';
$kouling=kouling($item['pic_url'].'_400x400',$item['title'],$quanurl);
$data=array(
			 'last_time'=>time(),
			 'quankouling'=>$kouling,
             'quanurl'=>$quanurl,
			 'ding'=>0,
			 'que'=>1
			 );
$re=$this->_mod->where(array(
                'num_iid' => $item['num_iid']
            ))->save($data);


if($re){
	
$item['quankouling']=$kouling;
$item['quanurl']=$quanurl;
$item['que']=1;
}else{
	
$api_err='yes';
	
}	
	
}else{
	
$api_err='yes';
	
}

}
}

if(C('yh_item_hit')){
			$hits_data = array('hits'=>array('exp','hits+1'));
			$this->_mod->where(array('id'=>$id))->setField($hits_data);
}

$this->_config_seo(C('yh_seo_config.item'), array(
            'title' => $item['title'],
            'intro' => $item['intro'],
			'price' => $item['price'],
			'quan' => floattostr($item['quan']),
			'coupon_price' => $item['coupon_price'],
			'tags' => $tags,
            'seo_title' => $item['seo_title'],
            'seo_keywords' => $item['seo_keys'],
            'seo_description' => $item['seo_desc'],
 ));
 
 $cid = $item["cate_id"];
 $where=array(
 'cate_id'=>$cid,
 'id'=>array('neq',$id)
 );
 $orlike = $this->_mod->where($where)->field('id,title,pic_url,coupon_price,price,shop_type')->limit('0,9')->order('id desc')->select();
 $this->assign('orlike', $orlike);
if(empty($item['quankouling']) || $item['quankouling']=='0' || $item['quankouling']=='undefined'){
	   $kouling=kouling($item['pic_url'].'_200x200.jpg',$item['title'],$item['quanurl']);
	   $item['quankouling']=$kouling;
	   $this->_mod->where(array(
                'num_iid' => $item['num_iid']
            ))->setField('quankouling',$kouling);
 }

$Now=time();
$this->assign('uptime',$Now-$item['up_time']);
$this->assign('item', $item);

if($this->getRobot()==false && $api_err=='yes')
 {
		 $last_time=date('Y-m-d',$item['last_time']);
		 $today=date('Y-m-d',time());
//if($last_time!=$today || $item['ding']==1 || ($item['tk']==1 && $item['que']==0) ){
if($last_time!=$today){	
if(function_exists('opcache_invalidate')){
$basedir = $_SERVER['DOCUMENT_ROOT']; 
$dir=$basedir.'/data/runtime/Data/coupon/disable_num_iids.php';
$ret=opcache_invalidate($dir,TRUE);
}
		    $disable_num_iids = F('coupon/disable_num_iids');
		    if(!$disable_num_iids){
                    $disable_num_iids = array();
             }
			$is=strpos(serialize($disable_num_iids),$item['num_iid']);
            if(empty($is)){
                    $disable_num_iids[] =array(
                    'num_iid'=>$item['num_iid'],
                    'rate'=>$item['commission_rate'],
                    'zc_id'=>$item['zc_id']
					); 
					
if(function_exists('opcache_invalidate')){
$basedir = $_SERVER['DOCUMENT_ROOT']; 
$dir=$basedir.'/data/runtime/Data/coupon/disable_num_iids.php';
$ret=opcache_invalidate($dir,TRUE);
}

			 F('coupon/disable_num_iids', $disable_num_iids);
			 $data=array(
			 'last_time'=>time(),
			  'ding'=>0,
			  'que'=>1,
			 );
			 $this->_mod->where(array(
                'num_iid' => $item['num_iid']
            ))->save($data);
			
              }
		 }
 }

$this->display();
	
}

	public function productinfo(){
		$num_iid=I('numiid');
		if ($num_iid) {
		
		$json = $this->taobaodetail($num_iid);
		$imglist = $json['small_images']['string'];
		$desc = '';
			foreach($imglist as $k=>$v){
				$desc = $desc.'<img class="lazy" src=' . $v . '>';
			}
			$json=array(
				'status'=>'ok',
				'content'=>$desc
				);
			exit(json_encode($json));
		}

	}

	
	
}



function floattostr( $val ){
    preg_match( "#^([\+\-]|)([0-9]*)(\.([0-9]*?)|)(0*)$#", trim($val), $o );
    return $o[1].sprintf('%d',$o[2]).($o[3]!='.'?$o[3]:'');
}	