<?php

namespace wechat\controllers;

use common\models\House;
use common\models\HouseComment;
use common\models\HouseCommentLikes;
use common\models\Sensitivewords;
use wechat\base\CommonController;
use wechat\server\IndexServer;
use wechat\server\WordMatcher;
use linslin\yii2\curl;
use Yii;
use wechat\base\Help;
//use yii\data\Pagination;
//use frontend\server\HouseServer;
//use frontend\base\Help;
use wechat\server\HouseServer;
use yii\helpers\Html;

class HouseController extends  CommonController
{
const LOCATIONCITYHOUSE = 'locationcityhouse';

        public $enableCsrfValidation = false;
        /*
         * 楼盘首页
         * */
        public function actionHome()
        {
//            $this->on(self::LOCATIONCITYHOUSE,[new \common\fm\HouseHandle(),'MobileCityLocation']);
//            $this->trigger(self::LOCATIONCITYHOUSE);
            $model = new HouseServer();
            $row = $model->Details();

            $str = [];
            if(empty($row))
            {
                return  Yii::$app->runAction('public/showhouse');
            }
            $type = $model->HouseType();

            return $this->render('home',['model'=>$row,'pince'=>$str,'type'=>$type]);
        }

    /**
      * 楼盘列表
      * */
    public function actionSearch()
    {
        $data = HouseServer::Search(1);
//        $model = new IndexServer();
//        $lunbo = $model->Lunbo();
//        'lunbo'=>$lunbo,
        $CityModel = new \common\models\CategoryCity();
        $city =  $CityModel->FindById(Yii::$app->hostserver->hostId);

        //遍历前10条数据
        $house = new HouseServer();
        $input = Yii::$app->request->get();
        $input['abroad'] = 1;
        $rows = $house->SearchForm($input);
        $start_time = date('Y/m').'/01-'.date('Y/m', strtotime('+ 2 month')).'/01';  //有效日期
        return $this->render('search',['model'=>$data,'city'=>$city,'house'=>$rows['data'],'start_time'=>$start_time,'house_count'=>$rows['count']]);
    }


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


    /*
     * 国外楼盘
     */
    public function actionAbroad()
    {
        $data = HouseServer::Search(2);
//        $model = new IndexServer();
//        $lunbo = $model->Lunbo();

        //遍历前10条数据
        $house = new HouseServer();
        $input = Yii::$app->request->get();
        $input['abroad'] = 2;
        $rows = $house->SearchForm($input);
        $start_time = date('Y/m').'/01-'.date('Y/m', strtotime('+ 2 month')).'/01';  //有效日期
        return $this->render('abroad',['model'=>$data,'house'=>$rows['data'],'start_time'=>$start_time,'house_count'=>$rows['count']]);
    }
    /*
       * 国外楼盘数据
       */
    public function actionSearchdatas()
    {
        $model = new HouseServer();
        $input = Yii::$app->request->get();
        $input['abroad'] = 2;
        $rows = $model->SearchForm($input);
        if(!empty($rows))
        {
            return Help::JsonData(Help::SUCCESS,'成功',$rows['count'],$rows['data']);
        }
        return Help::JsonCode(Help::ERROR,'暂无相关楼盘数据');
    }


        /*
         * 楼盘名称搜索
         * */
        public function actionSearchname()
        {
            $model = new HouseServer();
            $row = $model->SearchHouseName(Yii::$app->request->get());
            if(!empty($row))
            {
                return Help::JsonCode(Help::SUCCESS,'成功',$row);
            }
                return Help::JsonCode(Help::ERROR,'暂无搜索数据');
        }

        /*
         * 获取区域子类信息
         * */
        public function actionSoncity()
        {
            $CityModel = new \common\models\CategoryCity();
            $r =  $CityModel->GetAllSon(Yii::$app->request->post('pid'),['city_name','id','pinyin'],2);
            return Help::JsonCode(Help::SUCCESS,'成功',$r);
        }


        /*
         * 获取楼盘户型分组和分组总数
         * */
        public function actionType()
        {
            $model = new HouseServer();
            $row = $model->HouseType();
            $m = new House();
            if(empty($m->HouseIsshow(Yii::$app->request->get('hid'))))
            {
                return  Yii::$app->runAction('public/showhouse');
            }
            return $this->render('type',['model'=>$row,'hid'=>Yii::$app->request->get('hid')]);
        }

        /*
         *  楼盘户型分组后的数据
         * */
        public function actionHousetype()
        {
            $model = new HouseServer();
            $rows = $model->HouseTypeGroupAll(Yii::$app->request->post());
            if($rows != null)
            {
                return Help::JsonCode(Help::SUCCESS,'成功',$rows);
            }
                return Help::JsonCode(Help::ERROR,'暂无楼盘户型图数据');
        }

        /*
         * 楼盘户型数据
         * */
        public function actionHousetypeall()
        {
//            $model = new HouseServer();
//            $rows = $model->TypeAll(Yii::$app->request->post(),false);
//            if($rows != null)
//            {
//                return Help::JsonCode(Help::SUCCESS,'成功',$rows);
//            }
        }


        /*
         * 楼盘首页-楼盘动态
         * */
        public function actionDynamic()
        {
//            $model = new HouseServer();
//            $rows = $model->NewsDynamic(Yii::$app->request->post());
//            if($rows != null)
//            {
//                return Help::JsonCode(Help::SUCCESS,'成功',$rows);
//            }
//                return Help::JsonCode(Help::ERROR,'失败');
        }

        /*
         * 楼盘详细信息
         * */
        public function actionDetails()
        {
            $m = new House();
            if(empty($m->HouseIsshow(Yii::$app->request->get('hid'))))
            {
                return  Yii::$app->runAction('public/showhouse');
            }

            $model = new HouseServer();
            $row = $model->ParamsDetails();
            if(empty($row))
            {
                return  Yii::$app->runAction('public/showhouse');
            }

            return $this->render('details',['model'=>$row,'hid'=>Yii::$app->request->get('hid')]);
        }

        /*
         * 楼盘详细介绍
         * */
        public function actionIntro()
        {
            $model = new HouseServer();
            $row = $model->ParamsDetails();
            if($row) return $this->render('intro',['model'=>$row]);

           return $this->goBack();
        }

        /*
         * 楼盘资讯
         * */
        public function actionNews()
        {
            $model = new HouseServer();
            $rows = $model->HouseNewsList();
            $m = new House();
            if(empty($m->HouseIsshow(Yii::$app->request->get('hid'))))
            {
                return  Yii::$app->runAction('public/showhouse');
            }
            return $this->render('news',['model'=>$rows,'hid'=>Yii::$app->request->get('hid')]);
        }

        public function actionPeriphery()
        {
            return $this->render('periphery');
        }

        /*
         * 楼盘相册页面
         * */
        public function actionAlbum()
        {
            $model = new HouseServer();
            $rows = $model->HouseAlbum();

            $m = new House();
            if(empty($m->HouseIsshow(Yii::$app->request->get('hid'))))
            {
                return  Yii::$app->runAction('public/showhouse');
            }
//            p($rows);
//            exit;
            return $this->render('album',['model'=>$rows,'hid'=>Yii::$app->request->get('hid')]);
        }

        /*
         * 楼盘所有相册
         * */
        public function actionAlbumlist()
        {
            $model = new HouseServer();
            $rows = $model->HouseAlbumAll();
            if(!empty($rows))
            {
                return Help::JsonCode(Help::SUCCESS,'成功',$rows);
            }
                return Help::JsonCode(Help::ERROR,'暂无楼盘相册信息');
        }

        /*
         * 楼盘首页底部---推荐楼盘 同价位楼盘 周边楼盘
         * */
        public function actionTall()
        {
            $model = new \common\models\House();
            $input = Yii::$app->request->post();
            $arr = [];
            if(isset($input['type']))
            {
                switch($input['type'])
                {
                    case 1: //后期如果不要传递价格作为参数 在删除掉
                        $arr['price'] = $input['price'];
                        if($arr['price'] == 0)
                        {
                            $arr['price'] = "";
                        }
                        $model->price = $arr['price'];
                        $model->city = $input['city'];
                        break;
                    case 2:
                        $arr['city'] = $input['city'];
                        $model->city = $arr['city'];
                        break;
                }
            }


            $model->is_push = 2;
            $rows = $model->randList(3,$arr);
            if(!empty($rows))
            {
                foreach($rows as &$val)
                {
                    $val['characteristic'] = \common\fm\HouseHandle::ChangeCharacteristic($val['characteristic']);
                   $val['tel'] = \common\fm\HouseHandle::ChangeCityTel($val['city']);
                   $val['thumb'] = Yii::$app->params['httpImg']['hosts'].Yii::$app->params['httpImg']['houses'].$val['thumb'];
                }

                return Help::JsonCode(Help::SUCCESS,'成功',$rows);
            }
            return Help::JsonCode(Help::ERROR,'暂无数据');
        }

    /**
     * @return mixed
     * 获取楼盘ID,返回楼盘预售证书信息
     */
    public function actionLicence()
    {
        $input = Yii::$app->request->post();
        if(!empty($input['hid']))
        {
            $model = new \common\models\HousePermit();
            $rows = $model->getList($input);
            if($rows != null)
            {
                return Help::JsonCode(Help::SUCCESS,'成功',$rows);
            }
        }
        return Help::JsonCode(Help::ERROR,'失败');
    }

        /*
         * 楼盘---点评
         * */
//        public function actionRemark()
//        {
//            return $this->render('remark');
//        }

        /*
         * 楼盘--周边配套
         * */
//        public function actionPeitao()
//        {
//            return $this->render('peitao');
//        }

         public function actionRemarkfrom(){
            $input = Yii::$app->request->post();
            $model = new HouseComment();
            $input['ip'] = Yii::$app->request->userIP;
            
            $wordMatcher = new WordMatcher();
            $sensitivewords = new Sensitivewords();
            $senWordsInfo = $sensitivewords->getList();
            $list = explode("、",$senWordsInfo['info']);
            foreach($list as $val){
                $wordMatcher->addWord($val);
            }
            $wordMatcher->match($input['content'],$matched);
            if(!empty($matched)){
                return Help::JsonCode(Help::ERROR,'点评内容存在敏感词汇',$matched);
            }
            $true = $model->Iplimit($input);
            if($true === false)  return Help::JsonCode(Help::ERROR,'一个楼盘最多只能点评2次');
            $input['initiator'] = 2;
            $input['is_show'] = 2;
            if($model->load($input,'') && $model->save())
            {
                $curlArr = array(
                    "mobile" => $input['mobile'],
                    "source" => 40,
                    "equipment" => 1,
                    "hid" => $input['hid'],
                );
                $curl = new curl\Curl();
                $url='http://m.yigouf.com/enroll/signup';
                $response = $curl->reset()->setOption(CURLOPT_POSTFIELDS,http_build_query($curlArr))->post($url);
                return Help::JsonCode(Help::SUCCESS,Yii::t('app','点评成功'));
            }
            return Help::JsonCode(Help::ERROR,Yii::t('app','点评失败'),$model->errors);
        }

        public function actionRemarklikesfrom()
        {
            $input = Yii::$app->request->post();
            $model = new HouseCommentLikes();
            $input['ip'] = Yii::$app->request->userIP;
            $true = $model->Iplimit($input);
            if($true === false)  return Help::JsonCode(Help::ERROR,'一条点评最多只能赞一次');

            if($model->load($input,'') && $model->save())
            {
                $model->hid = $input['hid'];
                $model->hcid = $input['hcid'];
                $total = $model->Total();
                return Help::JsonCode(Help::SUCCESS,Yii::t('app','add_success'),$total);
            }
            return Help::JsonCode(Help::ERROR,Yii::t('app','add_error'),$model->errors);
        }
        
        public function actionHouseremark()
        {
            $model = new HouseServer();
            $houseComment = new HouseComment();
            $rows=$model->Details();
            $input = Yii::$app->request->get();
            $getHouseList = $model->getHouseRemarkList($input,10);
            $houseComment->hid = $input['hid'];
            $getHouseListTotal = $houseComment->getHouseListTotal();
            return $this->render('houseremark',['model'=>$rows,'hid'=>Yii::$app->request->get('hid'),'xuqiu' => $houseComment->xuqiu,'commentList'=>$getHouseList,'getHouseListTotal'=>$getHouseListTotal]);
        }


        public function actionHouseloadremark(){
            $model = new HouseServer();
            $houseComment = new HouseComment();
            $input['hid'] = Yii::$app->request->get('hid');
            $input['page'] = Yii::$app->request->get('page');
            $input['limit'] = Yii::$app->request->get('limit');
            $getHouseList = $model->getHouseRemarkList($input);
            if($getHouseList != null)
            {
                return Help::JsonCode(Help::SUCCESS,'成功',$getHouseList);
            }
            return Help::JsonCode(Help::ERROR,'失败');
        }
        /*
         * 楼盘详细信息
         * */
        public function actionLoan()
        {
          return $this->render('loan');
        }

         /*
         * 楼盘---点评详情
         * */
        public function actionRemarkdetails()
        {
            return $this->render('remarkdetails');
        }

         /*
         * 楼盘---相册放大页
         * */
        public function actionPvimage()
        {
            $input = Yii::$app->request->get();
            $house = House::find()->select(['name'])->andWhere(['id'=>$input['hid']])->asArray()->one();
            $model = new HouseServer();
            $rows = $model->HouseAlbum();

            return $this->render('pvimage',['hid'=>$input['hid'],'row'=>$rows,'house'=>$house]);
        }
         /*
         * 户型---相册放大页
         * */
        public function actionHximage()
        {
            $input = Yii::$app->request->get();
            $house = House::find()->select(['name'])->andWhere(['id'=>$input['hid']])->asArray()->one();
            $model = new HouseServer();
            $rows = $model->HouseType();

            return $this->render('hximage',['hid'=>$input['hid'],'row'=>$rows,'house'=>$house]);
        }
}