<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/4/6
 * Time: 下午1:42
 */

require_once (BASE_ROOT_PATH . '/helper/fcode/mfcode.php');
require_once (BASE_ROOT_PATH . '/helper/fcode/operator.php');
require_once (BASE_ROOT_PATH . '/helper/user_session/fcode.php');
require_once (BASE_ROOT_PATH . '/helper/goods_helper.php');
require_once (BASE_ROOT_PATH . '/helper/special_helper.php');


class member_fcodeControl extends mbMemberControl
{
    private $mFcodeBannerID;

    public function __construct()
    {
        parent::__construct();
        global $config;
        $this->mFcodeBannerID = $config['autosend_fcodes']['myfcode_sid'];
    }

    public function listOp()
    {
        $mod_fcode = Model('goods_fcode');
        $items = $mod_fcode->getFcodeList(array('mobile' => session_helper::cur_mobile()),'*','fc_state asc,usable_time desc,fc_id asc',$this->page_size());
        $page_count = $mod_fcode->gettotalpage();

        Log::record("pages={$page_count},pagesize={$this->page_size()},pageno={$this->page_no()} curpage={$_GET['curpage']}");

        if($this->page_no() == 1)
        {
            if(isset($_SESSION['fcodes'])) {
                unset($_SESSION['fcodes']);
            }
        }

        $fcodes = [];
        $gids = [];

        foreach ($items as $item)
        {
            $fcoder = new fcode\mfcode($item);
            $fcode = $fcoder->format();
            if($fcode != false) {
                $gids[] = intval($fcode['goods_id']);
                $fcodes[] = $fcode;
            }
        }

        if(empty($fcodes))
        {
            return self::outsuccess(array('special_list' => null,
                'fcodes' => null,
                'summary'  => null,
                'groupbuy' => null,
                'limitime' => null,
                'bundling' => null,
                'mobile_page' => mobile_page($page_count)));
        }
        else
        {
            if($this->page_no() == 1 && $this->mFcodeBannerID > 0)
            {
                $blocks = special_manager::instance()->special($this->mFcodeBannerID,$unused_gids);
                $items = $this->format($fcodes);
                $blocks = array_merge($blocks,$items);
            }
            else {
                $blocks = $this->format($fcodes);
            }

            $helper = new goods_helper(false);
            $ret = $helper->cart_summary($gids,$related_goods);

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

    private function format($fcodes)
    {
        $blocks = [];
        if(!empty($fcodes)) {
            $blocks[] = special_formater::def_divider();
        }

        foreach ($fcodes as $fcode)
        {
            $block = [];
            $block['item_title'] = '';
            $block['item_type'] = 'home1';
            $block['scale'] = 3.224299;

            $item['image'] = '';
            $item['show_type'] = "fcode";
            $item['show_data'] = strval($fcode['fcode_id']);
            $item['type'] = "goods";
            $item['data'] = strval($fcode['goods_id']);;
            $item['title'] = '';

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

        return $blocks;
    }

    public function addOp()
    {
        $fc_code  = $_GET['fcode'];
        $user_key = $_GET['key'];

        if(empty($fc_code) || empty($user_key)) {
            return self::outerr(errcode::ErrParamter,"参数错误，请输入正确的参数");
        }

        try
        {
            $fcode = new fcode\mfcode($fc_code);
            if($fcode->locked()) {
                return self::outerr(errcode::ErrParamter,"该F码处在锁定状态，不能添加.");
            }
            if($fcode->used()) {
                return self::outerr(errcode::ErrParamter,"该F码已经使用过了，不能添加.");
            }
            if($fcode->expired()) {
                return self::outerr(errcode::ErrParamter,"该F码已经过期，不能添加.");
            }

            if($fcode->user_key() != $user_key) {
                return self::outerr(errcode::ErrParamter,"密钥不正确，可能输入错误或者已经被其他人占用.");
            }

            $oper = new fcode\operator($fcode->commonid(),$fcode->batch_code(),session_helper::cur_mobile(),session_helper::session_id());
            $oper->change($fcode->params());

            $fcode = new user_session\fcode();
            $fcode->onStatus();

            return self::outsuccess(null);
        }
        catch (Exception $ex) {
            return self::outerr(errcode::ErrParamter,"参数错误，没有此F码.");
        }
    }
}