<?php

namespace App\Http\Controllers\Webprofile\Front;

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

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

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

            return view('webprofile.front.'.$setting['theme'].'.de.agenda', $data);
        } elseif (Session::get('selected_language') == 'ar') {
            $data = $this->getDataSa($setting, $title);

            return view('webprofile.front.'.$setting['theme'].'.ar.agenda', $data);
        } elseif (Session::get('selected_language') == 'zh') {
            $data = $this->getDataZh($setting, $title);

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

    public function getDataId($setting, $title)
    {
        $setting = webprofilesetting();
        $data = Agenda::where('slug', $title)->first();
        $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();
        $agendas = Agenda::where('info_status', '1')->orderBy('event_date', 'desc')->limit('5')->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();

        $viewer['viewer'] = (int) $data->viewer + 1;
        $data->update($viewer);

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

        return $data;
    }

    public function getDataEn($setting, $title)
    {
        $setting = webprofilesetting();
        $data = Agenda::where('slug', $title)->first();
        $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();
        $agendas = Agenda::with(['rEn'])->where('info_status', '1')->orderBy('event_date', 'desc')->limit('5')->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();

        $viewer['viewer'] = (int) $data->viewer + 1;
        $data->update($viewer);

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

        return $data;
    }

    public function getDataDe($setting, $title)
    {
        $setting = webprofilesetting();
        $data = Agenda::where('slug', $title)->first();
        $resend = Posts::with(['rDe'])->where('post_status', '1')->orderby('post_date', 'desc')->limit('5')->get();
        $hot = Posts::with(['rDe'])->where('post_status', '1')->orderby('viewer', 'desc')->limit('5')->get();
        $agendas = Agenda::with(['rDe'])->where('info_status', '1')->orderBy('event_date', 'desc')->limit('5')->get();
        $menu = Menu::with(['rDe'])->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();

        $viewer['viewer'] = (int) $data->viewer + 1;
        $data->update($viewer);

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

        return $data;
    }

    public function getDataSa($setting, $title)
    {
        $setting = webprofilesetting();
        $data = Agenda::where('slug', $title)->first();
        $resend = Posts::with(['rSa'])->where('post_status', '1')->orderby('post_date', 'desc')->limit('5')->get();
        $hot = Posts::with(['rSa'])->where('post_status', '1')->orderby('viewer', 'desc')->limit('5')->get();
        $agendas = Agenda::with(['rSa'])->where('info_status', '1')->orderBy('event_date', 'desc')->limit('5')->get();
        $menu = Menu::with(['rSa'])->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();

        $viewer['viewer'] = (int) $data->viewer + 1;
        $data->update($viewer);

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

        return $data;
    }

    public function getDataZh($setting, $title)
    {
        $setting = webprofilesetting();
        $data = Agenda::where('slug', $title)->first();
        $resend = Posts::with(['rZh'])->where('post_status', '1')->orderby('post_date', 'desc')->limit('5')->get();
        $hot = Posts::with(['rZh'])->where('post_status', '1')->orderby('viewer', 'desc')->limit('5')->get();
        $agendas = Agenda::with(['rZh'])->where('info_status', '1')->orderBy('event_date', 'desc')->limit('5')->get();
        $menu = Menu::with(['rZh'])->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();

        $viewer['viewer'] = (int) $data->viewer + 1;
        $data->update($viewer);

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

        return $data;
    }
}
