<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/4/19
 * Time: 下午2:52
 */
namespace mobile\controllers;
use mobile\base\CommonController;
use mobile\server\NewsServer;
use Yii;
use mobile\base\Help;
class NewsController extends  CommonController{

    // public function init()
    // {
    //     parent::init();
    //     return $this->goHome();
    // }
    public function actionAa()
    {
      p($_SERVER['HTTP_REFERER']);
    }

    public function actionHome()
    {

        $model = new NewsServer();
        $lei = $model->ColumnInfo();  //栏目
        if ($lei){
            foreach ($lei as &$val){
                $val['img'] = Yii::$app->params['httpImg']['host'].Yii::$app->params['httpImg']['newsthumb'].$val['img'];
            }
        }
        $rows = $model->Pushinfo(2);

        return $this->render('home',['lei'=>$lei,'top'=>$rows]);
    }

    public function actionHomeform()
    {
        $model = new \common\models\News();
        if( is_numeric(Yii::$app->request->get('cid')))
        {

            $model->category = Yii::$app->request->get('cid');

            if($model->category == 0) $model->category = null;
        }

        $newsList = $model->Mhomelist(Yii::$app->request->get());

        if(empty($newsList))
            {
                $m = new \common\models\House();
                $data = $m->HomegetList(5);
                $label = new  \common\api\Label;
                foreach ($data as &$val)
                {
                    if(!empty($val['characteristic']))
                    {
                        $val['characteristic'] =   $label->Characteristic(json_decode($val['characteristic'],true),3);
                    }
                }

                return $this->render('@app/views/news/newsxiajia.php',['model'=>$data]);
            }

        if(!empty($newsList))
        {
            foreach ($newsList as &$val)
            {
                if(!empty($val['thumb']))
                {
                    $val['thumb'] = json_decode($val['thumb']);
                }
            }
            $arr['url'] = Yii::$app->params['httpImg']['host'].Yii::$app->params['httpImg']['newsthumb'];
            $arr['data'] = $newsList;
            return Help::JsonCode(Help::SUCCESS,'成功',$arr);
        }
        return Help::JsonCode(Help::ERROR,'失败');
    }

    public function actionDetails()
    {
        $input = Yii::$app->request->get();
        $model=  new NewsServer();
        $hserver= new \mobile\server\HouseServer();
        $row = $model->NewsDetails($input);
        
        if(!isset($row) || empty($row))
        {
            $m = new \common\models\House();
            $data = $m->HomegetList(5);
            $label = new  \common\api\Label;
            foreach ($data as &$val)
            {
                if(!empty($val['characteristic']))
                {
                    $val['characteristic'] =   $label->Characteristic(json_decode($val['characteristic'],true),3);
                }
                $val['tel'] = $hserver->RandTel($val['city']);

            }

            return $this->render('@app/views/news/newsxiajia.php',['model'=>$data]);
        }

        $housetel = new \mobile\server\IndexServer();
        $tel = $housetel->RandTel($row['city']);

        $category = $model->Relevant($row['category']);

        //点击量排序
        $clickNews = $model->clickList(2);
        //该区域相关楼盘
        $cityHouse = $model->NewsCityHouse($row['city']);
		
        //热销楼盘
//        $model = new \common\models\PushHousesale();
//        $model->type = 1;
//        $hotHouse = $model->Homegetlist(4);
        
        return $this->render('details',[
            'model'=>$row,
            'category'=>$category,
            'hotnews'=>$clickNews,
            'cityhouse'=>$cityHouse,
//            'hothouse'=>$hotHouse,
            'tel'=>$tel
        ]);
    }
}