<?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_Hongbao_Hongbao extends Ctl
{
    
    public function index($page=1)
    {
    	$filter = $pager = array();
    	$pager['page'] = max(intval($page), 1);
    	$pager['limit'] = $limit = 50;
                if($SO = $this->GP('SO')){
            $pager['SO'] = $SO;
            if($SO['title']){$filter['title'] = "LIKE:%".$SO['title']."%";}
if(is_array($SO['amount'])){$a = intval($SO['amount'][0]);$b=intval($SO['amount'][1]);if($a && $b){$filter['amount'] = $a."~".$b;}}
if($SO['key']){$filter['key'] = $SO['key'];}
if(is_array($SO['dateline'])){if($SO['dateline'][0] && $SO['dateline'][1]){$a = strtotime($SO['dateline'][0]); $b = strtotime($SO['dateline'][1])+86400;$filter['dateline'] = $a."~".$b;}}
if(is_array($SO['usedline'])){if($SO['usedline'][0] && $SO['usedline'][1]){$a = strtotime($SO['usedline'][0]); $b = strtotime($SO['usedline'][1])+86400;$filter['usedline'] = $a."~".$b;}}
        }
        if($items = K::M('hongbao/hongbao')->items($filter, array('hongbao_id'=>'desc'), $page, $limit, $count)){
        	$pager['count'] = $count;
        	$pager['pagebar'] = $this->mkpage($count, $limit, $page, $this->mklink(null, array('{page}')), array('SO'=>$SO));
            $uids = array();
            foreach($items as $v){
                if($V['uid']){
                    $uids[$v['uid']] = $v['uid'];
                }
            }
            if($uids){
                $this->pagedata['member_list'] = K::M('member/member')->items_by_ids($uids);
            }
        }
        $this->pagedata['items'] = $items;
        $this->pagedata['pager'] = $pager;
        $this->tmpl = 'admin:hongbao/hongbao/items.html';
    }

    public function so()
    {
        $this->tmpl = 'admin:hongbao/hongbao/so.html';
    }

    public function detail($hongbao_id = null)
    {
        if(!$hongbao_id = (int)$hongbao_id){
            $this->err->add('未指定要查看内容的ID', 211);
        }else if(!$detail = K::M('hongbao/hongbao')->detail($hongbao_id)){
            $this->err->add('您要查看的内容不存在或已经删除', 212);
        }else{
            $this->pagedata['detail'] = $detail;
            $this->tmpl = 'admin:hongbao/hongbao/detail.html';
        }
    }

    public function add()
    {
        if($data = $this->checksubmit('data')){
            
            if($hongbao_id = K::M('hongbao/hongbao')->add($data)){
                $this->err->add('添加内容成功');
                $this->err->set_data('forward', '?hongbao/hongbao-index.html');
            } 
        }else{
           $this->tmpl = 'admin:hongbao/hongbao/add.html';
        }
    }
    

    
    public function create()
    {
        if($data = $this->checksubmit('data')){
            
            if($hongbao_id = K::M('hongbao/hongbao')->create($data)){
                $this->err->add('添加内容成功');
                $this->err->set_data('forward', '?hongbao/hongbao-index.html');
            } 
        }else{
           $this->tmpl = 'admin:hongbao/hongbao/create.html';
        }
    }

    public function edit($hongbao_id=null)
    {
        if(!($hongbao_id = (int)$hongbao_id) && !($hongbao_id = $this->GP('hongbao_id'))){
            $this->err->add('未指定要修改的内容ID', 211);
        }else if(!$detail = K::M('hongbao/hongbao')->detail($hongbao_id)){
            $this->err->add('您要修改的内容不存在或已经删除', 212);
        }else if($data = $this->checksubmit('data')){
            
            if(K::M('hongbao/hongbao')->update($hongbao_id, $data)){
                $this->err->add('修改内容成功');
            }  
        }else{
                $user= K::M('member/member')->detail($detail['uid']);
        	$this->pagedata['detail'] = $detail;
                $this->pagedata['user'] = $user;
        	$this->tmpl = 'admin:hongbao/hongbao/edit.html';
        }
    }



    public function delete($hongbao_id=null)
    {
        if($hongbao_id = (int)$hongbao_id){
            if(!$detail = K::M('hongbao/hongbao')->detail($hongbao_id)){
                $this->err->add('你要删除的内容不存在或已经删除', 211);
            }else{
                if(K::M('hongbao/hongbao')->delete($hongbao_id)){
                    $this->err->add('删除内容成功');
                }
            }
        }else if($ids = $this->GP('hongbao_id')){
            if(K::M('hongbao/hongbao')->delete($ids)){
                $this->err->add('批量删除内容成功');
            }
        }else{
            $this->err->add('未指定要删除的内容ID', 401);
        }
    }  

}