<?php


namespace backend\controllers;

use backend\server\UploadFile;
use Yii;
use backend\base\CommonController;
use backend\base\Help;
use common\models\MiniRotation;

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


    /*
     * 小程序首页轮播-页面
     * */
    public function actionRotation()
    {
        return $this->render('rotation', ['type' => 1]);
    }

    /*
     * 移动首页专题入口
     * */
    public function actionEntrance()
    {
        return $this->render('rotation', ['type' => 2]);
    }


    /*
     * 小程序首页轮播-数据
     * */
    public function actionRotationform()
    {
        $model = new MiniRotation();
        $rows = $model->getList(Yii::$app->request->post());
        if ($rows != null) {
            $imgUrl = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['push_lb'];
            foreach ($rows as &$val) {
                $val['create_at'] = date('Y-m-d H:i', $val['create_at']);
                $val['img'] = $imgUrl . $val['img'];
            }
            return Help::JsonData(0, Yii::t('app', 'get_success'), $model->Total(Yii::$app->request->post()), $rows);
        }
        return Help::JsonCode(Help::ERROR, Yii::t('app', 'get_error'));
    }


    /*
    * 小程序首页轮播-添加页面
    */
    public function actionRotationadd()
    {
        $cityModel = $this->City();
        return $this->render('rotationadd', ['city' => $cityModel, 'type' => Yii::$app->request->get('type')]);
    }


    /*
    * 小程序首页轮播-添加数据
    */
    public function actionRotationaddform()
    {
        try {
            $model = new MiniRotation();
            $input = Yii::$app->request->post();
            $url = Yii::$app->params['img_url']['push_lb'];
            $img = UploadFile::InstanceImgName_v1('img', $url);

            if (is_string($img)) {
                $input['img'] = $img;
            }
            $model->load($input, '');

            if ($model->save()) return Help::JsonCode(Help::SUCCESS, Yii::t('app', 'add_success'));

            return Help::JsonCode(Help::SUCCESS, Yii::t('app', 'add_error'), $model->errors);

        } catch (\Exception $e) {
            return Help::JsonCode(Help::SUCCESS, Yii::t('app', 'add_error'), $e->getMessage());

        }

    }


    /*
    * 小程序首页轮播-修改页面
    */
    public function actionRotationedit()
    {
        $model = new MiniRotation();
        $input = Yii::$app->request->get();
        $row = $model->FindById($input['id']);

        if ($row != null) {
            $imgUrl = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['push_lb'];
            $row['img'] = $imgUrl . $row['img'];
            $cityModel = $this->City();
            foreach ($cityModel as &$val) {
                $val['checked'] = '';
                if ($val['id'] == $row['city']) {
                    $val['checked'] = 'selected';
                }
            }
            return $this->render('rotationedit', ['model' => $row, 'city' => $cityModel]);
        }
    }


    /*
     * 小程序首页轮播-修改数据
     * */
    public function actionRotationeditform()
    {
        $model = new MiniRotation();
        $input = Yii::$app->request->post();
        $setAttribute = $model->Authenticator($input);
        if (is_object($setAttribute)) {
            $row = $model->FindById($input['id']);
            if ($row != null) {
                $row = Help::SetAttr($input, $model, $row);
                $url = Yii::$app->params['img_url']['push_lb'];
                $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'), $setAttribute);
    }


    /*
     *小程序首页轮播-删除and禁用
     * */
    public function actionImgdelshow()
    {
        $model = new MiniRotation();
        $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':
                    if ($row->delete()) {
                        UploadFile::delImg(Yii::$app->params['img_url']['push_lb'], $row->img);
                        return Help::JsonCode(Help::SUCCESS, Yii::t('app', 'edit_success'));
                    }
                    break;
                case 'expiration':
                    $row->expiration_date = $input['expiration'];
                    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'));
    }

    /**
     * PC搜索框热门区域
     * @return string
     */
    public function actionHotcity()
    {
        return $this->render('hotcity', ['type' => 3]);
    }


    /**
     * PC搜索框热门区域添加页面
     * @return string
     */
    public function actionHotcityadd()
    {
        return $this->render('hotcityadd', ['type' => Yii::$app->request->get('type')]);
    }

    /**
     * PC搜索框热门区域修改页面
     * @return string
     */
    public function actionHotcityedit()
    {
        $model = new MiniRotation();
        $input = Yii::$app->request->get();
        $row = $model->FindById($input['id']);

        if ($row != null) {
            $imgUrl = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['push_lb'];
            $row['img'] = $imgUrl . $row['img'];
            return $this->render('hotcityedit', ['model' => $row]);
        }
    }

}