<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by 娄凯.
 * Description:  抽奖页面
 * 一个号码可以抽奖一次，但是一个人最多只能中奖一次
 * Date: 2018\7\31 0031
 */

class lucky_prize extends MY_Controller{
    public $uid;
    function __construct()
    {
        parent::__construct();
        $this->load->model("lucky_model");
        $this->load->helper("template_helper");
        $this->uid = checklogin();
        if($this->uid<=0){
            $this->uid = 0;
        }
    }

    /**
     * 抽奖展示页面
     */
    public function index(){
        //获取页面需要的抽奖结果列表
        $data = array();
        setcoandsess('website_jumpto_city','nj'); //强制切换到南京
        $data['uid'] = $this->uid;
        $data['record'] = $this->lucky_model->getLuckyRecords();
        $this->load->view('lucky_prize',$data);
    }

    /**
     * 抽奖
     */
    public function getLuckyPrize(){
        if(!$this->uid){
            $this->ajaxReturn(7,'请先登录');  //前端该弹窗
        }

        //-------判断能够抽奖开始-------------
        //判断是否活动期间
        $bool = $this->lucky_model->checkIfWithinActivity();

        if(!$bool){
            $this->ajaxReturn(10,"活动已经结束啦,具体请查看活动规则呦！");
        }
        //该会员是否已中过奖.(如果有多个号码，可以有多个抽奖机会，但是一个人只能中一次)
        $record = $this->lucky_model->checkIfWonBefore($this->uid);
        if(!empty($record) && isset($record['prize_name'])){
            $this->ajaxReturn(9,'您已经中了'.$record['prize_name'].'哦，不要太贪心嘛！');
        }
        //有没有抽奖资格
        $bool = $this->lucky_model->checkIfQualified($this->uid);
        $ifusedall =  $this->lucky_model->getUsedAll();
        if($ifusedall == 1){
            $this->ajaxReturn(8,'抽奖机会已用完！您已经抽过奖了，不要太贪心哟！');
        }
        if(!$bool){
            $this->ajaxReturn(10,'您还没有获取抽奖次数哦，具体请查看活动规则呦！');
        }
        //-------判断能够抽奖结束-------------
        //-------抽奖开始---------------------
        //是否是第偶数个抽奖人
        $bool = $this->lucky_model->checkIfEven();

        if(!$bool){
            $this->lucky_model->InsertNoRecord($this->uid);
            $this->ajaxReturn(2,'您与奖品擦肩而过了！');
        }
        //奖品池是否有奖品
        $bool = $this->lucky_model->checkIfLeftPrizePool();
        if(!$bool){
            $this->lucky_model->InsertNoRecord($this->uid);
            $this->ajaxReturn(2,'您与奖品擦肩而过了！');
        }
        //抽奖
        //获取奖品池今日剩余奖品
        $prize = $this->lucky_model->getPoolPrize();
        if(!$prize){
            $this->lucky_model->InsertNoRecord($this->uid);
            $this->ajaxReturn(2,'您与奖品擦肩而过了！');
        }

        if(is_array($prize) && !empty($prize['prize_name']) && !empty($prize['prize_id'])){
            //写入数据库
            $res = $this->lucky_model->InsertYesRecord($this->uid,$prize['prize_name']);
            if($res){
                $this->ajaxReturn($prize['prize_id'],'ok',$prize['prize_name']);
            }else{
                $this->lucky_model->InsertNoRecord($this->uid);
                $this->ajaxReturn(2,'您与奖品擦肩而过了！');
            }
        }else{
            $this->lucky_model->InsertNoRecord($this->uid);
            $this->ajaxReturn(2,'您与奖品擦肩而过了！');
        }

    }


    /**
     * 我的中奖纪录
     */
    public function getMyRecord(){
        if(!$this->uid){
            $this->ajaxReturn('-4','请先登录');  //前端该弹窗
        }

        //判断是否抽过奖
        $res = $this->lucky_model->parseRecord($this->uid);

        if($res['status'] == 0){
            $this->ajaxReturn('-1','您还没有抽奖记录,请先抽奖');
        }

        if($res['status']== 4){
            $this->ajaxReturn('-2','谢谢参与，您与奖品擦肩而过了');
        }
        //已中奖，告知奖品
        if($res['status'] == 1){
            $this->ajaxReturn('-3',$res['prize_name']);
        }else{
            $this->ajaxReturn('0','服务器错误');
        }

    }

    // Return AJAX
    protected function ajaxReturn($status = 0, $message = '', $data = null)
    {
        header('Content-Type:text/html;charset=gbk');
        $return = array();
        $return['status'] = $status;
        $return['msg'] = urlencode(strval($message));
        $return['data'] = $data;
        echo urldecode(json_encode($return));
        exit();
    }




}


