<?php

namespace wechat\controllers;

use common\models\CategoryCity;
use common\models\PushmCity;
use common\models\PushmColumn;
use wechat\base\CommonController;
use wechat\server\IndexServer;
use Yii;
use wechat\base\Help;

class IndexController extends  CommonController{

    public $enableCsrfValidation = false;
    public function actionIndex()
    {

        //区域
        $cityM = new \wechat\server\CityServer();
        $Abroad_a = $cityM->CityAbroad_a();
        $Abroad_b = $cityM->CityAbroad_b();
        $city_a = getTree($Abroad_a,0);
        $city_b = getTree($Abroad_b,0);

        $model = new IndexServer();
        $lunbo = $model->Lunbo();
        $tuijian = $model->tuijian();  //易购头条
        $news = $model->newslist();
        $newsc = $model->NewsColumn();

        //分站后 区域id
        $gCity = Yii::$app->request->get('city');

        if(empty($gCity))
        {
            $gCity = Yii::$app->hostserver->hostId;
        }
        //精选楼盘
        $hot = $model->Hothouse(['city'=>!empty($gCity) ? $gCity : 0,'type'=>4,'rand'=>null]);
        if(!empty($hot))
        {

            foreach ($hot as &$val)
            {
                $val['thumb'] = Yii::$app->params['httpImg']['hosts'].Yii::$app->params['httpImg']['houses'].$val['thumb'].Yii::$app->params['pfgwatermark'];
            }
        }

        //品牌商
        $PushmColumn = new PushmColumn();
        $PushmColumn->type = 1;
        $pinpai = $PushmColumn->getList(['page'=>1,'limit'=>5])['data'];
        if($pinpai)
        {
            $m = new \common\models\PushmHouse();
            $m->developers_id = array_column($pinpai,'did');
            $result = $m->getListGroup();
            if($result)
            {
                $resultArr = array_column($result,'total','developers_id');
            }
            foreach ($pinpai as &$val)
            {
                $val['house_count']  = 0;
                if(isset($resultArr[$val['did']]))
                {
                    $val['house_count'] = $resultArr[$val['did']];
                }
                $val['img'] = Yii::$app->params['httpImg']['host'].Yii::$app->params['httpImg']['push_lb'].$val['img'];
            }
        }

        if(empty($gCity))
        {
                $gCity = Yii::$app->hostserver->hostId;
                if($gCity !=0)
                {
                    $cityName = \common\models\CategoryCity::findOne($gCity)['city_name'];
                }
               else
               {
                   $cityName = '全国';
               }
        }
        else
        {
                $gCity = Yii::$app->hostserver->hostId;

                $cityName =  \common\models\CategoryCity::findOne($gCity)['city_name'];
        }

        //导航
        $sysmenu = new \common\models\Frontend();
        $sysmenu->pid = 105;
        $SysRows = $sysmenu->SonList(8,['name','url','icon','function']);

        //热门区域
        $CityModel = new PushmCity();
        $CityModel->type = 1;
        $CityModel->abroad = 1;
        $HotCity = $CityModel->Homelist(7);

        $CityModel->abroad = 2;
        $HotCityb = $CityModel->Homelist(8);


        return $this->render('index',[
            'lunbo'=>$lunbo,
//            'tese'=>$tese,
            'tuijian'=>$tuijian,
            'news'=>$news,
            'newsc'=>$newsc,
            'city_all'=>['abroad_a'=>$city_a,'abroad_b'=>$city_b],
            'gcity'=>$gCity,
            'cityname'=>$cityName,
            'sysmenu'=>$SysRows,
            'hot_house'=>$hot,
            'pinpai'=>$pinpai,
            'hotcity'=>$HotCity,
            'hotcityb'=>$HotCityb
        ]);
    }

    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]);
    }

    /*renderPartial
     * 首页-底部 拨打电话图标
     * */
    public function actionTelrand()
    {
        //有获取区域的情况下
        $input = Yii::$app->request->post();
        $tel = new \common\models\CategoryTelCity();
        if(!empty($input['city']))
        {
            $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]]);
                }
            }
        }

        //没获取区域的情况下
        $model = new \common\models\PushmCity();
        $model->type = 2;
        $randRow = $model->RandFind();
        if(!empty($randRow))
        {

            $tel->cid =$randRow['city_id'];
            $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]]);
                }
            }
        }
        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()
//    {
//
//    }


}