<?php
// +----------------------------------------------------------------------
// | 角色权限编辑
// +----------------------------------------------------------------------
// | Copyright (c) 2018 https://blog.junphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小黄牛 <1731223728@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\admin\auth;
use app\controller\SuperClass\Admin;

class Role_menu extends Admin
{

    /**
     * @AopBefore(class="app\aop\AdminLogin", function="before")
     * @RequestMapping(route="/admin/auth/role_menu", method="GET|POST", title="角色权限编辑")
     * @Ioc(class="\x\Db", name="Db")
    */
    public function run() {
        if (\x\Request::is_ajax()) {
            return $this->_update();
        }
        $param = \x\Request::get();

        $info = $this->Db->name('auth_role')->where('id', $param['pid'])->field('id, name, menu_list')->find();
        $this->assign('info', $info);
        $this->assign('menu', json_encode($this->showlist($info['menu_list']), JSON_UNESCAPED_UNICODE));

        $this->Db->return();
        return $this->display();
    }

    // Ajax处理
    private function _update() {
        $param = \x\Request::post();
        $param['update_time'] = time();
        $res = $this->Db->name('auth_role')->where('id', $param['id'])->update($param);
        $this->Db->return();
        if ($res != false) {
            return $this->returnJson('00', '修改成功！');
        }

        return $this->returnJson('01', '修改失败！');
    }

    // 列表
    private function showlist($menu) {
        $array = explode(',', $menu);
        $list = $this->Db->name('auth_menu')->order('path ASC')->where('public', 0)->where('status', 1)->field('id as nid, pid as parentid, name')->select();

        foreach ($list as $k=>$v) {
            if (in_array($v['nid'], $array)) {
                $list[$k]['checked']= true;
            } else {
                $list[$k]['checked']= false;
            }
            
            $list[$k]['id'] = $v['nid'];
        }

        return $list;
    }
}

