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

require_once(BASE_ROOT_PATH . '/helper/fcode/operator.php');
require_once(BASE_ROOT_PATH . '/helper/fcode/mfcode.php');
require_once(BASE_ROOT_PATH . '/helper/session_helper.php');
require_once(BASE_ROOT_PATH . '/helper/third_author/wxauthor.php');
require_once(BASE_ROOT_PATH . '/helper/login_helper.php');
require_once(BASE_ROOT_PATH . '/helper/url_helper.php');

class fcodeControl extends mobileControl
{
    public function __construct()
    {
        parent::__construct();
    }

    public function indexOp()
    {
        $common_id = intval($_GET['common_id']);
        $batch_code = $_GET['batch_code'];
        $status = intval($_GET['status']);
        $mobile = empty(session_helper::cur_mobile()) ? $_GET['mobile'] : session_helper::cur_mobile();

        if(session_helper::need_wechat_author()) {
            $author = new thrid_author\wxauthor();
            $url = author_url::fcode_url($common_id,$batch_code);
            $url = $author->enter($url);
            return self::outsuccess(['direct_uri' => $url],"redirect");
        }

        if($common_id <= 0 || empty($batch_code)) {
            return self::outerr(errcode::ErrParamter,"错误的参数","fcode/error");
        }

        $oper = new fcode\operator($common_id,$batch_code,$mobile,session_helper::session_id());
        $fcode = $oper->grabed();

        $goods = $this->goods($common_id);
        if($fcode == false)
        {
            if(empty($goods)) {
                return self::outsuccess(array('error' => "商品已经下架，请关注其它F码"),"fcode/error",'wap');
            }
            elseif($status < 0) {
                return self::outsuccess(array('error' => "该商品F码已经被抢光～"),"fcode/error",'wap');
            }
            else {
                $name  = $goods['goods_name'];
                $price = $goods['goods_price'];
                $image = $goods['goods_image'];
                $image = cthumb($image, 1280, 6);
                return self::outsuccess(array('name' => $name,'price' => $price,'image' => $image,
                    'common_id' => $common_id,'batch_code' => $batch_code), "fcode/index",'wap');
            }
        }
        else
        {
            $name  = $goods['goods_name'];
            $price = $goods['goods_price'];
            $image = $goods['goods_image'];
            $image = cthumb($image, 1280, 6);

            $fcoder = new fcode\mfcode($fcode);
            if($fcoder->binded())
            {
                return self::outsuccess(array('fcode' => $fcode,'name' => $name,'price' => $price,'image' => $image),
                    "fcode/success",'wap');
            }
            else
            {
                if(session_helper::logined())
                {
                    $oper->bind($fcode);
                    $fcode = $oper->grabed();

                    return self::outsuccess(array('fcode' => $fcode,'name' => $name,'price' => $price,'image' => $image),
                        "fcode/success",'wap');
                } else {
                    return self::outsuccess(array('fcode' => $fcode,'name' => $name,'price' => $price,'image' => $image),
                        "fcode/bind",'wap');
                }
            }
        }
    }
    private function goods($common_id)
    {
        $goods_id = commonid_helper::instance()->one_goods($common_id);
        if($goods_id == false) return [];

        $mod = Model('goods');
        $goods = $mod->find($goods_id);

        return $goods;
    }

    public function openOp()
    {
        $common_id = intval($_GET['common_id']);
        $batch_code = $_GET['batch_code'];

        if($common_id <= 0 || empty($batch_code)) {
            $url = BASE_SITE_URL . "/mobile/index.php?act=fcode&op=index&common_id={$common_id}&batch_code={$batch_code}";
            return self::outsuccess(['url' => $url]);
        }

        $oper = new fcode\operator($common_id,$batch_code,session_helper::cur_mobile(),session_helper::session_id());
        $fcode = $oper->grabed();

        if($fcode == false)
        {
            $fcode = $oper->grab();
            if($fcode == false) {
                $url = BASE_SITE_URL . "/mobile/index.php?act=fcode&op=index&common_id={$common_id}&batch_code={$batch_code}&status=-1";
                return self::outsuccess(['url' => $url]);
            }
        }

        $url = BASE_SITE_URL . "/mobile/index.php?act=fcode&op=index&common_id={$common_id}&batch_code={$batch_code}";
        return self::outsuccess(['url' => $url]);
    }

    public function bindOp()
    {
        $common_id = intval($_GET['common_id']);
        $batch_code = $_GET['batch_code'];

        if($common_id <= 0 || empty($batch_code)) {
            return self::outerr(errcode::ErrParamter,"错误的参数");
        }

        $mobile = $_GET['mobile'];
        $validator = new Validator();
        $validator->setValidate(Validator::verify_mobile($mobile));
        $err = $validator->validate();
        if ($err != '') {
            return self::outerr(errcode::ErrParamter, $err);
        }

        $oper = new fcode\operator($common_id,$batch_code,$mobile,session_helper::session_id());
        $fcode = $oper->grabed();
        if($fcode == false) {
            return self::outerr(errcode::ErrParamter, "您没有该批次商品的F码.");
        }

        $fcoder = new \fcode\mfcode($fcode);
        if($fcoder->binded()) {
            return self::outsuccess(null);
        }

        if(!isset($_GET['code']) || empty($_GET['code'])) {
            return self::outerr(errcode::ErrParamter, "请输入验证码.");
        }
        $code = $_GET['code'];
        $ret = sms_helper::check_code(Sms::getfcode_code,$code,$mobile);
        if(is_array($ret)) {
            return self::outerr($ret['code'], $ret['msg']);
        }
        else
        {
            login_helper::onBinded($mobile,$code,session_helper::relay_id());
            $oper->bind($fcode);
            return self::outsuccess(null);
        }
    }
}