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

namespace backend\controllers;
use backend\base\CommonController;
use backend\base\Help;
use Yii;
use common\models\PushCityprice;
class PushcitypriceController extends CommonController
{

        /*
         * 首页- 区域价格 页面
         * */
        public function actionPrice()
        {
            return $this->render('price',['type'=>1]);
        }

        /*
         * 首页 - 区域价格数据
         * */
        public function actionPriceform()
        {
            $model = new PushCityprice();
            $model->type = Yii::$app->request->post('type');
            $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->Total(),$rows);
            }

        }

        /*
         * 首页-区域价格添加-页面
         * */
        public function actionPriceadd()
        {
            return $this->render('priceadd',['type'=>Yii::$app->request->get('type')]);
        }

        /*
         * 首页-区域价格添加-数据
         * */
        public function actionPricdeaddform()
        {
            $model = new PushCityprice();
            $input = Yii::$app->request->post();
            $true = false;
            if(!empty($input['city_id']))
            {
                   $transaction =  Yii::$app->db->beginTransaction();
                    foreach ($input['city_id'] as $val)
                    {
                        $_model = clone $model;
                        $cloneRow = $_model::find()->andWhere(['del'=>1])->andWhere(['type'=>$input['type']])->andWhere(['city_id'=>$val])->one();
                        if($cloneRow == null)
                        {
                                $_model->city_id = $val;
                                $_model->type = $input['type'];
                                if($_model->save(false))
                                {
                                    $true = true;
                                }
                                else
                                {
                                    $true = false;
                                    break;

                                }
                        }
                        else
                        {
                            return Help::JsonCode(Help::ERROR,'添加失败,请查看该区域是否存在!');
                        }
                    }
                    if($true){
                        $transaction->commit();
                        return Help::JsonCode(Help::SUCCESS,Yii::t('app','add_success'));
                    }
                        $transaction->rollBack();
            }
                        return Help::JsonCode(Help::ERROR,Yii::t('app','add_error'));
        }

        /*
         * 删除
         * */
        public function actionPriceeditanddel()
        {
            $model = new PushCityprice();
            $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'));

        }
}