<?php
/*
  Created by PhpStorm.
  User: xiaofeng
  Date: 2018/6/10
  Time: 下午3:15
 */

namespace mobile\controllers;

use common\models\House;
use common\models\SpecialTopic;
use mobile\base\CommonController;
use Yii;
use mobile\base\Help;

class ZtController extends CommonController
{
    public $enableCsrfValidation = false;

    // 元旦专题
    public function actionNewyear()
    {
        return $this->render('newyear');
    }

    // 元旦专题
    public function actionYuandan()
    {
        return $this->render('yuandan');
    }

    // 春节旅游看房团
    public function actionNewyearzt()
    {
        return $this->render('newyearzt');
    }

    /*金九银十专题*/
    public function actionJjys()
    {
        return $this->render('jjys');
    }

    /*国庆专题*/
    public function actionNationalzt()
    {
        return $this->render('nationalzt');
    }

    /*融创高隆湾 3天2晚游*/
    public function actionRcglw()
    {
        return $this->render('rcglw');
    }


    // 春节专题
    public function actionChunjiezt()
    {
        $ztmodel = new \common\models\SpecialTopic();
        $row = $ztmodel::findOne(7);
        $model = new \common\models\House();
        $cmodel = new \common\models\CategoryCity();
        $city = '';
        $house = '';
        $imgUrl = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['push_lb'];
        if (!empty(json_decode($row->hids, true))) {
            $hid = json_decode($row->hids, true);
            $query = $model::find();
            $query->andWhere(['pfg_house.id' => $hid]);
            $query->joinWith(['housedetail']);
            $query->with(['cityName']);
            $query->orderBy(['pfg_house.sort' => SORT_DESC]);
            $house = $query->asArray()->all();

            $pcity = array_column($house, 'province');
            $cquey = $cmodel::find();
            $cquey->select(['id', 'city_name']);
            $cquey->andWhere(['id' => $pcity]);
            $cquey->orderBy(['sort' => SORT_DESC]);
            $city = array_column($cquey->asArray()->all(), 'city_name', 'id');

        }
        empty($row->m_img) ? '' : $row->m_img = $imgUrl . $row->m_img;

        return $this->render('chunjiezt', ['city' => $city, 'house' => $house, 'model' => $row]);
    }

    public function actionChunjieztform()
    {
        $model = new \common\models\House();
        $input = Yii::$app->request->post();
        $ztmodel = new \common\models\SpecialTopic();
        $row = $ztmodel::findOne(7);
        $house = '';
        if (!empty(json_decode($row->hids, true))) {
            $hid = json_decode($row->hids, true);
            $query = $model::find();
            $query->andWhere(['pfg_house.id' => $hid]);
            if (!empty($input['id'])) {
                $query->andWhere(['pfg_house.province' => $input['id']]);
            }
            $query->joinWith(['housedetail']);
            $query->with(['cityName']);
            $query->orderBy(['pfg_house.sort' => SORT_DESC]);
            $house = $query->asArray()->all();
        }
        if (!empty($house)) {
            array_walk($house, function (&$val) use ($row) {
                $val['thumb'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['houses'] . $val['thumb'] . Yii::$app->params['pfgwatermark'];
                $val['expiration_date'] = $row->expiration_date;
            });
//            p($house);die;
            return Help::JsonCode(Help::SUCCESS, '加载成功', $house);
        }
        return Help::JsonCode(Help::ERROR, '暂无数据');
    }


    /**
     * 线上售楼处专题
     * @return string
     */
    public function actionSellhousezt()
    {
        $row = SpecialTopic::findOne(8);
        if (!empty($row)) {
            $hid = json_decode($row['hids'], true);
            $house = House::find()->select(['pfg_house.id', 'pfg_house.name', 'pfg_house_detail.address', 'pfg_house.thumb',
                'pfg_house_detail.preferential_zt', 'pfg_house.sale_price', 'pfg_house_detail.price_unit', 'pfg_category_city.pid'])
                ->leftJoin('pfg_house_detail', 'pfg_house.id = pfg_house_detail.hid')
                ->leftJoin('pfg_category_city', 'pfg_house.city = pfg_category_city.id')
                ->andWhere(['pfg_house.id' => $hid])
                ->orderBy(['pfg_house.sort' => SORT_DESC])->asArray()->all();
        }
        return $this->render('sellhousezt', ['house' => $house, 'row' => $row]);
    }

    //楼盘随机电话
    public function RandTel($city)
    {
        $tel = new \common\models\CategoryTelCity();
        $tel->cid = $city;
        $telModel = $tel->CityTelOne();
        if ($telModel != null) {
            $telArr = explode(',', $telModel['tel']);
            $randNum = array_rand($telArr, 1);
            if (!empty($telArr[$randNum])) {
                return $telArr[$randNum];
            }
        }
        return Yii::$app->params['default_dialtel'];
    }

    // 写字楼专题
    public function actionOffice()
    {
        $cityList = \common\models\PushplaceType::find()->where(['type' => 4, 'state' => 1])->limit(2)->orderBy(['sort' => SORT_DESC])->select('id,title')->asArray()->all();
        foreach ($cityList as &$val) {
            $val['house_list'] = \common\models\PushhouseType::find()
                ->select('pfg_pushhouse_type.*,pfg_house.name,pfg_house.city,pfg_house.sale_price,pfg_house.thumb,pfg_house_detail.price_unit,pfg_house_detail.address,pfg_house_detail.preferential,pfg_house_detail.preferential_price,pfg_house_detail.preferential_zt')
                ->where(['pfg_pushhouse_type.previd' => $val['id'], 'pfg_pushhouse_type.type' => 4])
                ->innerJoin('pfg_house', 'pfg_house.id = pfg_pushhouse_type.hid')
                ->innerJoin('pfg_house_detail', 'pfg_house_detail.hid = pfg_house.id')
                ->orderBy(['pfg_pushhouse_type.sort' => SORT_DESC])->asArray()->all();
            foreach ($val['house_list'] as &$value) {
                $value['thumb'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['houses'] . $value['thumb'] . '/same';
                $value['tel'] = $this->RandTel($value['city']);
            }
        }
        if (!empty($cityList)) {
            $cityList[0]['house_list'] = array_chunk($cityList[0]['house_list'], 3);
        }
        return $this->render('office', ['cityList' => $cityList]);
    }

    // 中秋专题
    public function actionAutumnzt()
    {
        $server = new \frontend\server\ZtServer();
        $data = $server->GuoQingZhongQiuY();
        return $this->render('autumnzt',['data'=>$data]);
    }


}