<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/4/19
 * Time: 上午10:52
 */

namespace backend\controllers;

use backend\base\CommonController;
use backend\base\Help;
use Yii;
use common\models\CategoryCity;
use backend\server\UploadFile;
use common\models\CityDetails;
class RoutinecityController  extends CommonController
{

        /*
         * 常规管理-区域介绍-首页
         * */
        public function actionIndex()
        {
            $model = new CategoryCity();
            $city = $model->getList([]);
            return $this->render('index',['city'=>$city]);
        }

        /*
         * 常规管理-区域介绍-首页获取数据
         * */
        public function actionIndexform()
        {
            $model = new CityDetails();
            $city = Yii::$app->request->post('city');
            if(!empty($city))
            {
                $model->city_id = $city;
            }
            $rows = $model->getList(Yii::$app->request->post());
            if($rows != null)
            {
                return Help::JsonData(0,Yii::t('app','get_success'),$model->Total(),$rows);
            }
                return Help::JsonCode(Help::ERROR,Yii::t('app','get_error'));

        }

        /*
         *  常规管理-区域介绍- 区域添加页面
         * */
        public function actionCityadd()
        {
            $model = new CategoryCity();
            $city = $model->getList([]);
            return $this->render('cityadd',['city'=>$city]);
        }

        /*
         * 常规管理-区域介绍- 区域添加数据
         * */
        public function actionCityaddform()
        {

            $model = new \common\models\CityDetails();
            $input = Yii::$app->request->post();
            $result = $model->Authenticator($input);
            if(is_object($result))
            {
                $url = Yii::$app->params['img_url']['city'];
                $img = UploadFile::InstanceImgName('img',$url);
                if(is_string($img))
                {
                    $result->thumb = $img;
                }
                $img_bg = UploadFile::InstanceImgName('img_bg',$url);
                if(is_string($img_bg))
                {
                    $result->background_img = $img_bg;
                }
                $imgs = UploadFile::InstancesImgName('imgs',$url);
                if(is_array($imgs))
                {
                    $result->img = json_encode($imgs);
                }

                if($result->save(false) == true) return Help::JsonCode(Help::SUCCESS,Yii::t('app','add_success'));
            }
                    return Help::JsonCode(Help::ERROR,Yii::t('app','add_error'),$result);
        }

        /*
         * 常规管理-区域介绍- 区域修改页面
         * */
        public function actionCityedit()
        {

            $model = new CategoryCity();
            $city = $model->getList([]);

            $detals = new CityDetails();
            $row = $detals->FingById(Yii::$app->request->get('id'));
            $url = Yii::$app->params['httpImg']['host']. Yii::$app->params['httpImg']['city'];
            if(is_string($row['img']))
            {
                $row['img'] = json_decode($row['img']);
            }
            return $this->render('cityedit',['city'=>$city,'details'=>$row,'url'=>$url]);
        }

        /*
         *  常规管理-区域介绍- 区域修改数据
         * */
        public function actionCityeditform()
        {
            $model = new CityDetails();
            $input = Yii::$app->request->post();

            $result = $model->Authenticator($input);

            if(is_object($result))
            {
                $row = $model->FingById($input['id']);

                if($row == null) return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'));
                $row = Help::SetAttr($input,$model,$row);

                $url = Yii::$app->params['img_url']['city'];

                $img = UploadFile::InstanceImgName('img',$url);
                if(is_string($img))
                {
                    UploadFile::delImg($url, $row->thumb);
                    $row->thumb = $img;
                }
                $img_bg = UploadFile::InstanceImgName('img_bg',$url);
                if(is_string($img_bg))
                {
                    UploadFile::delImg($url, $row->background_img);
                    $row->background_img = $img_bg;
                }

                if(isset($input['imgsdel']) && !empty($input['imgsdel']))
                {
                    $row['img'] = json_decode($row['img']);
                    $arrDelImg = array_values(array_diff( $row['img'],$input['imgsdel']));
                    $row->img = json_encode($arrDelImg);
                }
                $imgs = UploadFile::InstancesImgName('imgs',$url);
                if(is_array($imgs))
                {


                    if(!empty($row->img))
                    {
                        $arrAddImg =  array_merge($imgs,json_decode($row->img));
                    }
                    else
                    {
                        $arrAddImg = $imgs;
                    }
                   if(count($arrAddImg) > 3)  return Help::JsonCode(Help::ERROR,'只能上传3张区域图集');
//                   foreach ($imgs as $val)
//                   {
//                       UploadFile::delImg($url,$val);
//                   }


                    $row->img = json_encode($arrAddImg);
                }

                if(!empty($arrAddImg) && !empty($arrDelImg))
                {
                    $merge = array_merge($arrAddImg,$arrDelImg);
                    $merge = array_unique($merge);
                    if(count($merge) >3)   return Help::JsonCode(Help::ERROR,'只能上传3张区域图集');
                    $row->img = json_encode($merge);
                }

                if($row->save(false) == true) return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
            }
                return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'),$result);
        }

        /*
         * 常规管理-区域介绍-删除
         * */
        public function actionCitydel()
        {
            $model = new CityDetails();
            $input = Yii::$app->request->post();
            $row = $model->FingById($input['id']);
            if($row != null)
            {
                $row->del = 2;
                if($row->save(false))  return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
            }

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

}