<?php
namespace Home\Controller;

use Think\Controller;
use SignedCookie;
use Zhida\OAuthHelperV3;
use Think\Upload\Driver\Qiniu\QiniuStorage;

use Think\Log;

class IndexController extends Controller
{
    private $auth_type = OAuthHelperV3::SCOPE_USERINFO;
    private $cookie_uid_key = 'skp-rotor-v1';
    private $debug = 1;
    private $today;
    use ControllerTrait;
    
    public function index()
    {
        session_start();
        $_SESSION['joinTime'] = time();
        $user = $this->get_user_info_from_cookie();
        // 如果没有获取到用户信息，引导用户授权
        if (!$user) {
            $this->request_auth($this->get_url());
            return;
        }
        M('join_num')->where('id=1')->setInc('num');
        $joinNum = M('join_num')->where('id=1')->find();
        $this->assign('pt',$user['prize_type']);
        $this->assign('join_num',$joinNum['num']);
        $this->display();
    }

    public function get_random_prize($user)
    {
        $v = rand(0, 1000000) / 1000000;
        $today_ratio = 0.01;
        if ($v > $today_ratio) {
            return false;
        }
        $now = time();
        $type = 1;
        $c = M('prize')->where(array(
            'prize_type' => $type,
            'is_get' => 1
        ))->count();
        if ($c >= 8) {
            return false;
        }

        $prize = M('Prize')->where(array(
            'is_get' => 0,
            'prize_type' => $type,
            'uid' => 0
        ))->order('rand()')->limit(1)->find();
        if (!$prize) {
            return false;
        }
        $result = M('Prize')->where(array(
            'id' => $prize['id'],
            'is_get' => 0,
            'uid' => 0,
        ))->save(array(
            'is_get' => 1,
            'uid' => $user['id'],
            'add_time' => $now
        ));
        if ($result > 0) {
            
            return $prize;
        }
        return false;
    }
    public function tryPrize()
    {
        $referer = $_SERVER['HTTP_REFERER'];
        $refererInfo = parse_url( $referer );
        if( $refererInfo['host'] != $_SERVER['HTTP_HOST'] )
        {
            header("HTTP/1.0 404 Not Found");
            exit;
        }
        session_start();
        if(!$_SESSION['joinTime'] || time() - $_SESSION['joinTime'] < 2)
        {
            header("HTTP/1.0 404 Not Found");
            exit;
        }
        $user = $this->get_user_info_from_cookie();
        if (!$user) {
            $this->ajaxReturn(array(
                'status' => 404,
                'msg' => '用户信息错误',
                'step' => 0
            ));
            return;
        }
        if($user['prize_type'] !== '0') {
            $this->ajaxReturn(array(
                'status' => -1,
                'msg' => '很遗憾,没有中奖',
                'step' => 3,
                'times' => $playTimes,
            ));
            return;
        }
        $prize = $this->get_random_prize($user);
        if ($prize) {
            M('user')->where(array(
                'id' => $user['id']
            ))->save(array(
                'prize_type' => 1
            ));
            $this->ajaxReturn(array(
                'status' => 1,
                'msg' => '恭喜您中奖了!',
                'step' => 4
            ));
        } else {
            M('user')->where(array(
                'id' => $user['id']
            ))->save(array(
                'prize_type' => 2
            ));
            $this->ajaxReturn(array(
                'status' => 2,
                'msg' => '恭喜您中奖了!',
                'step' => 5,
            ));
        }
    }
    
    public function submitInfo(){
        $user = $this->get_user_info_from_cookie();
        if (!$user) {
            $this->ajaxReturn(array(
                'status' => 404,
                'msg' => '用户信息错误',
                'step' => 0
            ));
            return;
        }
        
        $name = I('post.name');
        $tel = I('post.tel');
        $address = I('post.address');
        
        $result = M('user')->where(array(
            'id' => $user['id']
            ))->save(array(
                'name' => $name,
                'tel' => $tel,
                'address' => $address
            ));
    
        $this->ajaxReturn(array(
                'status' => 1,
                'msg' => '信息提交成功',
                'step' => 0
            ));
            return;
    }
}