<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\MenuT;
use common\models\UserT;
use Yii;
use yii\web\Response;

class MenuController 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;
        $menu_items = MenuT::find()
            ->where('parent_id=0')
            ->orderBy('order_id','asc')
            ->all();

        $data = [];
        $data['total'] = count($menu_items);
        $data['rows'] = [];
        foreach($menu_items as $item) {
            $row = $item->toArray();
            $row['show_id'] = $item->id;
            $data['rows'][] = $row;
            $sub_items = $item->menus;
            foreach($sub_items as $sub_item) {
                $row = $sub_item->toArray();
                $row['show_id'] = '＋﹣'.$sub_item->id;
                $row['name'] = '＋﹣'.$sub_item->name;
                $data['rows'][] = $row;
            }
        }

        return $data;
    }

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

        $menu_items = MenuT::find()
            ->where('parent_id=0')
            ->orderby('order_id ASC')
            ->all();

        return $this->render('edit',[
            'info' => $info,
            'menu_items' => $menu_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');
            $parent_id = $request->post('parent_id');
            $path = $request->post('path');
            $order_id = $request->post('order_id');
            $icon = $request->post('icon');

            if($name == '') {
                $result['msg'] = '请输入菜单名称!';
                return $result;
            }
            if($id > 0 && $parent_id == $id) {
                $result['msg'] = '上级菜单不能选择自己!';
                return $result;
            }

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

            try {
                $row = null;
                if($id > 0) {
                    $row = MenuT::findOne(['id'=>$id]);
                } else {
                    $row = new MenuT();
                }
                $row->name = $name;
                $row->icon = $icon;
                $row->path = $path;
                $row->parent_id = $parent_id;
                $row->order_id = $order_id;
                $row->save();

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

            $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) {
                $tran = MenuT::getDb()->beginTransaction();
                try {
                    $row = MenuT::findOne(['id'=>$id]);
                    $children = $row->menus;
                    if(count($children) > 0) {
                        $result['msg'] = '请先删除子菜单';
                        return $result;
                    }
                    $row->unlinkAll('groups',true);
                    $row->delete();

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

                    $tran->commit();
                } catch(\Exception $e) {
                    $tran->rollBack();
                    throw $e;
                }
            }
        }
        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 = MenuT::getDb()->beginTransaction();

            try {
                foreach($order_ids as $id=>$order_id) {
                    $row = MenuT::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;
    }
}
