<?php
/**
 * Copy Right IJH.CC
 * Each engineer has a duty to keep the code elegant
 * $Id$
 */
if(!defined('__CORE_DIR')){
    exit("Access Denied");
}
class Ctl_Ucenter_Hongbao extends Ctl_Ucenter
{
    

    public function index()
    {
        $filter = $pager = array();
    	$pager['page'] = max(intval($page), 1);
    	$pager['limit'] = $limit = 20;
        $filter['uid'] = $this->uid;
        $status = $this->GP('arg');
        
        if($status == 1){ //未使用
            $filter['is_used'] = 0;
            $filter['endline'] = '>:'.time();
        }else{ //已使用或过期
             $filter[':OR'] = array('is_used'=>1,'endline'=>'<:'.time());
        }
        if($items = K::M('hongbao/hongbao')->items($filter, null, $page, $limit, $count)){
        	$pager['count'] = $count;
        	$pager['pagebar'] = $this->mkpage($count, $limit, $page, $this->mklink(null, array('{page}')));
        }
        $this->pagedata['time'] = time();
        $this->pagedata['status'] = $status;
        $this->pagedata['items'] = $items;
        $this->pagedata['pager'] = $pager;
        $this->tmpl = 'ucenter/hongbao/index.html';
        
    }
    

    
     public function exchange()
    {
         if($key = $this->GP('key')){
            $detail = K::M('hongbao/hongbao')->find(array('key'=>$key,'is_used'=>0,'endline'=>'>:'.time()));
             if(empty($detail)){
                 $this->msgbox->add('兑换的红包不存在或红包已过期',212);
             }else if($detail['uid'] != 0){
                 $this->msgbox->add('已经被兑换了',213);
             }else if(false !== K::M('hongbao/hongbao')->updates($detail['hongbao_id'],array('uid'=>$this->uid))){
                 $this->msgbox->add('兑换成功');
             }else{
                 $this->msgbox->add('兑换失败',211);
             }
         }else{
            $this->tmpl = 'ucenter/hongbao/exchange.html';
         }
    }

    
}