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

namespace frontend\controllers;
use frontend\base\CommonController;
use Yii;
use frontend\base\Help;
use common\models\NewsRenovation;
use common\models\NewsRenovationcontent;
use yii\data\Pagination;
class PjController extends CommonController
{
        public function actionHome()
        {

            $model = new NewsRenovation();
            $pages = new Pagination(['totalCount' =>   $model->PcgetListTotal(),'pageSize'=>8,  'pageSizeParam' => false, 'validatePage' => false ]);
            $rows = $model->PcgetList($pages);

//            p($rows);
            $houseModel = new \common\models\House();
            $houseModel->is_push = 2;
            $house = $houseModel->randList(10);

//            p($rows);
//            [title] => 111111
//            [id] => 14
//            [img] => ada881080c30d77d64b4a0c3e3eca44e.jpg
//        [click] => 0
//            [type] => 2
            return $this->render('home',['model'=>$rows,'page'=>$pages,'house'=>$house]);
        }

        public function actionDetails()
        {

            //楼盘
            $houseModel = new \common\models\House();
            $houseModel->is_push = 2;
            $house = $houseModel->randList(4);
            //
            $model = new NewsRenovation();
            $row = $model->FindById(Yii::$app->request->get('pid'));
            $h = [];
            if(!empty($row['hid']))
            {
                $houseModel->id = $row['hid'];
                $h = $houseModel->OneDetails(['name','id']);
            }
            //上一篇,和下一篇
            $ss = $model->RandList(2,['title','id']);

            //装修品鉴
            $weirdo = new \common\models\NewsWeirdo();
            $weirdoList = $weirdo->Mhomelist(['page'=>1,'limit'=>3]);

//            p($weirdoList);
            //品鉴相关信息
            $f = new NewsRenovationcontent();
            $f->nid = Yii::$app->request->get('pid');
            $col = $f->PcLabel();
//            [subject] => 1111111222
//            [click] => 12
//            [abstract] => 222222222222222222
//            [thumb] => cd7c7c5e9c75636db2de40ad183da3c9.jpg
//        [short_subject] =>
//            [id] => 3
            //记录查看次数
            $iprecord = new \common\models\AllclickRecord();
            $arr['id'] = Yii::$app->request->get('pid');
            $arr['ip'] = Yii::$app->request->userIP;;
            $arr['type'] = 1;
            $re = $iprecord->RecordQuery($arr);
            if(empty($re)){
                if($iprecord->addRecord($arr))
                {
                    $model->Clickadd(Yii::$app->request->get('pid'));
                }
            }
            return $this->render('details',['house'=>$house,'model'=>$row,'col'=>$col,'h'=>$h,'f'=>$ss,'weirdo'=>$weirdoList]);
        }


        public function actionContent()
        {
            $input = Yii::$app->request->post();
            $f = new NewsRenovationcontent();
            $res = $f->FindById($input['id']);
            if(!empty($res))
            {
                return Help::JsonCode(Help::SUCCESS,'成功',$res);
            }
        }

        public function actionSlice()
        {
            $model = new NewsRenovation();

            $page = Yii::$app->request->post();
            $m = new \stdClass();
            $m->offset = ($page['page'] - 1) * $page['limit'];
            $m->limit = $page['limit'];
            $row = $model->PcgetList($m);
            if(!empty($row))
            {
                $row[0]['title'] = mb_substr($row[0]['title'],0,5).'...';
                return Help::JsonCode(Help::SUCCESS,'成功',$row);
            }
                return Help::JsonCode(Help::ERROR,'失败');
        }
}