<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/4/19
 * Time: 下午2:52
 */

namespace mobile\controllers;

use common\models\CategoryCity;
use common\models\CategoryTelCity;
use common\models\PushRotation;
use common\models\PushmCity;
use common\models\PushmColumn;
use mobile\base\CommonController;
use mobile\server\IndexServer;
use Yii;
use mobile\base\Help;

class IndexController extends CommonController
{

    public $enableCsrfValidation = false;

    /*public function actionIndex()
    {
	    //获取当前模块
	    $module = Yii::$app->hostserver->module;
	    //导航
	    $sysmenu = new \common\models\Frontend();
	    $sysmenu->pid = 208;
	    $sysrows = $sysmenu->SonList(8, ['name', 'url', 'icon', 'id', 'function']);
	
	    $lunbo = (new PushRotation())->getRoationChart(5, 5);                 //轮播图
	
	    $IndexModel = new IndexServer();
	
	    $news = $IndexModel->newslist();                                //咨询列表
		
	    $housetou = $IndexModel->getHouseTouTiao(1,3);                     //获取房产头条推送
	
	    $hothouse = $IndexModel->getHotHouse(12,3);                                     //获取首页热榜楼盘
	
	    $PCIndexModel = new \frontend\server\IndexServer();
	    $vrhouse = $PCIndexModel->getVrHouse(4);                                                //获取VR看房
	    $pushaerial = $PCIndexModel->getPushAerial(4);                                          //获取航拍实景
	    //区域
	    $gCity = Yii::$app->request->get('city');
	    if (empty($gCity)) {
		    $gCity = 60;
		    if ($gCity != 0) {
			    $cityName = CategoryCity::findOne($gCity)['city_name'];
		    } else {
			    $cityName = '海南';
		    }
	    } else {
		    $cityName = CategoryCity::findOne($gCity)['city_name'];
	    }
	
	
	    return $this->render($this->action->id, [
		    'module' => $module['module'],
		    'sysmenu' => $sysrows,
		    'cityname' => $cityName,
		    'news' => $news,
		    'lunbo'=>$lunbo,
		    'housetou'=>$housetou,
		    'hothouse'=>$hothouse,
		    'vrhouse'=>$vrhouse,
		    'pushaerial'=>$pushaerial
	    ]);
    }*/
	
	/**
	 * 移动端 - 首页
	 * @return string
	 */
	public function actionIndex()
	{
		//获取当前模块
		$module = Yii::$app->hostserver->module;
		//导航
		$sysmenu = new \common\models\Frontend();
		$sysmenu->pid = 208;
		$sysrows = $sysmenu->SonList(8, ['name', 'url', 'icon', 'id', 'function']);
		
		$lunbo = (new PushRotation())->getRoationChart(5, 5);                 //轮播图
		
		$IndexModel = new IndexServer();
		
		$news = $IndexModel->newslist();                                //咨询列表
		
		$housetou = $IndexModel->getHouseTouTiao(1,3);                     //获取房产头条推送
		
		$hothouse = $IndexModel->getHotHouse(12,3);                                     //获取首页热榜楼盘
		
		$PCIndexModel = new \frontend\server\IndexServer();
		$vrhouse = $PCIndexModel->getVrHouse(4);                                                //获取VR看房
		$pushaerial = $PCIndexModel->getPushAerial(4);                                          //获取航拍实景
		
		//区域
		$gCity = Yii::$app->request->get('city');
		if (empty($gCity)) {
			$gCity = 68;
			if ($gCity != 0) {
				$cityName = CategoryCity::findOne($gCity)['city_name'];
			} else {
				$cityName = '海南';
			}
		} else {
			$cityName = CategoryCity::findOne($gCity)['city_name'];
		}
		
		
		return $this->render($this->action->id, [
			'module' => $module['module'],
			'sysmenu' => $sysrows,
			'cityname' => $cityName,
			'news' => $news,
			'lunbo'=>$lunbo,
			'housetou'=>$housetou,
			'hothouse'=>$hothouse,
			'vrhouse'=>$vrhouse,
			'pushaerial'=>$pushaerial
		]);
		
	}

    public function actionHothouses()
    {
//        $model = new IndexServer();
//        $hot = $model->Hothouse(Yii::$app->request->post());
//        if(!empty($hot))
//        {
//            foreach ($hot as &$val)
//            {
//                $val['thumb'] = Yii::$app->params['httpImg']['hosts'].Yii::$app->params['httpImg']['houses'].$val['thumb'];
//            }
//            return Help::JsonCode(Help::SUCCESS,'成功',$hot);
//        }
        return Help::JsonCode(Help::SUCCESS, '成功', []);
    }

    public function actionSearchalert()
    {
        $model = new IndexServer();
        $shaixuan = $model->shaixuan();
        $price = $model->PriceList();
        //热门区域
        $CityModel = new PushmCity();
        $CityModel->type = 1;
        $CityModel->abroad = 1;
        $HotCity = $CityModel->Homelist(7);

        $Housetype = \common\models\CategoryHousetype::find()->andWhere(['del' => 1])->andWhere(['state' => 1])->orderBy(['sort' => SORT_ASC])->limit(4)->asArray()->all();
        return $this->renderPartial('searchalert', ['shaixuan' => $Housetype, 'hot_city' => $HotCity]);
    }


    /**
     * 首页-底部 拨打电话图标
     * @return mixed
     */
    public function actionTelrand()
    {
        //二级域名,mhainan.xxx.com
        if (Yii::$app->hostserver->hostId != 0) {
            $input = Yii::$app->request->post();

            //没有选择区域的情况下
            if (empty($input['city']) || $input['city'] == 0) {
                $tel = new CategoryTelCity();

                //调取二级域名hostId的电话
                $hostid = Yii::$app->hostserver->hostId;
                $tel->cid = $hostid;

                if ($hostid == 37) {
                    $tel->cid = 40; //防城港
                }
                if ($hostid == 48) {
                    $tel->cid = 49; //昆明
                }
                if ($hostid == 28) {
                    $tel->cid = 62; //珠海
                }

                $resultTel = $tel->CityTelOne();
                if (!empty($resultTel)) {
                    $telArr = explode(',', $resultTel['tel']);
                    $index = array_rand($telArr, 1);
                    if (!empty($telArr[$index])) {
                        return Help::JsonCode(Help::SUCCESS, '成功', ['tel' => $telArr[$index]]);
                    }
                }
            }

            //有选择区域的情况下
            if (!empty($input['city']) && $input['city'] != 0) {
                $tel = new CategoryTelCity();
                //调取二级域名hostId的电话
                $tel->cid = $input['city'];
                $resultTel = $tel->CityTelOne();
                if (!empty($resultTel)) {
                    $telArr = explode(',', $resultTel['tel']);
                    $index = array_rand($telArr, 1);
                    if (!empty($telArr[$index])) {
                        return Help::JsonCode(Help::SUCCESS, '成功', ['tel' => $telArr[$index]]);
                    }
                } else {  //如果区域市县没有电话,返回域名的电话
                    $tel = new CategoryTelCity();
                    $tel->cid = Yii::$app->hostserver->hostId;
                    $resultTel = $tel->CityTelOne();
                    if (!empty($resultTel)) {
                        $telArr = explode(',', $resultTel['tel']);
                        $index = array_rand($telArr, 1);
                        if (!empty($telArr[$index])) {
                            return Help::JsonCode(Help::SUCCESS, '成功', ['tel' => $telArr[$index]]);
                        }
                    }
                }
            }

        } else if (Yii::$app->hostserver->hostId == 0) {
            //m.shuofangw.com//全国,一级域名
            $input = Yii::$app->request->post();
            //没有选择区域的情况下
            if (empty($input['city']) || $input['city'] == 0) {
                //调取三亚的电话
                $tel = new CategoryTelCity();
                $tel->cid = 6;
                $resultTel = $tel->CityTelOne();
                if (!empty($resultTel)) {
                    $telArr = explode(',', $resultTel['tel']);
                    $index = array_rand($telArr, 1);
                    if (!empty($telArr[$index])) {
                        return Help::JsonCode(Help::SUCCESS, '成功', ['tel' => $telArr[$index]]);
                    }
                }

            }

            //有选择区域的情况下
            if (!empty($input['city']) && $input['city'] != 0) {
                $city = CategoryCity::findOne($input['city']);
                $tel = new CategoryTelCity();
                $tel->cid = $input['city'];

                if ($input['city'] == 37) {
                    $tel->cid = 40; //防城港
                }
                if ($input['city'] == 48) {
                    $tel->cid = 49; //昆明
                }
                if ($input['city'] == 28) {
                    $tel->cid = 62; //珠海
                }

                //选择的是省
                if ($city['pid'] == 0) {
                    $resultTel = $tel->CityTelOne();
                    if (!empty($resultTel)) {
                        $telArr = explode(',', $resultTel['tel']);
                        $index = array_rand($telArr, 1);
                        if (!empty($telArr[$index])) {
                            return Help::JsonCode(Help::SUCCESS, '成功', ['tel' => $telArr[$index]]);
                        }
                    }
                }

                //选择的是市县
                if ($city['pid'] != 0) {
                    $resultTel = $tel->CityTelOne();
                    if (!empty($resultTel)) {
                        $telArr = explode(',', $resultTel['tel']);
                        $index = array_rand($telArr, 1);
                        if (!empty($telArr[$index])) {
                            return Help::JsonCode(Help::SUCCESS, '成功', ['tel' => $telArr[$index]]);
                        }
                    } else { //如果为空,返回省的电话
                        $telmodel = new CategoryTelCity();
                        $telmodel->cid = $city['pid'];
                        $result = $telmodel->CityTelOne();
                        if (!empty($result)) {
                            $telArr = explode(',', $result['tel']);
                            $index = array_rand($telArr, 1);
                            if (!empty($telArr[$index])) {
                                return Help::JsonCode(Help::SUCCESS, '成功', ['tel' => $telArr[$index]]);
                            }
                        }
                    }
                }

            }
        }
        return Help::JsonCode(Help::SUCCESS, '成功', ['tel' => Yii::$app->params['default_dialtel']]);
    }



//    /**
//     * 需求定制   价格-户型-区域
//     */
//    public function actionCustomized()
//    {
//            $arr['price'] = [10,20,30,40,50,60,70,80,90];
//            $typeModel = new \common\models\CategoryHousetype();
//            $typeModel->state = 1;
//            $arr['type']  = $typeModel->getList([],['id','huxing_name']);
//            $city = new \common\models\CategoryCity();
//            $city->pid = 0;
//            $arr['province'] =     $city->getList([],['id','city_name']);
//            p($arr);
//    }

    /**
     * 传递父类区域ID
     * 返回子类区域信息
     */
    public function actionCityson()
    {
        $input = Yii::$app->request->post();
        if (!empty($input['pid'])) {
            $city = new \common\models\CategoryCity();
            $city->pid = $input['pid'];
            $rows = $city->getList([], ['id', 'city_name']);
            if (!empty($rows)) {
                return Help::JsonCode(Help::SUCCESS, '成功', $rows);
            }
        }
        return Help::JsonCode(Help::ERROR, '暂无区域信息');
    }

    /**
     * 区域价格走势
     */
    public function actionCityprice()
    {
        $model = new \common\models\PushCityprice();
        $model->city_id = Yii::$app->request->post('id');
        $rows = $model->FindCityRecordPrice();
        if (!empty($rows)) {
            $timestrap = strtotime($rows['price_at']);
            $rows['price_at'] = trim(date('m', $timestrap) . '月', '0');
            return Help::JsonCode(Help::SUCCESS, '成功', $rows);
        }
    }


    //需求数据
//    public function actionCustomizedform()
//    {
//
//    }


    /**
     * 楼盘价格走势
     */
    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, '失败');
    }


}