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

/**
 * Vip统计相关
 */
class Vip extends MY_Controller
{
	public $postData = array('frombg' => 1, 'lid' => 1, 'sid' => 1);
	protected $pageConfig = array(
        'first_tag_open' => '<li>',
        'first_tag_close' => '</li>',
        'last_tag_open' => '<li>',
        'last_tag_close' => '</li>',
        'next_tag_open' => '<li>',
        'next_tag_close' => '</li>',
        'prev_tag_open' => '<li>',
        'prev_tag_close' => '</li>',
        'cur_tag_open' => "<li class='active'><a hef='#'>",
        'cur_tag_close' => "</a></li>",
        'num_tag_open' => "<li>",
        'num_tag_close' => "</li>",
    );
    public function __construct()
    {
        parent::__construct();
        $config = array(
            'savePath' => 'Excel/uploads',             //存储文件夹
            'maxSize'  => 1024*5,                   //允许的文件最大尺寸，单位KB
            'allowFiles' => array('.xls','.xlsx')  //允许的文件格式
        );
        $this->load->library('uploader', $config);
        $this->load->library('PHPExcel');
        $this->load->library('PHPExcel/PHPExcel/IOFactory');
        $this->load->library('curl');
        $this->load->library('pagination');
        $this->load->helper('cookie');
    }
    
    //积分等级分布
    public function lvDistribution(){
    	if( $this->env == 1 ){
			$url = $this->config->item('officialUrl');
		}else{
			$url = $this->config->item('localUrl');
			$this->postData['demo'] = 1;
		}

		$this->postData['mod'] = 'Vip';
		$this->postData['act'] = 'lvDistribution';
		$res = $this->curl->req($url, $this->postData);
		
		$retData = json_decode($res, TRUE);
		$data['data'] = $retData['data'];

        $this->load->view('vip/lvDistribution', $data);
    }
    
    //Vip开通人数
    public function vipOpen(){
    	if( $this->env == 1 ){
			$url = $this->config->item('officialUrl');
		}else{
			$url = $this->config->item('localUrl');
			$this->postData['demo'] = 1;
		}
		
		//VIP开通人数
		$this->postData['mod'] = 'Vip';
		$this->postData['act'] = 'lvDistribution';
		$this->postData['open'] = 1;
		$res = $this->curl->req($url, $this->postData);
		$retData = json_decode($res, TRUE);
		$data['data'] = $retData['data'];
		
		//VIP未开通人数
		$this->postData['open'] = 0;
		$res = $this->curl->req($url, $this->postData);
		$retData = json_decode($res, TRUE);
		$data['data2'] = $retData['data'];

        $this->load->view('vip/vipOpen', $data);
    }
    
    //Vip开通人数(日)
    public function todayVipOpenSta(){
    	if( $this->env == 1 ){
			$url = $this->config->item('officialUrl');
		}else{
			$url = $this->config->item('localUrl');
			$this->postData['demo'] = 1;
		}
		
		if($date = get_cookie('date')){
			
		}else{
			$date = date("Y-m-d",strtotime('-1 days 23:59:59'));
		}
		
		//VIP开通人数
		$this->postData['mod'] = 'Vip';
		$this->postData['act'] = 'todayVipOpenSta';
		$this->postData['date'] = $date;
		$res = $this->curl->req($url, $this->postData);
		$retData = json_decode($res, TRUE);
		
		$out = [];
		for($i = 1; $i <= 15; $i++){
			$out[$i] = empty($retData['data'][$i]) ? 0 : $retData['data'][$i];
		}
		
		$data['data'] = $out;
		$data['date'] = $date;
        $this->load->view('vip/todayVipOpenSta', $data);
    }
    
    //VIP玩家详情
    public function paySta($sort = 'time',$page = 0){
    	if($page == 0){
            $page = 10;
        }else{
            $page += 10;
        }
    	if( $this->env == 1 ){
            $url = $this->config->item('officialUrl');
        }else{
            $url = $this->config->item('localUrl');
            $this->postData['demo'] = 1;
        }

		$this->postData['page'] = $page/10;
        $this->postData['limit'] = 10;
        $this->postData['mod'] = 'Vip';
        $this->postData['act'] = 'paySta';
        $this->postData['sort'] = $sort;
        if(!is_null(get_cookie('uid'))){
            $this->postData['uid'] = get_cookie('uid');
            $data['uid'] = get_cookie('uid');
        }
        $retData = $this->curl->req($url, $this->postData);
        $ret = json_decode($retData, 1);
		$data['data'] = $ret['data'];
		
		//分页
        $data['total'] = $ret['total'];
        $data['page'] = $this->postData['page'];
        $data['editUserList'] = $this->editUserList;
        $pageConfig = $this->pageConfig;
        $pageConfig['base_url'] = base_url('#vip/paySta/'.$sort);
        $pageConfig['total_rows'] = $data['total'];
        $pageConfig['per_page'] = 10;
        $this->pagination->initialize($pageConfig);
        $pageurl = $this->pagination->create_links();
        $data['pageurl'] = $pageurl;
        $data['sort'] = $sort;
        
        $this->load->view('vip/paySta', $data);
    }
    
    //积分获得排行
    public function getRank(){
    	$data = array('stime' => '', 'etime' => '', 'rate' => 100);
		if( $this->env == 1 ){
			$url = $this->config->item('officialUrl');
		}else{
			$url = $this->config->item('localUrl');
			$this->postData['demo'] = 1;
		}
		//时间查询
		$dateArr = array();
		if( $stime = get_cookie('stime') ){
			$data['stime'] = $stime;
			$dateArr['stime'] = strtotime($stime.'0:0:1');
		}else{
			$dateArr['stime'] = strtotime('-7 days 0:0:1');//默认开始时间
			$data['stime'] = date('Y-m-d', $dateArr['stime']);
		}

		if( $etime = get_cookie('etime') ){
			$data['etime'] = $etime;
			$dateArr['etime'] = strtotime($etime.'23:59:59');
		}else{
			$dateArr['etime'] = strtotime('-1 days 23:59:59');//默认结束时间
			$data['etime'] = date('Y-m-d', $dateArr['etime']);
		}
		
		$this->postData['stime'] = $dateArr['stime'];
		$this->postData['etime'] = $dateArr['etime'];
        $this->postData['mod'] = 'Vip';
        $this->postData['act'] = 'getRank';
        $this->postData['rate'] = $rate;
        $retData = $this->curl->req($url, $this->postData);
        $ret = json_decode($retData, 1);
        
        $goods = [];
        foreach ($ret['data']['goods'] as $k => $v){
        	$goods[$v['gid']] = $v;
        }
        $data['goods'] = $goods;
		$data['data'] = $ret['data'];
		
		$this->load->view('vip/getRank', $data);
    }
}