<?php

namespace App\Http\Controllers\Webprofile\Front;

use App\Http\Controllers\Controller;
use App\Models\Webprofile\Design;
use App\Models\Webprofile\Gallery;
use App\Models\Webprofile\Information;
use App\Models\Webprofile\Menu;
use App\Models\Webprofile\Posts;
use App\Models\Webprofile\Slider;
use Session;

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

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

        if (Session::get('selected_language') == 'id') {
            $data = $this->getDataId($setting);

            return view('webprofile.front.'.$setting['theme'].'.index', $data);
        } elseif (Session::get('selected_language') == 'en') {
            $data = $this->getDataEn($setting);

            return view('webprofile.front.'.$setting['theme'].'.en.index', $data);
        }
    }

    private function getDataId($setting)
    {
        $posts = Posts::where('post_status', '1')->orderby('post_date', 'desc')->paginate($setting['post_per_page']);
        $resend = Posts::where('post_status', '1')->orderby('post_date', 'desc')->limit('5')->get();
        $hot = Posts::where('post_status', '1')->orderby('viewer', 'desc')->limit('5')->get();
        $info = Information::where('info_status', '1')->where('event_date', '>=', date('Y-m-d'))->orderby('event_date', 'asc')->get();
        $slider = Slider::where('is_active', '1')->orderby('created_at', 'desc')->get();
        $menu = Menu::orderby('urutan', 'asc')->get();
        $gallery = Gallery::where('is_active', '1')->orderBy('created_at', 'asc')->limit('4')->get();
        $quote = Design::where('name_design', 'quote')->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();
        $body = Design::where('name_design', 'body')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'posts' => $posts,
            'resend' => $resend,
            'hot' => $hot,
            'info' => $info,
            'slider' => $slider,
            'menu' => $menu,
            'gallery' => $gallery,
            'quote' => $quote,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'body' => $body,
            'setting' => $setting,
            'footer' => $footer,
        ];

        return $data;
    }

    private function getDataEn($setting)
    {
        $posts = Posts::with(['rEn'])->where('post_status', '1')->orderby('post_date', 'desc')->paginate($setting['post_per_page']);
        $resend = Posts::with(['rEn'])->where('post_status', '1')->orderby('post_date', 'desc')->limit('5')->get();
        $hot = Posts::with(['rEn'])->where('post_status', '1')->orderby('viewer', 'desc')->limit('5')->get();
        $info = Information::with(['rEn'])->where('info_status', '1')->where('event_date', '>=', date('Y-m-d'))->orderby('event_date', 'asc')->get();
        $slider = Slider::where('is_active', '1')->orderby('created_at', 'desc')->get();
        $menu = Menu::with(['rEn'])->orderby('urutan', 'asc')->get();
        $gallery = Gallery::where('is_active', '1')->orderBy('created_at', 'asc')->limit('4')->get();
        $quote = Design::where('name_design', 'quote')->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();
        $body = Design::where('name_design', 'body')->get();

        $data = [
            'posts' => $posts,
            'resend' => $resend,
            'hot' => $hot,
            'info' => $info,
            'slider' => $slider,
            'menu' => $menu,
            'gallery' => $gallery,
            'quote' => $quote,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'body' => $body,
            'setting' => $setting,
        ];

        return $data;
    }
}
