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

namespace backend\controllers;

//特色主题,价格区间,物业类型,建筑类别,楼盘标签,4=>装修品鉴
use backend\base\CommonController;
use backend\base\Help;
use backend\server\UploadFile;
use common\models\HousesPrice;
use common\models\Characteristic;
use common\models\CategoryLabel;
use Yii;
class DictionaryaController extends CommonController
{

    //===============================价格区间================================

    /*
     * 区间价格显示页面
     * **/
    public function actionPrice()
    {
        return $this->render('price');
    }

    /*
     * 获取价格区间数据
     * **/
    public function actionPriceform()
    {
        $model = new HousesPrice();
        $rows = $model->getList(Yii::$app->request->post());
        if($rows != null)
        {
            foreach ($rows as &$val)
            {
                $val['create_at'] = date('Y-m-d H:i',$val['create_at']);
                $val['update_at'] = date('Y-m-d H:i',$val['update_at']);
                $val['state'] = Yii::$app->params['state'][$val['state']];
            }
            return Help::JsonData(0,'成功',$model->Total(),$rows);
        }
    }

    /*
     * 添加价格区间页面
     * **/
    public function actionPriceadd()
    {
        return $this->render('priceadd');
    }

    /*
     * 添加价格区间数据
     * **/
    public function actionPriceaddform()
    {
        $model = new HousesPrice();
        $model->load(Yii::$app->request->post('data'),'');
        if($model->validate() && $model->save()) return Help::JsonCode(Help::SUCCESS,'添加成功');
        return Help::JsonCode(Help::ERROR,'添加失败');
    }

    /*
     * 价格区间修改页面
     * **/
    public function actionPriceedit()
    {
        $model = new HousesPrice();
        $row = $model->FindById(Yii::$app->request->get('id'));
        if($row != null)
        {
            return $this->render('priceedit',['model'=>$row]);
        }
    }

    /*
     * 价格区间修改数据
     * **/
    public function actionPriceeditform()
    {
        $model = new HousesPrice();
        $model->load(Yii::$app->request->post('data'),'');
        if(!$model->validate()) return  Help::JsonCode(Help::ERROR,'修改失败');
        $row = $model->FindById(Yii::$app->request->post('data')['id']);
        if($row != null)
        {
            $row->price = $model->price;
            $row->price_short = $model->price_short;
            $row->state = $model->state;
            if($row->update() == true) return Help::JsonCode(Help::SUCCESS,'修改成功');
        }
        return Help::JsonCode(Help::ERROR,'修改失败');
    }

    /*
     * 价格区间删除
     * **/
    public function actionPricedel()
    {
        $model = new HousesPrice();
        $row = $model->FindById(Yii::$app->request->post('id'));
        if($row != null)
        {
            $row->del = 2;
            if($row->update() == true) return Help::JsonCode(Help::SUCCESS,'删除成功');
        }
        return Help::JsonCode(Help::ERROR,'删除失败');
    }

    /*
* 价格区间 排序
* **/
    public function actionPricesort()
    {
        $model = new HousesPrice();
        $row = $model->FindById(Yii::$app->request->post('id'));
        if($row != null)
        {
            $row->sort = Yii::$app->request->post('sort');
            if($row->update() == true) return Help::JsonCode(Help::SUCCESS,'删除成功');
        }
        return Help::JsonCode(Help::ERROR,'删除失败');
    }


    //===============================特色主题================================

    /*
     * 特色主题显示页面
     * **/
    public function actionTheme()
    {
        return $this->render('theme');
    }

    /*
     * 获取特色主题数据
     * **/
    public function actionThemeform()
    {
        $model = new Characteristic();
        $rows = $model->getAllList(Yii::$app->request->post());
        if($rows != null)
        {
            foreach ($rows as &$val)
            {
                $val['create_at'] = date('Y-m-d H:i',$val['create_at']);
                $val['update_at'] = date('Y-m-d H:i',$val['update_at']);
            }
            return Help::JsonData(0,'成功',$model->Total(),$rows);
        }

    }

    /*
     * 特色主题添加页面
     * **/
    public function actionThemeadd()
    {
        return $this->render('themeadd');
    }

    /*
     * 特色主题添加数据
     * **/
    public function actionThemeaddform()
    {
        $model = new Characteristic();
        $model->scenario = 'add';
        $model->load(Yii::$app->request->post('data'),'');
        if($model->validate() && $model->save())
        {
            return Help::JsonCode(Help::SUCCESS,'添加成功');
        }
        return Help::JsonCode(Help::ERROR,'添加失败');
    }

    /*
     * 特色主题修改页面
     * **/
    public function actionThemeedit()
    {
        $model = new Characteristic();
        $row = $model->FindById(Yii::$app->request->get('id'));
        if($row != null)
        {
            return $this->render('themeedit',['model'=>$row]);
        }
    }

    public function actionThemeshow()
    {
        $input = Yii::$app->request->post();
        $model = new Characteristic();
        $row = $model->FindById($input['id']);
        if(!empty($row)){
            $row->is_show = $input['state'];
            if($row->save(false)){
                return Help::JsonCode(Help::SUCCESS, '修改成功');
            }
            return Help::JsonCode(Help::ERROR, '操作失败');
        }
        return Help::JsonCode(Help::ERROR, '操作失败');
    }
    /*
     * 特色主题修改数据
     * **/
    public function actionThemeeditform()
    {
        $model = new Characteristic();
        $model->load(Yii::$app->request->post('data'),'');
        if(!$model->validate()) return Help::JsonCode(Help::ERROR,'修改失败');
        $row = $model->FindById(Yii::$app->request->post('data')['id']);
        if($row != null)
        {
            $row->name = $model->name;
            if($row->update() == true)
            {
                return Help::JsonCode(Help::SUCCESS,'修改成功');
            }

        }
        return Help::JsonCode(Help::ERROR,'修改失败');
    }

    /*
     * 删除特色主题
     * **/
    public function actionThemedel()
    {
        $model = new Characteristic();
        $row = $model->FindById(Yii::$app->request->post('id'));
        if($row != null)
        {
            $row->del = 2;
            if($row->update() == true){
                return Help::JsonCode(Help::SUCCESS,'删除成功');
            }

        }
        return Help::JsonCode(Help::ERROR,'删除失败');
    }


    //===============================物业类型================================

    /*
     * 物业类型显示页面
     * **/
    public function actionLabel()
    {
        return $this->render('label');
    }

    /*
     * 获取物业类型数据页面
     * **/
    public function actionLabelform()
    {
        $model = new CategoryLabel();
        $model->type = 1;
        $rows = $model->getList(Yii::$app->request->post());
        if($rows != null)
        {
            foreach ($rows as &$val)
            {
                $val['create_at'] = date('Y-m-d H:i',$val['create_at']);
                $val['update_at'] = date('Y-m-d H:i',$val['update_at']);
            }
            return Help::JsonData(0,'成功',$model->Total(),$rows);
        }
    }

    /*
     * 添加物业类型页面
     * **/
    public function actionLabeladd()
    {
        return $this->render('labeladd',['type'=>1]);
    }

    /*
     * 添加物业类型数据
     * **/
    public function actionLabeladdform()
    {
        $model =  $model = new CategoryLabel();
        $model->load(Yii::$app->request->post('data'),'');
        if($model->validate() && $model->save()) return Help::JsonCode(Help::SUCCESS,'添加成功');
        return Help::JsonCode(Help::ERROR,'添加失败');
    }

    /*
     * 物业类型修改页面
     * **/
    public function actionLabeledit()
    {
        $model = $model = new CategoryLabel();
        $row = $model->FindById(Yii::$app->request->get('id'));
        if($row != null)
        {
            return $this->render('labeledit',['model'=>$row]);
        }
    }

    /*
     * 修改物业类型数据
     * **/
    public function actionLabeleditform()
    {
        $model = $model = new CategoryLabel();
        $model->load(Yii::$app->request->post('data'),'');
        if(!$model->validate()) return Help::JsonCode(Help::ERROR,'修改失败',$model->errors);
        $row = $model->FindById(Yii::$app->request->post('data')['id']);

        if($row != null)
        {
            $row->name = $model->name;
            if($row->update(false) == true) return Help::JsonCode(Help::SUCCESS,'修改成功');
        }
        return Help::JsonCode(Help::ERROR,'修改失败');
    }

    /*
     * 删除物业类型
     * **/
    public function actionLabeldel()
    {
        $model = $model = new CategoryLabel();
        $row = $model->FindById(Yii::$app->request->post('id'));
        if($row != null)
        {
            $row->del = 2;
            if($row->update() == true) return Help::JsonCode(Help::SUCCESS,'删除成功');
        }
        return Help::JsonCode(Help::ERROR,'删除失败');
    }



    //===============================建筑类别================================

    /*
     * 建筑类别显示页面
     * **/
    public function actionArchitecturetype()
    {
        return $this->render('architecturetype');
    }

    /*
     * 获取物业类型数据页面
     * **/
    public function actionArchitectureform()
    {
        $model = new CategoryLabel();
        $model->type = 2;
        $rows = $model->getList(Yii::$app->request->post());
        if($rows != null)
        {
            foreach ($rows as &$val)
            {
                $val['create_at'] = date('Y-m-d H:i',$val['create_at']);
                $val['update_at'] = date('Y-m-d H:i',$val['update_at']);
            }
            return Help::JsonData(0,'成功',$model->Total(),$rows);
        }
    }

    /*
     * 添加物业类型页面
     * **/
    public function actionArchitectureadd()
    {
        return $this->render('labeladd',['type'=>2]);
    }

    /*
     * 添加物业类型数据
     * **/
    public function actionArchitectureaddform()
    {
        $model =  $model = new CategoryLabel();
        $model->load(Yii::$app->request->post('data'),'');
        if($model->validate() && $model->save()) return Help::JsonCode(Help::SUCCESS,'添加成功');
        return Help::JsonCode(Help::ERROR,'添加失败');
    }

    /*
     * 物业类型修改页面
     * **/
    public function actionArchitectureedit()
    {
        $model = $model = new CategoryLabel();
        $row = $model->FindById(Yii::$app->request->get('id'));
        if($row != null)
        {
            return $this->render('labeledit',['model'=>$row]);
        }
    }

    /*
     * 修改物业类型数据
     * **/
    public function actionArchitectureeditform()
    {
        $model = $model = new CategoryLabel();
        $model->load(Yii::$app->request->post('data'),'');
        if(!$model->validate()) return Help::JsonCode(Help::ERROR,'修改失败');
        $row = $model->FindById(Yii::$app->request->post('data')['id']);
        if($row != null)
        {
            $row->name = $model->name;
            if($row->update() == true) return Help::JsonCode(Help::SUCCESS,'修改成功');
        }
        return Help::JsonCode(Help::ERROR,'修改失败');
    }

    /*
     * 删除物业类型
     * **/
    public function actionArchitecturedel()
    {
        $model = $model = new CategoryLabel();
        $row = $model->FindById(Yii::$app->request->post('id'));
        if($row != null)
        {
            $row->del = 2;
            if($row->update() == true) return Help::JsonCode(Help::SUCCESS,'删除成功');
        }
        return Help::JsonCode(Help::ERROR,'删除失败');
    }

    //===============================楼盘标签================================

    /*
     * 建筑类别显示页面
     * **/
    public function actionHouselabel()
    {
        return $this->render('houselabel');
    }

    /*
     * 获取物业类型数据页面
     * **/
    public function actionHouselabelform()
    {
        $model = new CategoryLabel();
        $model->type = 3;
        $rows = $model->getList(Yii::$app->request->post());
        if($rows != null)
        {
            foreach ($rows as &$val)
            {
                $val['create_at'] = date('Y-m-d',$val['create_at']);
                $val['update_at'] = date('Y-m-d',$val['update_at']);
            }
            return Help::JsonData(0,'成功',$model->Total(),$rows);
        }
    }

    /*
     * 添加物业类型页面
     * **/
    public function actionHouselabeladd()
    {
        return $this->render('labeladd',['type'=>3]);
    }

    /*
     * 添加物业类型数据
     * **/
    public function actionHouselabeladdform()
    {
        $model =  $model = new CategoryLabel();
        $model->load(Yii::$app->request->post('data'),'');
        if($model->validate() && $model->save()) return Help::JsonCode(Help::SUCCESS,'添加成功');
        return Help::JsonCode(Help::ERROR,'添加失败');
    }

    /*
     * 物业类型修改页面
     * **/
    public function actionHouselabeledit()
    {
        $model = $model = new CategoryLabel();
        $row = $model->FindById(Yii::$app->request->get('id'));
        if($row != null)
        {
            return $this->render('labeledit',['model'=>$row]);
        }
    }

    /*
     * 修改物业类型数据
     * **/
    public function actionHouselabeleditform()
    {
        $model = $model = new CategoryLabel();
        $model->load(Yii::$app->request->post('data'),'');
        if(!$model->validate()) return Help::JsonCode(Help::ERROR,'修改失败');
        $row = $model->FindById(Yii::$app->request->post('data')['id']);
        if($row != null)
        {
            $row->name = $model->name;
            if($row->update() == true) return Help::JsonCode(Help::SUCCESS,'修改成功');
        }
        return Help::JsonCode(Help::ERROR,'修改失败');
    }

    /*
     * 删除物业类型
     * **/
    public function actionHouselabeldel()
    {
        $model = $model = new CategoryLabel();
        $row = $model->FindById(Yii::$app->request->post('id'));
        if($row != null)
        {
            $row->del = 2;
            if($row->update() == true) return Help::JsonCode(Help::SUCCESS,'删除成功');
        }
        return Help::JsonCode(Help::ERROR,'删除失败');
    }

    /*
     *装修品鉴 - 页面
     **/
    public function actionRenovation()
    {
        return $this->render('renovation');
    }

    /*
     *装修品鉴 - 数据
     **/
    public function actionRenovationform()
    {
        $model = new CategoryLabel();
        $model->type = 4;
        $rows = $model->getList(Yii::$app->request->post());
        if($rows != null)
        {
            foreach ($rows as &$val)
            {
                $val['create_at'] = date('Y-m-d',$val['create_at']);
                $val['update_at'] = date('Y-m-d',$val['update_at']);
            }
            return Help::JsonData(0,'成功',$model->Total(),$rows);
        }
    }

    /*
     *装修品鉴-添加页面
     **/
    public function actionRenovationadd()
    {
        return $this->render('renovationadd');
    }

    /*
     *
     **/
    public function actionRenovationedit()
    {
        $model = new CategoryLabel();
        $row = $model->FindById(Yii::$app->request->get('id'));
        if(!empty($row))
        {
            return $this->render('renovationedit',['model'=>$row]);
        }

    }

    /*
     *
     **/
    public function actionRenovationeditform()
    {
        $model = new CategoryLabel();
        $row = $model->FindById(Yii::$app->request->post('id'));
        if(!empty($row))
        {
            if(!empty($_FILES['img']))
            {
                $img =  UploadFile::InstanceImgName('img',Yii::$app->params['img_url']['pj']);
                if(is_string($img))
                {
                    UploadFile::delImg(Yii::$app->params['img_url']['pj'],$row->img);
                    $row->img = $img;
                }
            }

            if(!empty($_FILES['img1']))
            {
                $img =  UploadFile::InstanceImgName('img1',Yii::$app->params['img_url']['pj']);
                if(is_string($img))
                {
                    UploadFile::delImg(Yii::$app->params['img_url']['pj'],$row->img1);
                    $row->img1 = $img;
                }
            }
            $row->name = Yii::$app->request->post('name');
            $row->sort = Yii::$app->request->post('sort');
            if($row->save()) return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
        }
        return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'));
    }
    /*
     *装修品鉴 - 添加数据
     ***/
    public function actionRenovationaddform()
    {
        $model =  $model = new CategoryLabel();
        $model->load(Yii::$app->request->post(),'');

        if($model->validate()){
            $img =  UploadFile::InstanceImgName('img',Yii::$app->params['img_url']['pj']);
            if(is_string($img))
            {
                $model->img = $img;
            }

            $img1 =  UploadFile::InstanceImgName('img1',Yii::$app->params['img_url']['pj']);
            if(is_string($img1))
            {
                $model->img1 = $img1;
            }
            if($model->save(false)){
                return Help::JsonCode(Help::SUCCESS,'添加成功');
            }
        }
        return Help::JsonCode(Help::ERROR,'添加失败');
    }



}