<?php

namespace App\Http\Controllers\nn_site;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\nn_menu;
use App\Models\nn_site_settings;
use App\Models\nn_translate;
use View;
use LaravelLocalization;
use Session;
use App\Models\nn_site_users;

class SiteController extends Controller
{
    // Site __construct
    public function __construct()
    {
        // primary menu
        $primaryMenu = nn_menu::getPrimaryMenu();
        $data['primaryMenu'] = [];
        $data['footerLeftMenu'] = [];
        $data['footerRightMenu'] = [];

        foreach ($primaryMenu as $item) {
            if ($item->show_in_main_menu == 1) {
                $data['primaryMenu'][] = $item;
            }
            if ($item->show_footer_left == 1) {
                $data['footerLeftMenu'][] = $item;
            }
            if ($item->show_footer_right == 1) {
                $data['footerRightMenu'][] = $item;
            }
        }

        $data['primaryMenu'] = collect($data['primaryMenu']);

        $data['siteSettings'] = nn_site_settings::first();
        $data['translate'] = nn_translate::join('nn_translate_lang', 'nn_translate_lang.translate_id', '=', 'nn_translate.id')
        ->where('nn_translate_lang.lang', getCurrentLocale())
        ->pluck('nn_translate_lang.name', 'nn_translate.trans_key');
        $data['translate'] =  (object)$data['translate'];
        $data['translate'] = json_decode($data['translate']);
        
        View::share($data);
    }

    public static function getUser($userId)
    {
        $user = nn_site_users::where('id', $userId)->first();

        return $user;
    }
}