<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/4/26
 * Time: 上午11:13
 */

namespace frontend\controllers;
use frontend\base\CommonController;
use Yii;

class CityController extends CommonController
{
        public function actionIndex()
        {
            /**
             * 所有城市
             */
            $cityModel = new \common\models\CityDetails();
            $cityAll = $cityModel->CityInfo();
            /**
             * 获取传递过来的ID 显示相应的城市信息
             */
            $rowCity = $cityModel->FingByName(Yii::$app->request->get('cid'));
            if(!empty($rowCity))
            {
                $rowCity['img'] = json_decode($rowCity['img']);
            }

            /**
             * 传递区域ID 返回相关楼盘信息
             */
            $houseModel =  new \common\models\House();
            if(!empty($rowCity['city_id']))
            {
                $houseModel->city = $rowCity['city_id'];
                $house = $houseModel->QgetList(['pfg_category_city.city_name','pfg_house.id','pfg_house.city','pfg_house.price','pfg_house.name','pfg_house_detail.address','pfg_house_detail.main_units','pfg_house_detail.price_unit','pfg_house.thumb','pfg_house.sale_price','pfg_house.characteristic','pfg_house_detail.longitude_latitude']);
                if($house['data'] != null)
                {
                    $characteristic = new \common\models\Characteristic();
                    $charList = $characteristic->getList([]);
                    $characteristicId = array_column($charList,'name','id');
                    $chara = array_column($house['data'],'characteristic','id');
                    $charaArrs = [];
                    //处理特色主题
                    foreach ($chara as $key=>$val)
                    {
                        $arr = json_decode($val,true);
                        if(is_array($arr) && !empty($arr))
                        {
                            foreach ($arr as $k=>$v)
                            {
                                if(isset($characteristicId[$v]))
                                {
                                    $charaArrs[$key][$k] = $characteristicId[$v];
                                }
                            }
                        }
                    }

                    $Tel = new \common\models\CategoryTelCity();
                    $telRow = $Tel->QgetAll();

                    $cityTel = [];
                    foreach ($telRow as &$v)
                    {
                        $cityTel[$v['cid']] = explode(',',$v['tel']);
                    }

                    $randArr = [];
                    foreach ($house['data'] as &$val)
                    {
                        if(isset($charaArrs[$val['id']]))
                        {
                            //随机选择3个特色主题
                            if(count($charaArrs[$val['id']]) > 3)
                            {
                                $ranChar = array_rand($charaArrs[$val['id']],3);
                                $randArr[] = $charaArrs[$val['id']][$ranChar[0]];
                                $randArr[] = $charaArrs[$val['id']][$ranChar[1]];
                                $randArr[] = $charaArrs[$val['id']][$ranChar[2]];
                                $val['characteristic'] = $randArr;
                                $randArr= [];
                            }
                            else
                            {
                                $val['characteristic'] = $charaArrs[$val['id']];
                            }

                        }

                        if(!empty($cityTel[$val['city']][0]))
                        {
                            $rand =  array_rand($cityTel[$val['city']],1);
                            $val['citytel'] =$cityTel[$val['city']][$rand];

                        }
                        else
                        {
                            $val['citytel'] = 1111;
                        }
                    }
                }
            }
            return $this->render('index',['city'=>$cityAll,'citydetails'=>$rowCity,'house'=>$house]);
        }

}