<?php

namespace app\admin\model;

class NavModel
{
    public function getNavs($roles = ''){

        $permissions = new PermissionModel();

        if(!empty($roles)){
            $parents = $permissions->where(['is_nav'=>1,'pid'=>0])->field('id,pid,title,is_nav,nav_url,nav_icon')->order('sort desc')->all($roles)->toArray();
        } else {
            $parents = $permissions->where(['is_nav'=>1,'pid'=>0])->field('id,pid,title,is_nav,nav_url,nav_icon')->order('sort desc')->all()->toArray();
        }

        foreach ($parents as $key => $parent) {
            if(!empty($roles)){
                $children = $permissions->where(['is_nav'=>1,'pid'=>$parent['id']])->field('id,pid,title,is_nav,nav_url')->order('sort desc')->all($roles)->toArray();
                $parents[$key]['children'] = $children;
            } else {
                $children = $permissions->where(['is_nav'=>1,'pid'=>$parent['id']])->field('id,pid,title,is_nav,nav_url')->order('sort desc')->all()->toArray();
                $parents[$key]['children'] = $children;
            }

        }
        return $parents;
    }
}