<?php

namespace backend\controllers;
use backend\base\CommonController;
use backend\base\Help;
use common\models\PushHousesale;
use Yii;
use common\models\PushRecommend;
use common\models\PushHouserecommend;

class PushrecommendController extends CommonController
{
       /*
        * 页面显示
        * */
        public function actionIndex()
        {
            return $this->render('index');
        }

        /**数据读取*/
        public function actionIndexform()
        {
            $model = new PushRecommend();
            $rows = $model->adminList(Yii::$app->request->post());

            if(!empty($rows))
            {
                foreach($rows as &$val)
                {
                    $val['create_at'] = date('Y-m-d H:i',$val['create_at']);
                }
                return Help::JsonData(0,Yii::t('app','get_success'),$model->Total(),$rows);
            }
        }

        /*
        * 加载楼盘 - 数据
        * */
        public function actionHouseloadform()
        {
            $model = new PushHouserecommend();
            $input = Yii::$app->request->post();
            $result = $model->getHouseList($input);
            if($result != null)
            {
                foreach ($result as &$val)
                {
                    $val['create_at'] = date('Y-m-d H:i',$val['create_at']);
                }
                return Help::JsonData(0,Yii::t('app','get_success'),$model->Total($input),$result);
            }
            return Help::JsonCode(Help::ERROR,Yii::t('app','get_error'));
        }

        /*
        * 加载楼盘 - 批量删除
        * */
        public function actionHouseloaddelall()
        {
            //接收表单数据
            $input = Yii::$app->request->post();
            $delall = PushHouserecommend::deleteAll(['id'=>$input['id']]);

            if ($delall){
                return Help::JsonCode(Help::SUCCESS,'批量删除成功');
            }
            return Help::JsonCode(Help::ERROR,'批量删除失败');
        }

    /*
     * 优选新房栏目添加
     * */
        public function actionAddform()
        {
            $model = new PushRecommend();
            $model->scenario = 'add';
            $recommend = $model->Authenticator(Yii::$app->request->post());
            if(is_object($recommend) && $recommend->insert() == true)
            {
               return Help::JsonCode(Help::SUCCESS,'添加成功');
            }
            return Help::JsonCode(Help::ERROR,Yii::t('app','add_error'),$recommend);
        }

        public function actionAdd()
        {
            $city=$this->City();
            return $this->render('add',['city'=>$city]);
        }

        public function actionEdit()
        {
            $model = new PushRecommend();
            $city=$this->City();
            $row = $model->FindById(Yii::$app->request->get('id'));
            return $this->render('edit',['city'=>$city,'row'=>$row]);
        }
        public function actionEditform()
        {
            $model = new PushRecommend();
            $row = $model->findOne(Yii::$app->request->post('id'));
            if($row != null)
            {
                $row->title = Yii::$app->request->post('title');
                $row->city = Yii::$app->request->post('city');
                if($row->save()) return Help::JsonCode(Help::SUCCESS,'关键词修改成功');
                
             }
            return Help::JsonCode(Help::ERROR,'关键词修改失败');
        }

        public function actionLoadhouse()
        {
           return $this->render('loadhouse',['cat_id'=>Yii::$app->request->get('cat_id')]);
        }

        private function City()
        {
            $city = new \common\models\CategoryCity();
            $city->state = 1;
            $city->pid   = 0;
            $cityModel = $city->getList([]);
            return $cityModel;
        }

        /*
     * 推送优选新房 - 数据
     * */
        public function actionGetHoustform()
        {
            $model = new PushHousesale();
            $input = Yii::$app->request->post();
            $result = $model->getList($input);
            if($result != null)
            {
                foreach ($result as &$val)
                {
                    $val['create_at'] = date('Y-m-d H:i',$val['create_at']);
                }
                return Help::JsonData(0,Yii::t('app','get_success'),$model->Total($input),$result);
            }
            return Help::JsonCode(Help::ERROR,Yii::t('app','get_error'));
        }
        /*
        *   推送优选新房 - 添加
        */
        public function actionHouseadd()
        {
            return $this->render('houseadd',['cat_id'=>Yii::$app->request->get('cat_id')]);
        }

     //添加楼盘
        public function actionHouseaddform()
        {
            $input = Yii::$app->request->post();
            $model = new PushHouserecommend();
            $model->scenario = 'add';
            $validate  = $model->Authenticator($input);
            if(is_object($validate))
            {
                $validate->city = \common\models\House::find()->select(['pfg_category_city.pid'])->leftJoin('pfg_category_city','pfg_house.city = pfg_category_city.id')->andWhere(['pfg_house.id'=>$input['hid']])->asArray()->one()['pid'];
                if($validate->save(false)) return Help::JsonCode(Help::SUCCESS,Yii::t('app','add_success'));
            }
            return Help::JsonCode(Help::ERROR,Yii::t('app','add_error'),$validate);
        }

        public function actionDelshow()
        {
            $model = new PushHouserecommend();
            $input = Yii::$app->request->post();
            $row = $model->FindById($input['id']);
            if($row != null)
            {
                switch ($input['type'])
                {
                    case 'show':
                        if($row->is_show == 1)
                        {
                            $row->is_show = 2;
                        }
                        else if($row->is_show == 2)
                        {
                            $row->is_show = 1;
                        }
                        break;
                    case 'del':
                        $row->del = 2;
                        break;
                    case 'sort':
                        if(is_numeric($input['sort']))
                        {
                            $row->sort = $input['sort'];
                        }
                        break;
                }
                if($row->save(false)) return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
            }
            return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'));
        }
        public function actionCatdelshow()
        {
            $model = new PushRecommend();
            $input = Yii::$app->request->post();
            $row = $model->FindById($input['id']);
            if($row != null)
            {
                switch ($input['type'])
                {
                    case 'show':
                        if($row->is_show == 1)
                        {
                            $row->is_show = 2;
                        }
                        else if($row->is_show == 2)
                        {
                            $row->is_show = 1;
                        }
                        break;
                    case 'del':
                        $row->del = 2;
                        break;
                    case 'sort':
                        if(is_numeric($input['sort']))
                        {
                            $row->sort = $input['sort'];
                        }
                        break;
                }
                if($row->save(false)) return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
            }
            return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'));
        }

         /*
         * 优选新房 - 批量删除
         * */
         public function actionHousedelall()
         {
             $input = Yii::$app->request->post();
             $delall = PushHouserecommend::deleteAll(['id'=>$input['id']]);

             if ($delall){
                 return Help::JsonCode(Help::SUCCESS,'批量删除成功');
             }
             return Help::JsonCode(Help::ERROR,'批量删除失败');
         }
        /********************below author:eit   end***********************/
}