<?php
namespace app\common\model;

use think\Db;
use tools\Phptree;

class Menu extends Base
{
    protected $autoWriteTimestamp = false;

    public function updates(){
        $data = request()->post();
        return $this->updateInfo($data,'Menu','');
    }

    // 获取所有权限
    public function getAllMenuTree(){
        $map[] = ['id','not in',[37,38,39,47,48]];
        $map[] = ['del','=',0];
        $lists = Db::name('menu')
            ->where('enable',1)
            ->where($map)
            ->field('id,title,pid')
            ->order('sort asc,id asc')
            ->select();
        $lists = $lists?$lists:[];
        foreach ($lists as $k=>$v){
            $pids = [];
            $pid = $v['pid'];
            while (true){
                if($pid == 0){
                    break;
                }else{
                    $pids[] = 'sub_'.$pid;
                    $cc = Db::name('menu')
                        ->where('id',$pid)
                        ->find();
                    if(!$cc){
                        break;
                    }
                    $pid = $cc['pid'];
                }
            }
            $lists[$k]['pids'] = $pids?implode(' ',$pids):'';
        }
        $tree = Phptree::makeTree(($lists), array(
            'primary_key'=>'id',
            'parent_key'=>'pid',
            'expanded' => true,
            'children_key' => 'children'
        ));
        return $tree;
    }

    // 获取某组织的所有权限
    public function getOrgAllMenuTree($orgId){
        $auths = model('Org')->getOrgAuths($orgId,1);
        $lists = [];
        if($auths){
            $map[] = ['id','not in',[37,38,39,47,48]];
            $map[] = ['del','=',0];
            $lists = Db::name('menu')
                ->where('enable',1)
                ->where('id','in',$auths)
                ->where($map)
                ->field('id,title,pid')
                ->order('sort asc,id asc')
                ->select();
            $lists = $lists?$lists:[];
            foreach ($lists as $k=>$v){
                $pids = [];
                $pid = $v['pid'];
                while (true){
                    if($pid == 0){
                        break;
                    }else{
                        $pids[] = 'sub_'.$pid;
                        $cc = Db::name('menu')
                            ->where('id',$pid)
                            ->find();
                        if(!$cc){
                            break;
                        }
                         $pid = $cc['pid'];
                    }
                }
                $lists[$k]['pids'] = $pids?implode(' ',$pids):'';
            }
        }
        $tree = Phptree::makeTree(($lists), array(
            'primary_key'=>'id',
            'parent_key'=>'pid',
            'expanded' => true,
            'children_key' => 'children'
        ));
        return $tree;
    }


    public function getOrgAllMenuTree_new($orgId){
        $auths = model('Org')->getOrgAuths($orgId,1);
        $lists = [];
        if($auths){
            $map[] = ['id','not in',[37,38,39,47,48]];
            $map[] = ['del','=',0];
            $lists = Db::name('menu')
                ->where('enable',1)
                ->where('id','in',$auths)
                ->where($map)
                ->field('id,title,pid')
                ->order('sort asc,id asc')
                ->select();
            $lists = $lists?$lists:[];
//            foreach ($lists as $k=>$v){
//                $pids = [];
//                $pid = $v['pid'];
//                while (true){
//                    if($pid == 0){
//                        break;
//                    }else{
//                        $pids[] = 'sub_'.$pid;
//                        $cc = Db::name('menu')
//                            ->where('id',$pid)
//                            ->find();
//                        if(!$cc){
//                            break;
//                        }
//                        $pid = $cc['pid'];
//                    }
//                }
//                $lists[$k]['pids'] = $pids?implode(' ',$pids):'';
//            }
        }
        $tree = Phptree::makeTree(($lists), array(
            'primary_key'=>'id',
            'parent_key'=>'pid',
            'expanded' => true,
            'children_key' => 'children'
        ));
        return $tree;
    }

    // 根据角色和组织获取用户菜单项
    public function getMenuTree($rolesId,$orgId){
        $map[] = ['enable','=',1];
        $map[] = ['del','=',0];
        $map[] = ['is_btn','=',0];
        if($rolesId == 1){ // 超级管理员

        }else if($rolesId == 2){ //总公司管理员
            $auths = model('Org')->getOrgAuths($orgId,1);
            if($auths){
                $map[] = ['id','in',$auths];
            }else{
                $map[] = ['id','=',0];
            }
        }else{ // 项目管理员
            $auths = model('Roles')->getRolesAuths($rolesId,1);
            if($auths){
                $map[] = ['id','in',$auths];
            }else{
                $map[] = ['id','=',0];
            }
        }
        if($rolesId!=1){
            /*3.只有admin管理员可见
            系统设置: 菜单管理、配置管理、安卓版本管理、苹果版本管理、
            模块管理、这些项目,在权限分配里面就不要出现了,不需要分配*/
            $map[] = ['id','not in',[37,38,39,47,48]];
        }
        $lists = Db::name('menu')
            ->where($map)
            ->field('id,title,url,icons,pid')
            ->order('sort asc,id asc')
            ->select();
        $lists = $lists?$lists:[];
        $first = [
            'id' => -1,
            'title' => '系统首页',
            'url' => $this->getNavUrl('Index/def'),
            'icons' => 'fa fa-home',
            'pid' => 0,
        ];
        $arr[] = $first;
        foreach ($lists as $k=>$v){
            $v['url'] = $this->getNavUrl($v['url']);
            $arr[] = $v;
        }
        $tree = list_to_tree($arr, 'pid', 'child');
        return $tree;
    }

    /**
     * 获取导航URL
     * @param  string $url 导航URL
     * @return string      解析或的url
     */
    public function getNavUrl($url){
        switch ($url) {
            case 'http://' === substr($url, 0, 7):
            case 'https://' === substr($url, 0, 8):
            case '#' === substr($url, 0, 1):
                break;
            default:
                $url = url($url);
                break;
        }
        return $url;
    }

}