<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 * 营销中心首页
 */

class market_index_zhou extends MY_Controller{
    
    private $broker;
    private $info;
    private $uid;
    private $config_cityflag;


    public function __construct($type = NULL) 
    {
        parent::__construct($type);
        $this->config_cityflag = 'nj';
        $this->load->model('broker_model');
        $this->growthBrokerCredit = new GrowthBrokerCredit($this->broker);
        $this->initGrowthBrokerBidBlockExpert();
        $this->initGrowthBrokerBidHouse();
    }
    
    public function index(){
        //用户信息初始化
        $this->initUserInfo();
        $groupId = $this->info['package_id'];//套餐
        $brokerType = $this->info['groupid'];//免费or付费
        //特权
        $my_privilege= $this->my_privilege($this->info['level'],$brokerType,  $this->info['credit'],$groupId);
        $this->load->model('payment_model');
        
		// 2016-1-14 添加星级数据 -----------start -------->
		
		$this->load->model('broker_zhou_model');
		//echo $uid;die;
		$broker_new_info =$this->broker_zhou_model->get_broker_info_by_uid($this->info['uid']);
		// 2016-1-14 添加星级数据 -----------end -------->

        $documentation = $this->config->item('documentation');
        $viewData['page_title'] = '营销中心-首页';
        $viewData['js'] = load_js('zsb/js/v1.0/js2.js,common/js/jquery.jcountdown.js');   
        $viewData['css'] = load_css('zsb/css/v1.0/style1.css');
        $viewData['broker'] = $this->broker;
        $viewData['info'] = $this->info;
        $viewData['my_privilege'] = $my_privilege;
        $viewData['documentation'] = $documentation;
		$viewData['broker_new_info'] = $broker_new_info;
        $viewData['payment_url'] = $this->payment_model->get_module_url('recharge');
		$viewData['google_analytics'] = get_ga_js_code('/营销中心/首页/');
        $this->view('market','market_index_zhou',$viewData);
    }
    
    
     /**
     * 竞投刷新战况
     * 
     */
    public function market_house_bid_action(){
        $bidBehavior = $this->config->item('bidBehavior');
        $action = $this->input->get('action');
        $request = new Request();
        if ($request->isAjax()) {
            if ($action == 'realData') {
                $this->load->model('broker_model');
                $info = $this->broker_model->get_user_session();
                $broker['id'] = $info['uid'];
                //经纪人成长值等级数据  
                $growthBrokerExp = new GrowthBrokerExp($broker);
                $broker['level'] = $growthBrokerExp->getLevel();
                //积分
                $growthBrokerCredit = new GrowthBrokerCredit($broker);
                $myCredit = $growthBrokerCredit->getValidCredit();
                $broker['credit'] = $myCredit;
                $growthBidPeriods = new GrowthBidPeriods();
                $cBidPeriods = $growthBidPeriods->currentBidPeriods(1);//最新一期（小区专家）
                $cBidPeriodExpertStatus = $cBidPeriods['status'];//最新一期小区竞投状态
                $growthBrokerBidBlockExpert = new GrowthBrokerBidBlockExpert($broker, $growthBrokerCredit);
               
                $bidList = $growthBrokerBidBlockExpert->findNewBidRecord($cBidPeriods);
                foreach($bidList as &$v) {
                    $v['projectname'] = iconv('GB2312', 'UTF-8', urlencode($v['projectname']));
                    $content = iconv('GB2312', 'UTF-8', urlencode($v['content']));
                    $v['content'] = "<a href= 'http://nj.sell.house365.com/district/dl_kw".urlencode($content).".html' target='_blank'>$content </a>"; 
                    $v['max_score'] = iconv('GB2312', 'UTF-8', urlencode($v['max_score']));
                    $v['bDescription'] = iconv('GB2312', 'UTF-8', urlencode($bidBehavior[$v['behavior']]));
                    if ($cBidPeriods['status'] == 2) {
                        $v['bidRankUrl'] = '/market_bid_block_expert/history/';
                    } else {
                        $v['bidRankUrl'] = '/market_bid_block_expert/on/';
                    }
                }
                $cBidPeriods = $growthBidPeriods->currentBidPeriods(2);//最新一期（房源）
                $cBidPeriodHouseStatus = $cBidPeriods['status'];//最新一期房源竞投状态
                $growthBrokerBidHouse = new GrowthBrokerBidHouse($broker, $growthBrokerCredit);
                $growthBrokerBidHouse->setCheckStar(true);//南京多图就是优质，多图看star字段
                $houseRecord = $growthBrokerBidHouse->findNewBidRecord($cBidPeriods);
          
                foreach($houseRecord as &$v) {
                    $v['projectname'] = iconv('GB2312', 'UTF-8', urlencode($v['projectname']));
                    //$v['content'] = iconv('GB2312', 'UTF-8', urlencode($v['content']));
					$v['content'] = "";
                    $words = iconv('GB2312', 'UTF-8', urlencode($v['words']));
                    $new_word = iconv('GB2312', 'UTF-8', urlencode($v['new_words']));//无括号的关键词
                    $v['words'] = "<a href= 'http://nj.sell.house365.com/district/dl_kw".urlencode($new_word).".html' target='_blank'>$words </a>"; 
                    $v['house_link'] = "<a href='http://nj.sell.house365.com/s_".$v['house_id'].".html' target='_blank'>".iconv('GB2312', 'UTF-8', urlencode('查看房源详情'))."</a>";
                    $v['max_score'] = iconv('GB2312', 'UTF-8', urlencode($v['max_score']));
                    $v['bDescription'] = iconv('GB2312', 'UTF-8', urlencode($bidBehavior[$v['behavior']]));
                    if($cBidPeriods['status']== 1 || $cBidPeriods['status']== 3){
                       
                        if($v['type']==2){
                            $keyword = 'hotword';
                        }elseif($v['type']==1){
                            $keyword = 'block';
                        }
                        $v['bidRankUrl'] = '/market_bid_house/' . $keyword
                        . '/on/' . $v['house_id'];
                    }else{
                        $v['bidRankUrl'] = '/market_bid_house_manage/history/';
                    }
                }
                //租房
                $cBidPeriods = $growthBidPeriods->currentBidPeriods(3);//最新一期（房源）
                $cBidPeriodRentStatus = $cBidPeriods['status'];//最新一期房源竞投状态
                $growthBrokerBidRent = new GrowthBrokerBidRent($broker, $growthBrokerCredit);
                $growthBrokerBidRent->setCheckStar(true);//南京多图就是优质，多图看star字段
                $rentRecord = $growthBrokerBidRent->findNewBidRecord($cBidPeriods);
                foreach($rentRecord as &$v) {
                    $v['projectname'] = iconv('GB2312', 'UTF-8', urlencode($v['projectname']));
                    //$v['content'] = iconv('GB2312', 'UTF-8', urlencode($v['content']));
					$v['content'] = "";
                    $words = iconv('GB2312', 'UTF-8', urlencode($v['words']));
                    $new_word = iconv('GB2312', 'UTF-8', urlencode($v['new_words']));//无括号的关键词
                    $v['words'] = "<a href= 'http://nj.rent.house365.com/district/dl_kw".urlencode($new_word).".html' target='_blank'>$words </a>"; 
                    $v['house_link'] = "<a href='http://nj.rent.house365.com/r_".$v['house_id'].".html' target='_blank'>".iconv('GB2312', 'UTF-8', urlencode('查看房源详情'))."</a>";
                    $v['max_score'] = iconv('GB2312', 'UTF-8', urlencode($v['max_score']));
                    $v['bDescription'] = iconv('GB2312', 'UTF-8', urlencode($bidBehavior[$v['behavior']]));
                    if($cBidPeriods['status']== 1 || $cBidPeriods['status']== 3){
                       
                        if($v['type']==2){
                            $keyword = 'hotword';
                        }elseif($v['type']==1){
                            $keyword = 'block';
                        }
                        $v['bidRankUrl'] = '/market_bid_rent/' . $keyword
                        . '/on/' . $v['house_id'];
                    }else{
                        $v['bidRankUrl'] = '/market_bid_rent_manage/history/';
                    }
                }



				$cBidPeriods = $growthBidPeriods->currentBidPeriods(4);//最新一期（学区专家）
                $cBidPeriodExpertStatus = $cBidPeriods['status'];//最新一期小区竞投状态
                $growthBrokerBidSchoolExpert = new GrowthBrokerBidSchoolExpert($broker, $growthBrokerCredit);
               
                $bidList = $growthBrokerBidSchoolExpert->findNewBidRecord($cBidPeriods);
                foreach($bidList as &$v) {
                    $v['projectname'] = iconv('GB2312', 'UTF-8', urlencode($v['projectname']));
                    $content = iconv('GB2312', 'UTF-8', urlencode($v['content']));
                    $v['content'] = "<a href= 'http://nj.sell.house365.com/district/dl_kw".urlencode($content).".html' target='_blank'>$content </a>"; 
                    $v['max_score'] = iconv('GB2312', 'UTF-8', urlencode($v['max_score']));
                    $v['bDescription'] = iconv('GB2312', 'UTF-8', urlencode($bidBehavior[$v['behavior']]));
                    if ($cBidPeriods['status'] == 2) {
                        $v['bidRankUrl'] = '/market_bid_school_expert/history/';
                    } else {
                        $v['bidRankUrl'] = '/market_bid_school_expert/on/';
                    }
                }
               
                $bidList = array_merge($bidList,$houseRecord);
                $bidList = array_merge($bidList,$rentRecord);
                //排序按竞投时间排序
                $volume = array();
                foreach($bidList as $key=>&$v) {
                    $v['utime'] = strtotime($v['utime']);
                    $volume[$key] = $v['utime'];
                }
                array_multisort($volume, SORT_DESC, $bidList);
                //经纪人竞投状态
                if(count($bidList)==0){
                    if($cBidPeriodHouseStatus==1||$cBidPeriodExpertStatus==1
                            ||$cBidPeriodRentStatus==1){
                        //竞投开放时期没有参加竞投
                       $brokerBidStatus = 'no_attend';
                    }elseif($cBidPeriodHouseStatus!=1&&$cBidPeriodExpertStatus!=1
                            ||$cBidPeriodRentStatus!=1){
                       //竞投结束&推广期
                       $brokerBidStatus = 'bid_over';
                    }
                }else{
                    //参加竞投
                    $brokerBidStatus = 'attend';
                }    
                
                $returnData = array('status'=>$brokerBidStatus,'info'=>$bidList);
         header('Content-Type:text/html;charset=GB2312'); 
		echo urldecode(json_encode($returnData));
	    }
	}
    }
    
    
    
    
    /**
     * 初始化经纪人信息
     */
    private function initUserInfo() {
        $info = $this->broker_model->get_user_session();
        
        $groupId = $info['package_id']; //套餐
        $brokerType = $info['groupid'];//免费or付费
        $gbe = new GrowthBrokerExp(array('id' => $info['uid'], 'groupid' => $groupId));
        $gbc = new GrowthBrokerCredit(array('id' => $info['uid'], 'groupid' => $groupId));
        $broker_detail = $this->broker_model->get_broker_detail();//经纪人详细信息
        if($broker_detail['master']){
            $accout_manager = $this->broker_model->get_account_manager($broker_detail['master']);//客户经理
            if (isset($accout_manager) && !empty($accout_manager)) {
                $info['accout_manager'] = $accout_manager['truename'];
                $info['manager_tel'] = $accout_manager['telno'];
            }
        }
        //等级
        $info['level'] = $gbe->getLevel(2);
        //经验
        $info['exp'] = $gbe->getExp();
        //成长值线        
        $allLevel = $this->allLevel();
        $growthAllLevelNum = count($allLevel);
        $info['level_num'] = intval(substr($info['level'],2));
	$info['level_line'] = ceil($info['level_num']/$growthAllLevelNum*100);
        //房豆
        $info['credit'] = $gbc->getValidCredit();
        $this->info = $info;
        $this->uid = $info['uid'];
        
        $broker['smallphoto'] = $info['smallphoto'] ? $info['smallphoto'] : "http://esf.fz.house365.com/zsb/images/v1.0/defaultface.jpg";
        $broker['id'] = $this->uid;
        $broker['level'] = 'LV'.$this->info['level'];
        $broker['credit'] = $this->info['credit'];
        $broker['groupid'] = $groupId;
        $broker['is_pay'] = $this->info['is_pay'];
        $this->broker = $broker;
    }
    
    
   

    //特权
    private function my_privilege($brokerLevel,$brokerType,$brokerCredit,$packageid) {
        $privilege = array();
        //$privilege[] = $this->getPrivilegeBlockExpert($brokerLevel, $brokerType, $brokerCredit,$packageid);
        //$privilege[] = $this->getPrivilegeBuy($brokerLevel, $brokerType, $brokerCredit,$packageid);
        //$privilege[] = $this->getPrivilegeRent($brokerLevel, $brokerType, $brokerCredit,$packageid);
        $this->load->library('TeQuan');
        $privilege = $this->tequan->init($brokerLevel,$brokerType,$brokerCredit,$packageid)->getAll();
        //var_dump($x);exit;
        $data = array();
        $data['privilege'] = $privilege;
    
        return $this->load->view('ucenter/my_privilege_zhou.php',$data,true);
    }
    
    
   /**
     * 初始化成长体系-经纪人小区专家竞投类
     */
    private function initGrowthBrokerBidBlockExpert() {
        $growthConfig = $this->config->item('growth');
        $mode = $growthConfig['mode'];
        $this->growthBrokerBidBlockExpert = new GrowthBrokerBidBlockExpert($this->broker, $this->growthBrokerCredit, $mode);
        //$this->growthBrokerBidHouse->setCheckStar(true);
        $additionalScore = $growthConfig['additionalScore']['block'];
        $this->growthBrokerBidBlockExpert->setAdditionalScore($additionalScore);
    }
    
    /**
     * 初始化成长体系类
     */
    private function initGrowthBrokerBidHouse() {
        $growthConfig = $this->config->item('growth');
        $mode = $growthConfig['mode'];
        $this->growthBrokerBidHouse = new GrowthBrokerBidHouse($this->broker, $this->growthBrokerCredit,$mode);
        $this->growthBrokerBidHouse->setCheckStar(true);
        $additionalScore = $growthConfig['additionalScore']['house'];
        $this->growthBrokerBidHouse->setAdditionalScore($additionalScore);
    }
    
        /**
     * 返回全部等级数组
     * @return type
     */
    private function allLevel() {
        $key = $this->config_cityflag."_growth_level_data";
        $allLevel = $this->mc->get($key);
        if($allLevel === false) {
            $growthLevel = new GrowthLevel();
            $allLevel = $growthLevel->findAllLevelData();
            $this->mc->set($key,$allLevel,600);	
        }
        return $allLevel;
    }
    
    
      
}