<?php

namespace frontend\controllers;

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

class GroupController 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 = GroupT::getTree();

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

        return $data;
    }

    public function actionEdit()
    {
        $request = Yii::$app->request;
        $id = $request->get('id',0);
        if($id > 0) {
            $info = GroupT::findOne(['id'=>$id]);
        } else {
            $info = new GroupT();
        }

        $group_items = GroupT::getTree();

        return $this->render('edit',[
            'info' => $info,
            'group_items' => $group_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 = intval($request->post('parent_id',0));
            $base_pay = floatval($request->post('base_pay',0));
            $job_pay = floatval($request->post('job_pay',0));
            $station_pay = floatval($request->post('station_pay',0));

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

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

            try {
                $row = null;
                if($id > 0) {
                    $row = GroupT::findOne(['id'=>$id]);
                    if($row->hasChild($parent_id)) {
                        $result['msg'] = '上级不能选择自己下级岗位!';
                        return $result;
                    }
                } else {
                    $row = new GroupT();
                }
                $row->name = $name;
                $row->parent_id = $parent_id;
                $row->base_pay = $base_pay;
                $row->job_pay = $job_pay;
                $row->station_pay = $station_pay;
                $row->save();

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

                $tran->commit();
            } catch(\Exception $e) {
                $tran->rollBack();
                throw $e;
            }
        }
        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 = GroupT::getDb()->beginTransaction();
                try {
                    $row = GroupT::findOne(['id'=>$id]);
                    $children = $row->groups;
                    if(count($children) > 0) {
                        $result['msg'] = '请先删除子项';
                        return $result;
                    }
                    $row->delete();

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

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

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

            $id = $request->post('group_id',0);
            $menu_ids = $request->post('menu_ids',array());

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

            try {
                $row = null;
                if($id > 0) {
                    $row = GroupT::findOne(['id'=>$id]);
                } else {
                    return $result;
                }

                $row->unlinkAll('menus',true);
                foreach($menu_ids as $menu_id) {
                    $menu_item = MenuT::findOne(['id'=>$menu_id]);
                    $row->link('menus',$menu_item);
                    $tmp_total = $row->getMenus()->where('id='.$menu_item->parent_id)->count();
                    if($tmp_total == 0) {
                        $parent = $menu_item->parent;
                        if($parent)
                            $row->link('menus',$parent);
                    }
                }

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

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

        $id = $request->get('id',0);
        $group_info = GroupT::findOne(['id'=>$id]);
        $menus = $group_info->menus;
        $menu_ids = array();
        foreach($menus as $item) {
            $menu_ids[] = $item->id;
        }
        $menu_items = MenuT::getTree();

        return $this->render('permission',[
            'group_info' => $group_info,
            'menu_items' => $menu_items,
            'group_id' => $id,
            'menu_ids' => $menu_ids
        ]);
    }
}
