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

/**
 * 用户中心 成长足迹
 */
class Ucenter_history extends MY_Controller {

    /**
     * 解析函数
     *
     * @access public
     * @return void
     */
    public function __construct() {
        parent::__construct();
    }

    /**
     * Index Page for this controller.
     */
    public function credit($day=0) {
        $css = load_css('zsb/css/v1.0/style1.css');
        show_404();
        $info = $this->broker_model->get_user_session();
        $brokerId = $info['uid'];
        $groupId = $info['package_id'];
        //$gbe = new GrowthBrokerExp(array('id'=>$info['uid'],'groupid'=>$groupId));
        $gbc = new GrowthBrokerCredit(array('id'=>$brokerId,'groupid'=>$groupId));
        $viewData = array();
        //当前房豆
        $viewData['credit'] = $gbc->getValidCredit();
        //房豆记录
        $last7Days = $this->last7Day();
        if(!(is_numeric($day)&&$day>=0&&$day<7)) {//时间错误，只能过去7天内
            show_404('页面');
        }
        $viewData['last7Days'] = $last7Days;
        $date = $last7Days[$day];
        $viewData['day'] =  $day;
        
	$growthRecord = new GrowthCreditRecord(date("Ymd",$date));
        $pageSegment = 5;
        $page = $this->uri->segment($pageSegment);
        $page = $page?$page:1;
        $per_page = 10;
        $start = ($page-1)*$per_page;
        //总条数
        $total = $growthRecord->findCountByBrokerId($brokerId,1);//echo $total;exit;
        if($total&&$total<$start+1) {//页数错误
            show_404();
        }
        //房豆记录
        $records = $total?$growthRecord->findByBrokerId($brokerId,$start,$per_page,'desc',1):array();
        $viewData['records'] = $records;
        
        //总共获得多少房豆
	$growthCreditDayCount = new GrowthCreditDayCount();
	$growthDaySum = $total?$growthRecord->findSumCreditByBrokerId($brokerId,1):0;
        $viewData['growthDaySum'] = $growthDaySum;
        
        //分页
        $base_url = '/ucenter_history/credit/'.$day.'/page/';
        $viewData['page'] = $this->page($total, $per_page, $pageSegment, $base_url);


        $viewData['page_title'] = '租售宝-个人中心-成长足迹-我的房豆记录';
        $viewData['css'] = $css;
        $viewData['con'] = 'credit';
        $viewData['curpage'] = $page;
		$viewData['google_analytics'] = get_ga_js_code('/个人中心/成长足迹/');
        $this->view('ucenter', 'ucenter/my_history', $viewData);
    }

    public function exp($day=0) {
        //加载broker模型类
        $this->load->model('broker_model');
        $css = load_css('zsb/css/v1.0/style1.css');
        
        $info = $this->broker_model->get_user_session();
        $brokerId = $info['uid'];
        $groupId = $info['package_id'];
        $gbe = new GrowthBrokerExp(array('id'=>$info['uid'],'groupid'=>$groupId));
        //$gbc = new GrowthBrokerCredit(array('id'=>$brokerId,'groupid'=>$groupId));
        $viewData = array();
        //当前成长值
        $viewData['exp'] = $gbe->getExp();
        //成长值记录记录
        $last7Days = $this->last7Day();
        if(!(is_numeric($day)&&$day>=0&&$day<7)) {//时间错误，只能过去7天内
            show_404('页面');
        }
        
        $viewData['last7Days'] = $last7Days;
        $date = $last7Days[$day];
        $viewData['day'] =  $day;
        
        $growthRecord = new GrowthExpRecord(date("Ymd",$date));
        $pageSegment = 5;
        $page = $this->uri->segment($pageSegment);
        $page = $page?$page:1;
        $per_page = 10;
        $start = ($page-1)*$per_page;
        //总条数
        $total = $growthRecord->findCountByBrokerId($brokerId);//echo $total;exit;
        if($total&&$total<$start+1) {//页数错误
            show_404();
        }
        //房豆记录
        $records = $total?$growthRecord->findByBrokerId($brokerId,$start,$per_page,'desc'):array();
        $viewData['records'] = $records;
        //总共获得多少房豆
	    $growthExpDayCount = new GrowthExpDayCount();
	    $growthDaySum = $total?$growthExpDayCount->findExpSumByTimeBrokerId(date("Y-m-d",$date),$brokerId):0;
        $viewData['growthDaySum'] = $growthDaySum;
        
        //分页
        $base_url = '/ucenter_history/exp/'.$day.'/page/';
        $viewData['page'] = $this->page($total, $per_page, $pageSegment, $base_url);
        
        $viewData['page_title'] = '租售宝-个人中心-成长足迹-我的成长记录';
        $viewData['css'] = $css;
        $viewData['con'] = 'exp';
        $this->view('ucenter', 'ucenter/my_history', $viewData);
    }
    
    /**
     * 过去7天数组
     */
    private function last7Day() {
        $arr = array();
        for($i=0;$i<7;$i++) {
            $arr[] = strtotime("-".$i." Days");
        }
        return $arr;
    }
    
    private function page($total,$per_page,$pageSegment,$base_url) {
        //分页配置
        $this->load->library('pagination');
        $config['use_page_numbers'] = TRUE;
        $config['full_tag_open'] = '<div class="picPreviewPag">';
        $config['full_tag_close'] = '</div>';
        //首页
        $config['first_link'] = '首页';
        $config['first_tag_open'] = '<span class="prev">';
        $config['first_tag_close'] = '</span>';
        //末页
        $config['last_link'] = '末页';
        $config['last_tag_open'] = '<span class="next">';
        $config['last_tag_close'] = '</span>';
        //下一页
        $config['next_link'] = '下一页&gt;';
        $config['next_tag_open'] = '<span class="next">';
        $config['next_tag_close'] = '</span>';
        //上一页
        $config['prev_link'] = '&lt;上一页';
        $config['prev_tag_open'] = '<span class="prev">';
        $config['prev_tag_close'] = '</span>';
        //当前页
        $config['cur_tag_open'] = '<span class="lin current">';
        $config['cur_tag_close'] = '</span>';
        //其他页
        $config['num_tag_open'] = '<span class="lin">';
        $config['num_tag_close'] = '</span>';
        $config['anchor_class'] = 'class="xx"';
        
        $config['base_url'] = $base_url;//
        $config['total_rows'] = $total;
        $config['per_page'] = $per_page;
        $config['uri_segment'] = $pageSegment;
        $config['num_links'] = 3;

        $this->pagination->initialize($config); 

        return $this->pagination->create_links();
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
