<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\CityT;
use common\models\DistrictT;
use common\models\UserT;
use Yii;
use yii\data\Pagination;
use yii\web\Response;

class DistrictController extends \yii\web\Controller
{
    public $my = null;

    public function init()
    {
        parent::init();

        $cookie = Yii::$app->request->cookies;
        $user_id = MyLib::encrypt($cookie->get('aid'),'DECODE');

        if($user_id != 0)
        {
            $this->my = UserT::findOne(['id'=>$user_id]);
        } else {
            Yii::$app->response->redirect('/common/login')->send();
            exit;
        }
    }

    public function actionIndex()
    {
        $request = Yii::$app->request;
        $name = $request->get('name');
        $city_id = $request->get('$city_id');
        $page = $request->get('page',1);

        $query = DistrictT::find();
        if($name != '') {
            $query = $query->andWhere(['like','name',$name]);
        }
        if($city_id != '') {
            $query = $query->andWhere('city_id='.$city_id);
        }

        $total = $query->count();

        $pagination = new Pagination(['totalCount' => $total,'pageSize'=>20]);
        $pagination->setPage($page-1);

        $query = $query->orderBy('order_id ASC');
        $query = $query->offset($pagination->offset)->limit($pagination->limit);
        $items = $query->all();

        $page_info = MyLib::getPageInfo($pagination);

        $city_items = CityT::find()->all();

        return $this->render('index',[
            'items' => $items,
            'page_info' => $page_info,
            'page' => $page,
            'name' => $name,
            'city_id' => $city_id,
            'city_items' => $city_items
        ]);
    }

    public function actionEdit()
    {
        $request = Yii::$app->request;
        $id = $request->get('id',0);
        if($id > 0) {
            $info = DistrictT::findOne(['id'=>$id]);
        } else {
            $info = new DistrictT();
            $order_id = 1;
            $tmp_row = DistrictT::find()
                ->orderBy('order_id DESC')
                ->one();
            if($tmp_row) {
                $order_id = $tmp_row->order_id + 1;
            }
            $info->order_id = $order_id;
        }

        $city_items = CityT::find()->all();

        return $this->render('edit',[
            'info' => $info,
            'city_items' => $city_items
        ]);
    }

    public function actionSave()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '保存失败';

        if($request->isPost) {
            $id = $request->post('id',0);
            $name = $request->post('name');
            $city_id = $request->post('city_id');
            $order_id = $request->post('order_id');

            if($name == '') {
                $result['msg'] = '请输入名称!';
                return $result;
            }

            $row = null;
            if($id > 0) {
                $row = DistrictT::findOne(['id'=>$id]);
            } else {
                $row = new DistrictT();
            }
            $row->name = $name;
            $row->order_id = $order_id;
            $row->city_id = $city_id;
            $row->save();

            $result['success'] = true;
            $result['msg'] = '保存成功';
        }
        return $result;
    }

    public function actionDelete()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '删除失败';

        if($request->isPost) {
            $id = $request->post('id',0);

            if($id > 0) {
                $row = DistrictT::findOne(['id'=>$id]);
                $row->delete();

                $result['success'] = true;
                $result['msg'] = '删除成功';
            }
        }
        return $result;
    }

    public function actionOrderSave()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '保存失败';

        if($request->isPost) {
            $order_ids = $request->post('order_ids',array());

            $tran = DistrictT::getDb()->beginTransaction();

            try {
                foreach($order_ids as $id=>$order_id) {
                    $row = DistrictT::findOne(['id'=>$id]);
                    if($row) {
                        $row->order_id = $order_id;
                        $row->save();
                    }
                }
                $result['success'] = true;
                $result['msg'] = '保存成功';

                $tran->commit();
            } catch(\Exception $e) {
                $tran->rollBack();
                throw $e;
            }
        }
        return $result;
    }
}