<?php

namespace App\Models;

use App\Http\Controllers\Admin\AdminRoleController;
use Illuminate\Database\Eloquent\Model;

class NavsModel extends BaseModel
{
    protected $table='dc_navs';
    protected function getTopMenu(){
        return $this->_get_menu(array(1));
    }
    protected function getLeftMenu()
    {
        return $this->_get_menu(array(0));
    }
    protected function getQuickMenu()
    {
        return $this->_get_menu(array(2));
    }

    private function _get_menu($types){
        $data=$this->whereIn('type',$types)->select('name as text','icon','href','id','parent_id','target','type','sort')->orderBy('sort','desc')->get();
        if(!$data){
            return -3001;
        }
        // dump(trans('menu.menu_navs'));
        $controller=new AdminRoleController;
        $code=$controller->getRoot();
        $allcode=S('ROLE_CODES');
        $status=intval(S("ENABLED_PRIVCHECK"))?intval(S("ENABLED_PRIVCHECK")):0;
        $data=$data->toArray();
        if(is_array($data) && count($data)>0){
            $navs=array();
            foreach($data as $k=>$v){
                $v['text']=trans('menu.'.trim($v['text']))=='menu.'.trim($v['text'])?trim($v['text']):trans('menu.'.trim($v['text']));
                if(intval($v['parent_id'])>0){
                    if(!isset($navs[$v['parent_id']]['subset'])){
                        $navs[$v['parent_id']]['subset']=array();
                    }

                    if($code==1||in_array(trim($v['href']),$code)||$status<1||(!in_array(trim($v['href']),$allcode))){
                        $tmp=$v;
                        unset($tmp['id']);
                        unset($tmp['parent_id']);
                        $navs[$v['parent_id']]['subset'][]=$tmp;
                    }
                }else{
                    if(isset($navs[$v['id']])){
                        if($code==1||in_array(trim($v['href']),$code)||$status<1||(!in_array(trim($v['href']),$allcode))) {
                            $navs[$v['id']] = array_merge($navs[$v['id']], $v);
                        }
                    }else{
                        if($code==1||in_array(trim($v['href']),$code)||$status<1||(!in_array(trim($v['href']),$allcode))) {
                            $navs[$v['id']] = $v;
                        }
                    }
                    unset($navs[$v['id']]['id']);
                    unset($navs[$v['id']]['parent_id']);
                }
            }
            $item=array();

            foreach($navs as $v1){
                if(isset($v1['subset'])){
                    if(count($v1['subset'])>0&&isset($v1['text'])){
                        $item[]=$v1;
                    }
                }else{
                    $item[]=$v1;
                }
            }
//            print_r($navs);
//            uasort($item, 'sortBySort');
            array_multisort(array_column($item,'sort'),SORT_DESC,$item);
//            print_r($item);
            return $item;
        }else{
            return -3002;
        }
    }
    function sortBySort($x, $y){
        return $x['sort'] > $y['sort'];
    }
}
