<?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 $enableCsrfValidation = false;
    public $layout = 'blue-main';

    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()
    {
        return $this->render('index');
    }

    public function actionIndexJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $items = DistrictT::find()
            ->orderBy('order_id asc')
            ->all();

        $data = [];
        $data['total'] = count($items);
        $data['rows'] = [];
        foreach($items as $item) {
            $row = $item->toArray();
            $row['city_name'] = $item->city->name;
            $data['rows'][] = $row;
        }

        return $data;
    }

    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;
    }
}
