<?php

namespace app\common\api;

use app\admin\model\Role;
use app\admin\model\SysRelation;
use app\admin\model\Menu;

/**
 * Description of MenuApi
 *
 * @author sgq
 */
class MenuApi {

    public static function getMenuListByRoleid($roleid) {
        $role = Role::find($roleid);
        $menu_ids = $role->sysRelation->column("menuid");
        $menus = Menu::where("id", "in", $menu_ids)->where("ismenu", 1)->where("status", 1)->order("levels asc,num asc")->select()->toArray();
        return self::buildMenu($menus);
    }

    public static function getPrivilagesByRoleid($roleid) {
        $role = Role::find($roleid);
        $menu_ids = $role->sysRelation->column("menuid");
        $menus = Menu::where("status", 1)->order("levels asc,num asc")->select()->toArray();
        $codeToPid = [];
        foreach ($menus as &$menu) {
            $codeToPid[$menu["code"]] = $menu["id"];
            $menu["checked"] = in_array($menu["id"], $menu_ids) ? true : false;
            $menu["pid"] = isset($codeToPid[$menu["pcode"]]) ? $codeToPid[$menu["pcode"]] : 0;
        }unset($menu);
        return $menus;
    }

    /**
     * 递归获取多级菜单集合
     * @param type $menus
     * @param type $pcode
     * @return type
     */
    private static function buildMenu($menus, $pcode = "0") {
        $result = [];
        foreach ($menus as $key => $menu) {
            if ($pcode == $menu["pcode"]) {
                unset($menus[$key]);
                $menu["children"] = self::buildMenu($menus, $menu["code"]);
                if ($menu["pcode"] == 0) {
                    $result[] = $menu;
                }
            }
        }
        return $result;
    }

    /**
     * 权限检查
     * @param type $url 新权限url
     * @param type $old_url 旧系统权限url
     * @return boolean
     */
    public static function chkPermission($url, $old_url) {
        $url = strtolower($url);
        $old_url = strtolower($old_url);
        $menu = Menu::whereRaw("`status`=1 and (new_url=:new_url or `url`=:url)", ["new_url" => $url, "url" => $old_url])->findOrEmpty()->toArray();
        if (!$menu)
            return true;
        $menuid = $menu["id"];
        $roleid = session("user")["roleid"];
        $role = Role::find($roleid);
        $menu_ids = $role->sysRelation->column("menuid");
        return in_array($menuid, $menu_ids);
    }

    public static function getEnterpriseMenuByType($type) {
        $menus = [];
        $menus[] = ["type" => [1, 2, 3], "code" => "qyzx", "pcode" => "0", "name" => "企业用户中心", "url" => "/enterprise/index/centerPage", "icon" => "fa-user"];
        $menus[] = ["type" => [1, 2], "code" => "yhfk", "pcode" => "0", "name" => "用户反馈", "url" => "", "icon" => "fa-bug"];
        $menus[] = ["type" => [1, 2], "code" => "rcrd", "pcode" => "0", "name" => "人才认定", "url" => "#", "icon" => "fa-thumbs-o-up"];
        $menus[] = ["type" => [1, 2], "code" => "rcrdsb", "pcode" => "rcrd", "name" => "人才认定申报", "url" => "/enterprise/talent", "icon" => "fa-thumbs-up"];
        $menus[] = ["type" => [1, 2], "code" => "yxrck", "pcode" => "rcrd", "name" => "优秀人才库", "url" => "", "icon" => "fa-thumbs-up"];
        $menus[] = ["type" => [1, 2], "code" => "lzsb", "pcode" => "rcrd", "name" => "离职申报", "url" => "", "icon" => "fa-thumbs-up"];
        $menus[] = ["type" => [1, 2], "code" => "gzdwbg", "pcode" => "rcrd", "name" => "工作单位变更", "url" => "", "icon" => "fa-thumbs-up"];
        $menus[] = ["type" => [1, 2], "code" => "yhzhbg", "pcode" => "rcrd", "name" => "银行账号变更", "url" => "", "icon" => "fa-thumbs-up"];
        $menus[] = ["type" => [1, 2], "code" => "rcccbg", "pcode" => "rcrd", "name" => "人才层次变更", "url" => "", "icon" => "fa-thumbs-up"];
        $menus[] = ["type" => [1, 2], "code" => "jcxxbg", "pcode" => "rcrd", "name" => "基础信息变更", "url" => "", "icon" => "fa-thumbs-up"];
        $menus[] = ["type" => [1, 2], "code" => "jbtmanage", "pcode" => "0", "name" => "津补贴申报", "url" => "", "icon" => "fa-money"];
        $menus[] = ["type" => [1, 2], "code" => "jbtsb", "pcode" => "jbtmanage", "name" => "津补贴申报", "url" => "", "icon" => "fa-thumbs-up"];
        $menus[] = ["type" => [1, 2], "code" => "gfbt", "pcode" => "0", "name" => "购房补贴", "url" => "", "icon" => "fa-university"];
        $menus[] = ["type" => [1, 2], "code" => "gfbtsb", "pcode" => "gfbt", "name" => "购房补贴申报", "url" => "", "icon" => "fa-thumbs-up"];
        $menus[] = ["type" => [2], "code" => "pxbtmanage", "pcode" => "0", "name" => "培训补贴", "url" => "", "icon" => "fa-building"];
        $menus[] = ["type" => [2], "code" => "pxbt", "pcode" => "pxbtmanage", "name" => "培训补贴", "url" => "", "icon" => "fa-thumbs-up"];
        $menus[] = ["type" => [2], "code" => "zdzxmanage", "pcode" => "0", "name" => "重大专项", "url" => "", "icon" => "fa-life-ring"];
        $menus[] = ["type" => [2], "code" => "zdzx", "pcode" => "zdzxmanage", "name" => "重大专项", "url" => "", "icon" => "fa-thumbs-up"];
        $menus[] = ["type" => [2], "code" => "znjy", "pcode" => "0", "name" => "子女教育", "url" => "", "icon" => "fa-graduation-cap"];
        $menus[] = ["type" => [2], "code" => "xzfbz", "pcode" => "znjy", "name" => "学杂费补助", "url" => "", "icon" => "fa-thumbs-up"];
        $menus[] = ["type" => [2], "code" => "ylbzmanage", "pcode" => "0", "name" => "医疗补助", "url" => "", "icon" => "fa-hospital-o"];
        $menus[] = ["type" => [2], "code" => "ylbz", "pcode" => "ylbzmanage", "name" => "医疗补助", "url" => "", "icon" => "fa-thumbs-up"];
        $menus[] = ["type" => [2], "code" => "zfbz", "pcode" => "0", "name" => "住房保障", "url" => "", "icon" => "fa-home"];
        $menus[] = ["type" => [2], "code" => "zfsq", "pcode" => "zfbz", "name" => "租房申请", "url" => "", "icon" => "fa-home"];
        $menus[] = ["type" => [2], "code" => "fzbz", "pcode" => "zfbz", "name" => "房租补助", "url" => "", "icon" => "fa-money"];
        $menus[] = ["type" => [2], "code" => "gfsq", "pcode" => "zfbz", "name" => "购房申请", "url" => "", "icon" => "fa-envira"];
        $menus[] = ["type" => [2], "code" => "gfbz", "pcode" => "zfbz", "name" => "购房补助", "url" => "", "icon" => "fa-envira"];
        $menus[] = ["type" => [1], "code" => "sbrc", "pcode" => "0", "name" => "硕博人才", "url" => "", "icon" => "fa-graduation-cap"];
        $menus[] = ["type" => [1], "code" => "shbt", "pcode" => "sbrc", "name" => "生活补贴", "url" => "", "icon" => "fa-graduation-cap"];
        $menus[] = ["type" => [3], "code" => "hxjh", "pcode" => "0", "name" => "海峡计划", "url" => "", "icon" => "fa-plane"];
        $menus[] = ["type" => [3], "code" => "hxjhsb", "pcode" => "hxjh", "name" => "海峡计划申报", "url" => "", "icon" => "fa-pencil"];
        foreach ($menus as $key => $menu) {
            if (!in_array($type, $menu["type"]))
                unset($menus[$key]);
        }
        return self::buildMenu($menus);
    }

}
