<?php

namespace App\Http\Controllers\Webprofile\Front;

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

class AgendaController 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'].'.archive', $data)->withTitle('Agenda');
        } elseif (Session::get('selected_language') == 'en') {
            $data = $this->getDataEn($setting);

            return view('webprofile.front.'.$setting['theme'].'.en.archive', $data)->withTitle('Agenda');
        }
    }

    public function getDataId($setting)
    {
        $setting = webprofilesetting();
        $data = Information::where('info_status', '1')->orderBy('event_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();
        $categories = Categories::where('is_active', '1')->get();
        $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();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'setting' => $setting,
            'data' => $data,
            'menu' => $menu,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'resend' => $resend,
            'hot' => $hot,
            'categories' => $categories,
            'footer' => $footer,
        ];

        return $data;
    }

    public function getDataEn($setting)
    {
        $setting = webprofilesetting();
        $data = Information::where('info_status', '1')->orderBy('event_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();
        $categories = Categories::with(['rEn'])->where('is_active', '1')->get();
        $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();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'setting' => $setting,
            'data' => $data,
            'menu' => $menu,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'resend' => $resend,
            'hot' => $hot,
            'categories' => $categories,
            'footer' => $footer,
        ];

        return $data;
    }
}
