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

namespace frontend\controllers;
use frontend\base\CommonController;
use frontend\base\Help;
use Yii;
use frontend\server\IndexServer;

class MapController extends CommonController
{
        public function actionHome()
        {
            $IndexModel = new IndexServer();
            $sub = $IndexModel->Subject();
            $price = $IndexModel->PriceList();
            $type = $IndexModel->HouseType();

            $city = new \common\models\CategoryCity();
            $city->pid = 0;
	        $city->state = 1;
	        $city->abroad = 1;
            $citylist = $city->getList([]);

            return $this->render('home',['sub'=>$sub,'price'=>$price,'type'=>$type,'city'=>$citylist]);
        }

        public function actionDetails()
        {
            return $this->render('details');
        }

        public function actionHouselist()
        {
            $model = new \common\models\House();
            $input = Yii::$app->request->post();
			$input['is_view']=1;
            if(!empty($input['data']['content']))
            {
                $str = $input['data']['content'];
                $mb = mb_strlen($str,'utf-8');
                $st = strlen($str);
                if($st==$mb)
                {
                    $input['pinyin'] = $str;
                }
                else
                {
                    $input['name'] = $input['data']['content'];
                }

            }

            if(!empty($input['data']['city']))
            {
                $input['city'] = $input['data']['city'];
            }

            if(!empty($input['data']['price']))
            {
                $input['price'] = $input['data']['price'];
            }

            if(!empty($input['data']['houseType']))
            {
                $type = new \common\models\HouseType();
                $type->type_id = $input['data']['houseType'];
                $typeModel = $type->getList(['hid']);
                if(!empty($typeModel))
                {
                    $input['id'] = array_unique(array_column($typeModel,'hid'));
                }
            }

            if(!empty($input['data']['trait']))
            {
                $input['characteristic']  = $input['data']['trait'];
            }

            $input['sort'] = 'map';
            $rows = $model->getList($input);
            if(!empty($rows))
            {
                $h = new \frontend\server\HouseServer();

                foreach ($rows as &$val)
                {
                    $val['characteristic'] = $h->Subject(json_encode($val['characteristic']),3);
                    $val['thumb'] = Yii::$app->params['httpImg']['hosts'].Yii::$app->params['httpImg']['houses'].$val['thumb'];
                }
                return Help::JsonData(Help::SUCCESS,'成功',$model->getListTotal($input),$rows);
            }
            return Help::JsonCode(Help::ERROR,'暂无相关楼盘信息');


        }

        //省份
        public function actionProvince()
        {
            //省份
            $cityQueryPid = \common\models\CategoryCity::find()->andWhere(['pid'=>0])->andWhere(['<>','ll','null'])->andWhere(['del'=>1,'state'=>1])->asArray()->all();

            $pid = array_column($cityQueryPid,'city_name','id');

            $pidll = array_column($cityQueryPid,'ll','id');
            //所有区域
            $cityQuerySon = \common\models\CategoryCity::find()->andWhere(['<>','pid',0])->andWhere(['state'=>1])->asArray()->all();

            $arr = [];
            foreach ($cityQuerySon as $val)
            {
                if(array_key_exists($val['pid'],$pid))
                {
                        $arr[$val['pid']]['city'] = $pid[$val['pid']];
                        $arr[$val['pid']]['cityid'][] = $val['id'];
                        $arr[$val['pid']]['pid'] = $val['pid'];
                        $arr[$val['pid']]['point'] = $pidll[$val['pid']];
                        $arr[$val['pid']]['rank'] = 1;
                }
            }
            $arr = array_values($arr);

            if(!empty($arr))
            {

                    foreach ($arr as &$val)
                    {
                      $val['count']  =  \common\models\House::find()->andWhere(['city'=>$val['cityid']])->andWhere(['del'=>1,'state'=>1])->andWhere(['is_view'=>1])->count();
                    }
                    return Help::JsonCode(Help::SUCCESS,'成功',$arr);
            }
                    return Help::JsonCode(Help::ERROR,'暂无资料');
        }

        //区域
        public function actionCity()
        {
            $cityQueryPid = \common\models\CategoryCity::find()->select(['id','city_name','ll'])->andFilterWhere(['pid'=>Yii::$app->request->get('pid')])->andWhere(['del'=>1])->asArray()->all();
            $arr = [];
            foreach ($cityQueryPid as $key=>$val)
            {
                $arr[$key]['city'] = $val['city_name'];
                $arr[$key]['pid'] = $val['id'];
                $arr[$key]['point'] = $val['ll'];
                $arr[$key]['rank'] = 2;
                $arr[$key]['count']  =  \common\models\House::find()->andWhere(['city'=>$val['id']])->andWhere(['del'=>1])->andWhere(['is_view'=>1])->count();
            }
            if(!empty($arr))
            {
                return Help::JsonCode(Help::SUCCESS,'成功',$arr);
            }
                return Help::JsonCode(Help::ERROR,'暂无资料');
        }

        //区域楼盘
        public function actionCityhouse()
        {
            $input = Yii::$app->request->get('pid');
            $model = new \common\models\House();
            $model->city = Yii::$app->request->get('pid');
            $rows = $model->CityHouse();
            if(!empty($rows))
            {
                $arr = [];
                    foreach ($rows as $key=>$val)
                    {
                        $arr[$key]['title'] = $val['name'];
                        $arr[$key]['hx'] = $val['main_units'];
                        $arr[$key]['dq'] = '['.$val['city_name'].']';
                        $arr[$key]['point'] =  str_replace(',','|',$val['longitude_latitude']);
                        $arr[$key]['isOpen'] =  0;
                        $arr[$key]['icon']['w'] =  21;
                        $arr[$key]['icon']['h'] =  21;
                        $arr[$key]['icon']['l'] =  0;
                        $arr[$key]['icon']['w'] =  0;
                        $arr[$key]['icon']['t'] =  0;
                        $arr[$key]['icon']['x'] =  0;
                        $arr[$key]['icon']['lb'] =  5;
                        $arr[$key]['content']['pic'] = !empty($val['sale_price']) ? $val['sale_price'].$val['price_unit'] : '待定';
                        $arr[$key]['content']['add'] = $val['address'];
                        $arr[$key]['content']['href'] = '/house/'.$val['id'].'/';
                        $arr[$key]['content']['img'] = Yii::$app->params['httpImg']['hosts'].Yii::$app->params['httpImg']['houses'].$val['thumb'];
                    }
                 return Help::JsonCode(Help::SUCCESS,'成功',$arr);
            }
        }

        public function actionPidcity()
        {
            $city = new \common\models\CategoryCity();
            $city->pid = Yii::$app->request->post('id');
            $citylist = $city->getList([]);
            if(!empty($citylist))
            {
                    return Help::JsonCode(Help::SUCCESS,'成功',$citylist);
            }
                    return Help::JsonCode(Help::ERROR,'失败');
        }

}