<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Goods extends Front_Controller {
    protected $conf = array(
        'cate'=>array(1=>'实物商品',2=>'虚拟商品'),
        'get_type'=>array(1=>'自取',2=>'代充')
        );
    public function __construct()
    {
        
        parent::__construct();
        $this->load->model('score/Goods_model','goods');
        $this->load->model('score/ShopInfo_model','shop');
        $this->load->model('score/Exchange_model','exchange');
    }


    public function index(){
        //可用积分计算

        $auth = $this->auth;
        $this->input->set_cookie('mocuz_auth', $auth, 0);
        
        
        $data = array(
            'title'=>'积分商城'
            );
        //推荐商品
        $goodslist = $this->checkmod(4);
        $data['user'] = $this->user;
        $data['goods'] = $goodslist;
        $data['mod'] = 4;

        //加载前端视图;
        $this->_view('credit_index',$data);


    }
     /**
     * [goodsType 按照条件获得商品信息]
     * @return [type] [description]
     */   
    public function goodsType($mod)
    {
        $data['title'] = '积分商城';
        $data['goods'] = $this->checkmod($mod);

        $data['mod'] = $mod;//判断是什么类型 实,虚,全,抽
        //加载前端视图;
        $data['turn'] = 1;//没有商品记录
        empty($data['goods'])?$this->_view('not_available',$data):$this->_view('credit_list',$data);
    }
     /**
     * [ checkmod 按照条件获得商品信息]
     * @return [type] [description]
     */  
    public function checkmod($mod)
    {

        switch ($mod) {
            case '1':
                $term =array('cate'=>1,'status'=>1);//实物  上架
                break;
            case '2':
                $term =array('cate'=>2,'status'=>1);//虚拟   上架
                break; 
            case '3':
                $term =array('type'=>2,'status'=>1);//抽奖  上架
                break;   
            case '4':
                $term =array('recommend'=>1,'status'=>1);//推荐  上架
                break;                                       
            default:
                $term = array('status'=>1);
                break;//全部  上架
        }
        return $this->getGoods($term);        
    }
    /**
     * [getGoods 按照条件获得商品信息]
     * @return [type] [description]
     */
    public function getGoods($term)
    {
        $page = empty($_POST['page'])? 1 :$_POST['page'];
        $count = $this->goods->countGoods($term);
      
        $pagesize = 10;
        $start = ($page - 1)*$pagesize;
        if($count ==0)
        {
            return $info =array();exit;
        }
        if(ceil($count/$pagesize) < $page)
        {
            echo json_encode(array('code'=>1,"msg"=>'没有可读数据'));exit;
        }
        $info = $this->goods->getGoods($term,$start, $pagesize);

        foreach ($info as $k => $v) {
            
            $info[$k]['cate'] = $this->conf['cate'][$v['cate']];
            $info[$k]['c_cate'] = $v['cate'];
            // $info[$k]['lastnum'] = $v['num'] - $this->exchange->exchNum($v['id']);
        } 

              
        if($page>1)
        {
            echo json_encode($info);
        }
        else
        {
            return $info;
        }
    }
    /**
     * [deltails 商品详情页面]
     * @return [type] [description]
     */
    public function deltails($gid)
    { 
        $data = $this->goods->getGoodsInfo($gid);
        $data['gid'] = $data['id'];
        $data['auth'] =  $this->auth;
        $data['c_cate'] = $data['cate'];
        $data['cate'] = $this->conf['cate'][$data['cate']];
        $data['get_type'] = $this->conf['get_type'][$data['get_type']];
        $data['shop'] = $this->shop->getShopInfo();
        $data['title'] = '积分商城';
        $data['url'] = MOCUZ_DIR.'/';
        $data['user'] = $this->user;
        //加载前端视图;
        $this->_view('product_details',$data);
    }

    /**
     * [exchange 前端兑换记录]
     * @return [type] [description]
     */

    public function exchange()
    {


        $term = array("uid"=>$this->user->uid);
        $data['title'] = '积分商城';
        $data['exchangeList'] = $this->getExchange($term, true);

        //加载视图
        $data['turn'] = 2;//没有兑换记录
        empty($data['exchangeList'])?$this->_view('not_available',$data):$this->_view('express',$data);
    }

    /**
     * [ajaxExchange 异步加载记录]
     * @param  [type] $uid [用户ID]
     * @return [type]      [description]
     */
    public function ajaxExchange()
    {
        $term = array("uid"=>$this->user->uid);
        return $this->getExchange($term);
    }
    /**
     * [getExchange 按照条件获得商品信息]
     * @return [type] [description]
     */
    public function getExchange($term, $return = false)
    {
        $page = empty($_POST['page'])?1:$_POST['page'];
        $count = $this->exchange->countExchange($term,0,0);

        $pagesize = 10;
        $start = ($page - 1)*$pagesize;
        if($count == 0)
        {
            return $info =array();exit;
        }
        if(ceil($count/$pagesize) < $page)
        {
            if($return){
                return false;
            }else{
                echo json_encode(array('code'=>1,"msg"=>'没有可读数据'));exit;
            }
            
        }
        $info = $this->exchange->getExchangeList($term,$start,$pagesize);
        foreach ($info as $k => $v) {
            $goods = $this->goods->getGoodsInfo($v['gid']);
            $info[$k]['cate'] = $this->conf['cate'][$goods['cate']];
            $info[$k]['name'] = $goods['name'];
            $info[$k]['score'] = $goods['score'];

            $info[$k]['c_cate'] = $goods['cate'];
            $info[$k]['exchange_time'] = date('Y-m-d H:i',$v['exchange_time']);

            $info[$k]['get_type'] = $this->conf['get_type'][$goods['get_type']];
            if($goods['cate'] == 1)
            {
                $info[$k]['address'] = $goods['address'];
            }
        }
        if($page>1)
        {
            echo json_encode($info);
        }
        else
        {
            return $info;
        }
    }
    /**
     * [recharge 显示代充账号输入页面]
     * @param  [type] $gid [description]
     * @return [type]      [description]
     */
    public function recharge($gid, $eid = '')
    {
        //用户
        if(!$this->user->uid){
            $this->login();   
        }        
        $data['auth'] =  $this->auth;
        //$data['auth'] = $auth = 'eca3tMj+dJPNOMSH/uRl7fW8ME0s0r0f4hFyNbP/1AyYJpaHGPgNtLLNVpaZg+X6a5Nvo20a3BJzDaJOFmSP';
        $data['title'] = '积分商城';
        $data['gid'] = $gid;
        $data['eid'] = $eid;
        $this->_view('recharge.php',$data);
    }


    public function update_exchange($eid)
    {
        $exchange = $this->exchange->getOne($eid);
        if($exchange['status'] != 2){
            redirect('scorefront/goods/success/'.$exchange['gid'].'/ok');
        }


        $extra = $this->input->post('extra');
        $rextra = $this->input->post('rextra');

        if(empty($extra) || $extra != $rextra){
            echo 'extra error';
            exit;
        }

        $this->exchange->update(array('extra' => $extra, 'status' => 1), $exchange['id']);
        redirect('scorefront/goods/success/'.$exchange['gid']);
    }




    public function lottery($eid){
        if(!$this->user->uid){
            $this->login();   
        }

        ///////////写入用户ID
        $term = array('uid'=>$this->user->uid,'gid'=>$gid);
        $info = $this->exchange->getSuccess($term);

        $goods = $this->goods->getGoodsInfo($info['gid']);

        if($goods['cate']){

        }

    }


    /**
     * [change 实物兑换]
     * @param  [type] $gid [description]
     * @return [type]      [description]
     */
    public function change($gid)
    {

        if(!$this->user->uid){
            $this->login();   
        }

        $goods = $this->goods->getGoodsInfo($gid);
        redirect('scorefront/goods/success/'.$gid);
    }
    /**
     * [charge 代充账号处理]
     * @return [type] [description]
     */
    public function charge()
    {

        $extra = trim($this->input->post('extra'));
        $rextra = trim($this->input->post('rextra'));
        if($extra !== $rextra)
        {
            return $this->json(0,'两次输入账号不同');exit;
        }

        $gid = $this->input->post('gid');
        $this->change($gid);
    }
    public function success($gid='', $is_ok = '')
    {
        $data['title'] = "积分商城";
        $data['score'] = '';//剩余积分

        if(!$this->user->uid){
            $this->login();   
        }


        
        ///////////写入用户ID
        $term = array('uid'=>$this->user->uid,'gid'=>$gid);
        $info = $this->exchange->getSuccess($term);
        if($info['cate'] == 2)
        {
            $data['extra'] = $info['extra'];
        }
        else
        {
            $goods = $this->goods->getGoodsInfo($gid);
            $data['address'] = $goods['address'];
        }
        $data['is_ok'] = $is_ok;
        $data['cate'] = $info['cate'];
        $data['user'] = $this->user;
        $this->_view('exchange_success',$data);
    }


}
