<?php

namespace App\Http\Controllers\nn_site;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\nn_slider;
use App\Models\nn_catalog;

use App\Models\nn_menu_item;
use App\Models\nn_menu_item_lang;
use App\Models\nn_article;
use App\Models\nn_menu;
use App\Models\nn_file;
use App\Models\nn_image;
use App\Models\nn_category;
use App\Models\nn_category_lang;
use App\Models\nn_info;

class SitePages extends SiteController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // Slider
   
        $data['isHomepage'] = 1;
        
        $data['about'] =    $about = nn_menu_item::join('nn_menu_item_lang', 'nn_menu_item.id', '=', 'nn_menu_item_lang.nn_menu_item_id')
        ->select('nn_menu_item.id', 'nn_menu_item.slug', 'nn_menu_item_lang.name', 'nn_menu_item_lang.body')
        ->where([['nn_menu_item.id', 25 ],['nn_menu_item_lang.lang', getCurrentLocale()]])
        ->first();

        $data['metat'] = nn_menu_item_lang::where('nn_menu_item_id', 28)->where('lang', getCurrentLocale())->first();

        /*$data['publications'] = nn_article::where(['nn_article_lang.lang' => getCurrentLocale(), 'collection_id' => 1])
        ->join('nn_article_lang', 'nn_article.id', '=', 'nn_article_lang.article_id')
        ->select('nn_article.id', 'nn_article_lang.name', 'nn_article_lang.description', 'nn_article.status')


        ->orderBy('nn_article.created_at','desc')


        ->get();
    */

        $data['publicationsMenuItem'] = nn_menu_item::where('slug', 'articles')->
        where('nn_menu_item_lang.lang', getCurrentLocale() )
        ->join('nn_menu_item_lang', 'nn_menu_item.id', '=', 'nn_menu_item_lang.nn_menu_item_id')
        ->select('nn_menu_item.collection_id', 'nn_menu_item_lang.name')->first();

        $data['publicationsMenuItemParent'] = nn_menu_item::where('slug', 'publications')
        ->join('nn_menu_item_lang', 'nn_menu_item_lang.nn_menu_item_id', '=', 'nn_menu_item.id')
        ->where('nn_menu_item_lang.lang', getCurrentLocale())
        ->select('nn_menu_item_lang.name')->first();

        // $data['publications'] = nn_catalog::where([
        //     'nn_catalog_lang.lang' => getCurrentLocale(), 'collection_id' => $data['publicationsMenuItem']->collection_id]
        // )
        // ->where('nn_category_lang.lang', getCurrentLocale() )
        // ->where('nn_catalog.show', 1)
        // ->join('nn_catalog_lang', 'nn_catalog.id', '=', 'nn_catalog_lang.catalog_id')
        // ->join('nn_category_lang', 'nn_catalog.category_id', '=', 'nn_category_lang.category_id')
        // ->select('nn_catalog.id','nn_catalog.sDate','nn_catalog.sdate', 'nn_catalog_lang.name','nn_category_lang.name as catname','nn_catalog.created_at','nn_catalog.slug', 'nn_catalog_lang.description', 'nn_catalog_lang.imgurl')
        // ->orderBy('nn_catalog.sDate','desc')
        // ->orderBy('nn_catalog.created_at','desc')
        // ->limit(3)
        // ->get();

        $data['publications'] = nn_catalog::where([
            'nn_catalog_lang.lang' => getCurrentLocale(), 'collection_id' => $data['publicationsMenuItem']->collection_id]
        )
        // ->where('nn_category_lang.lang', getCurrentLocale() )
        ->where('nn_catalog.show', 1)
        ->join('nn_catalog_lang', 'nn_catalog.id', '=', 'nn_catalog_lang.catalog_id')
        // ->join('nn_category_lang', 'nn_catalog.category_id', '=', 'nn_category_lang.category_id')
        ->select('nn_catalog.id','nn_catalog.sDate', 'nn_catalog.category_id', 'nn_catalog.sdate', 'nn_catalog_lang.name',
        // 'nn_category_lang.name as catname',
        'nn_catalog.created_at','nn_catalog.slug', 'nn_catalog_lang.description', 'nn_catalog_lang.imgurl')
        ->orderBy('nn_catalog.sDate','desc')
        ->orderBy('nn_catalog.created_at','desc')
        ->limit(3)
        ->get();

        foreach ($data['publications'] as $item) {
            if ($item->category_id) {
                $item->catname = nn_category_lang::where('category_id', $item->category_id)->where('lang', getCurrentLocale())->first()->name;
            }
        }

        $data['team'] = nn_info::where([
            'nn_info_lang.lang' => getCurrentLocale(), 'collection_id' => 6]
        )
        ->where('nn_info.show', 1)
        ->join('nn_info_lang', 'nn_info.id', '=', 'nn_info_lang.info_id')
        ->select('nn_info.id', 'nn_info_lang.name','nn_info.slug', 'nn_info_lang.teamposition', 'nn_info_lang.description','nn_info_lang.imgurl')
        ->orderBy('nn_info.position', 'ASC')
        ->get();

        return view('nn_site.pages.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function projects($status = '')
    {
       

        $data['projects'] = nn_article::where([
            'nn_article_lang.lang' => getCurrentLocale(), 'collection_id' => 1]
        )
        ->where('nn_article.show', 1);

        if($status != ''){
            
            $data['projects'] = $data['projects']->where('nn_article.status',$status);
        }


        $data['projects']=$data['projects']
        ->join('nn_article_lang', 'nn_article.id', '=', 'nn_article_lang.article_id')
        ->select('nn_article.id', 'nn_article_lang.name','nn_article.slug', 'nn_article_lang.description', 'nn_article.status', 'nn_article_lang.imgurl')
   
   
        ->orderBy('nn_article.created_at','desc')
    
        ->paginate(12);

        $menu_item_id = nn_menu_item::where('collection_id', 1)->select('id')->first()->id;
        self::breadcrumbs($menu_item_id);

        $data['breadcrumbs']=self::$breadcrumbsGl;

        $data['metat'] = nn_menu_item_lang::where('nn_menu_item_id', 29)->where('lang', getCurrentLocale())->first();

        return view('nn_site.pages.projects', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function singleProject($slug)
    {
        $data['article']  = nn_article::where('nn_article.slug', $slug) 
       ->where('nn_article_lang.lang', getCurrentLocale())
       ->join('nn_article_lang', 'nn_article.id', '=', 'nn_article_lang.article_id')->first();
       $data["metat"] = $data['article'] ;
      
       

       $data['files'] = nn_file::getFile('article',$data["article"]->article_id);
       $data['images'] = nn_image::getImage('article',$data["article"]->article_id);
    

       $menu_item_id = nn_menu_item::where('collection_id', $data["article"]->collection_id)->select('id')->first()->id;
     
       self::breadcrumbs($menu_item_id);

       $data['singlePageName'] =  $data['article']->name;
       $data['breadcrumbs']=self::$breadcrumbsGl;
       return view('nn_site.pages.project-full', $data);
    } 

    public function publications(Request $request, $slug = null)
    {
        $menu_item = nn_menu_item::where('slug', $slug)->
        where('nn_menu_item_lang.lang', getCurrentLocale() )
        ->join('nn_menu_item_lang', 'nn_menu_item.id', '=', 'nn_menu_item_lang.nn_menu_item_id')
        ->select('nn_menu_item.collection_id','nn_menu_item.parent_id','nn_menu_item_lang.name', 'nn_menu_item.id')->first();

        if (! $slug) {
            $data['menu_items'] = nn_menu_item::where(function($query){
                $query->where('slug', 'articles');
                $query->orWhere('slug', 'dissertations');
            })
            ->where('nn_menu_item_lang.lang', getCurrentLocale() )
            ->join('nn_menu_item_lang', 'nn_menu_item.id', '=', 'nn_menu_item_lang.nn_menu_item_id')
            ->select('nn_menu_item.slug', 'nn_menu_item_lang.name')->get();
        }

        $data["pubpages"] = nn_menu_item::where('nn_menu_item_lang.lang', getCurrentLocale() )
        ->where('nn_menu_item.parent_id', 30)
        // ->where('nn_menu_item.parent_id', $menu_item->parent_id)
        ->join('nn_menu_item_lang', 'nn_menu_item.id', '=', 'nn_menu_item_lang.nn_menu_item_id')
        ->select('nn_menu_item.slug','nn_menu_item_lang.name', 'nn_menu_item.id')->get();

        $data["menu_item"]=$menu_item ;

        
        $data['publications'] = nn_catalog::where(
            'nn_catalog_lang.lang', getCurrentLocale()
        );
        if ($menu_item) {
            $data['publications']->where('collection_id', $menu_item->collection_id);
        } else {
            $data['publications']->where(function($query){
                $query->where('collection_id', 2);
                $query->orWhere('collection_id', 3);
            });
        }

        $data['publications']
        
        ->where('nn_catalog.show', 1);

        if($request->get('category')){
            
            
            $data['publications']->where('nn_catalog.category_id', $request->get('category'));
        }

        if (! $slug) {
          
            $data['publications'] = $data['publications']
            ->leftJoin('nn_catalog_lang', 'nn_catalog.id', '=', 'nn_catalog_lang.catalog_id')
            // ->leftJoin('nn_category_lang', 'nn_catalog.category_id', '=', 'nn_category_lang.category_id')
            ->select('nn_catalog.id', 'nn_catalog.sDate','nn_catalog.collection_id','nn_catalog_lang.name',
            // 'nn_category_lang.name as catname',
            'nn_catalog.created_at','nn_catalog.slug', 'nn_catalog_lang.description', 'nn_catalog_lang.imgurl')
            ->orderBy('nn_catalog.sDate','desc')
            ->orderBy('nn_catalog.created_at','desc')
            ->paginate(8);

            foreach ($data['publications'] as $key => $item) {
                if ($item->category_id) {
                    $item->catname = nn_category_lang::where('category_id', $item->category_id)->where('lang', getCurrentLocale())->first()->name;
                }
            }
        } else {
            $data['publications'] = $data['publications']
           // ->where('nn_category_lang.lang', getCurrentLocale())
            ->leftJoin('nn_catalog_lang', 'nn_catalog.id', '=', 'nn_catalog_lang.catalog_id')
           //->leftJoin('nn_category_lang', 'nn_catalog.category_id', '=', 'nn_category_lang.category_id')
            ->select('nn_catalog.sDate','nn_catalog.collection_id','nn_catalog_lang.name',
           // 'nn_category_lang.name as catname',
            'nn_catalog.created_at','nn_catalog.slug', 'nn_catalog_lang.description', 'nn_catalog_lang.imgurl')
            ->orderBy('nn_catalog.sDate','desc')
            ->orderBy('nn_catalog.created_at','desc')
            ->paginate(8);
        }


        $data["category"] = nn_category::getCategory(); 

    
        if ($slug) {
            self::breadcrumbs($menu_item->id);
            $data['breadcrumbs']=self::$breadcrumbsGl;
        } else {

            $data['allPublicationsBreadcrumbs'] = nn_menu_item_lang::where('nn_menu_item_id', 30)->where('lang', getCurrentLocale())->select('name')->first();

        }

        $data['slug'] = $slug;
        
        if (! $slug) {
            $data['metat'] = nn_menu_item_lang::where('nn_menu_item_id', 30)->where('lang', getCurrentLocale())->first();
        } else {
            $data['metat'] = nn_menu_item_lang::where('nn_menu_item_id', $menu_item->id)->where('lang', getCurrentLocale())->first();
        }
        
        return view('nn_site.pages.publications', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function singlePublication($slug)
    {
      
        $data['publication'] = nn_catalog::where([
            'nn_catalog_lang.lang' => getCurrentLocale(), 'nn_catalog.slug' => $slug]
        )
        //->where('nn_category_lang.lang', getCurrentLocale() )
        ->join('nn_catalog_lang', 'nn_catalog.id', '=', 'nn_catalog_lang.catalog_id')
        // ->leftJoin('nn_category_lang', 'nn_catalog.category_id', '=', 'nn_category_lang.category_id')
        ->select('nn_catalog.id', 'nn_catalog.category_id', 'nn_catalog.sDate', 'nn_catalog_lang.name',
        // 'nn_category_lang.name as catname',
        'nn_catalog.created_at','nn_catalog.slug','nn_catalog.collection_id', 'nn_catalog_lang.description', 'nn_catalog_lang.body','nn_catalog_lang.imgurl','nn_catalog_lang.ogimgurl')
        ->first();

        if ($data['publication']->category_id) {
            $data['publication']->catname = nn_category_lang::where('category_id', $data['publication']->category_id)->where('lang', getCurrentLocale())->first()->name;
        }

        $data["metat"] = $data['publication'] ;
       $data['files'] = nn_file::getFile('catalog', $data["publication"]->id);

       $data["menu_item"] = nn_menu_item::where('nn_menu_item.collection_id', $data["publication"]->collection_id)->
       where('nn_menu_item.slug', '!=', 'publications')->
       where('nn_menu_item_lang.lang', getCurrentLocale() )
       ->join('nn_menu_item_lang', 'nn_menu_item.id', '=', 'nn_menu_item_lang.nn_menu_item_id')
       ->select('nn_menu_item.parent_id','nn_menu_item_lang.name', 'nn_menu_item.id')->first();
     
        
       self::breadcrumbs($data["menu_item"]->id);

       $data['singlePageName'] =  $data['publication']->name;
       $data['breadcrumbs']=self::$breadcrumbsGl;
       return view('nn_site.pages.publication-full', $data);
    } 

    public function career()
    {
       

        $data['career'] = nn_article::where([
            'nn_article_lang.lang' => getCurrentLocale(), 'collection_id' => 5]
        )
        ->where('nn_article.show', 1);


        $data['career']=$data['career']
        ->join('nn_article_lang', 'nn_article.id', '=', 'nn_article_lang.article_id')
        ->select('nn_article.id', 'nn_article_lang.name','nn_article.slug', 'nn_article_lang.body', 'nn_article.status', 'nn_article_lang.imgurl')
   
   
        ->orderBy('nn_article.created_at','desc')
    
        ->paginate(18);

        $menu_item_id = nn_menu_item::where('collection_id', 5)->select('id')->first()->id;
        self::breadcrumbs($menu_item_id);

        $data['breadcrumbs']=self::$breadcrumbsGl;

        $data['metat'] = nn_menu_item_lang::where('nn_menu_item_id', 33)->where('lang', getCurrentLocale())->first();

        return view('nn_site.pages.career', $data);
    }
    public function team()
    {
        $data['team'] = nn_info::where([
            'nn_info_lang.lang' => getCurrentLocale(), 'collection_id' => 6]
        )
        ->where('nn_info.show', 1);

        $data['team'] = $data['team']
        ->join('nn_info_lang', 'nn_info.id', '=', 'nn_info_lang.info_id')
        ->select('nn_info.id', 'nn_info_lang.name','nn_info.slug', 'nn_info_lang.teamposition', 'nn_info_lang.description','nn_info_lang.imgurl')
        ->orderBy('nn_info.position', 'ASC')
        // ->orderBy('nn_info.created_at','desc')
        ->get();
        

        $menu_item_id = nn_menu_item::where('collection_id', 6)->select('id')->first()->id;
        self::breadcrumbs($menu_item_id);

        $data['breadcrumbs']=self::$breadcrumbsGl;

        $data['metat'] = nn_menu_item_lang::where('nn_menu_item_id', 34)->where('lang', getCurrentLocale())->first();

        return view('nn_site.pages.team', $data);
    }
    public function singleTeam($slug)
    {
       

        $data['team'] = nn_info::where([
            'nn_info_lang.lang' => getCurrentLocale(), 'collection_id' => 6, 'nn_info.slug'=>$slug]
        );
       


        $data['team']=$data['team']
        ->join('nn_info_lang', 'nn_info.id', '=', 'nn_info_lang.info_id')
        ->select('nn_info.id', 'nn_info_lang.name','nn_info.slug', 'nn_info_lang.teamposition', 'nn_info_lang.description','nn_info_lang.imgurl','nn_info_lang.ogimgurl','nn_info_lang.moreinfo','nn_info_lang.publications','nn_info_lang.email','nn_info_lang.research','nn_info_lang.education','nn_info_lang.tab_1', 'nn_info_lang.tab_2','nn_info_lang.tab_3')
   
   
        ->orderBy('nn_info.created_at','desc')
    
        ->first();
        $data["metat"] = $data['team'] ;

        $menu_item_id = nn_menu_item::where('collection_id', 6)->select('id')->first()->id;
        self::breadcrumbs($menu_item_id);

        $data['singlePageName'] =  $data['team']->name;
        $data['breadcrumbs']=self::$breadcrumbsGl;
        return view('nn_site.pages.team-full', $data);
    }

    public function works($status = '')
    {
        $data['projects'] = nn_article::where([
            'nn_article_lang.lang' => getCurrentLocale(), 'collection_id' => 7]
        )
        ->where('nn_article.show', 1);

        if($status != ''){
            
            $data['projects'] = $data['projects']->where('nn_article.status', $status);
        }


        $data['projects']=$data['projects']
        ->join('nn_article_lang', 'nn_article.id', '=', 'nn_article_lang.article_id')
        ->select('nn_article.id', 'nn_article_lang.name','nn_article.slug', 'nn_article_lang.description', 'nn_article.status', 'nn_article_lang.imgurl')
   
   
        ->orderBy('nn_article.created_at','desc')
    
        ->paginate(12);

        $menu_item_id = nn_menu_item::where('collection_id', 7)->select('id')->first()->id;
        self::breadcrumbs($menu_item_id);

        $data['breadcrumbs']=self::$breadcrumbsGl;

        $data['metat'] = nn_menu_item_lang::where('nn_menu_item_id', 35)->where('lang', getCurrentLocale())->first();

        return view('nn_site.pages.works', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function singleWork($slug)
    {
        $data['article']  = nn_article::where('nn_article.slug', $slug) 
       ->where('nn_article_lang.lang', getCurrentLocale())
       ->join('nn_article_lang', 'nn_article.id', '=', 'nn_article_lang.article_id')->first();
       $data["metat"] = $data['article'] ;
       $data['files'] = nn_file::getFile('article',$data["article"]->article_id);
       $data['images'] = nn_image::getImage('article',$data["article"]->article_id);

    

       $menu_item_id = nn_menu_item::where('collection_id', $data["article"]->collection_id)->select('id')->first()->id;
     
       self::breadcrumbs($menu_item_id);

       $data['singlePageName'] =  $data['article']->name;
       $data['breadcrumbs']=self::$breadcrumbsGl;
       return view('nn_site.pages.work-full', $data);
    }

    public function search(Request $request)
    {
        $data['items'] = [];

        $data['text'] = nn_menu_item::where([
            ['nn_menu_item_lang.lang', getCurrentLocale()],
            ['nn_menu_item.type', 'text']
        ])
        ->where(function ($query) use ($request) {
            $query->where('nn_menu_item_lang.name', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_menu_item_lang.body', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_menu_item_lang.description', 'like', '%'.$request->get('keyword').'%');
        })
        ->join('nn_menu_item_lang','nn_menu_item.id', '=','nn_menu_item_lang.nn_menu_item_id')
        ->select('nn_menu_item_lang.name','nn_menu_item.id', 'nn_menu_item.slug')
        ->groupBy('nn_menu_item.id')
        ->get();

        $data['projects'] = nn_article::where([
            ['nn_article_lang.lang', getCurrentLocale()],
            ['nn_article.collection_id', 1],
            ['nn_article.show', 1]
        ])
        ->where(function ($query) use ($request) {
            $query->where('nn_article_lang.name', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_article_lang.body', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_article_lang.description', 'like', '%'.$request->get('keyword').'%');
        })
        ->join('nn_article_lang','nn_article_lang.article_id', '=','nn_article.id')
        ->select('nn_article_lang.name','nn_article.id', 'nn_article.slug')
        ->groupBy('nn_article.id')
        ->get();

        $data['publicationArticles'] = nn_catalog::where([
            ['nn_catalog_lang.lang', getCurrentLocale()],
            ['nn_catalog.collection_id', 2],
            ['nn_catalog.show', 1]
        ])
        ->where(function ($query) use ($request) {
            $query->where('nn_catalog_lang.name', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_catalog_lang.body', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_catalog_lang.description', 'like', '%'.$request->get('keyword').'%');
        })
        ->join('nn_catalog_lang','nn_catalog_lang.catalog_id', '=','nn_catalog.id')
        ->select('nn_catalog_lang.name','nn_catalog.id', 'nn_catalog.slug')
        ->groupBy('nn_catalog.id')
        ->get();
        $data['publicationDissertations'] = nn_catalog::where([
            ['nn_catalog_lang.lang', getCurrentLocale()],
            ['nn_catalog.collection_id', 3],
            ['nn_catalog.show', 1]
        ])
        ->where(function ($query) use ($request) {
            $query->where('nn_catalog_lang.name', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_catalog_lang.body', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_catalog_lang.description', 'like', '%'.$request->get('keyword').'%');
        })
        ->join('nn_catalog_lang','nn_catalog_lang.catalog_id', '=','nn_catalog.id')
        ->select('nn_catalog_lang.name','nn_catalog.id', 'nn_catalog.slug')
        ->groupBy('nn_catalog.id')
        ->get();

        $data['career'] = nn_article::where([
            ['nn_article_lang.lang', getCurrentLocale()],
            ['nn_article.collection_id', 5],
            ['nn_article.show', 1]
        ])
        ->where(function ($query) use ($request) {
            $query->where('nn_article_lang.name', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_article_lang.body', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_article_lang.description', 'like', '%'.$request->get('keyword').'%');
        })
        ->join('nn_article_lang','nn_article_lang.article_id', '=','nn_article.id')
        ->select('nn_article_lang.name','nn_article.id', 'nn_article.slug')
        ->groupBy('nn_article.id')
        ->get();

        $data['team'] = nn_info::where([
            ['nn_info_lang.lang', getCurrentLocale()],
            ['nn_info.collection_id', 6],
            ['nn_info.show', 1]
        ])
        ->where(function ($query) use ($request) {
            $query->where('nn_info_lang.name', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_info_lang.email', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_info_lang.research', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_info_lang.education', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_info_lang.teamposition', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_info_lang.description', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_info_lang.publications', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_info_lang.moreinfo', 'like', '%'.$request->get('keyword').'%');
        })
        ->join('nn_info_lang','nn_info_lang.info_id', '=','nn_info.id')
        ->select('nn_info_lang.name','nn_info.id', 'nn_info.slug')
        ->groupBy('nn_info.id')
        ->get();

        $data['works'] = nn_article::where([
            ['nn_article_lang.lang', getCurrentLocale()],
            ['nn_article.collection_id', 7],
            ['nn_article.show', 1]
        ])
        ->where(function ($query) use ($request) {
            $query->where('nn_article_lang.name', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_article_lang.body', 'like', '%'.$request->get('keyword').'%');
            $query->orWhere('nn_article_lang.description', 'like', '%'.$request->get('keyword').'%');
        })
        ->join('nn_article_lang','nn_article_lang.article_id', '=','nn_article.id')
        ->select('nn_article_lang.name','nn_article.id', 'nn_article.slug')
        ->groupBy('nn_article.id')
        ->get();

        if ($data['text']->count()) {
            $data['items'][] = ['searchedItems' => $data['text'], 'slug'=> 'text/'];
        }
        if ($data['projects']->count()) {
            $data['items'][] = ['searchedItems' => $data['projects'], 'slug'=> 'projects/details/'];
        }
        if ($data['publicationArticles']->count()) {
            $data['items'][] = ['searchedItems' => $data['publicationArticles'], 'slug'=> 'publication/'];
        }
        if ($data['publicationDissertations']->count()) {
            $data['items'][] = ['searchedItems' => $data['publicationDissertations'], 'slug'=> 'publication/'];
        }
        if ($data['career']->count()) {
            $data['items'][] = ['searchedItems' => $data['career'], 'slug'=> 'career'];
        }
        if ($data['team']->count()) {
            $data['items'][] = ['searchedItems' => $data['team'], 'slug'=> 'team/'];
        }
        if ($data['works']->count()) {
            $data['items'][] = ['searchedItems' => $data['works'], 'slug'=> 'projects/details/'];
        }
        
        $data['searchPage'] = 1;

        return view('nn_site.pages.search', $data);
    }

    public function subscribe(Request $request)
    {
        $email = $request->get('email');
        
        $isExists = \DB::table('subscribers')->where('email', $email)->first();
        
        if($isExists){
            return response()->json(array('exists' => true), 422);
        }
        
        $request->validate([
            'email' => 'required|email|unique:subscribers'
        ]);

        $queryState = \DB::table('subscribers')->insert([
            'email' => $request->get('email')
        ]);

        return $queryState;
    }
    
    public static function getMenu($menu_item_id){
        $nn = nn_menu_item::where('nn_menu_item.id',$menu_item_id)
        ->where('nn_menu_item_lang.lang', getCurrentLocale())
        ->join('nn_menu_item_lang','nn_menu_item.id', '=','nn_menu_item_lang.nn_menu_item_id')
        ->select('nn_menu_item_lang.name','nn_menu_item.slug','nn_menu_item.type','nn_menu_item.parent_id' )
        ->first();
       
        return $nn;
    }

    public static  function text($slug,$forMeta = ""){
        $data["item"] = nn_menu_item::where('nn_menu_item.slug', $slug)
        ->where('nn_menu_item_lang.lang', getCurrentLocale())
        ->join('nn_menu_item_lang','nn_menu_item.id', '=','nn_menu_item_lang.nn_menu_item_id')
        ->select('nn_menu_item_lang.name','nn_menu_item.id','nn_menu_item_lang.body','nn_menu_item_lang.imgurl','nn_menu_item_lang.ogimgurl')
        ->first();
        $data["metat"] =  $data["item"] ;
        if($forMeta == "1")return $data["item"];

        self::breadcrumbs( $data["item"]->id);

        $data['breadcrumbs']=self::$breadcrumbsGl;
       
        return view('nn_site.pages.text', $data);
    }

    protected static $breadcrumbsGl = [];
    public static function breadcrumbs($menu_item_id,$parent_id = 0){

        if($parent_id != 0){
            
            $menu_item_id = $parent_id;
           
        }

        $nn = self::getMenu($menu_item_id);
        array_unshift(self::$breadcrumbsGl,$nn);
        if($nn->parent_id != 0) self::breadcrumbs(0,$nn->parent_id );
    }
}
