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

/**
 * 营销中心——贷款管理
 */

class my_loan extends MY_Controller{
    /**
    * 当前用户uid
    *
    * @access private
    * @var int
    */
    private  $_uid = 0;

    /**
    * 当前页码
    *
    * @access private
    * @var string
    */
    private $_current_page = 1;

    /**
    * 每页条目数
    *
    * @access private
    * @var int
    */
    private $_limit = 10;

    /**
    * 偏移
    *
    * @access private
    * @var int
    */
    private $_offset = 0;

    /**
    * 条目总数
    *
    * @access private
    * @var int
    */
    private $_total_count = 0;
    
    
    /**
     * 解析函数
     *
     * @access public
     * @return void
     */
    public function __construct() {
        parent::__construct();
        $this->config_cityflag = 'nj';
        //经纪人账号uid
        $this->_uid = $this->user_arr['uid']; 
       
     
    }    
    
    /**
     * 消息管理
     */
    public function index(){        
      
        $type = $this->input->get('type',TRUE);       
        $post_param_page = $this->input->get('page',TRUE);
        $current_page = $post_param_page !='' ? intval($post_param_page) : 1;
        $loanProduct = new ZsbLoanProduct();
      
        $productType = array('1'=>'二手房贷款','2'=>'抵押贷款','3'=>'信用贷款','4'=>'租房贷款');//产品类型
        
        $arrModule = array_keys($productType);       
       
	    $type = in_array($type, $arrModule) ? $type : $arrModule[0];
        $data = array();
        $where = "";      
        $tab = "";
        $url = '?type=' . $type;        
   
     
        //符合条件的总行数
        $this->_total_count = $loanProduct->findNumberBy("type_id=$type");
		
        
        //计算总页数
        $pages  = $this->_total_count > 0 ? ceil( $this->_total_count / $this->_limit ) : 0;
        
        //如果页数大于实际页数的页数为1
        if($current_page>$pages) $current_page=1;
          
        /** 分页参数 */
        
        $this->_init_pagination($current_page);   
      
        //获取数据源
        $data_info = $loanProduct->findListByType($type,"id desc",$this->_offset , $this->_limit);

        //是否显示分页
        $pagination = NULL;
        if( $this->_total_count > $this->_limit){
           $pagination = helper_pagination( $current_page , $pages , $this->_total_count );
        }
       
       
      
         /** 页面初始化 */
        $data['page_title'] = '营销中心-贷款管理-'.$tab;
        $data['css'] = load_css('zsb/css/v1.0/style1.css'); //需要加载的css  
        $data['total_count'] = $this->_total_count;
        $data['data_info'] = $data_info;
        $data['type'] = $type;
        $data['pagination'] = $pagination;
        $data['page']  = $current_page;
        $data['reward'] = $this->getRewardRule();
        $data['type_name'] = $productType[$type];
        $data['username'] = $this->user_arr['username'];
      
       //$this->load->view( 'loan_index', $data);
	    $data['google_analytics'] = get_ga_js_code('/个人中心/365贷款/申请贷款/');
        $this->view('ucenter','loan_index',$data);
    }
    
    
    
    /**
     * 消息详情
     */
    public function record(){
        $post_param_page = $this->input->get('page',TRUE);
        $current_page = $post_param_page !='' ? intval($post_param_page) : 1;
        $zsbLoanRecord = new ZsbLoanRecord();
        $data_info = array();
        $data = array();      
        $settingType = array('1'=>'待审核','2'=>'办理成功','3'=>'未办理成功','4'=>'信息虚假驳回');//状态
        $productType = array('1'=>'二手房贷款','2'=>'抵押贷款','3'=>'信用贷款','4'=>'租房贷款');//产品类型
        //符合条件的总行数		
        $this->_total_count = $zsbLoanRecord->findNumberBy("broker_id=".$this->_uid);;
        
        //计算总页数
        $pages  = $this->_total_count > 0 ? ceil( $this->_total_count / $this->_limit ) : 0;
        
        //如果页数大于实际页数的页数为1
        if($current_page>$pages) $current_page=1;
        
        /** 分页参数 */
        $this->_init_pagination($current_page);   
        
        //获取数据源
        $data_info = $zsbLoanRecord->findByBroker($this->_uid,"id desc",$this->_offset , $this->_limit);
        //是否显示分页
        $pagination = NULL;
        if( $this->_total_count > $this->_limit){
           $pagination = helper_pagination( $current_page , $pages , $this->_total_count );
        }
       
        
        
         /** 页面初始化 */
        $data['page_title'] = '营销中心-贷款记录';
        $data['css'] = load_css('zsb/css/v1.0/style1.css'); //需要加载的css
        
        $data['total_count'] = $this->_total_count;
        $data['data_info'] = $data_info;      
        $data['pagination'] = $pagination;
        $data['page']  = $current_page;
        $data['reward'] = $this->getRewardRule();
        $data['productType'] = $productType;
        $data['settingType'] = $settingType;
       // $this->load->view( 'ucenter/my_message_detail', $data);
	    $data['google_analytics'] = get_ga_js_code('/个人中心/365贷款/我的贷款/');
        $this->view('ucenter','loan_record',$data);
        
    }
    
    
    /**
     * 添加记录
     * addLoan
     */
    public function addLoan(){
        $loan_name = $this->input->post('loan_name',TRUE);
        $loan_telno = $this->input->post('loan_telno',TRUE);
        $loan_amount = $this->input->post('loan_amount',TRUE);
        $pid = $this->input->post('pid',TRUE);
        $tid = $this->input->post('tid',TRUE);
        $loan_name = iconv("UTF-8","gb2312",$loan_name);
        if($loan_amount&&$loan_name&&$loan_telno){
            $zsbLoanRecord = new ZsbLoanRecord();   
            $fieldData = array('loan_user'=>$loan_name,'loan_telno'=>$loan_telno,'broker_id'=>$this->_uid,
                'broker_name'=>$this->user_arr['username'],'pid'=>$pid,'tid'=>$tid,'amount'=>$loan_amount,'status'=>1);
            $return = $zsbLoanRecord->insert($fieldData);
            if($return>0){
                echo json_encode(array('result'=>1));
                exit;
            }else{
                 echo json_encode(array('result'=>0));
                exit;
            }
        }else{
             echo json_encode(array('result'=>0));
               exit;
        }
        
        
        
        
    }
    
    
    
       /**
    * 初始化分页参数
    *
    * @access public
    * @param  int  $current_page
    * @param  int  $page_size
    * @return void
    */
    private function _init_pagination($current_page = 1 , $page_size = 0)
    {
        /** 当前页 */
        $this->_current_page = ( $current_page && is_numeric( $current_page ) ) ? intval( $current_page ) : 1;

        /** 每页多少项 */
        $this->_limit = ( $page_size && is_numeric( $page_size ) ) ? intval( $page_size ) : $this->_limit;
        
        
        /** 偏移量 */
        $this->_offset = ( $this->_current_page - 1 ) * $this->_limit;

        if($this->_offset < 0)
        {
            $this->jump('/my_loan/index/');
        }
    }
    
    
    /**
     * 奖励规则
     * getRewardRule
     */
    
    private function getRewardRule(){        
        $key = $this->config_cityflag."_loan_reward_rule";
		//$this->mc->delete($key);
        $rewardArr = $this->mc->get($key);
        if($rewardArr === false) {
            $zsbLoanReward = new ZsbLoanReward();
            $rewardArr = $zsbLoanReward->findAllByOrder('id','desc');
            $this->mc->set($key,$rewardArr,1800);	
        }
        return $rewardArr;     
    
    }
    
   
    
    
 
    

    

    
}
