<?php

namespace frontend\controllers;

use common\models\Brand;
use common\models\MiniRotation;
use frontend\base\CommonController;
use frontend\server\IndexServer;
use frontend\server\HouseServer;
use common\models\Characteristic;
use frontend\base\Help;
use Yii;

class IndexController extends CommonController
{
	
	
	public function actionIndex ()
	{
		
//		$IndexModel = new IndexServer();
//		$city       = $IndexModel->CityList();                //区域
//
//		$price = $IndexModel->PriceList();              //价格区间
//
//		$sub = $IndexModel->Subject();                  //特色主题
//
//		$type = $IndexModel->HouseType();               //楼盘户型
//
//		$lunbo = $IndexModel->Lunbo();                  //轮播图
//
//		$Hotactivity = $IndexModel->Hotactivity();      //热门活动
//
//		$pushCity = $IndexModel->getPushCity( 5 );                                                          //获取首页推送地区栏位区域数据
//
//		$Choice = $IndexModel->Choice();                //本周精选
//
//		$hotHouse      = $IndexModel->HotHouse();            //热销楼盘
//		$cityInfo      = $IndexModel->CityInfo();            //区域介绍
//		$KnowledgeBase = $IndexModel->getKnowledgeBase();    //知识库
//
//		$pushaerial = $IndexModel->getPushAerial( 4 );
//
//		$recommendcat    = $IndexModel->RecommendCat();            //精选新房
//		$recommendcatone = $IndexModel->RecommendCatOne();         //精选新房根据地区读一条  大坑,注意排序
//		$tuijian         = new \common\models\PushHousesale();
//		$tuijian->type   = 2;
//		$Hot             = $tuijian->Homegetlist( 4 );
//
//		$Ctype     = $IndexModel->Characteristic();
//		$news      = $IndexModel->newsColumn();
//		$CityPrice = $IndexModel->Pricetrend();
//		$Gouf      = $IndexModel->Goufang( 5 );
//		$newsGouf  = $IndexModel->Goufang( 6 );
//		$Villa     = $IndexModel->Villa();      //别墅专区


        //获取当前模块
        $IndexModel = new IndexServer();

        $city = $IndexModel->CityList();                                                                        //热门区域
        $price = $IndexModel->PriceList();                                                                      //价格区间

        $BrandModel = new Brand();
        $brand = $BrandModel->getPcSearchBrand(1, 12);                                                          //品牌筛选

        $hotHouse = $IndexModel->getHomeHotHouse(6);                                                      //首页热门楼盘

        $VrHouse = $IndexModel->getVrHouse(4);                                                            //首页VR全景看房推送

        $pushCity = $IndexModel->getPushCity(5);                                                          //获取首页推送地区栏位区域数据

        $pushaerial = $IndexModel->getPushAerial(4);                                                      //获取首页航拍看房推送

        $voluntarilylive = $IndexModel->getVoluntarilylive(3);                                            //自动获取直播回放3个

        $allcity = $IndexModel->getAllCityList();                                                               //获取所有地区

        $pushnews = $IndexModel->getPushNews(5);                                                          //获取推送资讯栏目

        $readhouseword = $IndexModel->getPushTitle(3,8);                                          //首页热搜词

        $gourylist = $IndexModel->getPushTitle(4,3);                                              //首页优惠团购列表

        $lunbo = $IndexModel->Lunbo( 5);                                                                      //轮播图

        $choice = $IndexModel->Lunbo(2);                                                         //本周精选

		
		return $this->render( 'index' ,[
            'city' => $city,
            'price' => $price,
            'lb' => $lunbo,
            'brand' => $brand,
            'hotHouse' => $hotHouse,
            'VrHouse' => $VrHouse,
            'pushCity' => $pushCity,
            'pushaerial'=>$pushaerial,
            'voluntarilylive'=>$voluntarilylive,
            'allcity'=>$allcity,
            'pushnews'=>$pushnews,
            'readhouseword'=>$readhouseword,
            'gourylist'=>$gourylist,
            'choice'=>$choice
        ]);
		
	}
	
	public function actionRecommendlist ()
	{
		$model = new \common\models\PushHouserecommend();
		$input = Yii::$app->request->get();
		if ( !empty( Yii::$app->request->get( 'city' ) ) ) {
			$cityModel       = new \common\models\CategoryCity();
			$city_ids        = $cityModel->TestingId( Yii::$app->request->get( 'city' ) );
			$input[ 'city' ] = $city_ids;
		}
		$recommend = $model->getList( $input );
		foreach ( $recommend as $k => $v ) {
			$recommend[ $k ][ 'characteristic' ] = $this->Subject( json_decode( $v[ 'characteristic' ] ) , 3 );
		}
		$recommendData[ 'recommend' ] = $recommend;
		$IndexModel                   = new IndexServer();
		$recommendData[ 'city' ]      = $IndexModel->CityList();
		if ( !empty( $recommendData ) ) {
			return Help::JsonCode( Help::SUCCESS , '成功' , $recommendData );
		}
		return Help::JsonCode( Help::ERROR , '暂无数据' );
	}
	
	public function actionSearch ()
	{
		$IndexModel = new IndexServer();
		$city       = $IndexModel->CityList();                //区域
		
		$price = $IndexModel->PriceList();              //价格区间
		
		$sub = $IndexModel->Subject();                  //特色主题
		
		$type = $IndexModel->HouseType();               //楼盘户型
		
		$tuijian       = new \common\models\PushHousesale();
		$tuijian->type = 2;
		$Hot           = $tuijian->Homegetlist( 4 );
		$tuijian->type = 4;
		$sea           = $tuijian->Homegetlist( 6 );
		
		//热门区域
		$hotcity = ( new MiniRotation() )->getAll( 4 , 3 );
		
		return $this->renderPartial( 'search' , [ 'city'    => $city ,
		                                          'price'   => $price ,
		                                          'type'    => $type ,
		                                          'sub'     => $sub ,
		                                          'hot'     => $Hot ,
		                                          'sea'     => $sea ,
		                                          'hotcity' => $hotcity ,
		] );
	}
	
	/*
	 * 弹窗搜索结果
	 * */
	public function actionSearchresult ()
	{
		$model = new HouseServer();
		$res   = $model->SearchHouseName( Yii::$app->request->get() );
		if ( !empty( $res ) ) {
			return Help::JsonCode( Help::SUCCESS , '成功' , $res );
		}
		return Help::JsonCode( Help::ERROR , '暂无数据' );
		
	}
	
	
	/**
	 * 区域价格走势
	 */
	public function actionCityprice ()
	{
		$model          = new \common\models\PushCityprice();
		$model->city_id = Yii::$app->request->get( 'id' );
		$rows           = $model->CityRecordPrice();
		if ( !empty( $rows ) ) {
			$arr = [];
			foreach ( $rows as &$val ) {
				//                    $timestrap = strtotime($val['price_at']);
				$time                  = $val[ 'price_at' ];
				$val[ 'price_at' ]     = trim( date( 'm' , $time ) . '月' , '0' );
				$arr[ 'price_at_n' ][] = trim( date( 'Y' , $time ) . '年' , '0' );
				$arr[ 'categories' ][] = $val[ 'price_at' ];
				$arr[ 'data' ][]       = $val[ 'price' ];
				$arr[ 'region' ][]     = $val[ 'city_name' ];
			}
			return Help::JsonCode( Help::SUCCESS , '成功' , $arr );
		}
	}
	
	/**
	 * 楼盘价格走势
	 */
	public function actionHouseprice ()
	{
		$input = Yii::$app->request->post();
		if ( is_numeric( $input[ 'id' ] ) ) {
			$model      = new \common\models\HousePriceRecord();
			$model->hid = $input[ 'id' ];
			$rows       = $model->getList( [] );
			if ( !empty( $rows ) ) {
				$arr = [];
				foreach ( $rows as $val ) {
					$arr[ 'categories' ][] = $val[ 'create_time' ];
					$arr[ 'data' ][]       = $val[ 'price' ];
					$arr[ 'unit' ][]       = $val[ 'price_unit' ];
				}
				return Help::JsonCode( Help::SUCCESS , '成功' , $arr );
			}
		}
		return Help::JsonCode( Help::ERROR , '失败' );
	}
	
	public function actionAa ()
	{
		phpinfo();
	}
	
	//特色主题 超过3个随机返回3个元素
	public function Subject ( $id , $num = 3 )
	{
		$char = new Characteristic();
		if ( is_array( $id ) && !empty( $id ) ) {
			$char->id = $id;
		}
		$charMode = array_column( $char->getList( [] , [ 'id' , 'name' ] ) , 'name' , 'id' );
		if ( $charMode != null ) {
			$count = count( $charMode );
			$arr   = [];
			if ( $count >= 3 ) {
				$rand = array_rand( $charMode , $num );
				foreach ( $rand as $key => $val ) {
					$arr[ $key ] = $charMode[ $val ];
				}
			} else {
				$arr = $charMode;
			}
			return $arr;
		}
	}
}