<?php
/*
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/7/10
 * Time: 上午9:44
 * 装修品鉴
 **/
namespace backend\controllers;

use backend\base\CommonController;
use backend\base\Help;
use common\models\NewsRenovationcontent;
use Yii;
use backend\server\UploadFile;
use common\models\NewsRenovation;
class RoutinedecorationController extends CommonController{


    /*
     * 常规管理 - 装修品鉴 - 页面
     **/
    public function actionHome()
    {
        return $this->render('home');
    }

    /*
     *常规管理 - 装修品鉴 - 数据
     ***/
    public function actionHomeform()
    {
        $model = new NewsRenovation();
        $model->title = Yii::$app->request->post('title');
        $result = $model->getList(Yii::$app->request->post());
        if(!empty($result))
        {
            $content = new \common\models\NewsRenovationcontent();

            foreach ($result as &$val)
            {
                $content->nid = $val['id'];
                $val['count'] =  $content->Total();
                $val['create_at'] = date('Y-m-d H:i',$val['create_at']);
            }
            return Help::JsonData(0,Yii::t('app','get_success'),$model->getListTotal(),$result);
        }
            return Help::JsonCode(Help::ERROR,Yii::t('app','get_error'));
    }

    /*
     *常规管理 - 装修品鉴 - 添加页面
     **/
    public function actionHomeadd()
    {
        $label = new \common\models\CategoryLabel();
        $label->type = 4;
        $labelList = $label->getList([]);
        return $this->render('homeadd',['label'=>$labelList]);
    }

    /*
     * 常规管理 - 装修品鉴 - 添加数据
     **/
    public function actionHomeaddform()
    {
        $model = new \common\models\NewsRenovation();
        $auth  = $model->Authenticator(Yii::$app->request->post());
        if(is_object($auth))
        {
            $img = UploadFile::InstanceImgName('img',Yii::$app->params['img_url']['pj']);
            if(is_string($img))
            {
                    $auth->img = $img;
            }
            if($auth->save())
            {
                return Help::JsonCode(Help::SUCCESS,Yii::t('app','add_success'));
            }
                UploadFile::delImg(Yii::$app->params['img_url']['pj'],$img);
        }
                return Help::JsonCode(Help::ERROR,Yii::t('app','add_error'),$auth);
    }

    /*
     * 常规管理 - 装修品鉴 - 修改页面
     **/
    public function actionHomeedit()
    {
        $model = new NewsRenovation();
        $row = $model->FindById(Yii::$app->request->get('id'));
        if(!empty($row))
        {
            $row['img'] = Yii::$app->params['httpImg']['host'].Yii::$app->params['httpImg']['pj'].$row['img'];
            return $this->render('homeedit',['model'=>$row,'housename'=>Yii::$app->request->get('name')]);
        }
    }

    /*
     * 常规管理 - 装修品鉴 - 修改数据
     **/
    public function actionHomeeditform()
    {
        $model = new NewsRenovation();
        $input = Yii::$app->request->post();
        $row = $model->Authenticator($input);
        if(is_object($row))
        {
            $row = $model->FindById($input['id']);
            if(!empty($row))
            {
                $row = Help::SetAttr($input,$model,$row);
                if(!empty($_FILES['img']))
                {
                    $url = Yii::$app->params['img_url']['pj'];
                    $img = UploadFile::InstanceImgName('img',$url);
                    if(is_string($img))
                    {
                        UploadFile::delImg($url,$row->img);
                        $row->img = $img;
                    }
                }
                if($row->save(false)) return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
            }
         }
                                 return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'),$row);
    }

    /*
     * 常规管理 - 装修品鉴 - 批量删除
     **/
    public function actionHomedel()
    {
        $id = Yii::$app->request->post('id');
        if(is_array($id))
        {

            $model = NewsRenovation::updateAll(['del'=>2,'update_at'=>$_SERVER['REQUEST_TIME']],['id'=>$id]);
            if($model >0)
            {
                return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
            }

        }
                return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_error'));
    }

    /*
     * 常规管理 - 装修品鉴 - 隐藏和显示
     **/
    public function actionHomeshow()
    {
        $model = new NewsRenovation();
        $row = $model->FindById(Yii::$app->request->post('id'));
        $input = Yii::$app->request->post();
        if(!empty($row))
        {
           switch ($input['type'])
           {
               case 'clicks':
                    $row->click = $input['clicks'];
                   break;
               case 'show':
                   if($row->is_show == 1)
                   {
                       $row->is_show = 2;
                   }
                   else
                   {
                       $row->is_show = 1;
                   }
                   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 actionCont()
    {
        return $this->render('cont',['post'=>Yii::$app->request->get()]);
    }

    /*
     * 常规管理 - 装修品鉴 - 内容数据
     **/
    public function actionContform()
    {
        $model = new NewsRenovationcontent();
        $model->nid = Yii::$app->request->post('nid');
        $rows = $model->getList(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->getListTotal(),$rows);
        }
    }

    /*
     *
     **/
    public function actionContadd()
    {
        $model = new \common\models\CategoryLabel();
        $model->type = 4;
        $rows = $model->getList([]);
        return $this->render('contadd',['post'=>Yii::$app->request->get(),'model'=>$rows]);
    }

    /*
     *
     **/
    public function actionContaddform()
    {
         $model = new NewsRenovationcontent();
         $model->scenario = 'add';
         $auth = $model->Authenticator(Yii::$app->request->post());
         if(is_object($auth) && $auth->save())
         {
                return Help::JsonCode(Help::SUCCESS,Yii::t('app','add_success'));
         }
                return Help::JsonCode(Help::ERROR,Yii::t('app','add_error'),$auth);
    }

    /*
     *常规管理 - 装修品鉴 - 内容排序
     **/
    public function actionConteditsort()
    {
        $model = new NewsRenovationcontent();
        $input = Yii::$app->request->post();
        $rows = $model->FindById($input['id']);
        if(!empty($rows))
        {
            if(!empty($input['sort']))
            {
                $rows->sort = $input['sort'];
            }

            if($input['is_show'] == 1)
            {
                if( $rows->is_show  == 1)
                {
                    $rows->is_show  =2;
                }
                else
                {
                    $rows->is_show = 1;
                }
            }
            if($rows->save())   return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
        }
                                return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'));
    }

    /*
     *
     **/
    public function actionContedit()
    {
        $model = new NewsRenovationcontent();
        $rows = $model->FindById(Yii::$app->request->get('id'));
        if(!empty($rows))
        {
            $label = new \common\models\CategoryLabel();
            $label->type = 4;
            $labelList = $label->getList([]);
           return $this->render('contedit',['model'=>$rows,'label'=>$labelList,'post'=>Yii::$app->request->get()]);
        }
    }

    /*
     *
     **/
    public function actionConteditform()
    {
        $model = new NewsRenovationcontent();
        $row = $model->FindById(Yii::$app->request->post('id'));
        if (!empty($row))
        {
            $row->content = Yii::$app->request->post('content');
            if($row->save(false)) return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
        }
            return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_error'));
    }

}