<?php
/**
 * Ʒ�ƻ�ȡ
 */


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

class h5_hbControl extends BaseH5Control
{
    const home_url = SHOP_SITE_URL . "/index.php";
    const code_expire = 5; // 验证码获取时间

    /**
     * 首页-获取红包
     */
    public function modeOp()
    {
        $type_id = $_GET['type_id'];

        $result = Model()->table('bonus_type')->where(array('type_id' => $type_id))->limit(1)->select();

        if (!empty($result)) {

            Tpl::output('sender', $result[0]['sender']);
            Tpl::output('link', self::home_url . "?act=h5_hb&op=bindtel&type_id={$type_id}");
            Tpl::showpage('mode', 'h5_hb/mode');
        } else {
            Tpl::showpage('mode', 'h5_hb/over');
        }
    }

    /**
     * 绑定手机号
     */
    public function bindtelOp()
    {
        $type_id = $_GET['type_id'];

        Tpl::output('url', self::home_url . "?act=h5_hb&op=lastStep&type_id={$type_id}");
        Tpl::output('get_code_link', self::home_url . "?act=h5_hb&op=getVerifyCode");
        Tpl::output('verify_code_link', self::home_url . "?act=h5_hb&op=verifyCode&type_id={$type_id}");

        Tpl::showpage('mode', 'h5_hb/bindtel');
    }

    public function getVerifyCodeOp()
    {
        $mobile = trim($_POST['phone_num']);
        if (!empty($mobile))
        {
            // 获取验证码
            $sms = new Sms();
            $code = makeSmscode();

            $status = $sms->send($mobile, array('code' => $code, 'type' => Sms::register_code, 'time' => '5'));
            if ($status == 0) {
                wcache($mobile, array('code' => $code, 'mobile' => $mobile), 'bonus', self::code_expire);
            } else {
                Log::record("红包获取验证码出错", Log::ERR);
            }
        }
    }

    public function checkCode($mobile, $code)
    {
        $ret = rcache($mobile, 'bonus');
        if (empty($ret)) {
            return false;
        }

        if ($ret['code'] == $code && $ret['mobile'] == $mobile) {
            return true;
        } else {
            return false;
        }
    }

    // 验证校验码
    public function verifyCodeOp()
    {
        $mobile = trim($_POST['phone_num']);
        $code = trim($_POST['verifyCode']);

        if (empty($mobile) || empty($code)) {
            echo(false);
        }

        $ret = $this->checkCode($mobile, $code);

        echo $ret;
    }

    /**
     * 获取一个红包
     */
    public function getOneBonus($type_id, $phone_num)
    {
        try {
            // 查找一条记录
            $condition['status'] = 0;
            $condition['type_id'] = $type_id;

            // 此时记得加载lock
            $result = Model()->table('user_bonus')->where($condition)->limit(1)->lock(true)->order('bonus_id asc')->select();
            if (empty($result)) {
                return null;
            }

            // 更新掉
            $cond2['bonus_id'] = $result[0]['bonus_id'];
            $update_info['get_time'] = time();
            $update_info['user_mobile'] = $phone_num;
            $update_info['status'] = 1;

            $trans = new trans_wapper(null,__METHOD__);
            $ret = Model()->table('user_bonus')->where($cond2)->update($update_info);
            $trans->commit();

            if($ret == false) {
                return null;
            }
            else {
                $return_value['bonus_value'] = $result[0]['bonus_value'];
                return $return_value;
            }
        }
        catch (Exception $e) {
            $trans->rollback();
            return null;
        }
    }

    // 获取红包最后一步
    public function lastStepOp()
    {
        $phone_num = trim($_GET['phone_num']);
        $type_id = trim($_GET['type_id']);
        $code = trim($_GET['verifyCode']);

        $check_result = $this->checkCode($phone_num, $code);
        if (!$check_result) {
            Tpl::output('bonus_value', '');
            Tpl::output('words', '亲，你已经领取了红包！');
            Tpl::showpage('mode', 'h5_hb/success');
        }

        $condition['user_mobile'] = $phone_num;
        $condition['type_id'] = $type_id;
        $ret = Model()->table('user_bonus')->where($condition)->select();

        if (!empty($ret)) {
            Tpl::output('bonus_value', '');
            Tpl::output('words', '亲，你已经领取了红包！');
            Tpl::showpage('mode', 'h5_hb/success');
        } else {
            if (!empty($phone_num) && !empty($type_id)) {

                $result = $this->getOneBonus($type_id, $phone_num);
                if (!empty($result)) {
                    Tpl::output('bonus_value', '¥' . $result['bonus_value']);
                    Tpl::output('words', '立刻通过熊猫美妆去购物吧!');
                    Tpl::showpage('mode', 'h5_hb/success');
                } else {
                    Tpl::showpage('mode', 'h5_hb/over');
                }
            }
        }
    }

}
