<?php
/**
 * 我的收藏
 *
 *
 *
 *

 */

//use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');

require_once (BASE_ROOT_PATH . '/helper/goods_helper.php');
require_once (BASE_ROOT_PATH . '/helper/special_helper.php');
require_once (BASE_ROOT_PATH . '/helper/user_session/favorite.php');

class member_favoritesControl extends mbMemberControl
{
    static $stTypes = array('goods','brand');

    public function __construct()
    {
        parent::__construct();
    }

    public function indexOp()
    {
        $mod_favorites = Model('favorites');
        $favorites  = $mod_favorites->getGoodsFavoritesList(array('member_id' => $_SESSION['member_id']), '*', $this->page_size);
        $page_count = $mod_favorites->gettotalpage();

        $gids = [];
        foreach ($favorites as $item)
        {
            if($item['fav_type'] == 'goods') {
                $gids[] = intval($item['fav_id']);
            }
            elseif($item['fav_type'] == 'brand') {

            }
            else {
                continue;
            }
        }

        if(empty($gids))
        {
            return self::outsuccess(array('special_list' => null,
                'summary'  => null,
                'groupbuy' => null,
                'limitime' => null,
                'bundling' => null,
                'mobile_page' => mobile_page($page_count)));
        }

        $helper = new goods_helper();
        $ret = $helper->online_summary($gids,$related_goods);
        $block = special_formater::format_goods($gids,'我的商品',$ret['sort_summary']);
        $blocks[] = $block;

        return self::outsuccess(array('special_list' => $blocks,
            'summary'  => $ret['summary'],
            'groupbuy' => $ret['groupbuy'],
            'limitime' => $ret['limitime'],
            'bundling' => $ret['bundling'],
            'mobile_page' => mobile_page($page_count)));
    }

    private function format($goods_ids,$sort_summary)
    {
        $result = [];

        foreach ($goods_ids as $goods_id)
        {
            $block = [];
            $block['item_title'] = '';
            $block['item_type'] = 'home1';

            $summary = $sort_summary[$goods_id];
            $item['image'] = $summary['goods_image_url'];
            $item['show_type'] = "favorite";
            $item['show_data'] = strval($goods_id);
            $item['type'] = "goods";
            $item['data'] = strval($goods_id);
            $item['title'] = $summary['goods_mobile_name'];

            $block['items'][] = $item;
            $result[] = $block;
            $result[] = special_formater::def_divider();
        }

        return $result;
    }

    public function addOp()
    {
        $fav_type = $_POST['fav_type'];
        $fav_id   = intval($_POST['fav_id']);

        if ($fav_id <= 0 || empty($fav_type) || !in_array($fav_type,self::$stTypes)) {
            return self::outerr(errcode::ErrParamter,"参数错误");
        }

        $mod_fav = Model('favorites');
        $favorites_info = $mod_fav->getOneFavorites(array('fav_id' => $fav_id, 'fav_type' => $fav_type, 'member_id' => $_SESSION['member_id']));
        if (!empty($favorites_info)) {
            return self::outsuccess(null);
        }
        else
        {
            $insert_arr = array();
            $insert_arr['member_id'] = $_SESSION['member_id'];
            $insert_arr['fav_id']    = $fav_id;
            $insert_arr['fav_type']  = $fav_type;
            $insert_arr['fav_time']  = time();
            $result = $mod_fav->addFavorites($insert_arr);

            if ($result)
            {
                if($fav_type == 'goods') {
                    $mod_goods = Model('goods');
                    $mod_goods->editGoodsById(array('goods_collect' => array('exp', 'goods_collect + 1')), array($fav_id));
                    $favorate = new user_session\favorite();
                    $favorate->add('goods',$fav_id);
                } else {
                    $favorate = new user_session\favorite();
                    $favorate->add('brand',$fav_id);
                }
                session_helper::clear_favorate();

                return self::outsuccess(null);
            } else {
                return self::outerr(errcode::ErrParamter,"参数错误");
            }
        }
    }

    public function delOp()
    {
        $fav_id = intval($_POST['fav_id']);
        $fav_type = $_POST['fav_type'];

        if ($fav_id <= 0 || empty($fav_type) || !in_array($fav_type,self::$stTypes)) {
            return self::outerr(errcode::ErrParamter,"参数错误");
        }

        $favorate = new user_session\favorite();
        $favorate->del($fav_type,$fav_id);

        $model_favorites = Model('favorites');
        $ret = $model_favorites->delFavorites(array('fav_id' => $fav_id,'fav_type' => $fav_type, 'member_id' => $_SESSION['member_id']));
        if($ret == false) {
            return self::outerr(errcode::ErrParamter,"该收藏不存在或者已经被删除.");
        }
        else {
            session_helper::clear_favorate();
            return self::outsuccess(array('fav_id' => $fav_id));
        }
    }
}
