<?php

namespace app\admin\controller;

use app\admin\common\AdminController;
use app\admin\api\RoleApi;

/**
 * Description of Role
 *
 * @author sgq
 */
class Role extends AdminController {

    /**
     * @auth {{/role}}
     * @return type
     */
    public function index() {
        return view();
    }

    /**
     * @auth {{/role/list}}
     * @return type
     */
    public function list() {
        $list = RoleApi::getList($this->request->param());
        return json($list);
    }

    /**
     * @auth {{/role/add}}
     * @return type
     */
    public function add() {
        if ($this->request->isPost()) {
            return json(RoleApi::create($this->request->param()));
        }
        return view();
    }

    /**
     * @auth {{/role/edit}}
     * @return type
     */
    public function edit() {
        if ($this->request->isPost()) {
            return json(RoleApi::update($this->request->param()));
        }
        $id = $this->request->param("id");
        return view("", ["role" => RoleApi::getOne($id)]);
    }

    /**
     * @auth {{/role/role_edit}}
     */
    public function role_edit() {
        
    }

    /**
     * @auth {{/role/role_assign}}
     * @return type
     */
    public function assign() {
        $role = RoleApi::getOne($this->request->param("id"));
        return view("", ["role" => $role]);
    }

    /**
     * @auth {{/role/setAuthority}}
     */
    public function set_authority() {
        if ($this->request->isPost()) {
            return json(RoleApi::set_authority($this->request["roleId"], $this->request["ids"]));
        }
    }

    /**
     * @auth {{/role/remove}}
     */
    public function delete() {
        if ($this->request->isPost()) {
            return json(RoleApi::delete($this->request->param("roleId")));
        }
    }

    function roleTreeListByUserId() {
        $userId = $this->request->param("userId");
        $user = \app\admin\api\UserApi::getOne($userId);
        $roleIds = array_filter(explode(",", $user["roleid"]));
        $list = getTreeList(RoleApi::getList([]));
        $format_list = [];
        foreach ($list as $item) {
            $format_list[] = [
                "checked" => in_array($item["id"], $roleIds) ? true : false,
                "id" => $item["id"],
                "isOpen" => true,
                "name" => $item["name"],
                "open" => $item["pid"] == 0 ? true : false,
                "pId" => $item["pid"]
            ];
        }
        return $format_list;
    }

    function treelist() {
        $list = getTreeList(RoleApi::getList([]));
        $format_list = [];
        foreach ($list as $item) {
            $format_list[] = [
                "checked" => false,
                "id" => $item["id"],
                "isOpen" => true,
                "name" => $item["name"],
                "open" => $item["pid"] == 0 ? true : false,
                "pId" => $item["pid"]
            ];
        }
        $format_list[] = ["checked" => true, "id" => "0", "isOpen" => true, "name" => "顶级", "open" => true, "pId" => "0"];
        return $format_list;
    }

}
