<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/4/19
 * Time: 下午2:52
 */

namespace mobile\controllers;

use common\fm\HouseHandle;
use common\models\Brand;
use common\models\CategoryLabel;
use common\models\Characteristic;
use common\models\House;
use common\models\HouseComment;
use common\models\HouseCommentLikes;
use common\models\Sensitivewords;
use mobile\base\CommonController;
use mobile\server\IndexServer;
use mobile\server\WordMatcher;
use linslin\yii2\curl;
use Yii;
use mobile\base\Help;

//use yii\data\Pagination;
//use frontend\server\HouseServer;
//use frontend\base\Help;
use mobile\server\HouseServer;
use yii\helpers\Html;

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

    public $enableCsrfValidation = false;

    public function beforeAction($action)
    {
        if (in_array(Yii::$app->controller->action->id, ['home', 'type', 'details', 'album', 'houseremark', 'news'])) {
            $input = Yii::$app->request->get();
            if (!is_numeric($input['hid'])) {
                $input['hid'] = explode('-', Yii::$app->request->get('hid'))[1];
            }
            if (empty((new House())->HouseIsshow($input['hid']))) {
                return $this->redirect('/public/showhouse')->send();
            }
        }
        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }

    /*
     * 楼盘首页
     * */
    public function actionHome()
    {
        $this->on(self::LOCATIONCITYHOUSE, [new \common\fm\HouseHandle(), 'MobileCityLocation']);
        $this->trigger(self::LOCATIONCITYHOUSE);
        $model = new HouseServer();
        $sub = new \mobile\server\IndexServer();
        $row = $model->Details();
        /***2020.6.18 lyy 获取楼盘直播视频****/
        $row['house']['live'] = $model->GetLiveInfo($row['house']['id']);
        $row['house']['provinceName'] = $sub->getProvinceName($row['house']['province']);
//            p($row);die;
        $str = [];
        $type = $model->HouseType();
        $row['seo'] = $model->SeoCity(isset($row['house']['city']) ? $row['house']['city'] : '');

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

    /**
     * 楼盘列表
     * */
    public function actionSearch()
    {
        $data = HouseServer::Search(1);
		$data['characteristic'] = (new Characteristic())->getListAll();
		
	    //物业类型
	    $labelType = new CategoryLabel();
	    $labelType->type = 1;
	    $labelType->state = 1;
	    $data['label'] = $labelType->getList([], ['id', 'name']); //物业
	
//        $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';  //有效日期

        $gCity = Yii::$app->hostserver->hostId;
        if ($gCity != 0) {
            $cityName = \common\models\CategoryCity::findOne($gCity)['city_name'];
        } else {
            $cityName = '全国';
        }
        //SEO
        $meta = $house->SeoCity(isset($input['city']) ? $input['city'] : (empty($gCity)? '' : $gCity ));

        return $this->render('search', [
        	'model' => $data,
	        'city' => $city,
	        'house' => $rows['data'],
	        'start_time' => $start_time,
	        'house_total' => $rows['count'],
	        'cityname' => $cityName,'seo'=>$meta],
        );
    }


    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();
        $publiHouse = $model->PublicDetails();
        $input['hid'] = Yii::$app->request->get('hid');
        $input['type_id'] = Yii::$app->request->get('type_id',$row[0]['type_id'] );
        $typeimg = $model->HouseTypeGroupAll($input);
        return $this->render('type', ['model' => $row, 'hid' => Yii::$app->request->get('hid'),'public' => $publiHouse,'typeimg'=>$typeimg]);
    }

    /*
     *  楼盘户型分组后的数据
     * */
    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()
    {
        $model = new HouseServer();
        $row = $model->ParamsDetails();
        $publiHouse = $model->PublicDetails();
//            $row['house']['state'] = Yii::$app->params['HouseSalesStatus'][$row['house']['state']];
        // p($row);die;
        return $this->render('details', ['model' => $row, 'hid' => Yii::$app->request->get('hid'),'public' => $publiHouse]);
    }

    /*
     * 楼盘详细介绍
     * */
    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();
        
        $publiHouse = $model->PublicDetails();
        return $this->render('news', ['model' => $rows, 'hid' => Yii::$app->request->get('hid'),'public' => $publiHouse]);
    }

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

    /*
     * 楼盘相册页面
     * */
    public function actionAlbum()
    {
        $model = new HouseServer();
//        $rows = $model->HouseAlbum();
        $rows = $model->AlbumType();
        $publiHouse = $model->PublicDetails();
		
        return $this->render('album', ['model' => $rows, 'hid' => Yii::$app->request->get('hid'),'public' => $publiHouse]);
    }

    /*
     * 楼盘所有相册
     * */
    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();
        $publiHouse = $model->PublicDetails();
        return $this->render('houseremark', ['model' => $rows, 'hid' => Yii::$app->request->get('hid'), 'xuqiu' => $houseComment->xuqiu, 'commentList' => $getHouseList, 'getHouseListTotal' => $getHouseListTotal,'public' => $publiHouse]);
    }


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


    //楼盘详情页价格走势
    public function actionHouseprice()
    {
        $get = Yii::$app->request->get();
        if (is_numeric($get['id'])) {
            $price = HouseHandle::HousePrice($get['id']);
            if ($price != null) {
                return Help::JsonCode(Help::SUCCESS, '成功', $price);
            }
            return Help::JsonCode(Help::ERROR, '失败');
        }
    }

    // 独立拔打电话页面
    public function actionSelftel()
    {
        $input = Yii::$app->request->get();
        $row = (new HouseServer())->Details($input);
        return $this->renderPartial('selftel', ['model' => $row]);
    }

    // 众趣播放页面
    public function actionZhongqu()
    {
        $input = Yii::$app->request->get();
        return $this->render('zhongqu',['url'=>$input['h']]);
    }
}