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

class Vas_packing_house extends MY_Controller {

    /**
     * 解析函数
     *
     * @access public
     * @return void
     */
    public function __construct($type = NULL) {
        parent::__construct($type);
        $this->config_cityflag = 'nj';
        $this->load->model('broker_model');
        $this->load->model('payment_model');
        $this->house = $this->config->item('house');
        $this->load->helper('common_url_helper');
          //用户信息初始化
        $info = $this->broker_model->get_user_session();

        $groupId = $info['package_id']; //套餐
        $gbc = new GrowthBrokerCredit(array('id' => $info['uid'], 'groupid' => $groupId));
        $gbe = new GrowthBrokerExp(array('id' => $info['uid'], 'groupid' => $groupId));
        //房豆
        $info['credit'] = $gbc->getValidCredit();
        //等级
        $info['level'] = $gbe->getLevel(2);
        //经验
        $info['exp'] = $gbe->getExp();
        $this->info = $info;
    }


    /**
     * 房源包装页面
     */
    public function index() {
         $check = $this->checkVasLevel();
        if ($check!==true) {
            $page_text = "您升级至&nbsp;<span style='color:green;'>{$check}</span>&nbsp;级就可以使用此功能哦 ！<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;先去升级吧，加油！";
            $this->jump('/', $page_text, 4000);
            return;
        }
        

        $viewData['page_title'] = '房源包装';
        $viewData['info'] = $this->info;
         $viewData['documentation'] = $this->config->item('documentation');
        //获取房源包装价格
        $VasPrice = new VasPrice();
        $viewData['price']  =   $VasPrice->findByType('Search');
        //获取房源包装已购买情况
        $VasQueueLimit  = new  VasQueueLimit();
        $viewData['buy_num']  =   $VasQueueLimit->findtypenum('Search');
        $viewData['limit_num'] =  $VasQueueLimit->findtypelimitnum('Search');
        $viewData['css'] = load_css('zsb/css/v1.0/style1.css');
        $viewData['payment_url'] = $this->payment_model->get_module_url('recharge');
        //加载房源类
        $this->load->model('sell_model');
        $uid = $this->user_arr['uid'];//账号
        //查询房源条件
        $cond_where = "uid = '".$uid."' AND esta = 1  AND infofrom = '5' ";
        $viewData['house_all'] = $this->sell_model->find_all_by( $cond_where , 0,200);
        $viewData['house_num'] = count( $viewData['house_all']);
		$viewData['google_analytics'] = get_ga_js_code('/营销中心/房源包装/我要购买/');
        $this->view('market','vas/vas_packing', $viewData);
    }

        /**
     * 房源包装订单列表
     */
    public function orderlist() {
          $check = $this->checkVasLevel();
        if ($check!==true) {
            $page_text = "您升级至&nbsp;<span style='color:green;'>{$check}</span>&nbsp;级就可以使用此功能哦 ！<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;先去升级吧，加油！";
            $this->jump('/', $page_text, 4000);
            return;
        }
		

         $viewData['page_title'] = '房源包装订单列表';
         $viewData['info'] = $this->info;
         $viewData['documentation'] = $this->config->item('documentation');
         $viewData['packing_house_staus'] = $this->config->item('packing_house_staus');
         $viewData['packing_house'] = $this->config->item('packing_house');
        //获取房源包装价格
        $VasPrice = new VasPrice();
        $viewData['price']  =   $VasPrice->findByType('Search');
        //获取房源包装已购买情况
        $VasQueueLimit  = new  VasQueueLimit();
        $viewData['buy_num']  =   $VasQueueLimit->findtypenum('Search');
        $viewData['limit_num'] =  $VasQueueLimit->findtypelimitnum('Search');

        $viewData['css'] = load_css('zsb/css/v1.0/style1.css');
        $viewData['payment_url'] = $this->payment_model->get_module_url('recharge');
        //获取订单
        $VasRecord = new VasRecord('vas_packing_house_record');
        $where = "uid = ".$this->user_arr['uid'];
        if($this->input->post('packing_house_staus')){
            $viewData['packing_staus']  = $this->input->post('packing_house_staus');
            $where .= " and status=".$viewData['packing_staus'];
        }
        $viewData['record_num'] = $VasRecord->findNumberBy($where);
        $viewData['pagesize'] = 15;//设定每一页显示的记录数
        $viewData['pages'] =  $viewData['record_num'] ? ceil( $viewData['record_num']/$viewData['pagesize']) : 0 ;  //计算总页数
        $viewData['page'] = $this->input->post('page') ? intval($this->input->post('page')) :1 ; // 获取当前页数
        $viewData['page'] = ($viewData['page']>$viewData['pages'] && $viewData['pages']!=0) ? $viewData['pages'] : $viewData['page'];  //判断跳转页数
        $viewData['offset'] = $viewData['pagesize']*($viewData['page']-1);   //计算记录偏移量
                //是否显示分页
        $pagination = NULL;
        if( $viewData['record_num'] > $viewData['pagesize'])
        {
            $viewData['pagination'] = helper_pagination( $viewData['page'] , $viewData['pages'] , $viewData['record_num']  );
        }
        $recordlist   = $VasRecord->findBy($where,'buy_time desc',$viewData['pagesize'], $viewData['offset']);
       $Vas_packing_house = new VasRecord('vas_packing_house');
        if(is_array($recordlist) && !empty($recordlist)){
            foreach($recordlist as $key=>$value){
                 $conf = "record_id =".$value['id'];
                 $housedetail = $Vas_packing_house->findBy($conf);
             if(is_array( $housedetail) && !empty($housedetail)){
                 $housestr='';
                 foreach($housedetail as $value2){
                   $housestr .= '<a class="c1" href="'.get_sellinfo_url($value2['houseid']).'" target="_blank">'.$value2['houseid'];
                   $housestr.= $value2['house_typ']==2?'备用</a> ':' </a>';
                    }
               }
               $viewData['recordlist'][$key] = $value;
               $viewData['recordlist'][$key]['housestr'] = $housestr;
            }
         }
		$viewData['google_analytics'] = get_ga_js_code('/营销中心/房源包装/我的购买记录/');
        $this->view('market','vas/vas_orderlist', $viewData);
    }

    /**
     * Index Page for this controller.
     */
    public function buy() {
         $check = $this->checkVasLevel();
        if ($check!==true) {
            $page_text = "您升级至&nbsp;<span style='color:green;'>{$check}</span>&nbsp;级就可以使用此功能哦 ！<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;先去升级吧，加油！";
            $this->jump('/', $page_text, 4000);
            return;
        }

        $viewData['page_title'] = '房源包装购买列表';
        $viewData['info'] = $this->info;
        //加载房源类
        $this->load->model('sell_model');
        $uid = $this->user_arr['uid'];//账号
        //查询房源条件
        $cond_where = "uid = '".$uid."' AND esta = 1  AND infofrom = '5' ";
        $viewData['house_all'] = $this->sell_model->find_all_by( $cond_where , 0,200);
        //获取房源包装价格
        $VasPrice = new VasPrice();
        $viewData['price']  =   $VasPrice->findByType('Search');
        //获取房源包装已购买情况
        $VasQueueLimit  = new  VasQueueLimit();
        $viewData['buy_num']  =   $VasQueueLimit->findtypenum('Search');
        $viewData['limit_num'] =  $VasQueueLimit->findtypelimitnum('Search');
        $viewData['css'] = load_css('zsb/css/v1.0/style1.css,zsb/css/v1.0/jquery.mCustomScrollbar.css');
        $viewData['payment_url'] = $this->payment_model->get_module_url('recharge');
        $this->load->model('street_model');
        $this->view('market','vas/vas_packing_buy', $viewData);
    }

    /**
     * 生成订单
     */
     public function addorder(){
       $broker['id'] = $this->info['uid'];
       $broker['groupid'] = $this->info['package_id'];
       $str = $this->input->post('str');
       $str = rtrim($str,',');
       $id_arr = explode(',',$str);
         //不满五条房源
       if(count($id_arr)<5){
             echo json_encode(array('status'=>'2','msg'=>'不满五条房源'));exit;
       }

               //获取房源包装已购买情况
        $VasQueueLimit  = new  VasQueueLimit();
        $viewData['buy_num']  =   $VasQueueLimit->findtypenum('Search');
        $viewData['limit_num'] =  $VasQueueLimit->findtypelimitnum('Search');
        if($viewData['buy_num']>=$viewData['limit_num'])
            {
                echo json_encode(array('status'=>'2','msg'=>'超过最大购买次数'));exit;
            }
              //获取房源包装价格
        $VasPrice = new VasPrice();
        $viewData['price']  =   $VasPrice->findByType('Search');
              //获取房源包装价格
        $GrowthBrokerCredit = new GrowthBrokerCredit($broker);
        $price = $viewData['price']['current_price']?$viewData['price']['current_price']:$viewData['price']['cost_price'];
        $pay_order  = $GrowthBrokerCredit->reduce('packingHouse','房源包装',$price);
        if($pay_order['status']==1){
            //记录详细信息
           $fieldData=array('type'=>'Search','uid'=>$this->info['uid'],'buy_time'=>date("Y-m-d H:i:s",time()),'order_id'=>$pay_order['transaction_id'],'cost'=>$price,'status'=>1);
           $VasSetPromotRecord = new VasSetPromotRecord('vas_packing_house_record');
           $record_id = $VasSetPromotRecord->insert($fieldData);
          //记录房源信息
            if(!empty( $id_arr)){
                   $Vas_packing_house = new VasSetPromotRecord('vas_packing_house');
                    foreach ($id_arr as $key=>$value) {
                        $house_typ = $key>=5?2:1;
                        $fieldData=array('record_id'=>$record_id,'houseid'=>$value,'house_typ'=>$house_typ);
                        $Vas_packing_house->insert($fieldData);
                    }

            }
            //更新购买次数
             $fieldData = array('number'=> $viewData['buy_num']+1);
             $conf = "type = 'Search'";
            $VasQueueLimit->update($fieldData,$conf);
            //添加购买人数
            $VasBroker = new VasBroker();
            //2表示房源包装
            $VasBroker->addJoinVasRecord(2, $broker['id'] );

        }
      print json_encode($pay_order);
     }

    /**
     *
     * @return boolean 成功时返回true，失败时返回等级
     */
    private function checkVasLevel() {
        //判断有无权访问
        $brokerLevel = $this->info['level'];
        //经纪人操作等级最低权限
        $permi = new GrowthLevelWayPermission();
        $minLevel = $permi->findMinLevelByWayAliasAndType('packingHouse',2);

        if ($brokerLevel < $minLevel || $minLevel == '') {
            return $minLevel;
        } else {
            return true;
        }
    }


}

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

