<?php

namespace App\Http\Controllers\Webprofile\Front;

use App\Http\Controllers\Controller;
use App\Models\Webprofile\Design;
use App\Models\Webprofile\Menu;
use App\Models\Webprofile\Pages;
use Session;

class PageController extends Controller
{
    public function index($title)
    {
        $setting = webprofilesetting();

        if (!Session::has('selected_language')) {
            session(['selected_language' => 'id']);
        }

        if (Session::get('selected_language') == 'id') {
            $data = $this->getDataId($setting, $title);
            return view('webprofile.front.' . $setting['theme'] . '.page', $data);
        } else if (Session::get('selected_language') == 'en') {
            $data = $this->getDataEn($setting, $title);
            return view('webprofile.front.' . $setting['theme'] . '.en.page', $data);
        }
    }

    public function getDataId($setting, $title)
    {
        $setting        = webprofilesetting();
        $data           = Pages::where('slug', $title)->first();
        $menu           = Menu::orderby('urutan', 'asc')->get();
        $widget_right   = Design::where('name_design', 'widget_right')->orderBy('urutan', 'ASC')->get();
        $widget_left    = Design::where('name_design', 'widget_left')->orderBy('urutan', 'ASC')->get();

        $data = [
            'setting'       => $setting,
            'data'          => $data,
            'menu'          => $menu,
            'widget_right'  => $widget_right,
            'widget_left'   => $widget_left,
        ];

        return $data;
    }
    public function getDataEn($setting, $title)
    {
        $setting        = webprofilesetting();
        $data           = Pages::with(['rEn'])->where('slug', $title)->first();
        $menu           = Menu::with(['rEn'])->orderby('urutan', 'asc')->get();
        $widget_right   = Design::where('name_design', 'widget_right')->orderBy('urutan', 'ASC')->get();
        $widget_left    = Design::where('name_design', 'widget_left')->orderBy('urutan', 'ASC')->get();

        $data = [
            'setting'       => $setting,
            'data'          => $data,
            'menu'          => $menu,
            'widget_right'  => $widget_right,
            'widget_left'   => $widget_left,
        ];

        return $data;
    }
}
