<?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_Mall extends Ctl
{

        public function index($params){
        if(!$cates = K::M('mall/cate')->fetch_all()) {
            $cates = array();
        }
        if($advs = K::M('adv/item')->items(array('adv_id'=>2,'audit'=>1,'closed'=>0),array('item_id'=>'asc'), $page,$limit,$count)) {
        	foreach($advs as $k=>$v) {
	        	$advs[$k] = $this->filter_fields('adv_id,title,link,thumb', $v);
	        }
        }else {
        	$advs= array();
        }
        $page = max((int)$params['page'], 1);
        if($products = K::M('mall/product')->items(null, null, $page, 10)){
            foreach($products as $k=>$v){
                $products[$k] = $this->filter_fields('product_id,cate_id,title,photo,jifen,views,sales,sku,info', $v);
            }
        }else{
            $products = array();
        }
        $items['advs'] = array_values($advs);
        $items['cates'] = array_values($cates);
        $items['products'] = array_values($products);
        $this->msgbox->set_data('data', array('items'=>$items));        
        $this->msgbox->add('success');
    }
    
    // 积分商城商品分类列表
    public function cate()
    {
        if(!$items = K::M('mall/cate')->fetch_all()) {
            $items = array();
        }
        $this->msgbox->add('success');
        $this->msgbox->set_data('data', array('items'=>array_values($items)));
    }

    // 积分商城商品列表
    public function product($params)
    {

        
        $filter = $orderby = array();
        $new = (int)$params['new'];
    
        if($cate_id == 0){
            unset($filter);
        }else if($cate_id = (int)$params['cate_id']){
            $filter['cate_id'] = $cate_id;
        }
        
        $page = max((int)$params['page'], 1);
        if($items = K::M('mall/product')->items($filter, null, $page, 10)){
            foreach($items as $k=>$v){
                $items[$k] = $this->filter_fields('product_id,cate_id,title,photo,jifen,views,sales,sku', $v);
            }
        }else{
           $items = array();
       }

 
        $this->msgbox->add('success');
        $this->msgbox->set_data('data', array('items'=>array_values($items)));
    }


    public function detail($params)
    {
        if(!$product_id = (int)$params['product_id']){
            $this->msgbox->add('商品不存在',211);
        }else if(!$detail = K::M('mall/product')->detail($product_id)){
            $this->msgbox->add('商品不存在',212);
        }else{
            $this->msgbox->add('success');
            $this->msgbox->set_data('data', array('detail'=>$detail));
        }
    }

}
