<?php

namespace wechat\controllers;

use wechat\base\CommonController;
use wechat\base\Help;
use Yii;
use wechat\server\HouseServer;

class MapController extends  CommonController{

    /*地图*/
    public function actionMap()
    {
        $privince=$this->province();
        return $this->render('mapindex',['privince'=>$privince]);
    }
    private function province()
    {
        $city = new \common\models\CategoryCity();
        $city->abroad = 1;
        $city->state = 1;
        $city->pid   = 0;
        $province = $city->getList([]);
        return $province;
    }

    /**地区 价格 特色 户型--->因改版V2此方法字18年11月26日废掉*/
    public function actionGetprovince()
    {
       $input=Yii::$app->request->post();
      if(empty($input)) return  false;
        switch ($input['typeName'])
        {
            case 'area':
                $city = new \common\models\CategoryCity();
                $city->state = 1;
                $city->pid   = 0;
                $province = $city->getList([]);
                if(!empty($province)) return Help::JsonCode(Help::SUCCESS,'成功',$province);
            break;
            case 'price':
                $PriceModel = new \common\models\HousesPrice();
                $price = $PriceModel->getList([],['id','price']);
                if(!empty($price)) return Help::JsonCode(Help::SUCCESS,'成功',$price);
            break;
            case 'characteristic':
                $zhutiModel = new \common\models\Characteristic();
                $theme = $zhutiModel->getList([],['id','name']);
                if(!empty($theme)) return Help::JsonCode(Help::SUCCESS,'成功',$theme);
            break;
            case 'type':
                $TypeModel = new \common\models\CategoryHousetype();
                $TypeModel->state=1;
                $type = $TypeModel->getList([],['id','huxing_name']);
                if(!empty($type)) return Help::JsonCode(Help::SUCCESS,'成功',$type);
            break;
        }
    }


    /*传入父id获取城市*/
    public function actionGetcity()
    {
        $model = new \common\models\CategoryCity();
        $input = Yii::$app->request->post();
        $model->pid = $input['city'];
        $rows = $model->getList([],['id','city_name','ll']); //获取所有子类
        $rows=$this->houseCount($rows);
        if(!empty($rows)) return Help::JsonCode(Help::SUCCESS,Yii::t('app','get_success'),$rows);
    }

    private function houseCount($city)
    {
        if(!is_array($city)) return false;
        $rows=[];
        $input=[];
//        p($city);
        $cityId = array_column($city,'id');
        $query = \common\models\House::find();
        $query->select(["count('city') as num",'city']);
        $query->andWhere(['city'=>$cityId]);
        $query->andWhere(['del'=>1]);
        $query->andWhere(['is_view'=>1]);
        $query->groupBy('city');
        $cityCount = $query->asArray()->all();
        if($cityCount)
        {
            $cityRows = array_column($cityCount,'num','city');
            foreach ($city as &$val)
            {
                $val['number'] = isset($cityRows[$val['id']]) ? $cityRows[$val['id']] : 0;
                $val['point'] = $val['ll'];
                $val['name'] = $val['city_name'];
            }
            return $city;
        }
        return false;

//        $model = new HouseServer();
//        foreach($city as $k=>$v)
//        {
//            $city[$k]['city']=$v['id'];
//            $rows[$k] = $model->SearchForm($city[$k]);
//        }
//        foreach($rows as $key=>$val)
//        {
//            foreach($val['data'] as $kk=>$vv)
//            {
//                $input[$key]['id']=$vv['city'];
//                $input[$key]['name']=$vv['city_name'];
//                $input[$key]['point']=$vv['ll'];
//                $input[$key]['number']=$val['count'];
//            }
//        }

    }

     /*加载楼盘*/
    public function actionHouselist()
    {
        $model = new \common\models\House();
        $input = Yii::$app->request->post();
        $input['limit'] = 20;
        if(isset($input['city']) && !empty($input['city']))
        {
            $city = (new \common\models\CategoryCity())->getCityNameTurnId($input);
            if($city)
            {
                $model->city = $city['id'];
            }
        }

        $data = $model->PcHouseList($input,['pfg_category_city.city_name','pfg_category_city.ll','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_detail.longitude_latitude','pfg_house.thumb','pfg_house.sale_price','pfg_house.characteristic']);

        if($data != null) {
            $characteristic = new \common\models\Characteristic();
            $charList = $characteristic->getList([]);
            $characteristicId = array_column($charList, 'name', 'id');
            $chara = array_column($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 ($data as &$val) {
                $val['thumb'] = Yii::$app->params['httpImg']['hosts'].Yii::$app->params['httpImg']['houses'].$val['thumb'].'/'.'same';
                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 (isset($cityTel[$val['city']])) {
                    $rand = array_rand($cityTel[$val['city']], 1);
                    $val['citytel'] = $cityTel[$val['city']][$rand];

                } else {
                    $val['citytel'] = Yii::$app->params['default_dialtel'];
                }
            }
            $rows = ['data'=>$data];
        }

        if(!empty($rows))
        {
            return Help::JsonCode(Help::SUCCESS,'成功',$rows);
        }
            return Help::JsonCode(Help::ERROR,'暂无数据');
    }
    private function cityNameId($cityname)
    {
        $model = new \common\models\CategoryCity();
        $row=$model->getCityNameTurnId($cityname);
        if($row) return $row;
        $row['city']=$row['id'];
        return $row;
    }

    /*搜索*/
    public function actionSearch()
    {
        $model = new HouseServer();
        $rows = $model->SearchForm(Yii::$app->request->post('input'));
        if(!empty($rows['data']))
        {
            return Help::JsonData(Help::SUCCESS,'成功',$rows['count'],$rows);
        }
        return Help::JsonCode(Help::ERROR,'暂无相关楼盘数据');
    }

    /*详情*/
    public function actionDetails()
    {
        $model = new HouseServer();
        $row = $model->Details();
        $arr=[];
        if(!empty($row))
        {
            $arr['id']=$row['house']['id'];
            $arr['name']=$row['house']['name'];
            $arr['state']=$row['house']['state'];
            $arr['thumb']=Yii::$app->params['httpImg']['hosts'].Yii::$app->params['httpImg']['houses'].$row['house']['thumb'].'/pfgwatermark';
            $arr['sale_price']=$row['house']['sale_price'];
            $arr['price_unit']=$row['house']['price_unit'];
            $arr['main_units']=$row['house']['main_units'];
            $arr['city_name']=$row['house']['city_name'];
            $arr['characteristic']=$row['characteristic'];
            return Help::JsonCode(Help::SUCCESS,'成功',$arr);
        }
        return Help::JsonCode(Help::ERROR,'暂无相关楼盘数据');
    }
}